Skip to content
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

--moduleResolution hybrid #2

Open
wants to merge 33 commits into
base: module-resolution/ts-extensions
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
86dd5da
Add options
andrewbranch Oct 20, 2022
8846f31
Add customConditions option
andrewbranch Oct 21, 2022
9bc0f07
Add first tests
andrewbranch Oct 24, 2022
4545afb
CJS constructs are not allowed
andrewbranch Oct 24, 2022
e82e61d
Add another test
andrewbranch Oct 24, 2022
038783d
Fix extension adding/replacing priority
andrewbranch Oct 25, 2022
870d807
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Nov 8, 2022
a482180
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Nov 15, 2022
89bcd01
Update test to reflect the choice not to block on unrecognized extens…
andrewbranch Nov 16, 2022
6074661
Add auto-imports and string completions tests
andrewbranch Nov 17, 2022
9e03db2
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Nov 18, 2022
c4320a2
Comment test
andrewbranch Nov 18, 2022
f04f1b5
Auto-imports of declaration files cannot use .ts extension
andrewbranch Nov 18, 2022
29039de
Have declaration file auto imports default to extensionless instead
andrewbranch Nov 18, 2022
e83ca2a
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Nov 18, 2022
f98730a
Add test for custom conditions
andrewbranch Nov 18, 2022
5a01dd6
Fix indentation
andrewbranch Nov 18, 2022
4dd6d01
Add baseline showing resolvePackageJsonImports/Exports compatibility
andrewbranch Nov 18, 2022
81b9e70
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Nov 28, 2022
f6fa5f3
Fix test and prevent CJS require from resolving
andrewbranch Nov 28, 2022
aeb23be
Update unit test baselines
andrewbranch Nov 28, 2022
e0c0375
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Dec 5, 2022
f9414aa
Fix bad merge conflict resolution
andrewbranch Dec 5, 2022
dadc1a8
Make resolvedUsingTsExtension optional
andrewbranch Dec 5, 2022
5089aab
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Dec 5, 2022
a88b8ad
Update missed baselines
andrewbranch Dec 6, 2022
224bb10
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Dec 6, 2022
b2e283c
Revert now-unnecessary API implementation changes
andrewbranch Dec 6, 2022
30afa86
Clean up
andrewbranch Dec 9, 2022
0baca5b
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Dec 9, 2022
6e763d9
Update baselines to es5 emit
andrewbranch Dec 9, 2022
22fbb44
Merge branch 'module-resolution/ts-extensions' into module-resolution…
andrewbranch Dec 13, 2022
d4a3b3c
Rename to `bundler`
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
3 changes: 3 additions & 0 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ import {
getContainingClass,
getEffectiveContainerForJSDocTemplateTag,
getElementOrPropertyAccessName,
getEmitModuleResolutionKind,
getEmitScriptTarget,
getEnclosingBlockScopeContainer,
getErrorSpanForNode,
Expand Down Expand Up @@ -235,6 +236,7 @@ import {
ModifierFlags,
ModuleBlock,
ModuleDeclaration,
ModuleResolutionKind,
Mutable,
NamespaceExportDeclaration,
Node,
Expand Down Expand Up @@ -3520,6 +3522,7 @@ function createBinder(): (file: SourceFile, options: CompilerOptions) => void {
if (!isBindingPattern(node.name)) {
const possibleVariableDecl = node.kind === SyntaxKind.VariableDeclaration ? node : node.parent.parent;
if (isInJSFile(node) &&
getEmitModuleResolutionKind(options) !== ModuleResolutionKind.Bundler &&
isVariableDeclarationInitializedToBareOrAccessedRequire(possibleVariableDecl) &&
!getJSDocTypeTag(node) &&
!(getCombinedModifierFlags(node) & ModifierFlags.Export)
Expand Down
20 changes: 14 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ import {
getResolutionModeOverrideForClause,
getResolvedExternalModuleName,
getResolvedModule,
getResolveJsonModule,
getRestParameterElementType,
getRootDeclaration,
getScriptTargetFeatures,
Expand Down Expand Up @@ -347,8 +348,8 @@ import {
hasAccessorModifier,
hasAmbientModifier,
hasContextSensitiveParameters,
HasDecorators,
hasDecorators,
HasDecorators,
hasDynamicName,
hasEffectiveModifier,
hasEffectiveModifiers,
Expand All @@ -357,8 +358,8 @@ import {
hasExtension,
HasIllegalDecorators,
HasIllegalModifiers,
HasInitializer,
hasInitializer,
HasInitializer,
hasJSDocNodes,
hasJSDocParameterTags,
hasJsonModuleEmitEnabled,
Expand Down Expand Up @@ -4546,6 +4547,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (
namespace.valueDeclaration &&
isInJSFile(namespace.valueDeclaration) &&
getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Bundler &&
isVariableDeclaration(namespace.valueDeclaration) &&
namespace.valueDeclaration.initializer &&
isCommonJsRequire(namespace.valueDeclaration.initializer)
Expand Down Expand Up @@ -4875,7 +4877,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (tsExtension) {
errorOnTSExtensionImport(tsExtension);
}
else if (!compilerOptions.resolveJsonModule &&
else if (!getResolveJsonModule(compilerOptions) &&
fileExtensionIs(moduleReference, Extension.Json) &&
moduleResolutionKind !== ModuleResolutionKind.Classic &&
hasJsonModuleEmitEnabled(compilerOptions)) {
Expand Down Expand Up @@ -33324,7 +33326,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

// In JavaScript files, calls to any identifier 'require' are treated as external module imports
if (isInJSFile(node) && isCommonJsRequire(node)) {
if (isInJSFile(node) && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Bundler && isCommonJsRequire(node)) {
return resolveExternalModuleTypeByLiteral(node.arguments![0] as StringLiteral);
}

Expand Down Expand Up @@ -42726,6 +42728,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// Import equals declaration is deprecated in es6 or above
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);
}
else if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Bundler) {
grammarErrorOnNode(node, Diagnostics.Import_assignment_is_not_allowed_when_moduleResolution_is_set_to_bundler_Consider_using_import_Asterisk_as_ns_from_mod_import_a_from_mod_import_d_from_mod_or_another_module_format_instead);
}
}
}
}
Expand Down Expand Up @@ -42948,6 +42953,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// system modules does not support export assignment
grammarErrorOnNode(node, Diagnostics.Export_assignment_is_not_supported_when_module_flag_is_system);
}
else if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Bundler) {
grammarErrorOnNode(node, Diagnostics.Export_assignment_cannot_be_used_when_moduleResolution_is_set_to_bundler_Consider_using_export_default_or_another_module_format_instead);
}
}
}

Expand Down Expand Up @@ -44042,7 +44050,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
// 4). type A = import("./f/*gotToDefinitionHere*/oo")
if ((isExternalModuleImportEqualsDeclaration(node.parent.parent) && getExternalModuleImportEqualsDeclarationExpression(node.parent.parent) === node) ||
((node.parent.kind === SyntaxKind.ImportDeclaration || node.parent.kind === SyntaxKind.ExportDeclaration) && (node.parent as ImportDeclaration).moduleSpecifier === node) ||
((isInJSFile(node) && isRequireCall(node.parent, /*checkArgumentIsStringLiteralLike*/ false)) || isImportCall(node.parent)) ||
((isInJSFile(node) && getEmitModuleResolutionKind(compilerOptions) !== ModuleResolutionKind.Bundler && isRequireCall(node.parent, /*checkArgumentIsStringLiteralLike*/ false)) || isImportCall(node.parent)) ||
(isLiteralTypeNode(node.parent) && isLiteralImportTypeNode(node.parent.parent) && node.parent.parent.argument === node.parent)
) {
return resolveExternalModuleName(node, node as LiteralExpression, ignoreErrors);
Expand Down Expand Up @@ -47348,4 +47356,4 @@ class SymbolTrackerImpl implements SymbolTracker {
private onDiagnosticReported() {
this.context.reportedDiagnostic = true;
}
}
}
44 changes: 36 additions & 8 deletions src/compiler/commandLineParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -965,6 +965,7 @@ const commandOptionsWithoutBuild: CommandLineOption[] = [
classic: ModuleResolutionKind.Classic,
node16: ModuleResolutionKind.Node16,
nodenext: ModuleResolutionKind.NodeNext,
bundler: ModuleResolutionKind.Bundler,
})),
affectsModuleResolution: true,
paramType: Diagnostics.STRATEGY,
Expand Down Expand Up @@ -1081,14 +1082,41 @@ 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,
// },
{
name: "allowImportingTsExtensions",
type: "boolean",
affectsModuleResolution: true,
category: Diagnostics.Modules,
description: Diagnostics.Allow_imports_to_include_TypeScript_file_extensions_Requires_moduleResolution_bundler_and_either_noEmit_or_emitDeclarationOnly_to_be_set,
defaultValueDescription: false,
},
{
name: "resolvePackageJsonExports",
type: "boolean",
affectsModuleResolution: true,
category: Diagnostics.Modules,
description: Diagnostics.Use_the_package_json_exports_field_when_resolving_package_imports,
defaultValueDescription: Diagnostics.true_when_moduleResolution_is_node16_nodenext_or_bundler_otherwise_false,
},
{
name: "resolvePackageJsonImports",
type: "boolean",
affectsModuleResolution: true,
category: Diagnostics.Modules,
description: Diagnostics.Use_the_package_json_imports_field_when_resolving_imports,
defaultValueDescription: Diagnostics.true_when_moduleResolution_is_node16_nodenext_or_bundler_otherwise_false,
},
{
name: "customConditions",
type: "list",
element: {
name: "condition",
type: "string",
},
affectsModuleResolution: true,
category: Diagnostics.Modules,
description: Diagnostics.Conditions_to_set_in_addition_to_the_resolver_specific_defaults_when_resolving_imports,
},

