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 13 commits
Commits
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
19 changes: 14 additions & 5 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 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
29 changes: 29 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
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
Loading