Skip to content

Commit

Permalink
Add test for dynamic import type, but it doesn't work due to swc fail…
Browse files Browse the repository at this point in the history
…ing to parse
  • Loading branch information
dsherret committed Nov 26, 2024
1 parent 1d8125e commit 3169e42
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 24 deletions.
42 changes: 18 additions & 24 deletions cli/tsc/99_main_compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,13 @@ delete Object.prototype.__proto__;
}

/**
* @param {ts.Expression | undefined} lit
* @param {ts.ImportAttributes | undefined} attribs
* @returns {ts.ResolutionMode | undefined}
*/
function getResolutionModeFromStringLit(lit) {
function getResolutionMode(attribs) {
const lit = attribs?.elements
.find((e) => e.name.text === "resolution-mode")
?.value;
if (!lit || !ts.isStringLiteral(lit)) {
return undefined;
}
Expand All @@ -131,28 +134,19 @@ delete Object.prototype.__proto__;
// non-type import declarations as being import declarations
const parent = usage.parent;
if (ts.isImportDeclaration(parent)) {
return getResolutionModeFromStringLit(
parent.attributes?.elements.find((e) =>
e.name.text === "resolution-mode"
)?.value,
) ?? ts.ModuleKind.ESNext;
} else if (
ts.isCallExpression(parent) &&
parent.expression.kind === ts.SyntaxKind.ImportKeyword
) {
const secondArg = parent.arguments[1];
const objArg = secondArg && ts.isObjectLiteralExpression(secondArg)
? secondArg
: undefined;
const resolutionModeProp = objArg?.properties.find((e) =>
e.name && ts.isStringLiteral(e.name) &&
e.name.text === "resolution-mode"
);
return getResolutionModeFromStringLit(
resolutionModeProp && ts.isPropertyAssignment(resolutionModeProp)
? resolutionModeProp.initializer
: undefined,
) ?? ts.ModuleKind.ESNext;
if (file.isDeclarationFile || parent.importClause?.isTypeOnly) {
return getResolutionMode(parent.attributes) ?? ts.ModuleKind.ESNext;
} else {
return ts.ModuleKind.ESNext;
}
} else if (ts.isExportDeclaration(parent)) {
if (file.isDeclarationFile || parent.isTypeOnly) {
return getResolutionMode(parent.attributes) ?? ts.ModuleKind.ESNext;
} else {
return ts.ModuleKind.ESNext;
}
} else if (ts.isImportTypeNode(parent)) {
return getResolutionMode(parent.attributes) ?? ts.ModuleKind.ESNext;
} else {
return ts.getModeForUsageLocation(file, usage, compilerOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@
"args": "check resolution_mode_require_jsdoc.js",
"output": "resolution_mode_require_jsdoc.out",
"exitCode": 1
},
"resolution_mode_require_import_type": {
// waiting on https://github.com/swc-project/swc/issues/9377
"ignore": true,
"args": "check resolution_mode_require_import_type.ts",
"output": "resolution_mode_require_import_type.out",
"exitCode": 1
}
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Value = typeof import("package", { with: { 'resolution-mode': 'require' } }).kind;

const value: Value = "value";
console.log(value);

0 comments on commit 3169e42

Please sign in to comment.