// Source Maps
{
Expand Down
32 changes: 30 additions & 2 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4225,14 +4225,26 @@
"category": "Error",
"code": 5095
},
"Option 'allowImportingTsExtensions' can only be used when 'moduleResolution' is set to 'minimal' and either 'noEmit' or 'emitDeclarationOnly' is set.": {
"Option 'allowImportingTsExtensions' can only be used when 'moduleResolution' is set to 'bundler' 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
},
"Option '{0}' can only be used when 'moduleResolution' is set to 'node16', 'nodenext', or 'bundler'.": {
"category": "Error",
"code": 5098
},
"Import assignment is not allowed when 'moduleResolution' is set to 'bundler'. Consider using 'import * as ns from \"mod\"', 'import {a} from \"mod\"', 'import d from \"mod\"', or another module format instead.": {
"category": "Error",
"code": 5099
},
"Export assignment cannot be used when 'moduleResolution' is set to 'bundler'. Consider using 'export default' or another module format instead.": {
"category": "Error",
"code": 5100
},

"Generates a sourcemap for each corresponding '.d.ts' file.": {
"category": "Message",
Expand Down Expand Up @@ -5438,10 +5450,26 @@
"category": "Message",
"code": 6406
},
"Allow imports to include TypeScript file extensions. Requires '--moduleResolution minimal' and either '--noEmit' or '--emitDeclarationOnly' to be set.": {
"Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set.": {
"category": "Message",
"code": 6407
},
"Use the package.json 'exports' field when resolving package imports.": {
"category": "Message",
"code": 6408
},
"Use the package.json 'imports' field when resolving imports.": {
"category": "Message",
"code": 6409
},
"Conditions to set in addition to the resolver-specific defaults when resolving imports.": {
"category": "Message",
"code": 6410
},
"`true` when 'moduleResolution' is 'node16', 'nodenext', or 'bundler'; otherwise `false`.": {
"category": "Message",
"code": 6411
},

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