mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Respect package.json "type" and module-format-specific file extensions in more module modes (#57896)
This commit is contained in:
@@ -55,7 +55,7 @@ export * from "../transformers/generators";
|
||||
export * from "../transformers/module/module";
|
||||
export * from "../transformers/module/system";
|
||||
export * from "../transformers/module/esnextAnd2015";
|
||||
export * from "../transformers/module/node";
|
||||
export * from "../transformers/module/impliedNodeFormatDependent";
|
||||
export * from "../transformers/declarations/diagnostics";
|
||||
export * from "../transformers/declarations";
|
||||
export * from "../transformer";
|
||||
|
||||
+63
-34
@@ -60,6 +60,7 @@ import {
|
||||
canHaveJSDoc,
|
||||
canHaveLocals,
|
||||
canHaveModifiers,
|
||||
canHaveModuleSpecifier,
|
||||
canHaveSymbol,
|
||||
canUsePropertyAccess,
|
||||
cartesianProduct,
|
||||
@@ -3585,27 +3586,36 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
|| isNamespaceExport(node));
|
||||
}
|
||||
|
||||
function getUsageModeForExpression(usage: Expression) {
|
||||
return isStringLiteralLike(usage) ? host.getModeForUsageLocation(getSourceFileOfNode(usage), usage) : undefined;
|
||||
function getEmitSyntaxForModuleSpecifierExpression(usage: Expression) {
|
||||
return isStringLiteralLike(usage) ? host.getEmitSyntaxForUsageLocation(getSourceFileOfNode(usage), usage) : undefined;
|
||||
}
|
||||
|
||||
function isESMFormatImportImportingCommonjsFormatFile(usageMode: ResolutionMode, targetMode: ResolutionMode) {
|
||||
return usageMode === ModuleKind.ESNext && targetMode === ModuleKind.CommonJS;
|
||||
}
|
||||
|
||||
function isOnlyImportedAsDefault(usage: Expression) {
|
||||
const usageMode = getUsageModeForExpression(usage);
|
||||
return usageMode === ModuleKind.ESNext && endsWith((usage as StringLiteralLike).text, Extension.Json);
|
||||
function isOnlyImportableAsDefault(usage: Expression) {
|
||||
// In Node.js, JSON modules don't get named exports
|
||||
if (ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext) {
|
||||
const usageMode = getEmitSyntaxForModuleSpecifierExpression(usage);
|
||||
return usageMode === ModuleKind.ESNext && endsWith((usage as StringLiteralLike).text, Extension.Json);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function canHaveSyntheticDefault(file: SourceFile | undefined, moduleSymbol: Symbol, dontResolveAlias: boolean, usage: Expression) {
|
||||
const usageMode = file && getUsageModeForExpression(usage);
|
||||
if (file && usageMode !== undefined && ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext) {
|
||||
const result = isESMFormatImportImportingCommonjsFormatFile(usageMode, file.impliedNodeFormat);
|
||||
if (usageMode === ModuleKind.ESNext || result) {
|
||||
return result;
|
||||
const usageMode = file && getEmitSyntaxForModuleSpecifierExpression(usage);
|
||||
if (file && usageMode !== undefined) {
|
||||
const targetMode = host.getImpliedNodeFormatForEmit(file);
|
||||
if (usageMode === ModuleKind.ESNext && targetMode === ModuleKind.CommonJS && ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext) {
|
||||
// In Node.js, CommonJS modules always have a synthetic default when imported into ESM
|
||||
return true;
|
||||
}
|
||||
if (usageMode === ModuleKind.ESNext && targetMode === ModuleKind.ESNext) {
|
||||
// No matter what the `module` setting is, if we're confident that both files
|
||||
// are ESM, there cannot be a synthetic default.
|
||||
return false;
|
||||
}
|
||||
// fallthrough on cjs usages so we imply defaults for interop'd imports, too
|
||||
}
|
||||
if (!allowSyntheticDefaultImports) {
|
||||
return false;
|
||||
@@ -3658,7 +3668,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
if (!specifier) {
|
||||
return exportDefaultSymbol;
|
||||
}
|
||||
const hasDefaultOnly = isOnlyImportedAsDefault(specifier);
|
||||
const hasDefaultOnly = isOnlyImportableAsDefault(specifier);
|
||||
const hasSyntheticDefault = canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, specifier);
|
||||
if (!exportDefaultSymbol && !hasSyntheticDefault && !hasDefaultOnly) {
|
||||
if (hasExportAssignmentSymbol(moduleSymbol) && !allowSyntheticDefaultImports) {
|
||||
@@ -3837,7 +3847,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
let symbolFromModule = getExportOfModule(targetSymbol, name, specifier, dontResolveAlias);
|
||||
if (symbolFromModule === undefined && name.escapedText === InternalSymbolName.Default) {
|
||||
const file = moduleSymbol.declarations?.find(isSourceFile);
|
||||
if (isOnlyImportedAsDefault(moduleSpecifier) || canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, moduleSpecifier)) {
|
||||
if (isOnlyImportableAsDefault(moduleSpecifier) || canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias, moduleSpecifier)) {
|
||||
symbolFromModule = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias);
|
||||
}
|
||||
}
|
||||
@@ -4540,7 +4550,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
findAncestor(location, isImportDeclaration)?.moduleSpecifier ||
|
||||
findAncestor(location, isExternalModuleImportEqualsDeclaration)?.moduleReference.expression ||
|
||||
findAncestor(location, isExportDeclaration)?.moduleSpecifier;
|
||||
const mode = contextSpecifier && isStringLiteralLike(contextSpecifier) ? host.getModeForUsageLocation(currentSourceFile, contextSpecifier) : currentSourceFile.impliedNodeFormat;
|
||||
const mode = contextSpecifier && isStringLiteralLike(contextSpecifier)
|
||||
? host.getModeForUsageLocation(currentSourceFile, contextSpecifier)
|
||||
: host.getDefaultResolutionModeForFile(currentSourceFile);
|
||||
const moduleResolutionKind = getEmitModuleResolutionKind(compilerOptions);
|
||||
const resolvedModule = host.getResolvedModule(currentSourceFile, moduleReference, mode)?.resolvedModule;
|
||||
const resolutionDiagnostic = resolvedModule && getResolutionDiagnostic(compilerOptions, resolvedModule, currentSourceFile);
|
||||
@@ -4827,7 +4839,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
|
||||
const targetFile = moduleSymbol?.declarations?.find(isSourceFile);
|
||||
const isEsmCjsRef = targetFile && isESMFormatImportImportingCommonjsFormatFile(getUsageModeForExpression(reference), targetFile.impliedNodeFormat);
|
||||
const isEsmCjsRef = targetFile && isESMFormatImportImportingCommonjsFormatFile(getEmitSyntaxForModuleSpecifierExpression(reference), host.getImpliedNodeFormatForEmit(targetFile));
|
||||
if (getESModuleInterop(compilerOptions) || isEsmCjsRef) {
|
||||
let sigs = getSignaturesOfStructuredType(type, SignatureKind.Call);
|
||||
if (!sigs || !sigs.length) {
|
||||
@@ -7673,8 +7685,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
return getSourceFileOfNode(getNonAugmentationDeclaration(symbol)!).fileName; // A resolver may not be provided for baselines and errors - in those cases we use the fileName in full
|
||||
}
|
||||
const enclosingDeclaration = getOriginalNode(context.enclosingDeclaration);
|
||||
const originalModuleSpecifier = canHaveModuleSpecifier(enclosingDeclaration) ? tryGetModuleSpecifierFromDeclaration(enclosingDeclaration) : undefined;
|
||||
const contextFile = context.enclosingFile;
|
||||
const resolutionMode = overrideImportMode || contextFile?.impliedNodeFormat;
|
||||
const resolutionMode = overrideImportMode
|
||||
|| originalModuleSpecifier && host.getModeForUsageLocation(contextFile, originalModuleSpecifier)
|
||||
|| contextFile && host.getDefaultResolutionModeForFile(contextFile);
|
||||
const cacheKey = createModeAwareCacheKey(contextFile.path, resolutionMode);
|
||||
const links = getSymbolLinks(symbol);
|
||||
let specifier = links.specifierCache && links.specifierCache.get(cacheKey);
|
||||
@@ -35925,7 +35941,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
|
||||
function getTypeWithSyntheticDefaultOnly(type: Type, symbol: Symbol, originalSymbol: Symbol, moduleSpecifier: Expression) {
|
||||
const hasDefaultOnly = isOnlyImportedAsDefault(moduleSpecifier);
|
||||
const hasDefaultOnly = isOnlyImportableAsDefault(moduleSpecifier);
|
||||
if (hasDefaultOnly && type && !isErrorType(type)) {
|
||||
const synthType = type as SyntheticDefaultModuleType;
|
||||
if (!synthType.defaultOnlyType) {
|
||||
@@ -42566,7 +42582,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
|
||||
function checkCollisionWithRequireExportsInGeneratedCode(node: Node, name: Identifier | undefined) {
|
||||
// No need to check for require or exports for ES6 modules and later
|
||||
if (moduleKind >= ModuleKind.ES2015 && !(moduleKind >= ModuleKind.Node16 && getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.CommonJS)) {
|
||||
if (host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) >= ModuleKind.ES2015) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -44450,7 +44466,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
function checkClassNameCollisionWithObject(name: Identifier): void {
|
||||
if (
|
||||
languageVersion >= ScriptTarget.ES5 && name.escapedText === "Object"
|
||||
&& (moduleKind < ModuleKind.ES2015 || getSourceFileOfNode(name).impliedNodeFormat === ModuleKind.CommonJS)
|
||||
&& host.getEmitModuleFormatOfFile(getSourceFileOfNode(name)) < ModuleKind.ES2015
|
||||
) {
|
||||
error(name, Diagnostics.Class_name_cannot_be_Object_when_targeting_ES5_with_module_0, ModuleKind[moduleKind]); // https://github.com/Microsoft/TypeScript/issues/17494
|
||||
}
|
||||
@@ -45785,7 +45801,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
if (
|
||||
compilerOptions.verbatimModuleSyntax &&
|
||||
node.parent.kind === SyntaxKind.SourceFile &&
|
||||
(moduleKind === ModuleKind.CommonJS || node.parent.impliedNodeFormat === ModuleKind.CommonJS)
|
||||
host.getEmitModuleFormatOfFile(node.parent) === ModuleKind.CommonJS
|
||||
) {
|
||||
const exportModifier = node.modifiers?.find(m => m.kind === SyntaxKind.ExportKeyword);
|
||||
if (exportModifier) {
|
||||
@@ -46065,10 +46081,22 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
compilerOptions.verbatimModuleSyntax &&
|
||||
node.kind !== SyntaxKind.ImportEqualsDeclaration &&
|
||||
!isInJSFile(node) &&
|
||||
(moduleKind === ModuleKind.CommonJS || getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.CommonJS)
|
||||
host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS
|
||||
) {
|
||||
error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
|
||||
}
|
||||
else if (
|
||||
moduleKind === ModuleKind.Preserve &&
|
||||
node.kind !== SyntaxKind.ImportEqualsDeclaration &&
|
||||
node.kind !== SyntaxKind.VariableDeclaration &&
|
||||
host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS
|
||||
) {
|
||||
// In `--module preserve`, ESM input syntax emits ESM output syntax, but there will be times
|
||||
// when we look at the `impliedNodeFormat` of this file and decide it's CommonJS. To avoid
|
||||
// that inconsistency, we disallow ESM syntax in files that are unambiguously CommonJS in
|
||||
// this mode.
|
||||
error(node, Diagnostics.ESM_syntax_is_not_allowed_in_a_CommonJS_module_when_module_is_set_to_preserve);
|
||||
}
|
||||
}
|
||||
|
||||
if (isImportSpecifier(node)) {
|
||||
@@ -46117,7 +46145,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
node.kind === SyntaxKind.ImportSpecifier &&
|
||||
idText(node.propertyName || node.name) === "default" &&
|
||||
getESModuleInterop(compilerOptions) &&
|
||||
moduleKind !== ModuleKind.System && (moduleKind < ModuleKind.ES2015 || getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.CommonJS)
|
||||
host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) < ModuleKind.System
|
||||
) {
|
||||
checkExternalEmitHelpers(node, ExternalEmitHelpers.ImportDefault);
|
||||
}
|
||||
@@ -46138,7 +46166,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
return; // Other grammar checks do not apply to type-only imports with resolution mode assertions
|
||||
}
|
||||
|
||||
const mode = (moduleKind === ModuleKind.NodeNext) && declaration.moduleSpecifier && getUsageModeForExpression(declaration.moduleSpecifier);
|
||||
const mode = (moduleKind === ModuleKind.NodeNext) && declaration.moduleSpecifier && getEmitSyntaxForModuleSpecifierExpression(declaration.moduleSpecifier);
|
||||
if (mode !== ModuleKind.ESNext && moduleKind !== ModuleKind.ESNext && moduleKind !== ModuleKind.Preserve) {
|
||||
const message = isImportAttributes
|
||||
? moduleKind === ModuleKind.NodeNext
|
||||
@@ -46182,7 +46210,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
if (importClause.namedBindings) {
|
||||
if (importClause.namedBindings.kind === SyntaxKind.NamespaceImport) {
|
||||
checkImportBinding(importClause.namedBindings);
|
||||
if (moduleKind !== ModuleKind.System && (moduleKind < ModuleKind.ES2015 || getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.CommonJS) && getESModuleInterop(compilerOptions)) {
|
||||
if (host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) < ModuleKind.System && getESModuleInterop(compilerOptions)) {
|
||||
// import * as ns from "foo";
|
||||
checkExternalEmitHelpers(node, ExternalEmitHelpers.ImportStar);
|
||||
}
|
||||
@@ -46231,8 +46259,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (moduleKind >= ModuleKind.ES2015 && moduleKind !== ModuleKind.Preserve && getSourceFileOfNode(node).impliedNodeFormat === undefined && !node.isTypeOnly && !(node.flags & NodeFlags.Ambient)) {
|
||||
// Import equals declaration is deprecated in es6 or above
|
||||
if (ModuleKind.ES2015 <= moduleKind && moduleKind <= ModuleKind.ESNext && !node.isTypeOnly && !(node.flags & NodeFlags.Ambient)) {
|
||||
// Import equals declaration cannot be emitted as ESM
|
||||
grammarErrorOnNode(node, Diagnostics.Import_assignment_cannot_be_used_when_targeting_ECMAScript_modules_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead);
|
||||
}
|
||||
}
|
||||
@@ -46272,7 +46300,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
else if (node.exportClause) {
|
||||
checkAliasSymbol(node.exportClause);
|
||||
}
|
||||
if (moduleKind !== ModuleKind.System && (moduleKind < ModuleKind.ES2015 || getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.CommonJS)) {
|
||||
if (host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) < ModuleKind.System) {
|
||||
if (node.exportClause) {
|
||||
// export * as ns from "foo";
|
||||
// For ES2015 modules, we emit it as a pair of `import * as a_1 ...; export { a_1 as ns }` and don't need the helper.
|
||||
@@ -46331,8 +46359,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
else {
|
||||
if (
|
||||
getESModuleInterop(compilerOptions) &&
|
||||
moduleKind !== ModuleKind.System &&
|
||||
(moduleKind < ModuleKind.ES2015 || getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.CommonJS) &&
|
||||
host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) < ModuleKind.System &&
|
||||
idText(node.propertyName || node.name) === "default"
|
||||
) {
|
||||
checkExternalEmitHelpers(node, ExternalEmitHelpers.ImportDefault);
|
||||
@@ -46373,7 +46400,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
const isIllegalExportDefaultInCJS = !node.isExportEquals &&
|
||||
!(node.flags & NodeFlags.Ambient) &&
|
||||
compilerOptions.verbatimModuleSyntax &&
|
||||
(moduleKind === ModuleKind.CommonJS || getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.CommonJS);
|
||||
host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS;
|
||||
|
||||
if (node.expression.kind === SyntaxKind.Identifier) {
|
||||
const id = node.expression as Identifier;
|
||||
@@ -46471,8 +46498,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
if (
|
||||
moduleKind >= ModuleKind.ES2015 &&
|
||||
moduleKind !== ModuleKind.Preserve &&
|
||||
((node.flags & NodeFlags.Ambient && getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.ESNext) ||
|
||||
(!(node.flags & NodeFlags.Ambient) && getSourceFileOfNode(node).impliedNodeFormat !== ModuleKind.CommonJS))
|
||||
((node.flags & NodeFlags.Ambient && host.getImpliedNodeFormatForEmit(getSourceFileOfNode(node)) === ModuleKind.ESNext) ||
|
||||
(!(node.flags & NodeFlags.Ambient) && host.getImpliedNodeFormatForEmit(getSourceFileOfNode(node)) !== ModuleKind.CommonJS))
|
||||
) {
|
||||
// export assignment is not supported in es6 modules
|
||||
grammarErrorOnNode(node, Diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_modules_Consider_using_export_default_or_another_module_format_instead);
|
||||
@@ -49199,7 +49226,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
// ModuleDeclaration needs to be checked that it is uninstantiated later
|
||||
node.kind !== SyntaxKind.ModuleDeclaration &&
|
||||
node.parent.kind === SyntaxKind.SourceFile &&
|
||||
(moduleKind === ModuleKind.CommonJS || getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.CommonJS)
|
||||
host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) === ModuleKind.CommonJS
|
||||
) {
|
||||
return grammarErrorOnNode(modifier, Diagnostics.A_top_level_export_modifier_cannot_be_used_on_value_declarations_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled);
|
||||
}
|
||||
@@ -50358,7 +50385,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
|
||||
}
|
||||
|
||||
if (
|
||||
(moduleKind < ModuleKind.ES2015 || getSourceFileOfNode(node).impliedNodeFormat === ModuleKind.CommonJS) && moduleKind !== ModuleKind.System &&
|
||||
host.getEmitModuleFormatOfFile(getSourceFileOfNode(node)) < ModuleKind.System &&
|
||||
!(node.parent.parent.flags & NodeFlags.Ambient) && hasSyntacticModifier(node.parent.parent, ModifierFlags.Export)
|
||||
) {
|
||||
checkESModuleMarker(node.name);
|
||||
@@ -50988,6 +51015,8 @@ function createBasicNodeBuilderModuleSpecifierResolutionHost(host: TypeCheckerHo
|
||||
fileExists: fileName => host.fileExists(fileName),
|
||||
getFileIncludeReasons: () => host.getFileIncludeReasons(),
|
||||
readFile: host.readFile ? (fileName => host.readFile!(fileName)) : undefined,
|
||||
getDefaultResolutionModeForFile: file => host.getDefaultResolutionModeForFile(file),
|
||||
getModeForResolutionAtIndex: (file, index) => host.getModeForResolutionAtIndex(file, index),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -967,6 +967,10 @@
|
||||
"category": "Error",
|
||||
"code": 1292
|
||||
},
|
||||
"ESM syntax is not allowed in a CommonJS module when 'module' is set to 'preserve'.": {
|
||||
"category": "Error",
|
||||
"code": 1293
|
||||
},
|
||||
|
||||
"'with' statements are not allowed in an async function block.": {
|
||||
"category": "Error",
|
||||
|
||||
@@ -130,6 +130,7 @@ import {
|
||||
getEmitFlags,
|
||||
getEmitHelpers,
|
||||
getEmitModuleKind,
|
||||
getEmitModuleResolutionKind,
|
||||
getEmitScriptTarget,
|
||||
getExternalModuleName,
|
||||
getIdentifierTypeArguments,
|
||||
@@ -800,6 +801,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
|
||||
newLine: compilerOptions.newLine,
|
||||
noEmitHelpers: compilerOptions.noEmitHelpers,
|
||||
module: getEmitModuleKind(compilerOptions),
|
||||
moduleResolution: getEmitModuleResolutionKind(compilerOptions),
|
||||
target: getEmitScriptTarget(compilerOptions),
|
||||
sourceMap: compilerOptions.sourceMap,
|
||||
inlineSourceMap: compilerOptions.inlineSourceMap,
|
||||
@@ -867,6 +869,7 @@ export function emitFiles(resolver: EmitResolver, host: EmitHost, targetSourceFi
|
||||
newLine: compilerOptions.newLine,
|
||||
noEmitHelpers: true,
|
||||
module: compilerOptions.module,
|
||||
moduleResolution: compilerOptions.moduleResolution,
|
||||
target: compilerOptions.target,
|
||||
sourceMap: !forceDtsEmit && compilerOptions.declarationMap,
|
||||
inlineSourceMap: compilerOptions.inlineSourceMap,
|
||||
|
||||
@@ -53,10 +53,12 @@ import {
|
||||
getAllAccessorDeclarations,
|
||||
getEmitFlags,
|
||||
getEmitHelpers,
|
||||
getEmitModuleFormatOfFileWorker,
|
||||
getEmitModuleKind,
|
||||
getESModuleInterop,
|
||||
getExternalModuleName,
|
||||
getExternalModuleNameFromPath,
|
||||
getImpliedNodeFormatForEmitWorker,
|
||||
getJSDocType,
|
||||
getJSDocTypeTag,
|
||||
getModifiers,
|
||||
@@ -712,7 +714,7 @@ export function createExternalHelpersImportDeclarationIfNeeded(nodeFactory: Node
|
||||
if (compilerOptions.importHelpers && isEffectiveExternalModule(sourceFile, compilerOptions)) {
|
||||
let namedBindings: NamedImportBindings | undefined;
|
||||
const moduleKind = getEmitModuleKind(compilerOptions);
|
||||
if ((moduleKind >= ModuleKind.ES2015 && moduleKind <= ModuleKind.ESNext) || sourceFile.impliedNodeFormat === ModuleKind.ESNext) {
|
||||
if ((moduleKind >= ModuleKind.ES2015 && moduleKind <= ModuleKind.ESNext) || getImpliedNodeFormatForEmitWorker(sourceFile, compilerOptions) === ModuleKind.ESNext) {
|
||||
// use named imports
|
||||
const helpers = getEmitHelpers(sourceFile);
|
||||
if (helpers) {
|
||||
@@ -769,10 +771,8 @@ export function getOrCreateExternalHelpersModuleNameIfNeeded(factory: NodeFactor
|
||||
return externalHelpersModuleName;
|
||||
}
|
||||
|
||||
const moduleKind = getEmitModuleKind(compilerOptions);
|
||||
let create = (hasExportStarsToExportValues || (getESModuleInterop(compilerOptions) && hasImportStarOrImportDefault))
|
||||
&& moduleKind !== ModuleKind.System
|
||||
&& (moduleKind < ModuleKind.ES2015 || node.impliedNodeFormat === ModuleKind.CommonJS);
|
||||
&& getEmitModuleFormatOfFileWorker(node, compilerOptions) < ModuleKind.System;
|
||||
if (!create) {
|
||||
const helpers = getEmitHelpers(node);
|
||||
if (helpers) {
|
||||
|
||||
@@ -36,9 +36,9 @@ import {
|
||||
getBaseFileName,
|
||||
GetCanonicalFileName,
|
||||
getConditions,
|
||||
getDefaultResolutionModeForFileWorker,
|
||||
getDirectoryPath,
|
||||
getEmitModuleResolutionKind,
|
||||
getModeForResolutionAtIndex,
|
||||
getModuleNameStringLiteralAt,
|
||||
getModuleSpecifierEndingPreference,
|
||||
getNodeModulePathParts,
|
||||
@@ -136,12 +136,13 @@ export interface ModuleSpecifierPreferences {
|
||||
/**
|
||||
* @param syntaxImpliedNodeFormat Used when the import syntax implies ESM or CJS irrespective of the mode of the file.
|
||||
*/
|
||||
getAllowedEndingsInPreferredOrder(syntaxImpliedNodeFormat?: SourceFile["impliedNodeFormat"]): ModuleSpecifierEnding[];
|
||||
getAllowedEndingsInPreferredOrder(syntaxImpliedNodeFormat?: ResolutionMode): ModuleSpecifierEnding[];
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function getModuleSpecifierPreferences(
|
||||
{ importModuleSpecifierPreference, importModuleSpecifierEnding }: UserPreferences,
|
||||
host: Pick<ModuleSpecifierResolutionHost, "getDefaultResolutionModeForFile">,
|
||||
compilerOptions: CompilerOptions,
|
||||
importingSourceFile: Pick<SourceFile, "fileName" | "impliedNodeFormat">,
|
||||
oldImportSpecifier?: string,
|
||||
@@ -156,8 +157,10 @@ export function getModuleSpecifierPreferences(
|
||||
importModuleSpecifierPreference === "project-relative" ? RelativePreference.ExternalNonRelative :
|
||||
RelativePreference.Shortest,
|
||||
getAllowedEndingsInPreferredOrder: syntaxImpliedNodeFormat => {
|
||||
const preferredEnding = syntaxImpliedNodeFormat !== importingSourceFile.impliedNodeFormat ? getPreferredEnding(syntaxImpliedNodeFormat) : filePreferredEnding;
|
||||
if ((syntaxImpliedNodeFormat ?? importingSourceFile.impliedNodeFormat) === ModuleKind.ESNext) {
|
||||
const impliedNodeFormat = getDefaultResolutionModeForFile(importingSourceFile, host, compilerOptions);
|
||||
const preferredEnding = syntaxImpliedNodeFormat !== impliedNodeFormat ? getPreferredEnding(syntaxImpliedNodeFormat) : filePreferredEnding;
|
||||
const moduleResolution = getEmitModuleResolutionKind(compilerOptions);
|
||||
if ((syntaxImpliedNodeFormat ?? impliedNodeFormat) === ModuleKind.ESNext && ModuleResolutionKind.Node16 <= moduleResolution && moduleResolution <= ModuleResolutionKind.NodeNext) {
|
||||
if (shouldAllowImportingTsExtension(compilerOptions, importingSourceFile.fileName)) {
|
||||
return [ModuleSpecifierEnding.TsExtension, ModuleSpecifierEnding.JsExtension];
|
||||
}
|
||||
@@ -197,7 +200,7 @@ export function getModuleSpecifierPreferences(
|
||||
}
|
||||
return getModuleSpecifierEndingPreference(
|
||||
importModuleSpecifierEnding,
|
||||
resolutionMode ?? importingSourceFile.impliedNodeFormat,
|
||||
resolutionMode ?? getDefaultResolutionModeForFile(importingSourceFile, host, compilerOptions),
|
||||
compilerOptions,
|
||||
isFullSourceFile(importingSourceFile) ? importingSourceFile : undefined,
|
||||
);
|
||||
@@ -218,7 +221,7 @@ export function updateModuleSpecifier(
|
||||
oldImportSpecifier: string,
|
||||
options: ModuleSpecifierOptions = {},
|
||||
): string | undefined {
|
||||
const res = getModuleSpecifierWorker(compilerOptions, importingSourceFile, importingSourceFileName, toFileName, host, getModuleSpecifierPreferences({}, compilerOptions, importingSourceFile, oldImportSpecifier), {}, options);
|
||||
const res = getModuleSpecifierWorker(compilerOptions, importingSourceFile, importingSourceFileName, toFileName, host, getModuleSpecifierPreferences({}, host, compilerOptions, importingSourceFile, oldImportSpecifier), {}, options);
|
||||
if (res === oldImportSpecifier) return undefined;
|
||||
return res;
|
||||
}
|
||||
@@ -238,7 +241,7 @@ export function getModuleSpecifier(
|
||||
host: ModuleSpecifierResolutionHost,
|
||||
options: ModuleSpecifierOptions = {},
|
||||
): string {
|
||||
return getModuleSpecifierWorker(compilerOptions, importingSourceFile, importingSourceFileName, toFileName, host, getModuleSpecifierPreferences({}, compilerOptions, importingSourceFile), {}, options);
|
||||
return getModuleSpecifierWorker(compilerOptions, importingSourceFile, importingSourceFileName, toFileName, host, getModuleSpecifierPreferences({}, host, compilerOptions, importingSourceFile), {}, options);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
@@ -268,7 +271,7 @@ function getModuleSpecifierWorker(
|
||||
const info = getInfo(importingSourceFileName, host);
|
||||
const modulePaths = getAllModulePaths(info, toFileName, host, userPreferences, options);
|
||||
return firstDefined(modulePaths, modulePath => tryGetModuleNameAsNodeModule(modulePath, info, importingSourceFile, host, compilerOptions, userPreferences, /*packageNameOnly*/ undefined, options.overrideImportMode)) ||
|
||||
getLocalModuleSpecifier(toFileName, info, compilerOptions, host, options.overrideImportMode || importingSourceFile.impliedNodeFormat, preferences);
|
||||
getLocalModuleSpecifier(toFileName, info, compilerOptions, host, options.overrideImportMode || getDefaultResolutionModeForFile(importingSourceFile, host, compilerOptions), preferences);
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
@@ -388,7 +391,7 @@ export function getLocalModuleSpecifierBetweenFileNames(
|
||||
compilerOptions,
|
||||
host,
|
||||
importMode,
|
||||
getModuleSpecifierPreferences({}, compilerOptions, importingFile),
|
||||
getModuleSpecifierPreferences({}, host, compilerOptions, importingFile),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -402,7 +405,7 @@ function computeModuleSpecifiers(
|
||||
forAutoImport: boolean,
|
||||
): readonly string[] {
|
||||
const info = getInfo(importingSourceFile.fileName, host);
|
||||
const preferences = getModuleSpecifierPreferences(userPreferences, compilerOptions, importingSourceFile);
|
||||
const preferences = getModuleSpecifierPreferences(userPreferences, host, compilerOptions, importingSourceFile);
|
||||
const existingSpecifier = isFullSourceFile(importingSourceFile) && forEach(modulePaths, modulePath =>
|
||||
forEach(
|
||||
host.getFileIncludeReasons().get(toPath(modulePath.path, host.getCurrentDirectory(), info.getCanonicalFileName)),
|
||||
@@ -410,7 +413,11 @@ function computeModuleSpecifiers(
|
||||
if (reason.kind !== FileIncludeKind.Import || reason.file !== importingSourceFile.path) return undefined;
|
||||
// If the candidate import mode doesn't match the mode we're generating for, don't consider it
|
||||
// TODO: maybe useful to keep around as an alternative option for certain contexts where the mode is overridable
|
||||
if (importingSourceFile.impliedNodeFormat && importingSourceFile.impliedNodeFormat !== getModeForResolutionAtIndex(importingSourceFile, reason.index, compilerOptions)) return undefined;
|
||||
const existingMode = host.getModeForResolutionAtIndex(importingSourceFile, reason.index);
|
||||
const targetMode = options.overrideImportMode ?? host.getDefaultResolutionModeForFile(importingSourceFile);
|
||||
if (existingMode !== targetMode && existingMode !== undefined && targetMode !== undefined) {
|
||||
return undefined;
|
||||
}
|
||||
const specifier = getModuleNameStringLiteralAt(importingSourceFile, reason.index).text;
|
||||
// If the preference is for non relative and the module specifier is relative, ignore it
|
||||
return preferences.relativePreference !== RelativePreference.NonRelative || !pathIsRelative(specifier) ?
|
||||
@@ -1047,7 +1054,7 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
|
||||
|
||||
// Simplify the full file path to something that can be resolved by Node.
|
||||
|
||||
const preferences = getModuleSpecifierPreferences(userPreferences, options, importingSourceFile);
|
||||
const preferences = getModuleSpecifierPreferences(userPreferences, host, options, importingSourceFile);
|
||||
const allowedEndings = preferences.getAllowedEndingsInPreferredOrder();
|
||||
let moduleSpecifier = path;
|
||||
let isPackageRootPath = false;
|
||||
@@ -1107,7 +1114,7 @@ function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCan
|
||||
const cachedPackageJson = host.getPackageJsonInfoCache?.()?.getPackageJsonInfo(packageJsonPath);
|
||||
if (isPackageJsonInfo(cachedPackageJson) || cachedPackageJson === undefined && host.fileExists(packageJsonPath)) {
|
||||
const packageJsonContent: Record<string, any> | undefined = cachedPackageJson?.contents.packageJsonContent || tryParseJson(host.readFile!(packageJsonPath)!);
|
||||
const importMode = overrideMode || importingSourceFile.impliedNodeFormat;
|
||||
const importMode = overrideMode || getDefaultResolutionModeForFile(importingSourceFile, host, options);
|
||||
if (getResolvePackageJsonExports(options)) {
|
||||
// The package name that we found in node_modules could be different from the package
|
||||
// name in the package.json content via url/filepath dependency specifiers. We need to
|
||||
@@ -1302,3 +1309,7 @@ function getRelativePathIfInSameVolume(path: string, directoryPath: string, getC
|
||||
function isPathRelativeToParent(path: string): boolean {
|
||||
return startsWith(path, "..");
|
||||
}
|
||||
|
||||
function getDefaultResolutionModeForFile(file: Pick<SourceFile, "fileName" | "impliedNodeFormat" | "packageJsonScope">, host: Pick<ModuleSpecifierResolutionHost, "getDefaultResolutionModeForFile">, compilerOptions: CompilerOptions) {
|
||||
return isFullSourceFile(file) ? host.getDefaultResolutionModeForFile(file) : getDefaultResolutionModeForFileWorker(file, compilerOptions);
|
||||
}
|
||||
|
||||
+147
-36
@@ -168,6 +168,7 @@ import {
|
||||
ImportClause,
|
||||
ImportDeclaration,
|
||||
ImportOrExportSpecifier,
|
||||
importSyntaxAffectsModuleResolution,
|
||||
InternalEmitFlags,
|
||||
inverseJsxOptionMap,
|
||||
isAmbientModule,
|
||||
@@ -834,9 +835,11 @@ export function flattenDiagnosticMessageText(diag: string | DiagnosticMessageCha
|
||||
* @internal
|
||||
*/
|
||||
export interface SourceFileImportsList {
|
||||
/** @internal */ imports: SourceFile["imports"];
|
||||
/** @internal */ moduleAugmentations: SourceFile["moduleAugmentations"];
|
||||
imports: SourceFile["imports"];
|
||||
moduleAugmentations: SourceFile["moduleAugmentations"];
|
||||
impliedNodeFormat?: ResolutionMode;
|
||||
fileName: string;
|
||||
packageJsonScope?: SourceFile["packageJsonScope"];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -882,22 +885,47 @@ export function isExclusivelyTypeOnlyImportOrExport(decl: ImportDeclaration | Ex
|
||||
|
||||
/**
|
||||
* Use `program.getModeForUsageLocation`, which retrieves the correct `compilerOptions`, instead of this function whenever possible.
|
||||
* Calculates the final resolution mode for a given module reference node. This is the resolution mode explicitly provided via import
|
||||
* attributes, if present, or the syntax the usage would have if emitted to JavaScript. In `--module node16` or `nodenext`, this may
|
||||
* depend on the file's `impliedNodeFormat`. In `--module preserve`, it depends only on the input syntax of the reference. In other
|
||||
* `module` modes, when overriding import attributes are not provided, this function returns `undefined`, as the result would have no
|
||||
* impact on module resolution, emit, or type checking.
|
||||
* Calculates the final resolution mode for a given module reference node. This function only returns a result when module resolution
|
||||
* settings allow differing resolution between ESM imports and CJS requires, or when a mode is explicitly provided via import attributes,
|
||||
* which cause an `import` or `require` condition to be used during resolution regardless of module resolution settings. In absence of
|
||||
* overriding attributes, and in modes that support differing resolution, the result indicates the syntax the usage would emit to JavaScript.
|
||||
* Some examples:
|
||||
*
|
||||
* ```ts
|
||||
* // tsc foo.mts --module nodenext
|
||||
* import {} from "mod";
|
||||
* // Result: ESNext - the import emits as ESM due to `impliedNodeFormat` set by .mts file extension
|
||||
*
|
||||
* // tsc foo.cts --module nodenext
|
||||
* import {} from "mod";
|
||||
* // Result: CommonJS - the import emits as CJS due to `impliedNodeFormat` set by .cts file extension
|
||||
*
|
||||
* // tsc foo.ts --module preserve --moduleResolution bundler
|
||||
* import {} from "mod";
|
||||
* // Result: ESNext - the import emits as ESM due to `--module preserve` and `--moduleResolution bundler`
|
||||
* // supports conditional imports/exports
|
||||
*
|
||||
* // tsc foo.ts --module preserve --moduleResolution node10
|
||||
* import {} from "mod";
|
||||
* // Result: undefined - the import emits as ESM due to `--module preserve`, but `--moduleResolution node10`
|
||||
* // does not support conditional imports/exports
|
||||
*
|
||||
* // tsc foo.ts --module commonjs --moduleResolution node10
|
||||
* import type {} from "mod" with { "resolution-mode": "import" };
|
||||
* // Result: ESNext - conditional imports/exports always supported with "resolution-mode" attribute
|
||||
* ```
|
||||
*
|
||||
* @param file The file the import or import-like reference is contained within
|
||||
* @param usage The module reference string
|
||||
* @param compilerOptions The compiler options for the program that owns the file. If the file belongs to a referenced project, the compiler options
|
||||
* should be the options of the referenced project, not the referencing project.
|
||||
* @returns The final resolution mode of the import
|
||||
*/
|
||||
export function getModeForUsageLocation(file: { impliedNodeFormat?: ResolutionMode; }, usage: StringLiteralLike, compilerOptions: CompilerOptions) {
|
||||
export function getModeForUsageLocation(file: SourceFile, usage: StringLiteralLike, compilerOptions: CompilerOptions) {
|
||||
return getModeForUsageLocationWorker(file, usage, compilerOptions);
|
||||
}
|
||||
|
||||
function getModeForUsageLocationWorker(file: { impliedNodeFormat?: ResolutionMode; }, usage: StringLiteralLike, compilerOptions?: CompilerOptions) {
|
||||
function getModeForUsageLocationWorker(file: Pick<SourceFile, "fileName" | "impliedNodeFormat" | "packageJsonScope">, usage: StringLiteralLike, compilerOptions?: CompilerOptions) {
|
||||
if ((isImportDeclaration(usage.parent) || isExportDeclaration(usage.parent))) {
|
||||
const isTypeOnly = isExclusivelyTypeOnlyImportOrExport(usage.parent);
|
||||
if (isTypeOnly) {
|
||||
@@ -913,20 +941,36 @@ function getModeForUsageLocationWorker(file: { impliedNodeFormat?: ResolutionMod
|
||||
return override;
|
||||
}
|
||||
}
|
||||
if (compilerOptions && getEmitModuleKind(compilerOptions) === ModuleKind.Preserve) {
|
||||
return (usage.parent.parent && isImportEqualsDeclaration(usage.parent.parent) || isRequireCall(usage.parent, /*requireStringLiteralLikeArgument*/ false))
|
||||
? ModuleKind.CommonJS
|
||||
: ModuleKind.ESNext;
|
||||
|
||||
if (compilerOptions && importSyntaxAffectsModuleResolution(compilerOptions)) {
|
||||
return getEmitSyntaxForUsageLocationWorker(file, usage, compilerOptions);
|
||||
}
|
||||
if (file.impliedNodeFormat === undefined) return undefined;
|
||||
if (file.impliedNodeFormat !== ModuleKind.ESNext) {
|
||||
// in cjs files, import call expressions are esm format, otherwise everything is cjs
|
||||
return isImportCall(walkUpParenthesizedExpressions(usage.parent)) ? ModuleKind.ESNext : ModuleKind.CommonJS;
|
||||
}
|
||||
|
||||
function getEmitSyntaxForUsageLocationWorker(file: Pick<SourceFile, "fileName" | "impliedNodeFormat" | "packageJsonScope">, usage: StringLiteralLike, compilerOptions?: CompilerOptions): ResolutionMode {
|
||||
if (!compilerOptions) {
|
||||
// This should always be provided, but we try to fail somewhat
|
||||
// gracefully to allow projects like ts-node time to update.
|
||||
return undefined;
|
||||
}
|
||||
// in esm files, import=require statements are cjs format, otherwise everything is esm
|
||||
// imports are only parent'd up to their containing declaration/expression, so access farther parents with care
|
||||
const exprParentParent = walkUpParenthesizedExpressions(usage.parent)?.parent;
|
||||
return exprParentParent && isImportEqualsDeclaration(exprParentParent) ? ModuleKind.CommonJS : ModuleKind.ESNext;
|
||||
if (exprParentParent && isImportEqualsDeclaration(exprParentParent) || isRequireCall(usage.parent, /*requireStringLiteralLikeArgument*/ false)) {
|
||||
return ModuleKind.CommonJS;
|
||||
}
|
||||
if (isImportCall(walkUpParenthesizedExpressions(usage.parent))) {
|
||||
return shouldTransformImportCallWorker(file, compilerOptions) ? ModuleKind.CommonJS : ModuleKind.ESNext;
|
||||
}
|
||||
// If we're in --module preserve on an input file, we know that an import
|
||||
// is an import. But if this is a declaration file, we'd prefer to use the
|
||||
// impliedNodeFormat. Since we want things to be consistent between the two,
|
||||
// we need to issue errors when the user writes ESM syntax in a definitely-CJS
|
||||
// file, until/unless declaration emit can indicate a true ESM import. On the
|
||||
// other hand, writing CJS syntax in a definitely-ESM file is fine, since declaration
|
||||
// emit preserves the CJS syntax.
|
||||
const fileEmitMode = getEmitModuleFormatOfFileWorker(file, compilerOptions);
|
||||
return fileEmitMode === ModuleKind.CommonJS ? ModuleKind.CommonJS :
|
||||
emitModuleKindIsNonNodeESM(fileEmitMode) || fileEmitMode === ModuleKind.Preserve ? ModuleKind.ESNext :
|
||||
undefined;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
@@ -1017,7 +1061,7 @@ function getTypeReferenceResolutionName<T extends FileReference | string>(entry:
|
||||
|
||||
const typeReferenceResolutionNameAndModeGetter: ResolutionNameAndModeGetter<FileReference | string, SourceFile | undefined> = {
|
||||
getName: getTypeReferenceResolutionName,
|
||||
getMode: (entry, file) => getModeForFileReference(entry, file?.impliedNodeFormat),
|
||||
getMode: (entry, file, compilerOptions) => getModeForFileReference(entry, file && getDefaultResolutionModeForFileWorker(file, compilerOptions)),
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
@@ -1339,16 +1383,11 @@ export function getImpliedNodeFormatForFileWorker(
|
||||
host: ModuleResolutionHost,
|
||||
options: CompilerOptions,
|
||||
) {
|
||||
switch (getEmitModuleResolutionKind(options)) {
|
||||
case ModuleResolutionKind.Node16:
|
||||
case ModuleResolutionKind.NodeNext:
|
||||
return fileExtensionIsOneOf(fileName, [Extension.Dmts, Extension.Mts, Extension.Mjs]) ? ModuleKind.ESNext :
|
||||
fileExtensionIsOneOf(fileName, [Extension.Dcts, Extension.Cts, Extension.Cjs]) ? ModuleKind.CommonJS :
|
||||
fileExtensionIsOneOf(fileName, [Extension.Dts, Extension.Ts, Extension.Tsx, Extension.Js, Extension.Jsx]) ? lookupFromPackageJson() :
|
||||
undefined; // other extensions, like `json` or `tsbuildinfo`, are set as `undefined` here but they should never be fed through the transformer pipeline
|
||||
default:
|
||||
return undefined;
|
||||
}
|
||||
return fileExtensionIsOneOf(fileName, [Extension.Dmts, Extension.Mts, Extension.Mjs]) ? ModuleKind.ESNext :
|
||||
fileExtensionIsOneOf(fileName, [Extension.Dcts, Extension.Cts, Extension.Cjs]) ? ModuleKind.CommonJS :
|
||||
fileExtensionIsOneOf(fileName, [Extension.Dts, Extension.Ts, Extension.Tsx, Extension.Js, Extension.Jsx]) ? lookupFromPackageJson() :
|
||||
undefined; // other extensions, like `json` or `tsbuildinfo`, are set as `undefined` here but they should never be fed through the transformer pipeline
|
||||
|
||||
function lookupFromPackageJson(): Partial<CreateSourceFileOptions> {
|
||||
const state = getTemporaryModuleResolutionState(packageJsonInfoCache, host, options);
|
||||
const packageJsonLocations: string[] = [];
|
||||
@@ -1899,6 +1938,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
|
||||
isSourceFileFromExternalLibrary,
|
||||
isSourceFileDefaultLibrary,
|
||||
getModeForUsageLocation,
|
||||
getEmitSyntaxForUsageLocation,
|
||||
getModeForResolutionAtIndex,
|
||||
getSourceFileFromReference,
|
||||
getLibFileFromReference,
|
||||
@@ -1926,6 +1966,11 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
|
||||
forEachResolvedProjectReference,
|
||||
isSourceOfProjectReferenceRedirect,
|
||||
getRedirectReferenceForResolutionFromSourceOfProject,
|
||||
getCompilerOptionsForFile,
|
||||
getDefaultResolutionModeForFile,
|
||||
getEmitModuleFormatOfFile,
|
||||
getImpliedNodeFormatForEmit,
|
||||
shouldTransformImportCall,
|
||||
emitBuildInfo,
|
||||
fileExists,
|
||||
readFile,
|
||||
@@ -2640,6 +2685,10 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
|
||||
getSymlinkCache,
|
||||
writeFile: writeFileCallback || writeFile,
|
||||
isEmitBlocked,
|
||||
shouldTransformImportCall,
|
||||
getEmitModuleFormatOfFile,
|
||||
getDefaultResolutionModeForFile,
|
||||
getModeForResolutionAtIndex,
|
||||
readFile: f => host.readFile(f),
|
||||
fileExists: f => {
|
||||
// Use local caches
|
||||
@@ -3871,11 +3920,15 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
|
||||
// store resolved type directive on the file
|
||||
const fileName = toFileNameLowerCase(ref.fileName);
|
||||
resolutionsInFile.set(fileName, getModeForFileReference(ref, file.impliedNodeFormat), resolvedTypeReferenceDirective);
|
||||
const mode = ref.resolutionMode || file.impliedNodeFormat;
|
||||
const mode = ref.resolutionMode || getDefaultResolutionModeForFile(file);
|
||||
processTypeReferenceDirective(fileName, mode, resolvedTypeReferenceDirective, { kind: FileIncludeKind.TypeReferenceDirective, file: file.path, index });
|
||||
}
|
||||
}
|
||||
|
||||
function getCompilerOptionsForFile(file: SourceFile): CompilerOptions {
|
||||
return getRedirectReferenceForResolution(file)?.commandLine.options || options;
|
||||
}
|
||||
|
||||
function processTypeReferenceDirective(
|
||||
typeReferenceDirective: string,
|
||||
mode: ResolutionMode,
|
||||
@@ -4033,7 +4086,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
|
||||
const resolutions = resolvedModulesProcessing?.get(file.path) ||
|
||||
resolveModuleNamesReusingOldState(moduleNames, file);
|
||||
Debug.assert(resolutions.length === moduleNames.length);
|
||||
const optionsForFile = getRedirectReferenceForResolution(file)?.commandLine.options || options;
|
||||
const optionsForFile = getCompilerOptionsForFile(file);
|
||||
const resolutionsInFile = createModeAwareCache<ResolutionWithFailedLookupLocations>();
|
||||
(resolvedModules ??= new Map()).set(file.path, resolutionsInFile);
|
||||
for (let index = 0; index < moduleNames.length; index++) {
|
||||
@@ -4604,7 +4657,8 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
|
||||
if (locationReason && fileIncludeReasons?.length === 1) fileIncludeReasons = undefined;
|
||||
const location = locationReason && getReferencedFileLocation(program, locationReason);
|
||||
const fileIncludeReasonDetails = fileIncludeReasons && chainDiagnosticMessages(fileIncludeReasons, Diagnostics.The_file_is_in_the_program_because_Colon);
|
||||
const redirectInfo = file && explainIfFileIsRedirectAndImpliedFormat(file);
|
||||
const optionsForFile = file && getCompilerOptionsForFile(file) || options;
|
||||
const redirectInfo = file && explainIfFileIsRedirectAndImpliedFormat(file, optionsForFile);
|
||||
const chain = chainDiagnosticMessages(redirectInfo ? fileIncludeReasonDetails ? [fileIncludeReasonDetails, ...redirectInfo] : redirectInfo : fileIncludeReasonDetails, diagnostic, ...args || emptyArray);
|
||||
return location && isReferenceFileLocation(location) ?
|
||||
createFileDiagnosticFromMessageChain(location.file, location.pos, location.end - location.pos, chain, relatedInfo) :
|
||||
@@ -4934,13 +4988,70 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
|
||||
}
|
||||
|
||||
function getModeForUsageLocation(file: SourceFile, usage: StringLiteralLike): ResolutionMode {
|
||||
const optionsForFile = getRedirectReferenceForResolution(file)?.commandLine.options || options;
|
||||
return getModeForUsageLocationWorker(file, usage, optionsForFile);
|
||||
return getModeForUsageLocationWorker(file, usage, getCompilerOptionsForFile(file));
|
||||
}
|
||||
|
||||
function getEmitSyntaxForUsageLocation(file: SourceFile, usage: StringLiteralLike): ResolutionMode {
|
||||
return getEmitSyntaxForUsageLocationWorker(file, usage, getCompilerOptionsForFile(file));
|
||||
}
|
||||
|
||||
function getModeForResolutionAtIndex(file: SourceFile, index: number): ResolutionMode {
|
||||
return getModeForUsageLocation(file, getModuleNameStringLiteralAt(file, index));
|
||||
}
|
||||
|
||||
function getDefaultResolutionModeForFile(sourceFile: SourceFile): ResolutionMode {
|
||||
return getDefaultResolutionModeForFileWorker(sourceFile, getCompilerOptionsForFile(sourceFile));
|
||||
}
|
||||
|
||||
function getImpliedNodeFormatForEmit(sourceFile: SourceFile): ResolutionMode {
|
||||
return getImpliedNodeFormatForEmitWorker(sourceFile, getCompilerOptionsForFile(sourceFile));
|
||||
}
|
||||
|
||||
function getEmitModuleFormatOfFile(sourceFile: SourceFile): ModuleKind {
|
||||
return getEmitModuleFormatOfFileWorker(sourceFile, getCompilerOptionsForFile(sourceFile));
|
||||
}
|
||||
|
||||
function shouldTransformImportCall(sourceFile: SourceFile): boolean {
|
||||
return shouldTransformImportCallWorker(sourceFile, getCompilerOptionsForFile(sourceFile));
|
||||
}
|
||||
}
|
||||
|
||||
function shouldTransformImportCallWorker(sourceFile: Pick<SourceFile, "fileName" | "impliedNodeFormat" | "packageJsonScope">, options: CompilerOptions): boolean {
|
||||
const moduleKind = getEmitModuleKind(options);
|
||||
if (ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext || moduleKind === ModuleKind.Preserve) {
|
||||
return false;
|
||||
}
|
||||
return getEmitModuleFormatOfFileWorker(sourceFile, options) < ModuleKind.ES2015;
|
||||
}
|
||||
/** @internal Prefer `program.getEmitModuleFormatOfFile` when possible. */
|
||||
export function getEmitModuleFormatOfFileWorker(sourceFile: Pick<SourceFile, "fileName" | "impliedNodeFormat" | "packageJsonScope">, options: CompilerOptions): ModuleKind {
|
||||
return getImpliedNodeFormatForEmitWorker(sourceFile, options) ?? getEmitModuleKind(options);
|
||||
}
|
||||
/** @internal Prefer `program.getImpliedNodeFormatForEmit` when possible. */
|
||||
export function getImpliedNodeFormatForEmitWorker(sourceFile: Pick<SourceFile, "fileName" | "impliedNodeFormat" | "packageJsonScope">, options: CompilerOptions): ResolutionMode {
|
||||
const moduleKind = getEmitModuleKind(options);
|
||||
if (ModuleKind.Node16 <= moduleKind && moduleKind <= ModuleKind.NodeNext) {
|
||||
return sourceFile.impliedNodeFormat;
|
||||
}
|
||||
if (
|
||||
sourceFile.impliedNodeFormat === ModuleKind.CommonJS
|
||||
&& (sourceFile.packageJsonScope?.contents.packageJsonContent.type === "commonjs"
|
||||
|| fileExtensionIsOneOf(sourceFile.fileName, [Extension.Cjs, Extension.Cts]))
|
||||
) {
|
||||
return ModuleKind.CommonJS;
|
||||
}
|
||||
if (
|
||||
sourceFile.impliedNodeFormat === ModuleKind.ESNext
|
||||
&& (sourceFile.packageJsonScope?.contents.packageJsonContent.type === "module"
|
||||
|| fileExtensionIsOneOf(sourceFile.fileName, [Extension.Mjs, Extension.Mts]))
|
||||
) {
|
||||
return ModuleKind.ESNext;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
/** @internal Prefer `program.getDefaultResolutionModeForFile` when possible. */
|
||||
export function getDefaultResolutionModeForFileWorker(sourceFile: Pick<SourceFile, "fileName" | "impliedNodeFormat" | "packageJsonScope">, options: CompilerOptions): ResolutionMode {
|
||||
return importSyntaxAffectsModuleResolution(options) ? getImpliedNodeFormatForEmitWorker(sourceFile, options) : undefined;
|
||||
}
|
||||
|
||||
interface HostForUseSourceOfProjectReferenceRedirect {
|
||||
|
||||
@@ -65,10 +65,10 @@ import {
|
||||
transformESDecorators,
|
||||
transformESNext,
|
||||
transformGenerators,
|
||||
transformImpliedNodeFormatDependentModule,
|
||||
transformJsx,
|
||||
transformLegacyDecorators,
|
||||
transformModule,
|
||||
transformNodeModule,
|
||||
transformSystemModule,
|
||||
transformTypeScript,
|
||||
VariableDeclaration,
|
||||
@@ -77,17 +77,23 @@ import * as performance from "./_namespaces/ts.performance";
|
||||
|
||||
function getModuleTransformer(moduleKind: ModuleKind): TransformerFactory<SourceFile | Bundle> {
|
||||
switch (moduleKind) {
|
||||
case ModuleKind.Preserve:
|
||||
// `transformECMAScriptModule` contains logic for preserving
|
||||
// CJS input syntax in `--module preserve`
|
||||
return transformECMAScriptModule;
|
||||
case ModuleKind.ESNext:
|
||||
case ModuleKind.ES2022:
|
||||
case ModuleKind.ES2020:
|
||||
case ModuleKind.ES2015:
|
||||
case ModuleKind.Preserve:
|
||||
return transformECMAScriptModule;
|
||||
case ModuleKind.System:
|
||||
return transformSystemModule;
|
||||
case ModuleKind.Node16:
|
||||
case ModuleKind.NodeNext:
|
||||
return transformNodeModule;
|
||||
case ModuleKind.CommonJS:
|
||||
// Wraps `transformModule` and `transformECMAScriptModule` and
|
||||
// selects between them based on the `impliedNodeFormat` of the
|
||||
// source file.
|
||||
return transformImpliedNodeFormatDependentModule;
|
||||
case ModuleKind.System:
|
||||
return transformSystemModule;
|
||||
default:
|
||||
return transformModule;
|
||||
}
|
||||
|
||||
+5
-4
@@ -14,7 +14,7 @@ import {
|
||||
} from "../../_namespaces/ts";
|
||||
|
||||
/** @internal */
|
||||
export function transformNodeModule(context: TransformationContext) {
|
||||
export function transformImpliedNodeFormatDependentModule(context: TransformationContext) {
|
||||
const previousOnSubstituteNode = context.onSubstituteNode;
|
||||
const previousOnEmitNode = context.onEmitNode;
|
||||
|
||||
@@ -30,6 +30,7 @@ export function transformNodeModule(context: TransformationContext) {
|
||||
|
||||
const cjsOnSubstituteNode = context.onSubstituteNode;
|
||||
const cjsOnEmitNode = context.onEmitNode;
|
||||
const getEmitModuleFormatOfFile = (file: SourceFile) => context.getEmitHost().getEmitModuleFormatOfFile(file);
|
||||
|
||||
context.onSubstituteNode = onSubstituteNode;
|
||||
context.onEmitNode = onEmitNode;
|
||||
@@ -51,7 +52,7 @@ export function transformNodeModule(context: TransformationContext) {
|
||||
if (!currentSourceFile) {
|
||||
return previousOnSubstituteNode(hint, node);
|
||||
}
|
||||
if (currentSourceFile.impliedNodeFormat === ModuleKind.ESNext) {
|
||||
if (getEmitModuleFormatOfFile(currentSourceFile) >= ModuleKind.ES2015) {
|
||||
return esmOnSubstituteNode(hint, node);
|
||||
}
|
||||
return cjsOnSubstituteNode(hint, node);
|
||||
@@ -65,14 +66,14 @@ export function transformNodeModule(context: TransformationContext) {
|
||||
if (!currentSourceFile) {
|
||||
return previousOnEmitNode(hint, node, emitCallback);
|
||||
}
|
||||
if (currentSourceFile.impliedNodeFormat === ModuleKind.ESNext) {
|
||||
if (getEmitModuleFormatOfFile(currentSourceFile) >= ModuleKind.ES2015) {
|
||||
return esmOnEmitNode(hint, node, emitCallback);
|
||||
}
|
||||
return cjsOnEmitNode(hint, node, emitCallback);
|
||||
}
|
||||
|
||||
function getModuleTransformForFile(file: SourceFile): typeof esmTransform {
|
||||
return file.impliedNodeFormat === ModuleKind.ESNext ? esmTransform : cjsTransform;
|
||||
return getEmitModuleFormatOfFile(file) >= ModuleKind.ES2015 ? esmTransform : cjsTransform;
|
||||
}
|
||||
|
||||
function transformSourceFile(node: SourceFile) {
|
||||
@@ -789,7 +789,7 @@ export function transformModule(context: TransformationContext): (x: SourceFile
|
||||
case SyntaxKind.PartiallyEmittedExpression:
|
||||
return visitPartiallyEmittedExpression(node as PartiallyEmittedExpression, valueIsDiscarded);
|
||||
case SyntaxKind.CallExpression:
|
||||
if (isImportCall(node) && currentSourceFile.impliedNodeFormat === undefined) {
|
||||
if (isImportCall(node) && host.shouldTransformImportCall(currentSourceFile)) {
|
||||
return visitImportCallExpression(node);
|
||||
}
|
||||
break;
|
||||
|
||||
+81
-10
@@ -4720,21 +4720,79 @@ export interface Program extends ScriptReferenceHost {
|
||||
isSourceFileFromExternalLibrary(file: SourceFile): boolean;
|
||||
isSourceFileDefaultLibrary(file: SourceFile): boolean;
|
||||
/**
|
||||
* Calculates the final resolution mode for a given module reference node. This is the resolution mode explicitly provided via import
|
||||
* attributes, if present, or the syntax the usage would have if emitted to JavaScript. In `--module node16` or `nodenext`, this may
|
||||
* depend on the file's `impliedNodeFormat`. In `--module preserve`, it depends only on the input syntax of the reference. In other
|
||||
* `module` modes, when overriding import attributes are not provided, this function returns `undefined`, as the result would have no
|
||||
* impact on module resolution, emit, or type checking.
|
||||
* Calculates the final resolution mode for a given module reference node. This function only returns a result when module resolution
|
||||
* settings allow differing resolution between ESM imports and CJS requires, or when a mode is explicitly provided via import attributes,
|
||||
* which cause an `import` or `require` condition to be used during resolution regardless of module resolution settings. In absence of
|
||||
* overriding attributes, and in modes that support differing resolution, the result indicates the syntax the usage would emit to JavaScript.
|
||||
* Some examples:
|
||||
*
|
||||
* ```ts
|
||||
* // tsc foo.mts --module nodenext
|
||||
* import {} from "mod";
|
||||
* // Result: ESNext - the import emits as ESM due to `impliedNodeFormat` set by .mts file extension
|
||||
*
|
||||
* // tsc foo.cts --module nodenext
|
||||
* import {} from "mod";
|
||||
* // Result: CommonJS - the import emits as CJS due to `impliedNodeFormat` set by .cts file extension
|
||||
*
|
||||
* // tsc foo.ts --module preserve --moduleResolution bundler
|
||||
* import {} from "mod";
|
||||
* // Result: ESNext - the import emits as ESM due to `--module preserve` and `--moduleResolution bundler`
|
||||
* // supports conditional imports/exports
|
||||
*
|
||||
* // tsc foo.ts --module preserve --moduleResolution node10
|
||||
* import {} from "mod";
|
||||
* // Result: undefined - the import emits as ESM due to `--module preserve`, but `--moduleResolution node10`
|
||||
* // does not support conditional imports/exports
|
||||
*
|
||||
* // tsc foo.ts --module commonjs --moduleResolution node10
|
||||
* import type {} from "mod" with { "resolution-mode": "import" };
|
||||
* // Result: ESNext - conditional imports/exports always supported with "resolution-mode" attribute
|
||||
* ```
|
||||
*/
|
||||
getModeForUsageLocation(file: SourceFile, usage: StringLiteralLike): ResolutionMode;
|
||||
/**
|
||||
* Calculates the final resolution mode for an import at some index within a file's `imports` list. This is the resolution mode
|
||||
* explicitly provided via import attributes, if present, or the syntax the usage would have if emitted to JavaScript. In
|
||||
* `--module node16` or `nodenext`, this may depend on the file's `impliedNodeFormat`. In `--module preserve`, it depends only on the
|
||||
* input syntax of the reference. In other `module` modes, when overriding import attributes are not provided, this function returns
|
||||
* `undefined`, as the result would have no impact on module resolution, emit, or type checking.
|
||||
* Calculates the final resolution mode for an import at some index within a file's `imports` list. This function only returns a result
|
||||
* when module resolution settings allow differing resolution between ESM imports and CJS requires, or when a mode is explicitly provided
|
||||
* via import attributes, which cause an `import` or `require` condition to be used during resolution regardless of module resolution
|
||||
* settings. In absence of overriding attributes, and in modes that support differing resolution, the result indicates the syntax the
|
||||
* usage would emit to JavaScript. Some examples:
|
||||
*
|
||||
* ```ts
|
||||
* // tsc foo.mts --module nodenext
|
||||
* import {} from "mod";
|
||||
* // Result: ESNext - the import emits as ESM due to `impliedNodeFormat` set by .mts file extension
|
||||
*
|
||||
* // tsc foo.cts --module nodenext
|
||||
* import {} from "mod";
|
||||
* // Result: CommonJS - the import emits as CJS due to `impliedNodeFormat` set by .cts file extension
|
||||
*
|
||||
* // tsc foo.ts --module preserve --moduleResolution bundler
|
||||
* import {} from "mod";
|
||||
* // Result: ESNext - the import emits as ESM due to `--module preserve` and `--moduleResolution bundler`
|
||||
* // supports conditional imports/exports
|
||||
*
|
||||
* // tsc foo.ts --module preserve --moduleResolution node10
|
||||
* import {} from "mod";
|
||||
* // Result: undefined - the import emits as ESM due to `--module preserve`, but `--moduleResolution node10`
|
||||
* // does not support conditional imports/exports
|
||||
*
|
||||
* // tsc foo.ts --module commonjs --moduleResolution node10
|
||||
* import type {} from "mod" with { "resolution-mode": "import" };
|
||||
* // Result: ESNext - conditional imports/exports always supported with "resolution-mode" attribute
|
||||
* ```
|
||||
*/
|
||||
getModeForResolutionAtIndex(file: SourceFile, index: number): ResolutionMode;
|
||||
/**
|
||||
* @internal
|
||||
* The resolution mode to use for module resolution or module specifier resolution
|
||||
* outside the context of an existing module reference, where
|
||||
* `program.getModeForUsageLocation` should be used instead.
|
||||
*/
|
||||
getDefaultResolutionModeForFile(sourceFile: SourceFile): ResolutionMode;
|
||||
/** @internal */ getImpliedNodeFormatForEmit(sourceFile: SourceFile): ResolutionMode;
|
||||
/** @internal */ getEmitModuleFormatOfFile(sourceFile: SourceFile): ModuleKind;
|
||||
/** @internal */ shouldTransformImportCall(sourceFile: SourceFile): boolean;
|
||||
|
||||
// For testing purposes only.
|
||||
// This is set on created program to let us know how the program was created using old program
|
||||
@@ -4789,6 +4847,7 @@ export interface Program extends ScriptReferenceHost {
|
||||
/** @internal */ getResolvedProjectReferenceByPath(projectReferencePath: Path): ResolvedProjectReference | undefined;
|
||||
/** @internal */ getRedirectReferenceForResolutionFromSourceOfProject(filePath: Path): ResolvedProjectReference | undefined;
|
||||
/** @internal */ isSourceOfProjectReferenceRedirect(fileName: string): boolean;
|
||||
/** @internal */ getCompilerOptionsForFile(file: SourceFile): CompilerOptions;
|
||||
/** @internal */ getBuildInfo?(): BuildInfo;
|
||||
/** @internal */ emitBuildInfo(writeFile?: WriteFileCallback, cancellationToken?: CancellationToken): EmitResult;
|
||||
/**
|
||||
@@ -4904,8 +4963,12 @@ export interface TypeCheckerHost extends ModuleSpecifierResolutionHost {
|
||||
getSourceFile(fileName: string): SourceFile | undefined;
|
||||
getProjectReferenceRedirect(fileName: string): string | undefined;
|
||||
isSourceOfProjectReferenceRedirect(fileName: string): boolean;
|
||||
getEmitSyntaxForUsageLocation(file: SourceFile, usage: StringLiteralLike): ResolutionMode;
|
||||
getRedirectReferenceForResolutionFromSourceOfProject(filePath: Path): ResolvedProjectReference | undefined;
|
||||
getModeForUsageLocation(file: SourceFile, usage: StringLiteralLike): ResolutionMode;
|
||||
getDefaultResolutionModeForFile(sourceFile: SourceFile): ResolutionMode;
|
||||
getImpliedNodeFormatForEmit(sourceFile: SourceFile): ResolutionMode;
|
||||
getEmitModuleFormatOfFile(sourceFile: SourceFile): ModuleKind;
|
||||
|
||||
getResolvedModule(f: SourceFile, moduleName: string, mode: ResolutionMode): ResolvedModuleWithFailedLookupLocations | undefined;
|
||||
|
||||
@@ -5560,6 +5623,9 @@ export interface RequireVariableDeclarationList extends VariableDeclarationList
|
||||
readonly declarations: NodeArray<VariableDeclarationInitializedTo<RequireOrImportCall>>;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export type CanHaveModuleSpecifier = AnyImportOrBareOrAccessedRequire | AliasDeclarationNode | ExportDeclaration | ImportTypeNode;
|
||||
|
||||
/** @internal */
|
||||
export type LateVisibilityPaintedStatement =
|
||||
| AnyImportOrJsDocImport
|
||||
@@ -8256,6 +8322,8 @@ export interface EmitHost extends ScriptReferenceHost, ModuleSpecifierResolution
|
||||
getCanonicalFileName(fileName: string): string;
|
||||
|
||||
isEmitBlocked(emitFileName: string): boolean;
|
||||
shouldTransformImportCall(sourceFile: SourceFile): boolean;
|
||||
getEmitModuleFormatOfFile(sourceFile: SourceFile): ModuleKind;
|
||||
|
||||
writeFile: WriteFileCallback;
|
||||
getBuildInfo(): BuildInfo | undefined;
|
||||
@@ -9497,6 +9565,7 @@ export interface PrinterOptions {
|
||||
omitTrailingSemicolon?: boolean;
|
||||
noEmitHelpers?: boolean;
|
||||
/** @internal */ module?: CompilerOptions["module"];
|
||||
/** @internal */ moduleResolution?: CompilerOptions["moduleResolution"];
|
||||
/** @internal */ target?: CompilerOptions["target"];
|
||||
/** @internal */ sourceMap?: boolean;
|
||||
/** @internal */ inlineSourceMap?: boolean;
|
||||
@@ -9631,6 +9700,8 @@ export interface ModuleSpecifierResolutionHost {
|
||||
isSourceOfProjectReferenceRedirect(fileName: string): boolean;
|
||||
getFileIncludeReasons(): MultiMap<Path, FileIncludeReason>;
|
||||
getCommonSourceDirectory(): string;
|
||||
getDefaultResolutionModeForFile(sourceFile: SourceFile): ResolutionMode;
|
||||
getModeForResolutionAtIndex(file: SourceFile, index: number): ResolutionMode;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
|
||||
@@ -5,7 +5,6 @@ import {
|
||||
addRange,
|
||||
affectsDeclarationPathOptionDeclarations,
|
||||
affectsEmitOptionDeclarations,
|
||||
AliasDeclarationNode,
|
||||
AllAccessorDeclarations,
|
||||
AmbientModuleDeclaration,
|
||||
AmpersandAmpersandEqualsToken,
|
||||
@@ -41,6 +40,7 @@ import {
|
||||
canHaveDecorators,
|
||||
canHaveLocals,
|
||||
canHaveModifiers,
|
||||
type CanHaveModuleSpecifier,
|
||||
CaseBlock,
|
||||
CaseClause,
|
||||
CaseOrDefaultClause,
|
||||
@@ -167,6 +167,7 @@ import {
|
||||
getCommonSourceDirectory,
|
||||
getContainerFlags,
|
||||
getDirectoryPath,
|
||||
getImpliedNodeFormatForEmitWorker,
|
||||
getJSDocAugmentsTag,
|
||||
getJSDocDeprecatedTagNoCache,
|
||||
getJSDocImplementsTags,
|
||||
@@ -4017,7 +4018,26 @@ export function isFunctionSymbol(symbol: Symbol | undefined) {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function tryGetModuleSpecifierFromDeclaration(node: AnyImportOrBareOrAccessedRequire | AliasDeclarationNode | ExportDeclaration | ImportTypeNode | JSDocImportTag): StringLiteralLike | undefined {
|
||||
export function canHaveModuleSpecifier(node: Node | undefined): node is CanHaveModuleSpecifier {
|
||||
switch (node?.kind) {
|
||||
case SyntaxKind.VariableDeclaration:
|
||||
case SyntaxKind.BindingElement:
|
||||
case SyntaxKind.ImportDeclaration:
|
||||
case SyntaxKind.ExportDeclaration:
|
||||
case SyntaxKind.ImportEqualsDeclaration:
|
||||
case SyntaxKind.ImportClause:
|
||||
case SyntaxKind.NamespaceExport:
|
||||
case SyntaxKind.NamespaceImport:
|
||||
case SyntaxKind.ExportSpecifier:
|
||||
case SyntaxKind.ImportSpecifier:
|
||||
case SyntaxKind.ImportType:
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function tryGetModuleSpecifierFromDeclaration(node: CanHaveModuleSpecifier | JSDocImportTag): StringLiteralLike | undefined {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.VariableDeclaration:
|
||||
case SyntaxKind.BindingElement:
|
||||
@@ -8589,11 +8609,13 @@ function isFileModuleFromUsingJSXTag(file: SourceFile): Node | undefined {
|
||||
* Note that this requires file.impliedNodeFormat be set already; meaning it must be set very early on
|
||||
* in SourceFile construction.
|
||||
*/
|
||||
function isFileForcedToBeModuleByFormat(file: SourceFile): true | undefined {
|
||||
function isFileForcedToBeModuleByFormat(file: SourceFile, options: CompilerOptions): true | undefined {
|
||||
// Excludes declaration files - they still require an explicit `export {}` or the like
|
||||
// for back compat purposes. The only non-declaration files _not_ forced to be a module are `.js` files
|
||||
// that aren't esm-mode (meaning not in a `type: module` scope).
|
||||
return (file.impliedNodeFormat === ModuleKind.ESNext || (fileExtensionIsOneOf(file.fileName, [Extension.Cjs, Extension.Cts, Extension.Mjs, Extension.Mts]))) && !file.isDeclarationFile ? true : undefined;
|
||||
//
|
||||
// TODO: extension check never considered compilerOptions; should impliedNodeFormat?
|
||||
return (getImpliedNodeFormatForEmitWorker(file, options) === ModuleKind.ESNext || (fileExtensionIsOneOf(file.fileName, [Extension.Cjs, Extension.Cts, Extension.Mjs, Extension.Mts]))) && !file.isDeclarationFile ? true : undefined;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
@@ -8614,17 +8636,29 @@ export function getSetExternalModuleIndicator(options: CompilerOptions): (file:
|
||||
// If module is nodenext or node16, all esm format files are modules
|
||||
// If jsx is react-jsx or react-jsxdev then jsx tags force module-ness
|
||||
// otherwise, the presence of import or export statments (or import.meta) implies module-ness
|
||||
const checks: ((file: SourceFile) => Node | true | undefined)[] = [isFileProbablyExternalModule];
|
||||
const checks: ((file: SourceFile, options: CompilerOptions) => Node | true | undefined)[] = [isFileProbablyExternalModule];
|
||||
if (options.jsx === JsxEmit.ReactJSX || options.jsx === JsxEmit.ReactJSXDev) {
|
||||
checks.push(isFileModuleFromUsingJSXTag);
|
||||
}
|
||||
checks.push(isFileForcedToBeModuleByFormat);
|
||||
const combined = or(...checks);
|
||||
const callback = (file: SourceFile) => void (file.externalModuleIndicator = combined(file));
|
||||
const callback = (file: SourceFile) => void (file.externalModuleIndicator = combined(file, options));
|
||||
return callback;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @internal
|
||||
* Returns true if an `import` and a `require` of the same module specifier
|
||||
* can resolve to a different file.
|
||||
*/
|
||||
export function importSyntaxAffectsModuleResolution(options: CompilerOptions) {
|
||||
const moduleResolution = getEmitModuleResolutionKind(options);
|
||||
return ModuleResolutionKind.Node16 <= moduleResolution && moduleResolution <= ModuleResolutionKind.NodeNext
|
||||
|| getResolvePackageJsonExports(options)
|
||||
|| getResolvePackageJsonImports(options);
|
||||
}
|
||||
|
||||
type CompilerOptionKeys = keyof { [K in keyof CompilerOptions as string extends K ? never : K]: any; };
|
||||
function createComputedCompilerOptions<T extends Record<string, CompilerOptionKeys[]>>(
|
||||
options: {
|
||||
|
||||
@@ -56,6 +56,7 @@ import {
|
||||
getDefaultLibFileName,
|
||||
getDirectoryPath,
|
||||
getEmitScriptTarget,
|
||||
getImpliedNodeFormatForEmitWorker,
|
||||
getLineAndCharacterOfPosition,
|
||||
getNewLineCharacter,
|
||||
getNormalizedAbsolutePath,
|
||||
@@ -352,13 +353,14 @@ export function explainFiles(program: Program, write: (s: string) => void) {
|
||||
for (const file of program.getSourceFiles()) {
|
||||
write(`${toFileName(file, relativeFileName)}`);
|
||||
reasons.get(file.path)?.forEach(reason => write(` ${fileIncludeReasonToDiagnostics(program, reason, relativeFileName).messageText}`));
|
||||
explainIfFileIsRedirectAndImpliedFormat(file, relativeFileName)?.forEach(d => write(` ${d.messageText}`));
|
||||
explainIfFileIsRedirectAndImpliedFormat(file, program.getCompilerOptionsForFile(file), relativeFileName)?.forEach(d => write(` ${d.messageText}`));
|
||||
}
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
export function explainIfFileIsRedirectAndImpliedFormat(
|
||||
file: SourceFile,
|
||||
options: CompilerOptions,
|
||||
fileNameConvertor?: (fileName: string) => string,
|
||||
): DiagnosticMessageChain[] | undefined {
|
||||
let result: DiagnosticMessageChain[] | undefined;
|
||||
@@ -377,7 +379,7 @@ export function explainIfFileIsRedirectAndImpliedFormat(
|
||||
));
|
||||
}
|
||||
if (isExternalOrCommonJsModule(file)) {
|
||||
switch (file.impliedNodeFormat) {
|
||||
switch (getImpliedNodeFormatForEmitWorker(file, options)) {
|
||||
case ModuleKind.ESNext:
|
||||
if (file.packageJsonScope) {
|
||||
(result ??= []).push(chainDiagnosticMessages(
|
||||
|
||||
@@ -43,10 +43,12 @@ import {
|
||||
getDefaultExportInfoWorker,
|
||||
getDefaultLikeExportInfo,
|
||||
getDirectoryPath,
|
||||
getEmitModuleFormatOfFileWorker,
|
||||
getEmitModuleKind,
|
||||
getEmitModuleResolutionKind,
|
||||
getEmitScriptTarget,
|
||||
getExportInfoMap,
|
||||
getImpliedNodeFormatForEmitWorker,
|
||||
getMeaningFromDeclaration,
|
||||
getMeaningFromLocation,
|
||||
getNameForExportedSymbol,
|
||||
@@ -332,7 +334,7 @@ function createImportAdderWorker(sourceFile: SourceFile | FutureSourceFile, prog
|
||||
compilerOptions,
|
||||
createModuleSpecifierResolutionHost(program, host),
|
||||
);
|
||||
const importKind = getImportKind(futureExportingSourceFile, exportKind, compilerOptions);
|
||||
const importKind = getImportKind(futureExportingSourceFile, exportKind, program);
|
||||
const addAsTypeOnly = getAddAsTypeOnly(
|
||||
isImportUsageValidAsTypeOnly,
|
||||
/*isForNewImportDeclaration*/ true,
|
||||
@@ -686,7 +688,7 @@ export interface ImportSpecifierResolver {
|
||||
/** @internal */
|
||||
export function createImportSpecifierResolver(importingFile: SourceFile, program: Program, host: LanguageServiceHost, preferences: UserPreferences): ImportSpecifierResolver {
|
||||
const packageJsonImportFilter = createPackageJsonImportFilter(importingFile, preferences, host);
|
||||
const importMap = createExistingImportMap(program.getTypeChecker(), importingFile, program.getCompilerOptions());
|
||||
const importMap = createExistingImportMap(importingFile, program);
|
||||
return { getModuleSpecifierForBestExportInfo };
|
||||
|
||||
function getModuleSpecifierForBestExportInfo(
|
||||
@@ -895,7 +897,7 @@ function getImportFixes(
|
||||
sourceFile: SourceFile | FutureSourceFile,
|
||||
host: LanguageServiceHost,
|
||||
preferences: UserPreferences,
|
||||
importMap = isFullSourceFile(sourceFile) ? createExistingImportMap(program.getTypeChecker(), sourceFile, program.getCompilerOptions()) : undefined,
|
||||
importMap = isFullSourceFile(sourceFile) ? createExistingImportMap(sourceFile, program) : undefined,
|
||||
fromCacheOnly?: boolean,
|
||||
): { computedWithoutCacheCount: number; fixes: readonly ImportFixWithModuleSpecifier[]; } {
|
||||
const checker = program.getTypeChecker();
|
||||
@@ -1061,7 +1063,8 @@ function tryAddToExistingImport(existingImports: readonly FixAddToExistingImport
|
||||
}
|
||||
}
|
||||
|
||||
function createExistingImportMap(checker: TypeChecker, importingFile: SourceFile, compilerOptions: CompilerOptions) {
|
||||
function createExistingImportMap(importingFile: SourceFile, program: Program) {
|
||||
const checker = program.getTypeChecker();
|
||||
let importMap: MultiMap<SymbolId, AnyImportOrRequire> | undefined;
|
||||
for (const moduleSpecifier of importingFile.imports) {
|
||||
const i = importFromModuleSpecifier(moduleSpecifier);
|
||||
@@ -1091,7 +1094,7 @@ function createExistingImportMap(checker: TypeChecker, importingFile: SourceFile
|
||||
&& !every(matchingDeclarations, isJSDocImportTag)
|
||||
) return emptyArray;
|
||||
|
||||
const importKind = getImportKind(importingFile, exportKind, compilerOptions);
|
||||
const importKind = getImportKind(importingFile, exportKind, program);
|
||||
return matchingDeclarations.map(declaration => ({ declaration, importKind, symbol, targetFlags }));
|
||||
},
|
||||
};
|
||||
@@ -1115,8 +1118,9 @@ function shouldUseRequire(sourceFile: SourceFile | FutureSourceFile, program: Pr
|
||||
|
||||
// 4. In --module nodenext, assume we're not emitting JS -> JS, so use
|
||||
// whatever syntax Node expects based on the detected module kind
|
||||
if (sourceFile.impliedNodeFormat === ModuleKind.CommonJS) return true;
|
||||
if (sourceFile.impliedNodeFormat === ModuleKind.ESNext) return false;
|
||||
// TODO: consider removing `impliedNodeFormatForEmit`
|
||||
if (getImpliedNodeFormatForEmit(sourceFile, program) === ModuleKind.CommonJS) return true;
|
||||
if (getImpliedNodeFormatForEmit(sourceFile, program) === ModuleKind.ESNext) return false;
|
||||
|
||||
// 5. Match the first other JS file in the program that's unambiguously CJS or ESM
|
||||
for (const otherFile of program.getSourceFiles()) {
|
||||
@@ -1169,7 +1173,7 @@ function getNewImportFixes(
|
||||
// `position` should only be undefined at a missing jsx namespace, in which case we shouldn't be looking for pure types.
|
||||
return { kind: ImportFixKind.JsdocTypeImport, moduleSpecifier, usagePosition, exportInfo, isReExport: i > 0 };
|
||||
}
|
||||
const importKind = getImportKind(sourceFile, exportInfo.exportKind, compilerOptions);
|
||||
const importKind = getImportKind(sourceFile, exportInfo.exportKind, program);
|
||||
let qualification: Qualification | undefined;
|
||||
if (usagePosition !== undefined && importKind === ImportKind.CommonJS && exportInfo.exportKind === ExportKind.Named) {
|
||||
// Compiler options are restricting our import options to a require, but we need to access
|
||||
@@ -1386,8 +1390,8 @@ function getUmdSymbol(token: Node, checker: TypeChecker): Symbol | undefined {
|
||||
*
|
||||
* @internal
|
||||
*/
|
||||
export function getImportKind(importingFile: SourceFile | FutureSourceFile, exportKind: ExportKind, compilerOptions: CompilerOptions, forceImportKeyword?: boolean): ImportKind {
|
||||
if (compilerOptions.verbatimModuleSyntax && (getEmitModuleKind(compilerOptions) === ModuleKind.CommonJS || importingFile.impliedNodeFormat === ModuleKind.CommonJS)) {
|
||||
export function getImportKind(importingFile: SourceFile | FutureSourceFile, exportKind: ExportKind, program: Program, forceImportKeyword?: boolean): ImportKind {
|
||||
if (program.getCompilerOptions().verbatimModuleSyntax && getEmitModuleFormatOfFile(importingFile, program) === ModuleKind.CommonJS) {
|
||||
// TODO: if the exporting file is ESM under nodenext, or `forceImport` is given in a JS file, this is impossible
|
||||
return ImportKind.CommonJS;
|
||||
}
|
||||
@@ -1397,22 +1401,22 @@ export function getImportKind(importingFile: SourceFile | FutureSourceFile, expo
|
||||
case ExportKind.Default:
|
||||
return ImportKind.Default;
|
||||
case ExportKind.ExportEquals:
|
||||
return getExportEqualsImportKind(importingFile, compilerOptions, !!forceImportKeyword);
|
||||
return getExportEqualsImportKind(importingFile, program.getCompilerOptions(), !!forceImportKeyword);
|
||||
case ExportKind.UMD:
|
||||
return getUmdImportKind(importingFile, compilerOptions, !!forceImportKeyword);
|
||||
return getUmdImportKind(importingFile, program, !!forceImportKeyword);
|
||||
default:
|
||||
return Debug.assertNever(exportKind);
|
||||
}
|
||||
}
|
||||
|
||||
function getUmdImportKind(importingFile: SourceFile | FutureSourceFile, compilerOptions: CompilerOptions, forceImportKeyword: boolean): ImportKind {
|
||||
function getUmdImportKind(importingFile: SourceFile | FutureSourceFile, program: Program, forceImportKeyword: boolean): ImportKind {
|
||||
// Import a synthetic `default` if enabled.
|
||||
if (getAllowSyntheticDefaultImports(compilerOptions)) {
|
||||
if (getAllowSyntheticDefaultImports(program.getCompilerOptions())) {
|
||||
return ImportKind.Default;
|
||||
}
|
||||
|
||||
// When a synthetic `default` is unavailable, use `import..require` if the module kind supports it.
|
||||
const moduleKind = getEmitModuleKind(compilerOptions);
|
||||
const moduleKind = getEmitModuleKind(program.getCompilerOptions());
|
||||
switch (moduleKind) {
|
||||
case ModuleKind.AMD:
|
||||
case ModuleKind.CommonJS:
|
||||
@@ -1432,7 +1436,7 @@ function getUmdImportKind(importingFile: SourceFile | FutureSourceFile, compiler
|
||||
return ImportKind.Namespace;
|
||||
case ModuleKind.Node16:
|
||||
case ModuleKind.NodeNext:
|
||||
return importingFile.impliedNodeFormat === ModuleKind.ESNext ? ImportKind.Namespace : ImportKind.CommonJS;
|
||||
return getImpliedNodeFormatForEmit(importingFile, program) === ModuleKind.ESNext ? ImportKind.Namespace : ImportKind.CommonJS;
|
||||
default:
|
||||
return Debug.assertNever(moduleKind, `Unexpected moduleKind ${moduleKind}`);
|
||||
}
|
||||
@@ -2035,3 +2039,11 @@ export function moduleSpecifierToValidIdentifier(moduleSpecifier: string, target
|
||||
// Need `|| "_"` to ensure result isn't empty.
|
||||
return !isStringANonContextualKeyword(res) ? res || "_" : `_${res}`;
|
||||
}
|
||||
|
||||
function getImpliedNodeFormatForEmit(file: SourceFile | FutureSourceFile, program: Program) {
|
||||
return isFullSourceFile(file) ? program.getImpliedNodeFormatForEmit(file) : getImpliedNodeFormatForEmitWorker(file, program.getCompilerOptions());
|
||||
}
|
||||
|
||||
function getEmitModuleFormatOfFile(file: SourceFile | FutureSourceFile, program: Program) {
|
||||
return isFullSourceFile(file) ? program.getEmitModuleFormatOfFile(file) : getEmitModuleFormatOfFileWorker(file, program.getCompilerOptions());
|
||||
}
|
||||
|
||||
@@ -1158,11 +1158,13 @@ function getJSDocParamAnnotation(
|
||||
? createSnippetPrinter({
|
||||
removeComments: true,
|
||||
module: options.module,
|
||||
moduleResolution: options.moduleResolution,
|
||||
target: options.target,
|
||||
})
|
||||
: createPrinter({
|
||||
removeComments: true,
|
||||
module: options.module,
|
||||
moduleResolution: options.moduleResolution,
|
||||
target: options.target,
|
||||
});
|
||||
setEmitFlags(typeNode, EmitFlags.SingleLine);
|
||||
@@ -1456,6 +1458,7 @@ function getExhaustiveCaseSnippets(
|
||||
const printer = createSnippetPrinter({
|
||||
removeComments: true,
|
||||
module: options.module,
|
||||
moduleResolution: options.moduleResolution,
|
||||
target: options.target,
|
||||
newLine: getNewLineKind(newLineChar),
|
||||
});
|
||||
@@ -1717,7 +1720,7 @@ function createCompletionEntry(
|
||||
if (originIsResolvedExport(origin)) {
|
||||
sourceDisplay = [textPart(origin.moduleSpecifier)];
|
||||
if (importStatementCompletion) {
|
||||
({ insertText, replacementSpan } = getInsertTextAndReplacementSpanForImportCompletion(name, importStatementCompletion, origin, useSemicolons, sourceFile, options, preferences));
|
||||
({ insertText, replacementSpan } = getInsertTextAndReplacementSpanForImportCompletion(name, importStatementCompletion, origin, useSemicolons, sourceFile, program, preferences));
|
||||
isSnippet = preferences.includeCompletionsWithSnippetText ? true : undefined;
|
||||
}
|
||||
}
|
||||
@@ -1943,6 +1946,7 @@ function getEntryForMemberCompletion(
|
||||
const printer = createSnippetPrinter({
|
||||
removeComments: true,
|
||||
module: options.module,
|
||||
moduleResolution: options.moduleResolution,
|
||||
target: options.target,
|
||||
omitTrailingSemicolon: false,
|
||||
newLine: getNewLineKind(getNewLineOrDefaultFromHost(host, formatContext?.options)),
|
||||
@@ -2169,6 +2173,7 @@ function getEntryForObjectLiteralMethodCompletion(
|
||||
const printer = createSnippetPrinter({
|
||||
removeComments: true,
|
||||
module: options.module,
|
||||
moduleResolution: options.moduleResolution,
|
||||
target: options.target,
|
||||
omitTrailingSemicolon: false,
|
||||
newLine: getNewLineKind(getNewLineOrDefaultFromHost(host, formatContext?.options)),
|
||||
@@ -2183,6 +2188,7 @@ function getEntryForObjectLiteralMethodCompletion(
|
||||
const signaturePrinter = createPrinter({
|
||||
removeComments: true,
|
||||
module: options.module,
|
||||
moduleResolution: options.moduleResolution,
|
||||
target: options.target,
|
||||
omitTrailingSemicolon: true,
|
||||
});
|
||||
@@ -2485,14 +2491,14 @@ function completionEntryDataToSymbolOriginInfo(data: CompletionEntryData, comple
|
||||
return unresolvedOrigin;
|
||||
}
|
||||
|
||||
function getInsertTextAndReplacementSpanForImportCompletion(name: string, importStatementCompletion: ImportStatementCompletionInfo, origin: SymbolOriginInfoResolvedExport, useSemicolons: boolean, sourceFile: SourceFile, options: CompilerOptions, preferences: UserPreferences) {
|
||||
function getInsertTextAndReplacementSpanForImportCompletion(name: string, importStatementCompletion: ImportStatementCompletionInfo, origin: SymbolOriginInfoResolvedExport, useSemicolons: boolean, sourceFile: SourceFile, program: Program, preferences: UserPreferences) {
|
||||
const replacementSpan = importStatementCompletion.replacementSpan;
|
||||
const quotedModuleSpecifier = escapeSnippetText(quote(sourceFile, preferences, origin.moduleSpecifier));
|
||||
const exportKind = origin.isDefaultExport ? ExportKind.Default :
|
||||
origin.exportName === InternalSymbolName.ExportEquals ? ExportKind.ExportEquals :
|
||||
ExportKind.Named;
|
||||
const tabStop = preferences.includeCompletionsWithSnippetText ? "$1" : "";
|
||||
const importKind = codefix.getImportKind(sourceFile, exportKind, options, /*forceImportKeyword*/ true);
|
||||
const importKind = codefix.getImportKind(sourceFile, exportKind, program, /*forceImportKeyword*/ true);
|
||||
const isImportSpecifierTypeOnly = importStatementCompletion.couldBeTypeOnlyImportSpecifier;
|
||||
const topLevelTypeOnlyText = importStatementCompletion.isTopLevelTypeOnly ? ` ${tokenToString(SyntaxKind.TypeKeyword)} ` : " ";
|
||||
const importSpecifierTypeOnlyText = isImportSpecifierTypeOnly ? `${tokenToString(SyntaxKind.TypeKeyword)} ` : "";
|
||||
|
||||
@@ -342,7 +342,7 @@ export function getReferenceAtPosition(sourceFile: SourceFile, position: number,
|
||||
|
||||
const typeReferenceDirective = findReferenceInPosition(sourceFile.typeReferenceDirectives, position);
|
||||
if (typeReferenceDirective) {
|
||||
const reference = program.getResolvedTypeReferenceDirectives().get(typeReferenceDirective.fileName, typeReferenceDirective.resolutionMode || sourceFile.impliedNodeFormat)?.resolvedTypeReferenceDirective;
|
||||
const reference = program.getResolvedTypeReferenceDirectives().get(typeReferenceDirective.fileName, typeReferenceDirective.resolutionMode || program.getDefaultResolutionModeForFile(sourceFile))?.resolvedTypeReferenceDirective;
|
||||
const file = reference && program.getSourceFile(reference.resolvedFileName!); // TODO:GH#18217
|
||||
return file && { reference: typeReferenceDirective, fileName: file.fileName, file, unverified: false };
|
||||
}
|
||||
|
||||
@@ -480,7 +480,7 @@ export function findModuleReferences(program: Program, sourceFiles: readonly Sou
|
||||
}
|
||||
}
|
||||
for (const ref of referencingFile.typeReferenceDirectives) {
|
||||
const referenced = program.getResolvedTypeReferenceDirectives().get(ref.fileName, ref.resolutionMode || referencingFile.impliedNodeFormat)?.resolvedTypeReferenceDirective;
|
||||
const referenced = program.getResolvedTypeReferenceDirectives().get(ref.fileName, ref.resolutionMode || program.getDefaultResolutionModeForFile(referencingFile))?.resolvedTypeReferenceDirective;
|
||||
if (referenced !== undefined && referenced.resolvedFileName === (searchSourceFile as SourceFile).fileName) {
|
||||
refs.push({ kind: "reference", referencingFile, ref });
|
||||
}
|
||||
|
||||
@@ -197,7 +197,7 @@ export function getStringLiteralCompletions(
|
||||
includeSymbol: boolean,
|
||||
): CompletionInfo | undefined {
|
||||
if (isInReferenceComment(sourceFile, position)) {
|
||||
const entries = getTripleSlashReferenceCompletion(sourceFile, position, options, host);
|
||||
const entries = getTripleSlashReferenceCompletion(sourceFile, position, program, host);
|
||||
return entries && convertPathCompletions(entries);
|
||||
}
|
||||
if (isInString(sourceFile, position, contextToken)) {
|
||||
@@ -593,8 +593,8 @@ function getStringLiteralCompletionsFromModuleNamesWorker(sourceFile: SourceFile
|
||||
const extensionOptions = getExtensionOptions(compilerOptions, ReferenceKind.ModuleSpecifier, sourceFile, typeChecker, preferences, mode);
|
||||
|
||||
return isPathRelativeToScript(literalValue) || !compilerOptions.baseUrl && !compilerOptions.paths && (isRootedDiskPath(literalValue) || isUrl(literalValue))
|
||||
? getCompletionEntriesForRelativeModules(literalValue, scriptDirectory, compilerOptions, host, scriptPath, extensionOptions)
|
||||
: getCompletionEntriesForNonRelativeModules(literalValue, scriptDirectory, mode, compilerOptions, host, extensionOptions, typeChecker);
|
||||
? getCompletionEntriesForRelativeModules(literalValue, scriptDirectory, program, host, scriptPath, extensionOptions)
|
||||
: getCompletionEntriesForNonRelativeModules(literalValue, scriptDirectory, mode, program, host, extensionOptions);
|
||||
}
|
||||
|
||||
interface ExtensionOptions {
|
||||
@@ -614,20 +614,21 @@ function getExtensionOptions(compilerOptions: CompilerOptions, referenceKind: Re
|
||||
resolutionMode,
|
||||
};
|
||||
}
|
||||
function getCompletionEntriesForRelativeModules(literalValue: string, scriptDirectory: string, compilerOptions: CompilerOptions, host: LanguageServiceHost, scriptPath: Path, extensionOptions: ExtensionOptions) {
|
||||
function getCompletionEntriesForRelativeModules(literalValue: string, scriptDirectory: string, program: Program, host: LanguageServiceHost, scriptPath: Path, extensionOptions: ExtensionOptions) {
|
||||
const compilerOptions = program.getCompilerOptions();
|
||||
if (compilerOptions.rootDirs) {
|
||||
return getCompletionEntriesForDirectoryFragmentWithRootDirs(
|
||||
compilerOptions.rootDirs,
|
||||
literalValue,
|
||||
scriptDirectory,
|
||||
extensionOptions,
|
||||
compilerOptions,
|
||||
program,
|
||||
host,
|
||||
scriptPath,
|
||||
);
|
||||
}
|
||||
else {
|
||||
return arrayFrom(getCompletionEntriesForDirectoryFragment(literalValue, scriptDirectory, extensionOptions, host, /*moduleSpecifierIsRelative*/ true, scriptPath).values());
|
||||
return arrayFrom(getCompletionEntriesForDirectoryFragment(literalValue, scriptDirectory, extensionOptions, program, host, /*moduleSpecifierIsRelative*/ true, scriptPath).values());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -665,12 +666,13 @@ function getBaseDirectoriesFromRootDirs(rootDirs: string[], basePath: string, sc
|
||||
);
|
||||
}
|
||||
|
||||
function getCompletionEntriesForDirectoryFragmentWithRootDirs(rootDirs: string[], fragment: string, scriptDirectory: string, extensionOptions: ExtensionOptions, compilerOptions: CompilerOptions, host: LanguageServiceHost, exclude: string): readonly NameAndKind[] {
|
||||
function getCompletionEntriesForDirectoryFragmentWithRootDirs(rootDirs: string[], fragment: string, scriptDirectory: string, extensionOptions: ExtensionOptions, program: Program, host: LanguageServiceHost, exclude: string): readonly NameAndKind[] {
|
||||
const compilerOptions = program.getCompilerOptions();
|
||||
const basePath = compilerOptions.project || host.getCurrentDirectory();
|
||||
const ignoreCase = !(host.useCaseSensitiveFileNames && host.useCaseSensitiveFileNames());
|
||||
const baseDirectories = getBaseDirectoriesFromRootDirs(rootDirs, basePath, scriptDirectory, ignoreCase);
|
||||
return deduplicate<NameAndKind>(
|
||||
flatMap(baseDirectories, baseDirectory => arrayFrom(getCompletionEntriesForDirectoryFragment(fragment, baseDirectory, extensionOptions, host, /*moduleSpecifierIsRelative*/ true, exclude).values())),
|
||||
flatMap(baseDirectories, baseDirectory => arrayFrom(getCompletionEntriesForDirectoryFragment(fragment, baseDirectory, extensionOptions, program, host, /*moduleSpecifierIsRelative*/ true, exclude).values())),
|
||||
(itemA, itemB) => itemA.name === itemB.name && itemA.kind === itemB.kind && itemA.extension === itemB.extension,
|
||||
);
|
||||
}
|
||||
@@ -686,6 +688,7 @@ function getCompletionEntriesForDirectoryFragment(
|
||||
fragment: string,
|
||||
scriptDirectory: string,
|
||||
extensionOptions: ExtensionOptions,
|
||||
program: Program,
|
||||
host: LanguageServiceHost,
|
||||
moduleSpecifierIsRelative: boolean,
|
||||
exclude?: string,
|
||||
@@ -725,7 +728,7 @@ function getCompletionEntriesForDirectoryFragment(
|
||||
if (versionPaths) {
|
||||
const packageDirectory = getDirectoryPath(packageJsonPath);
|
||||
const pathInPackage = absolutePath.slice(ensureTrailingDirectorySeparator(packageDirectory).length);
|
||||
if (addCompletionEntriesFromPaths(result, pathInPackage, packageDirectory, extensionOptions, host, versionPaths)) {
|
||||
if (addCompletionEntriesFromPaths(result, pathInPackage, packageDirectory, extensionOptions, program, host, versionPaths)) {
|
||||
// A true result means one of the `versionPaths` was matched, which will block relative resolution
|
||||
// to files and folders from here. All reachable paths given the pattern match are already added.
|
||||
return result;
|
||||
@@ -748,7 +751,7 @@ function getCompletionEntriesForDirectoryFragment(
|
||||
continue;
|
||||
}
|
||||
|
||||
const { name, extension } = getFilenameWithExtensionOption(getBaseFileName(filePath), host.getCompilationSettings(), extensionOptions, /*isExportsWildcard*/ false);
|
||||
const { name, extension } = getFilenameWithExtensionOption(getBaseFileName(filePath), program, extensionOptions, /*isExportsWildcard*/ false);
|
||||
result.add(nameAndKind(name, ScriptElementKind.scriptElement, extension));
|
||||
}
|
||||
}
|
||||
@@ -768,7 +771,7 @@ function getCompletionEntriesForDirectoryFragment(
|
||||
return result;
|
||||
}
|
||||
|
||||
function getFilenameWithExtensionOption(name: string, compilerOptions: CompilerOptions, extensionOptions: ExtensionOptions, isExportsWildcard: boolean): { name: string; extension: Extension | undefined; } {
|
||||
function getFilenameWithExtensionOption(name: string, program: Program, extensionOptions: ExtensionOptions, isExportsWildcard: boolean): { name: string; extension: Extension | undefined; } {
|
||||
const nonJsResult = moduleSpecifiers.tryGetRealFileNameForNonJsDeclarationFileName(name);
|
||||
if (nonJsResult) {
|
||||
return { name: nonJsResult, extension: tryGetExtensionFromPath(nonJsResult) };
|
||||
@@ -779,7 +782,8 @@ function getFilenameWithExtensionOption(name: string, compilerOptions: CompilerO
|
||||
|
||||
let allowedEndings = getModuleSpecifierPreferences(
|
||||
{ importModuleSpecifierEnding: extensionOptions.endingPreference },
|
||||
compilerOptions,
|
||||
program,
|
||||
program.getCompilerOptions(),
|
||||
extensionOptions.importingSourceFile,
|
||||
).getAllowedEndingsInPreferredOrder(extensionOptions.resolutionMode);
|
||||
|
||||
@@ -793,7 +797,7 @@ function getFilenameWithExtensionOption(name: string, compilerOptions: CompilerO
|
||||
if (fileExtensionIsOneOf(name, supportedTSImplementationExtensions)) {
|
||||
return { name, extension: tryGetExtensionFromPath(name) };
|
||||
}
|
||||
const outputExtension = moduleSpecifiers.tryGetJSExtensionForFile(name, compilerOptions);
|
||||
const outputExtension = moduleSpecifiers.tryGetJSExtensionForFile(name, program.getCompilerOptions());
|
||||
return outputExtension
|
||||
? { name: changeExtension(name, outputExtension), extension: outputExtension }
|
||||
: { name, extension: tryGetExtensionFromPath(name) };
|
||||
@@ -807,7 +811,7 @@ function getFilenameWithExtensionOption(name: string, compilerOptions: CompilerO
|
||||
return { name: removeFileExtension(name), extension: tryGetExtensionFromPath(name) };
|
||||
}
|
||||
|
||||
const outputExtension = moduleSpecifiers.tryGetJSExtensionForFile(name, compilerOptions);
|
||||
const outputExtension = moduleSpecifiers.tryGetJSExtensionForFile(name, program.getCompilerOptions());
|
||||
return outputExtension
|
||||
? { name: changeExtension(name, outputExtension), extension: outputExtension }
|
||||
: { name, extension: tryGetExtensionFromPath(name) };
|
||||
@@ -819,6 +823,7 @@ function addCompletionEntriesFromPaths(
|
||||
fragment: string,
|
||||
baseDirectory: string,
|
||||
extensionOptions: ExtensionOptions,
|
||||
program: Program,
|
||||
host: LanguageServiceHost,
|
||||
paths: MapLike<string[]>,
|
||||
) {
|
||||
@@ -830,7 +835,7 @@ function addCompletionEntriesFromPaths(
|
||||
const lengthB = typeof patternB === "object" ? patternB.prefix.length : b.length;
|
||||
return compareValues(lengthB, lengthA);
|
||||
};
|
||||
return addCompletionEntriesFromPathsOrExports(result, /*isExports*/ false, fragment, baseDirectory, extensionOptions, host, getOwnKeys(paths), getPatternsForKey, comparePaths);
|
||||
return addCompletionEntriesFromPathsOrExports(result, /*isExports*/ false, fragment, baseDirectory, extensionOptions, program, host, getOwnKeys(paths), getPatternsForKey, comparePaths);
|
||||
}
|
||||
|
||||
/** @returns whether `fragment` was a match for any `paths` (which should indicate whether any other path completions should be offered) */
|
||||
@@ -840,6 +845,7 @@ function addCompletionEntriesFromPathsOrExports(
|
||||
fragment: string,
|
||||
baseDirectory: string,
|
||||
extensionOptions: ExtensionOptions,
|
||||
program: Program,
|
||||
host: LanguageServiceHost,
|
||||
keys: readonly string[],
|
||||
getPatternsForKey: (key: string) => string[] | undefined,
|
||||
@@ -874,7 +880,7 @@ function addCompletionEntriesFromPathsOrExports(
|
||||
if (typeof pathPattern === "string" || matchedPath === undefined || comparePaths(key, matchedPath) !== Comparison.GreaterThan) {
|
||||
pathResults.push({
|
||||
matchedPattern: isMatch,
|
||||
results: getCompletionsForPathMapping(keyWithoutLeadingDotSlash, patterns, fragment, baseDirectory, extensionOptions, isExports && isMatch, host)
|
||||
results: getCompletionsForPathMapping(keyWithoutLeadingDotSlash, patterns, fragment, baseDirectory, extensionOptions, isExports && isMatch, program, host)
|
||||
.map(({ name, kind, extension }) => nameAndKind(name, kind, extension)),
|
||||
});
|
||||
}
|
||||
@@ -896,11 +902,12 @@ function getCompletionEntriesForNonRelativeModules(
|
||||
fragment: string,
|
||||
scriptPath: string,
|
||||
mode: ResolutionMode,
|
||||
compilerOptions: CompilerOptions,
|
||||
program: Program,
|
||||
host: LanguageServiceHost,
|
||||
extensionOptions: ExtensionOptions,
|
||||
typeChecker: TypeChecker,
|
||||
): readonly NameAndKind[] {
|
||||
const typeChecker = program.getTypeChecker();
|
||||
const compilerOptions = program.getCompilerOptions();
|
||||
const { baseUrl, paths } = compilerOptions;
|
||||
|
||||
const result = createNameAndKindSet();
|
||||
@@ -908,12 +915,12 @@ function getCompletionEntriesForNonRelativeModules(
|
||||
|
||||
if (baseUrl) {
|
||||
const absolute = normalizePath(combinePaths(host.getCurrentDirectory(), baseUrl));
|
||||
getCompletionEntriesForDirectoryFragment(fragment, absolute, extensionOptions, host, /*moduleSpecifierIsRelative*/ false, /*exclude*/ undefined, result);
|
||||
getCompletionEntriesForDirectoryFragment(fragment, absolute, extensionOptions, program, host, /*moduleSpecifierIsRelative*/ false, /*exclude*/ undefined, result);
|
||||
}
|
||||
|
||||
if (paths) {
|
||||
const absolute = getPathsBasePath(compilerOptions, host)!;
|
||||
addCompletionEntriesFromPaths(result, fragment, absolute, extensionOptions, host, paths);
|
||||
addCompletionEntriesFromPaths(result, fragment, absolute, extensionOptions, program, host, paths);
|
||||
}
|
||||
|
||||
const fragmentDirectory = getFragmentDirectory(fragment);
|
||||
@@ -921,7 +928,7 @@ function getCompletionEntriesForNonRelativeModules(
|
||||
result.add(nameAndKind(ambientName, ScriptElementKind.externalModuleName, /*extension*/ undefined));
|
||||
}
|
||||
|
||||
getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, fragmentDirectory, extensionOptions, result);
|
||||
getCompletionEntriesFromTypings(host, program, scriptPath, fragmentDirectory, extensionOptions, result);
|
||||
|
||||
if (moduleResolutionUsesNodeModules(moduleResolution)) {
|
||||
// If looking for a global package name, don't just include everything in `node_modules` because that includes dependencies' own dependencies.
|
||||
@@ -940,7 +947,7 @@ function getCompletionEntriesForNonRelativeModules(
|
||||
let ancestorLookup: (directory: string) => void | undefined = ancestor => {
|
||||
const nodeModules = combinePaths(ancestor, "node_modules");
|
||||
if (tryDirectoryExists(host, nodeModules)) {
|
||||
getCompletionEntriesForDirectoryFragment(fragment, nodeModules, extensionOptions, host, /*moduleSpecifierIsRelative*/ false, /*exclude*/ undefined, result);
|
||||
getCompletionEntriesForDirectoryFragment(fragment, nodeModules, extensionOptions, program, host, /*moduleSpecifierIsRelative*/ false, /*exclude*/ undefined, result);
|
||||
}
|
||||
};
|
||||
if (fragmentDirectory && getResolvePackageJsonExports(compilerOptions)) {
|
||||
@@ -977,6 +984,7 @@ function getCompletionEntriesForNonRelativeModules(
|
||||
fragmentSubpath,
|
||||
packageDirectory,
|
||||
extensionOptions,
|
||||
program,
|
||||
host,
|
||||
keys,
|
||||
key => singleElementArray(getPatternFromFirstMatchingCondition(exports[key], conditions)),
|
||||
@@ -1020,6 +1028,7 @@ function getCompletionsForPathMapping(
|
||||
packageDirectory: string,
|
||||
extensionOptions: ExtensionOptions,
|
||||
isExportsWildcard: boolean,
|
||||
program: Program,
|
||||
host: LanguageServiceHost,
|
||||
): readonly NameAndKind[] {
|
||||
if (!endsWith(path, "*")) {
|
||||
@@ -1031,9 +1040,9 @@ function getCompletionsForPathMapping(
|
||||
const remainingFragment = tryRemovePrefix(fragment, pathPrefix);
|
||||
if (remainingFragment === undefined) {
|
||||
const starIsFullPathComponent = path[path.length - 2] === "/";
|
||||
return starIsFullPathComponent ? justPathMappingName(pathPrefix, ScriptElementKind.directory) : flatMap(patterns, pattern => getModulesForPathsPattern("", packageDirectory, pattern, extensionOptions, isExportsWildcard, host)?.map(({ name, ...rest }) => ({ name: pathPrefix + name, ...rest })));
|
||||
return starIsFullPathComponent ? justPathMappingName(pathPrefix, ScriptElementKind.directory) : flatMap(patterns, pattern => getModulesForPathsPattern("", packageDirectory, pattern, extensionOptions, isExportsWildcard, program, host)?.map(({ name, ...rest }) => ({ name: pathPrefix + name, ...rest })));
|
||||
}
|
||||
return flatMap(patterns, pattern => getModulesForPathsPattern(remainingFragment, packageDirectory, pattern, extensionOptions, isExportsWildcard, host));
|
||||
return flatMap(patterns, pattern => getModulesForPathsPattern(remainingFragment, packageDirectory, pattern, extensionOptions, isExportsWildcard, program, host));
|
||||
|
||||
function justPathMappingName(name: string, kind: ScriptElementKind.directory | ScriptElementKind.scriptElement): readonly NameAndKind[] {
|
||||
return startsWith(name, fragment) ? [{ name: removeTrailingDirectorySeparator(name), kind, extension: undefined }] : emptyArray;
|
||||
@@ -1046,6 +1055,7 @@ function getModulesForPathsPattern(
|
||||
pattern: string,
|
||||
extensionOptions: ExtensionOptions,
|
||||
isExportsWildcard: boolean,
|
||||
program: Program,
|
||||
host: LanguageServiceHost,
|
||||
): readonly NameAndKind[] | undefined {
|
||||
if (!host.readDirectory) {
|
||||
@@ -1094,7 +1104,7 @@ function getModulesForPathsPattern(
|
||||
if (containsSlash(trimmedWithPattern)) {
|
||||
return directoryResult(getPathComponents(removeLeadingDirectorySeparator(trimmedWithPattern))[1]);
|
||||
}
|
||||
const { name, extension } = getFilenameWithExtensionOption(trimmedWithPattern, host.getCompilationSettings(), extensionOptions, isExportsWildcard);
|
||||
const { name, extension } = getFilenameWithExtensionOption(trimmedWithPattern, program, extensionOptions, isExportsWildcard);
|
||||
return nameAndKind(name, ScriptElementKind.scriptElement, extension);
|
||||
}
|
||||
});
|
||||
@@ -1138,7 +1148,8 @@ function getAmbientModuleCompletions(fragment: string, fragmentDirectory: string
|
||||
return nonRelativeModuleNames;
|
||||
}
|
||||
|
||||
function getTripleSlashReferenceCompletion(sourceFile: SourceFile, position: number, compilerOptions: CompilerOptions, host: LanguageServiceHost): readonly PathCompletion[] | undefined {
|
||||
function getTripleSlashReferenceCompletion(sourceFile: SourceFile, position: number, program: Program, host: LanguageServiceHost): readonly PathCompletion[] | undefined {
|
||||
const compilerOptions = program.getCompilerOptions();
|
||||
const token = getTokenAtPosition(sourceFile, position);
|
||||
const commentRanges = getLeadingCommentRanges(sourceFile.text, token.pos);
|
||||
const range = commentRanges && find(commentRanges, commentRange => position >= commentRange.pos && position <= commentRange.end);
|
||||
@@ -1153,13 +1164,14 @@ function getTripleSlashReferenceCompletion(sourceFile: SourceFile, position: num
|
||||
|
||||
const [, prefix, kind, toComplete] = match;
|
||||
const scriptPath = getDirectoryPath(sourceFile.path);
|
||||
const names = kind === "path" ? getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, getExtensionOptions(compilerOptions, ReferenceKind.Filename, sourceFile), host, /*moduleSpecifierIsRelative*/ true, sourceFile.path)
|
||||
: kind === "types" ? getCompletionEntriesFromTypings(host, compilerOptions, scriptPath, getFragmentDirectory(toComplete), getExtensionOptions(compilerOptions, ReferenceKind.ModuleSpecifier, sourceFile))
|
||||
const names = kind === "path" ? getCompletionEntriesForDirectoryFragment(toComplete, scriptPath, getExtensionOptions(compilerOptions, ReferenceKind.Filename, sourceFile), program, host, /*moduleSpecifierIsRelative*/ true, sourceFile.path)
|
||||
: kind === "types" ? getCompletionEntriesFromTypings(host, program, scriptPath, getFragmentDirectory(toComplete), getExtensionOptions(compilerOptions, ReferenceKind.ModuleSpecifier, sourceFile))
|
||||
: Debug.fail();
|
||||
return addReplacementSpans(toComplete, range.pos + prefix.length, arrayFrom(names.values()));
|
||||
}
|
||||
|
||||
function getCompletionEntriesFromTypings(host: LanguageServiceHost, options: CompilerOptions, scriptPath: string, fragmentDirectory: string | undefined, extensionOptions: ExtensionOptions, result = createNameAndKindSet()): NameAndKindSet {
|
||||
function getCompletionEntriesFromTypings(host: LanguageServiceHost, program: Program, scriptPath: string, fragmentDirectory: string | undefined, extensionOptions: ExtensionOptions, result = createNameAndKindSet()): NameAndKindSet {
|
||||
const options = program.getCompilerOptions();
|
||||
// Check for typings specified in compiler options
|
||||
const seen = new Map<string, true>();
|
||||
|
||||
@@ -1194,7 +1206,7 @@ function getCompletionEntriesFromTypings(host: LanguageServiceHost, options: Com
|
||||
const baseDirectory = combinePaths(directory, typeDirectoryName);
|
||||
const remainingFragment = tryRemoveDirectoryPrefix(fragmentDirectory, packageName, hostGetCanonicalFileName(host));
|
||||
if (remainingFragment !== undefined) {
|
||||
getCompletionEntriesForDirectoryFragment(remainingFragment, baseDirectory, extensionOptions, host, /*moduleSpecifierIsRelative*/ false, /*exclude*/ undefined, result);
|
||||
getCompletionEntriesForDirectoryFragment(remainingFragment, baseDirectory, extensionOptions, program, host, /*moduleSpecifierIsRelative*/ false, /*exclude*/ undefined, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -66,7 +66,7 @@ export function computeSuggestionDiagnostics(sourceFile: SourceFile, program: Pr
|
||||
program.getSemanticDiagnostics(sourceFile, cancellationToken);
|
||||
const diags: DiagnosticWithLocation[] = [];
|
||||
const checker = program.getTypeChecker();
|
||||
const isCommonJSFile = sourceFile.impliedNodeFormat === ModuleKind.CommonJS || fileExtensionIsOneOf(sourceFile.fileName, [Extension.Cts, Extension.Cjs]);
|
||||
const isCommonJSFile = program.getImpliedNodeFormatForEmit(sourceFile) === ModuleKind.CommonJS || fileExtensionIsOneOf(sourceFile.fileName, [Extension.Cts, Extension.Cjs]);
|
||||
|
||||
if (
|
||||
!isCommonJSFile &&
|
||||
|
||||
@@ -97,6 +97,7 @@ import {
|
||||
getEmitModuleKind,
|
||||
getEmitScriptTarget,
|
||||
getExternalModuleImportEqualsDeclarationExpression,
|
||||
getImpliedNodeFormatForEmitWorker,
|
||||
getImpliedNodeFormatForFile,
|
||||
getImpliedNodeFormatForFileWorker,
|
||||
getIndentString,
|
||||
@@ -2486,6 +2487,8 @@ export function createModuleSpecifierResolutionHost(program: Program, host: Lang
|
||||
getNearestAncestorDirectoryWithPackageJson: maybeBind(host, host.getNearestAncestorDirectoryWithPackageJson),
|
||||
getFileIncludeReasons: () => program.getFileIncludeReasons(),
|
||||
getCommonSourceDirectory: () => program.getCommonSourceDirectory(),
|
||||
getDefaultResolutionModeForFile: file => program.getDefaultResolutionModeForFile(file),
|
||||
getModeForResolutionAtIndex: (file, index) => program.getModeForResolutionAtIndex(file, index),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -4262,11 +4265,13 @@ export function fileShouldUseJavaScriptRequire(file: SourceFile | string, progra
|
||||
if (!hasJSFileExtension(fileName)) {
|
||||
return false;
|
||||
}
|
||||
const compilerOptions = program.getCompilerOptions();
|
||||
const compilerOptions = typeof file === "string" ? program.getCompilerOptions() : program.getCompilerOptionsForFile(file);
|
||||
const moduleKind = getEmitModuleKind(compilerOptions);
|
||||
const impliedNodeFormat = typeof file === "string"
|
||||
? getImpliedNodeFormatForFile(toPath(file, host.getCurrentDirectory(), hostGetCanonicalFileName(host)), program.getPackageJsonInfoCache?.(), host, compilerOptions)
|
||||
: file.impliedNodeFormat;
|
||||
const sourceFileLike = typeof file === "string" ? {
|
||||
fileName: file,
|
||||
impliedNodeFormat: getImpliedNodeFormatForFile(toPath(file, host.getCurrentDirectory(), hostGetCanonicalFileName(host)), program.getPackageJsonInfoCache?.(), host, compilerOptions),
|
||||
} : file;
|
||||
const impliedNodeFormat = getImpliedNodeFormatForEmitWorker(sourceFileLike, compilerOptions);
|
||||
|
||||
if (impliedNodeFormat === ModuleKind.ESNext) {
|
||||
return false;
|
||||
|
||||
@@ -44,6 +44,51 @@ describe("unittests:: tsc:: projectReferences::", () => {
|
||||
commandLineArgs: ["--p", "src/project"],
|
||||
});
|
||||
|
||||
verifyTsc({
|
||||
scenario: "projectReferences",
|
||||
subScenario: "default import interop uses referenced project settings",
|
||||
fs: () =>
|
||||
loadProjectFromFiles({
|
||||
"/node_modules/ambiguous-package/package.json": `{ "name": "ambiguous-package" }`,
|
||||
"/node_modules/ambiguous-package/index.d.ts": "export declare const ambiguous: number;",
|
||||
"/node_modules/esm-package/package.json": `{ "name": "esm-package", "type": "module" }`,
|
||||
"/node_modules/esm-package/index.d.ts": "export declare const esm: number;",
|
||||
"/lib/tsconfig.json": jsonToReadableText({
|
||||
compilerOptions: {
|
||||
composite: true,
|
||||
declaration: true,
|
||||
rootDir: "src",
|
||||
outDir: "dist",
|
||||
module: "esnext",
|
||||
moduleResolution: "bundler",
|
||||
},
|
||||
include: ["src"],
|
||||
}),
|
||||
"/lib/src/a.ts": "export const a = 0;",
|
||||
"/lib/dist/a.d.ts": "export declare const a = 0;",
|
||||
"/app/tsconfig.json": jsonToReadableText({
|
||||
compilerOptions: {
|
||||
module: "esnext",
|
||||
moduleResolution: "bundler",
|
||||
rootDir: "src",
|
||||
outDir: "dist",
|
||||
},
|
||||
include: ["src"],
|
||||
references: [
|
||||
{ path: "../lib" },
|
||||
],
|
||||
}),
|
||||
"/app/src/local.ts": "export const local = 0;",
|
||||
"/app/src/index.ts": `
|
||||
import local from "./local"; // Error
|
||||
import esm from "esm-package"; // Error
|
||||
import referencedSource from "../../lib/src/a"; // Error
|
||||
import referencedDeclaration from "../../lib/dist/a"; // Error
|
||||
import ambiguous from "ambiguous-package"; // Ok`,
|
||||
}),
|
||||
commandLineArgs: ["--p", "app", "--pretty", "false"],
|
||||
});
|
||||
|
||||
verifyTsc({
|
||||
scenario: "projectReferences",
|
||||
subScenario: "referencing ambient const enum from referenced project with preserveConstEnums",
|
||||
|
||||
@@ -178,19 +178,19 @@ describe("unittests:: tsc-watch:: emit file --incremental", () => {
|
||||
version: system.createHash(libFile.content),
|
||||
signature: system.createHash(libFile.content),
|
||||
affectsGlobalScope: true,
|
||||
impliedFormat: undefined,
|
||||
impliedFormat: ts.ModuleKind.CommonJS,
|
||||
});
|
||||
assert.deepEqual(state.fileInfos.get(file1.path as ts.Path), {
|
||||
version: system.createHash(file1.content),
|
||||
signature: system.createHash(file1.content),
|
||||
affectsGlobalScope: undefined,
|
||||
impliedFormat: undefined,
|
||||
impliedFormat: ts.ModuleKind.CommonJS,
|
||||
});
|
||||
assert.deepEqual(state.fileInfos.get(file2.path as ts.Path), {
|
||||
version: system.createHash(fileModified.content),
|
||||
signature: system.createHash(fileModified.content),
|
||||
affectsGlobalScope: undefined,
|
||||
impliedFormat: undefined,
|
||||
impliedFormat: ts.ModuleKind.CommonJS,
|
||||
});
|
||||
|
||||
assert.deepEqual(state.compilerOptions, {
|
||||
|
||||
Reference in New Issue
Block a user