Skip to content

Prepare module resolution for resolving TS extensions #51171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
e9f0a1a
WIP
andrewbranch Jul 28, 2022
5f9cf9d
Add extension error back unless noEmit is set
andrewbranch Jul 29, 2022
2421cf5
Add non-relative tests
andrewbranch Jul 29, 2022
660026e
Add error for importing from declaration file
andrewbranch Jul 29, 2022
2daa136
Merge branch 'main' into module-resolution/minimal
andrewbranch Sep 14, 2022
f49a368
Update unit test
andrewbranch Sep 14, 2022
af03675
Add explicit flag for importing from .ts extensions
andrewbranch Sep 14, 2022
496ef99
Add module specifier resolution changes
andrewbranch Sep 15, 2022
458c125
Add auto-import tests
andrewbranch Sep 19, 2022
7d64928
Disallow relative imports into node_modules
andrewbranch Sep 26, 2022
0249e0a
Ensure auto-imports don’t suggest ./node_modules;
andrewbranch Sep 26, 2022
6242ee3
Test a non-portable declaration emit issue
andrewbranch Sep 26, 2022
6cc0e18
Test auto-importing TSX file
andrewbranch Sep 26, 2022
240533a
Update path completions
andrewbranch Sep 26, 2022
eb9a4bb
Merge branch 'main' into module-resolution/minimal
andrewbranch Sep 26, 2022
619843e
Fix lint due to merge
andrewbranch Sep 27, 2022
2ceb4bc
Remove minimal-specific stuff
andrewbranch Oct 13, 2022
aad7fe7
Remove minimal tests
andrewbranch Oct 13, 2022
7fd3e72
Update unit tests
andrewbranch Oct 13, 2022
7cca6f3
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Oct 14, 2022
ea12012
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Oct 25, 2022
8655cf9
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 7, 2022
c55c454
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 8, 2022
6e1989e
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 15, 2022
fa188b1
Revamp string completions ending preferences
andrewbranch Nov 18, 2022
c6b963b
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 18, 2022
7225671
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Nov 28, 2022
3b1d554
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Dec 5, 2022
5909643
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Dec 5, 2022
c3575de
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Dec 6, 2022
c4ba4a5
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Dec 9, 2022
4e73a46
Merge branch 'main' into module-resolution/ts-extensions
andrewbranch Dec 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 41 additions & 14 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ import {
ElementFlags,
EmitFlags,
EmitHint,
emitModuleKindIsNonNodeESM,
EmitResolver,
EmitTextWriter,
emptyArray,
Expand Down Expand Up @@ -458,6 +459,7 @@ import {
isConstructorTypeNode,
isConstTypeReference,
isDeclaration,
isDeclarationFileName,
isDeclarationName,
isDeclarationReadonly,
isDecorator,
Expand Down Expand Up @@ -897,6 +899,7 @@ import {
setTextRangePosEnd,
setValueDeclaration,
ShorthandPropertyAssignment,
shouldAllowImportingTsExtension,
shouldPreserveConstEnums,
Signature,
SignatureDeclaration,
Expand Down Expand Up @@ -4727,6 +4730,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
(isModuleDeclaration(location) ? location : location.parent && isModuleDeclaration(location.parent) && location.parent.name === location ? location.parent : undefined)?.name ||
(isLiteralImportTypeNode(location) ? location : undefined)?.argument.literal;
const mode = contextSpecifier && isStringLiteralLike(contextSpecifier) ? getModeForUsageLocation(currentSourceFile, contextSpecifier) : currentSourceFile.impliedNodeFormat;
const moduleResolutionKind = getEmitModuleResolutionKind(compilerOptions);
const resolvedModule = getResolvedModule(currentSourceFile, moduleReference, mode);
const resolutionDiagnostic = resolvedModule && getResolutionDiagnostic(compilerOptions, resolvedModule);
const sourceFile = resolvedModule
Expand All @@ -4737,11 +4741,28 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (resolutionDiagnostic) {
error(errorNode, resolutionDiagnostic, moduleReference, resolvedModule.resolvedFileName);
}

if (resolvedModule.resolvedUsingTsExtension && isDeclarationFileName(moduleReference)) {
const importOrExport =
findAncestor(location, isImportDeclaration)?.importClause ||
findAncestor(location, or(isImportEqualsDeclaration, isExportDeclaration));
if (importOrExport && !importOrExport.isTypeOnly || findAncestor(location, isImportCall)) {
error(
errorNode,
Diagnostics.A_declaration_file_cannot_be_imported_without_import_type_Did_you_mean_to_import_an_implementation_file_0_instead,
getSuggestedImportSource(Debug.checkDefined(tryExtractTSExtension(moduleReference))));
}
}
else if (resolvedModule.resolvedUsingTsExtension && !shouldAllowImportingTsExtension(compilerOptions, currentSourceFile.fileName)) {
const tsExtension = Debug.checkDefined(tryExtractTSExtension(moduleReference));
error(errorNode, Diagnostics.An_import_path_can_only_end_with_a_0_extension_when_allowImportingTsExtensions_is_enabled, tsExtension);
}

if (sourceFile.symbol) {
if (resolvedModule.isExternalLibraryImport && !resolutionExtensionIsTSOrJson(resolvedModule.extension)) {
errorOnImplicitAnyModule(/*isError*/ false, errorNode, resolvedModule, moduleReference);
}
if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Node16 || getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeNext) {
if (moduleResolutionKind === ModuleResolutionKind.Node16 || moduleResolutionKind === ModuleResolutionKind.NodeNext) {
const isSyncImport = (currentSourceFile.impliedNodeFormat === ModuleKind.CommonJS && !findAncestor(location, isImportCall)) || !!findAncestor(location, isImportEqualsDeclaration);
const overrideClauseHost = findAncestor(location, l => isImportTypeNode(l) || isExportDeclaration(l) || isImportDeclaration(l)) as ImportTypeNode | ImportDeclaration | ExportDeclaration | undefined;
const overrideClause = overrideClauseHost && isImportTypeNode(overrideClauseHost) ? overrideClauseHost.assertions?.assertClause : overrideClauseHost?.assertClause;
Expand Down Expand Up @@ -4849,25 +4870,14 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
else {
const tsExtension = tryExtractTSExtension(moduleReference);
const isExtensionlessRelativePathImport = pathIsRelative(moduleReference) && !hasExtension(moduleReference);
const moduleResolutionKind = getEmitModuleResolutionKind(compilerOptions);
const resolutionIsNode16OrNext = moduleResolutionKind === ModuleResolutionKind.Node16 ||
moduleResolutionKind === ModuleResolutionKind.NodeNext;
if (tsExtension) {
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead;
const importSourceWithoutExtension = removeExtension(moduleReference, tsExtension);
let replacedImportSource = importSourceWithoutExtension;
/**
* Direct users to import source with .js extension if outputting an ES module.
* @see https://github.com/microsoft/TypeScript/issues/42151
*/
if (moduleKind >= ModuleKind.ES2015) {
replacedImportSource += tsExtension === Extension.Mts ? ".mjs" : tsExtension === Extension.Cts ? ".cjs" : ".js";
}
error(errorNode, diag, tsExtension, replacedImportSource);
errorOnTSExtensionImport(tsExtension);
}
else if (!compilerOptions.resolveJsonModule &&
fileExtensionIs(moduleReference, Extension.Json) &&
getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Classic &&
moduleResolutionKind !== ModuleResolutionKind.Classic &&
hasJsonModuleEmitEnabled(compilerOptions)) {
error(errorNode, Diagnostics.Cannot_find_module_0_Consider_using_resolveJsonModule_to_import_module_with_json_extension, moduleReference);
}
Expand All @@ -4889,6 +4899,23 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}
return undefined;

function errorOnTSExtensionImport(tsExtension: string) {
const diag = Diagnostics.An_import_path_cannot_end_with_a_0_extension_Consider_importing_1_instead;
error(errorNode, diag, tsExtension, getSuggestedImportSource(tsExtension));
}

function getSuggestedImportSource(tsExtension: string) {
const importSourceWithoutExtension = removeExtension(moduleReference, tsExtension);
/**
* Direct users to import source with .js extension if outputting an ES module.
* @see https://github.com/microsoft/TypeScript/issues/42151
*/
if (emitModuleKindIsNonNodeESM(moduleKind) || mode === ModuleKind.ESNext) {
return importSourceWithoutExtension + (tsExtension === Extension.Mts ? ".mjs" : tsExtension === Extension.Cts ? ".cjs" : ".js");
}
return importSourceWithoutExtension;
}
}

function errorOnImplicitAnyModule(isError: boolean, errorNode: Node, { packageId, resolvedFileName }: ResolvedModuleFull, moduleReference: string): void {
Expand Down
8 changes: 8 additions & 0 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,14 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
category: Diagnostics.Modules,
description: Diagnostics.List_of_file_name_suffixes_to_search_when_resolving_a_module,
},
// {
// name: "allowImportingTsExtensions",
// type: "boolean",
// affectsModuleResolution: true,
// category: Diagnostics.Modules,
// description: Diagnostics.Allow_imports_to_include_TypeScript_file_extensions_Requires_moduleResolution_minimal_and_either_noEmit_or_emitDeclarationOnly_to_be_set,
// defaultValueDescription: false,
// },

// Source Maps
{
Expand Down
20 changes: 16 additions & 4 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3563,6 +3563,10 @@
"category": "Error",
"code": 2845
},
"A declaration file cannot be imported without 'import type'. Did you mean to import an implementation file '{0}' instead?": {
"category": "Error",
"code": 2846
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down Expand Up @@ -4221,6 +4225,14 @@
"category": "Error",
"code": 5095
},
"Option 'allowImportingTsExtensions' can only be used when 'moduleResolution' is set to 'minimal' and either 'noEmit' or 'emitDeclarationOnly' is set.": {
"category": "Error",
"code": 5096
},
"An import path can only end with a '{0}' extension when 'allowImportingTsExtensions' is enabled.": {
"category": "Error",
"code": 5097
},

"Generates a sourcemap for each corresponding '.d.ts' file.": {
"category": "Message",
Expand Down Expand Up @@ -4443,10 +4455,6 @@
"category": "Message",
"code": 6066
},
"Specify module resolution strategy: 'node' (Node.js) or 'classic' (TypeScript pre-1.6).": {
"category": "Message",
"code": 6069
},
"Initializes a TypeScript project and creates a tsconfig.json file.": {
"category": "Message",
"code": 6070
Expand Down Expand Up @@ -5430,6 +5438,10 @@
"category": "Message",
"code": 6406
},
"Allow imports to include TypeScript file extensions. Requires '--moduleResolution minimal' and either '--noEmit' or '--emitDeclarationOnly' to be set.": {
"category": "Message",
"code": 6407
},

"The expected type comes from property '{0}' which is declared here on type '{1}'": {
"category": "Message",
Expand Down
Loading