Skip to content

Commit 8be83db

Browse files
committed
Simplify slightly
1 parent 6fadcda commit 8be83db

File tree

3 files changed

+33
-39
lines changed

3 files changed

+33
-39
lines changed

src/compiler/emitter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ export function getOutputDeclarationFileName(inputFileName: string, configFile:
598598

599599
/** @internal */
600600
export function getOutputDeclarationFileNameWithoutConfigFile(inputFileName: string, options: CompilerOptions, ignoreCase: boolean, currentDirectory: string, getCommonSourceDirectory: () => string) {
601-
const directory = (options.outDir || options.declarationDir) ? getNormalizedAbsolutePath(options.outDir || options.declarationDir!, currentDirectory) : undefined;
601+
const directory = (options.declarationDir || options.outDir) ? getNormalizedAbsolutePath(options.declarationDir || options.outDir!, currentDirectory) : undefined;
602602
const outputPathWithoutChangedExtension = directory
603603
? resolvePath(directory, getRelativePathFromDirectory(getCommonSourceDirectory(), inputFileName, ignoreCase))
604604
: inputFileName;

src/compiler/program.ts

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3527,56 +3527,50 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
35273527
}
35283528

35293529
function getImpliedNodeFormatForFile(fileName: string) {
3530+
// Map to an output file before looking up a package.json that might
3531+
// contain `"type": "module"`. This allows the same input `.ts` file to be
3532+
// compiled as both CommonJS and ESM depending on the tsconfig's `outDir`.
35303533
let fileNameForModuleFormatDetection = getNormalizedAbsolutePath(fileName, currentDirectory);
35313534
if (moduleFormatNeedsPackageJsonLookup(fileName, options)) {
35323535
const projectReference = getResolvedProjectReferenceToRedirect(fileName);
35333536
if (projectReference) {
35343537
// We have a source file that is an input to a referenced project.
3535-
// This should only happen with `useSourceOfProjectReferenceRedirect` enabled.
3538+
// This should only happen with `useSourceOfProjectReferenceRedirect`
3539+
// enabled (i.e. TS Server scenarios).
35363540
Debug.assert(useSourceOfProjectReferenceRedirect);
3537-
if (projectReference.commandLine.options.outDir) {
3538-
const outputFile = getOutputJSFileName(
3541+
fileNameForModuleFormatDetection =
3542+
getOutputDeclarationFileName(
35393543
fileNameForModuleFormatDetection,
35403544
projectReference.commandLine,
3541-
!host.useCaseSensitiveFileNames());
3542-
if (outputFile) {
3543-
fileNameForModuleFormatDetection = outputFile;
3544-
}
3545-
}
3546-
else if (projectReference.commandLine.options.declaration && projectReference.commandLine.options.declarationDir) {
3547-
const outputFile = getOutputDeclarationFileName(
3545+
!host.useCaseSensitiveFileNames()) ||
3546+
getOutputJSFileName(
35483547
fileNameForModuleFormatDetection,
35493548
projectReference.commandLine,
3550-
!host.useCaseSensitiveFileNames());
3551-
if (outputFile) {
3552-
fileNameForModuleFormatDetection = outputFile;
3553-
}
3554-
}
3555-
}
3556-
else if (options.outDir) {
3557-
const outputFile = getOutputJSFileNameWithoutConfigFile(
3558-
fileNameForModuleFormatDetection,
3559-
options,
3560-
!host.useCaseSensitiveFileNames(),
3561-
currentDirectory,
3562-
getAssumedCommonSourceDirectory);
3563-
if (outputFile) {
3564-
fileNameForModuleFormatDetection = outputFile;
3565-
}
3549+
!host.useCaseSensitiveFileNames()) ||
3550+
fileNameForModuleFormatDetection;
35663551
}
3567-
else if (options.declaration && options.declarationDir) {
3568-
const outputFile = getOutputDeclarationFileNameWithoutConfigFile(
3569-
fileNameForModuleFormatDetection,
3570-
options,
3571-
!host.useCaseSensitiveFileNames(),
3572-
currentDirectory,
3573-
getAssumedCommonSourceDirectory);
3574-
if (outputFile) {
3575-
fileNameForModuleFormatDetection = outputFile;
3576-
}
3552+
else {
3553+
fileNameForModuleFormatDetection =
3554+
getOutputDeclarationFileNameWithoutConfigFile(
3555+
fileNameForModuleFormatDetection,
3556+
options,
3557+
!host.useCaseSensitiveFileNames(),
3558+
currentDirectory,
3559+
getAssumedCommonSourceDirectory) ||
3560+
getOutputJSFileNameWithoutConfigFile(
3561+
fileNameForModuleFormatDetection,
3562+
options,
3563+
!host.useCaseSensitiveFileNames(),
3564+
currentDirectory,
3565+
getAssumedCommonSourceDirectory) ||
3566+
fileNameForModuleFormatDetection;
35773567
}
35783568
}
3579-
return getImpliedNodeFormatForFileWorker(fileNameForModuleFormatDetection, moduleResolutionCache?.getPackageJsonInfoCache(), host, options);
3569+
return getImpliedNodeFormatForFileWorker(
3570+
fileNameForModuleFormatDetection,
3571+
moduleResolutionCache?.getPackageJsonInfoCache(),
3572+
host,
3573+
options);
35803574
}
35813575

35823576
function getCreateSourceFileOptions(fileName: string): CreateSourceFileOptions {

tests/baselines/reference/nodeMOdulesOutDirPackageJson05_declarationDir.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//// [tests/cases/conformance/node/nodeMOdulesOutDirPackageJson05_declarationDir.ts] ////
1+
//// [tests/cases/conformance/node/nodeModulesOutDirPackageJson05_declarationDir.ts] ////
22

33
//// [package.json]
44
{ "type": "module" }

0 commit comments

Comments
 (0)