MCPcopy Index your code
hub / github.com/nodejs/node / resolveExternalModule

Function resolveExternalModule

test/fixtures/snapshot/typescript.js:51157–51294  ·  view source on GitHub ↗
(location, moduleReference, moduleNotFoundError, errorNode, isForAugmentation)

Source from the content-addressed store, hash-verified

51155 : undefined;
51156 }
51157 function resolveExternalModule(location, moduleReference, moduleNotFoundError, errorNode, isForAugmentation) {
51158 var _a, _b, _c, _d, _e, _f, _g, _h;
51159 if (isForAugmentation === void 0) { isForAugmentation = false; }
51160 if (ts.startsWith(moduleReference, "@types/")) {
51161 var diag = ts.Diagnostics.Cannot_import_type_declaration_files_Consider_importing_0_instead_of_1;
51162 var withoutAtTypePrefix = ts.removePrefix(moduleReference, "@types/");
51163 error(errorNode, diag, withoutAtTypePrefix, moduleReference);
51164 }
51165 var ambientModule = tryFindAmbientModule(moduleReference, /*withAugmentations*/ true);
51166 if (ambientModule) {
51167 return ambientModule;
51168 }
51169 var currentSourceFile = ts.getSourceFileOfNode(location);
51170 var contextSpecifier = ts.isStringLiteralLike(location)
51171 ? location
51172 : ((_a = ts.findAncestor(location, ts.isImportCall)) === null || _a === void 0 ? void 0 : _a.arguments[0]) ||
51173 ((_b = ts.findAncestor(location, ts.isImportDeclaration)) === null || _b === void 0 ? void 0 : _b.moduleSpecifier) ||
51174 ((_c = ts.findAncestor(location, ts.isExternalModuleImportEqualsDeclaration)) === null || _c === void 0 ? void 0 : _c.moduleReference.expression) ||
51175 ((_d = ts.findAncestor(location, ts.isExportDeclaration)) === null || _d === void 0 ? void 0 : _d.moduleSpecifier) ||
51176 ((_e = (ts.isModuleDeclaration(location) ? location : location.parent && ts.isModuleDeclaration(location.parent) && location.parent.name === location ? location.parent : undefined)) === null || _e === void 0 ? void 0 : _e.name) ||
51177 ((_f = (ts.isLiteralImportTypeNode(location) ? location : undefined)) === null || _f === void 0 ? void 0 : _f.argument.literal);
51178 var mode = contextSpecifier && ts.isStringLiteralLike(contextSpecifier) ? ts.getModeForUsageLocation(currentSourceFile, contextSpecifier) : currentSourceFile.impliedNodeFormat;
51179 var resolvedModule = ts.getResolvedModule(currentSourceFile, moduleReference, mode);
51180 var resolutionDiagnostic = resolvedModule && ts.getResolutionDiagnostic(compilerOptions, resolvedModule);
51181 var sourceFile = resolvedModule
51182 && (!resolutionDiagnostic || resolutionDiagnostic === ts.Diagnostics.Module_0_was_resolved_to_1_but_jsx_is_not_set)
51183 && host.getSourceFile(resolvedModule.resolvedFileName);
51184 if (sourceFile) {
51185 // If there's a resolutionDiagnostic we need to report it even if a sourceFile is found.
51186 if (resolutionDiagnostic) {
51187 error(errorNode, resolutionDiagnostic, moduleReference, resolvedModule.resolvedFileName);
51188 }
51189 if (sourceFile.symbol) {
51190 if (resolvedModule.isExternalLibraryImport && !ts.resolutionExtensionIsTSOrJson(resolvedModule.extension)) {
51191 errorOnImplicitAnyModule(/*isError*/ false, errorNode, resolvedModule, moduleReference);
51192 }
51193 if (ts.getEmitModuleResolutionKind(compilerOptions) === ts.ModuleResolutionKind.Node16 || ts.getEmitModuleResolutionKind(compilerOptions) === ts.ModuleResolutionKind.NodeNext) {
51194 var isSyncImport = (currentSourceFile.impliedNodeFormat === ts.ModuleKind.CommonJS && !ts.findAncestor(location, ts.isImportCall)) || !!ts.findAncestor(location, ts.isImportEqualsDeclaration);
51195 var overrideClauseHost = ts.findAncestor(location, function (l) { return ts.isImportTypeNode(l) || ts.isExportDeclaration(l) || ts.isImportDeclaration(l); });
51196 var overrideClause = overrideClauseHost && ts.isImportTypeNode(overrideClauseHost) ? (_g = overrideClauseHost.assertions) === null || _g === void 0 ? void 0 : _g.assertClause : overrideClauseHost === null || overrideClauseHost === void 0 ? void 0 : overrideClauseHost.assertClause;
51197 // An override clause will take effect for type-only imports and import types, and allows importing the types across formats, regardless of
51198 // normal mode restrictions
51199 if (isSyncImport && sourceFile.impliedNodeFormat === ts.ModuleKind.ESNext && !ts.getResolutionModeOverrideForClause(overrideClause)) {
51200 error(errorNode, ts.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);
51201 }
51202 }
51203 // merged symbol is module declaration symbol combined with all augmentations
51204 return getMergedSymbol(sourceFile.symbol);
51205 }
51206 if (moduleNotFoundError) {
51207 // report errors only if it was requested
51208 error(errorNode, ts.Diagnostics.File_0_is_not_a_module, sourceFile.fileName);
51209 }
51210 return undefined;
51211 }
51212 if (patternAmbientModules) {
51213 var pattern = ts.findBestPatternMatch(patternAmbientModules, function (_) { return _.pattern; }, moduleReference);
51214 if (pattern) {

Calls 6

tryFindAmbientModuleFunction · 0.85
errorOnImplicitAnyModuleFunction · 0.85
getMergedSymbolFunction · 0.85
errorFunction · 0.70
getMethod · 0.65
findMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…