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

Function getModeForUsageLocation

test/fixtures/snapshot/typescript.js:116232–116259  ·  view source on GitHub ↗

* Calculates the final resolution mode for a given module reference node. This is generally the explicitly provided resolution mode, if * one exists, or the mode of the containing source file. (Excepting import=require, which is always commonjs, and dynamic import, which is always esm). *

(file, usage)

Source from the content-addressed store, hash-verified

116230 * @returns The final resolution mode of the import
116231 */
116232 function getModeForUsageLocation(file, usage) {
116233 var _a, _b;
116234 if (file.impliedNodeFormat === undefined)
116235 return undefined;
116236 if ((ts.isImportDeclaration(usage.parent) || ts.isExportDeclaration(usage.parent))) {
116237 var isTypeOnly = isExclusivelyTypeOnlyImportOrExport(usage.parent);
116238 if (isTypeOnly) {
116239 var override = getResolutionModeOverrideForClause(usage.parent.assertClause);
116240 if (override) {
116241 return override;
116242 }
116243 }
116244 }
116245 if (usage.parent.parent && ts.isImportTypeNode(usage.parent.parent)) {
116246 var override = getResolutionModeOverrideForClause((_a = usage.parent.parent.assertions) === null || _a === void 0 ? void 0 : _a.assertClause);
116247 if (override) {
116248 return override;
116249 }
116250 }
116251 if (file.impliedNodeFormat !== ts.ModuleKind.ESNext) {
116252 // in cjs files, import call expressions are esm format, otherwise everything is cjs
116253 return ts.isImportCall(ts.walkUpParenthesizedExpressions(usage.parent)) ? ts.ModuleKind.ESNext : ts.ModuleKind.CommonJS;
116254 }
116255 // in esm files, import=require statements are cjs format, otherwise everything is esm
116256 // imports are only parent'd up to their containing declaration/expression, so access farther parents with care
116257 var exprParentParent = (_b = ts.walkUpParenthesizedExpressions(usage.parent)) === null || _b === void 0 ? void 0 : _b.parent;
116258 return exprParentParent && ts.isImportEqualsDeclaration(exprParentParent) ? ts.ModuleKind.CommonJS : ts.ModuleKind.ESNext;
116259 }
116260 ts.getModeForUsageLocation = getModeForUsageLocation;
116261 /* @internal */
116262 function getResolutionModeOverrideForClause(clause, grammarErrorOnNode) {

Callers 1

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…