Use TSImportType.source (#18319)

This commit is contained in:
fisker Cheung authored and GitHub committed 2025-11-25 04:16:22 +08:00
1 parent 138f92764a
commit 864bb7c042
6 files changed
+22 -30

No files matched your search

-5
View File
@@ -186,11 +186,6 @@ function clean(original, cloned, parent) {
cloned.type = "TSNonNullExpression";
cloned.expression.type = "ChainExpression";
}
// https://github.com/prettier/prettier/pull/18316
if (original.type === "TSImportType") {
delete cloned.source;
}
}
clean.ignoredProperties = ignoredProperties;
@@ -151,6 +151,15 @@ function postprocess(ast, options) {
node.options = node.attributes;
}
break;
// https://github.com/babel/babel/issues/17506
// https://github.com/oxc-project/oxc/issues/16074
case "TSImportType":
if (!node.source && node.argument.type === "TSLiteralType") {
node.source = node.argument.literal;
delete node.argument;
}
break;
}
/* c8 ignore next 3 */
+6 -13
View File
@@ -108,10 +108,14 @@ function printCallExpression(path, options, print) {
function printCallee(path, print) {
const { node } = path;
if (node.type === "ImportExpression" || node.type === "TSImportType") {
if (node.type === "ImportExpression") {
return `import${node.phase ? `.${node.phase}` : ""}`;
}
if (node.type === "TSImportType") {
return "import";
}
if (node.type === "TSExternalModuleReference") {
return "require";
}
@@ -152,18 +156,7 @@ function isSimpleModuleImport(path) {
const args = getCallArguments(node);
if (args.length !== 1 || hasComment(args[0])) {
return false;
}
let source = args[0];
// TODO: remove this once https://github.com/typescript-eslint/typescript-eslint/issues/11583 get fixed
if (node.type === "TSImportType" && source.type === "TSLiteralType") {
source = source.literal;
}
return isStringLiteral(source);
return args.length === 1 && isStringLiteral(args[0]) && !hasComment(args[0]);
}
function isCommonsJsOrAmdModuleDefinition(path) {
@@ -44,8 +44,9 @@ const excludeVisitorKeys = {
ExportAllDeclaration: ["assertions"],
ImportDeclaration: ["assertions"],
// https://github.com/prettier/prettier/pull/18316
TSImportType: ["source"],
// https://github.com/babel/babel/issues/17506
// https://github.com/oxc-project/oxc/issues/16074
TSImportType: ["argument"],
};
// https://github.com/babel/babel/issues/17524
+3 -9
View File
@@ -875,7 +875,7 @@ function getCallArguments(node) {
let args;
if (node.type === "ImportExpression" || node.type === "TSImportType") {
args = [node.type === "ImportExpression" ? node.source : node.argument];
args = [node.source];
if (node.options) {
args.push(node.options);
@@ -901,10 +901,7 @@ function iterateCallArgumentsPath(path, iteratee) {
}
if (node.type === "ImportExpression" || node.type === "TSImportType") {
path.call(
() => iteratee(path, 0),
node.type === "ImportExpression" ? "source" : "argument",
);
path.call(() => iteratee(path, 0), "source");
if (node.options) {
path.call(() => iteratee(path, 1), "options");
@@ -925,10 +922,7 @@ function getCallArgumentSelector(node, index) {
if (node.type === "ImportExpression" || node.type === "TSImportType") {
if (index === 0 || index === (node.options ? -2 : -1)) {
return [
...selectors,
node.type === "ImportExpression" ? "source" : "argument",
];
return [...selectors, "source"];
}
if (node.options && (index === 1 || index === -1)) {
return [...selectors, "options"];
@@ -892,9 +892,9 @@ exports[`visitor keys estree 1`] = `
"moduleReference",
],
"TSImportType": [
"argument",
"options",
"qualifier",
"source",
"typeArguments",
],
"TSIndexSignature": [