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

Initial import/export/self-name support #68

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 15 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ namespace ts {
host.getSourceFiles().forEach(sf => {
if (!sf.resolvedModules) return;

forEachEntry(sf.resolvedModules, r => {
sf.resolvedModules.forEach(r => {
if (r && r.packageId) map.set(r.packageId.name, r.extension === Extension.Dts || !!map.get(r.packageId.name));
});
});
Expand Down Expand Up @@ -1832,7 +1832,7 @@ namespace ts {
isInExternalModule = true;
// falls through
case SyntaxKind.ModuleDeclaration:
const moduleExports = getSymbolOfNode(location as SourceFile | ModuleDeclaration).exports || emptySymbols;
const moduleExports = getSymbolOfNode(location as SourceFile | ModuleDeclaration)?.exports || emptySymbols;
if (location.kind === SyntaxKind.SourceFile || (isModuleDeclaration(location) && location.flags & NodeFlags.Ambient && !isGlobalScopeAugmentation(location))) {

// It's an external module. First see if the module has an export default and if the local
Expand Down Expand Up @@ -1875,7 +1875,7 @@ namespace ts {
}
break;
case SyntaxKind.EnumDeclaration:
if (result = lookup(getSymbolOfNode(location)!.exports!, name, meaning & SymbolFlags.EnumMember)) {
if (result = lookup(getSymbolOfNode(location)?.exports || emptySymbols, name, meaning & SymbolFlags.EnumMember)) {
break loop;
}
break;
Expand Down Expand Up @@ -3377,7 +3377,15 @@ namespace ts {
return ambientModule;
}
const currentSourceFile = getSourceFileOfNode(location);
const resolvedModule = getResolvedModule(currentSourceFile, moduleReference)!; // TODO: GH#18217
const contextSpecifier = isStringLiteralLike(location)
? location
: findAncestor(location, isImportCall)?.arguments[0] ||
weswigham marked this conversation as resolved.
Show resolved Hide resolved
findAncestor(location, isImportDeclaration)?.moduleSpecifier ||
findAncestor(location, isExternalModuleImportEqualsDeclaration)?.moduleReference.expression ||
findAncestor(location, isExportDeclaration)?.moduleSpecifier ||
(isModuleDeclaration(location) ? location : location.parent && isModuleDeclaration(location.parent) && location.parent.name === location ? location.parent : undefined)?.name ||
(isLiteralImportTypeNode(location) ? location : undefined)?.argument.literal;
const resolvedModule = getResolvedModule(currentSourceFile, moduleReference, contextSpecifier && isStringLiteralLike(contextSpecifier) ? getModeForUsageLocation(currentSourceFile, contextSpecifier) : undefined)!; // TODO: GH#18217
const resolutionDiagnostic = resolvedModule && getResolutionDiagnostic(compilerOptions, resolvedModule);
const sourceFile = resolvedModule && !resolutionDiagnostic && host.getSourceFile(resolvedModule.resolvedFileName);
if (sourceFile) {
Expand All @@ -3388,7 +3396,7 @@ namespace ts {
if (getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.Node12 || getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeNext) {
const isSyncImport = (currentSourceFile.impliedNodeFormat === ModuleKind.CommonJS && !findAncestor(location, isImportCall)) || !!findAncestor(location, isImportEqualsDeclaration);
if (isSyncImport && sourceFile.impliedNodeFormat === ModuleKind.ESNext) {
error(errorNode, Diagnostics.Module_0_cannot_be_imported_using_this_construct_The_specifier_only_resolves_to_an_es_module_which_cannot_be_imported_synchronously_Use_dynamic_import_instead, moduleReference);
error(errorNode, Diagnostics.Module_0_cannot_be_imported_using_this_construct_The_specifier_only_resolves_to_an_ES_module_which_cannot_be_imported_synchronously_Use_dynamic_import_instead, moduleReference);
}
}
// merged symbol is module declaration symbol combined with all augmentations
Expand Down Expand Up @@ -3453,7 +3461,7 @@ namespace ts {
* @see https://github.com/microsoft/TypeScript/issues/42151
*/
if (moduleKind >= ModuleKind.ES2015) {
replacedImportSource += ".js";
replacedImportSource += tsExtension === Extension.Mts ? ".mjs" : tsExtension === Extension.Cts ? ".cjs" : ".js";
}
error(errorNode, diag, tsExtension, replacedImportSource);
}
Expand Down Expand Up @@ -4603,6 +4611,7 @@ namespace ts {
isSourceOfProjectReferenceRedirect: fileName => host.isSourceOfProjectReferenceRedirect(fileName),
fileExists: fileName => host.fileExists(fileName),
getFileIncludeReasons: () => host.getFileIncludeReasons(),
readFile: host.readFile ? (fileName => host.readFile!(fileName)) : undefined,
} : undefined },
encounteredError: false,
reportedDiagnostic: false,
Expand Down
31 changes: 30 additions & 1 deletion src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1397,7 +1397,7 @@
"category": "Error",
"code": 1470
},
"Module '{0}' cannot be imported using this construct. The specifier only resolves to an es module, which cannot be imported synchronously. Use dynamic import instead.": {
"Module '{0}' cannot be imported using this construct. The specifier only resolves to an ES module, which cannot be imported synchronously. Use dynamic import instead.": {
"category": "Error",
"code": 1471
},
Expand Down Expand Up @@ -4895,6 +4895,35 @@
"code": 6258
},

"Directory '{0}' has no containing package.json scope. Imports will not resolve.": {
"category": "Message",
"code": 6270
},
"Import specifier '{0}' does not exist in package.json scope at path '{1}'.": {
"category": "Message",
"code": 6271
},
"Invalid import specifier '{0}' has no possible resolutions.": {
"category": "Message",
"code": 6272
},
"package.json scope '{0}' has no imports defined.": {
"category": "Message",
"code": 6273
},
"package.json scope '{0}' explicitly maps specifier '{1}' to null.": {
"category": "Message",
"code": 6274
},
"package.json scope '{0}' has invalid type for target of specifier '{1}'": {
"category": "Message",
"code": 6275
},
"Export specifier '{0}' does not exist in package.json scope at path '{1}'.": {
"category": "Message",
"code": 6276
},

"Enable project compilation": {
"category": "Message",
"code": 6302
Expand Down