Unify require() and import() formatting logic (#17902)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
6 files changed
+67
-30
No files matched your search
@@ -78,24 +78,6 @@ function printCallArguments(path, options, print) {
|
|||||||
printedArguments.push(argDoc);
|
printedArguments.push(argDoc);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Don't break simple import with long module name
|
|
||||||
if (
|
|
||||||
(node.type === "TSImportType" || node.type === "ImportExpression") &&
|
|
||||||
args.length === 1 &&
|
|
||||||
!hasComment(args[0])
|
|
||||||
) {
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isStringLiteral(source)) {
|
|
||||||
return group(["(", printedArguments, ")"]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const maybeTrailingComma =
|
const maybeTrailingComma =
|
||||||
// Angular does not allow trailing comma
|
// Angular does not allow trailing comma
|
||||||
!options.parser.startsWith("__ng_") &&
|
!options.parser.startsWith("__ng_") &&
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { group, join } from "../../document/builders.js";
|
|||||||
import pathNeedsParens from "../needs-parens.js";
|
import pathNeedsParens from "../needs-parens.js";
|
||||||
import {
|
import {
|
||||||
getCallArguments,
|
getCallArguments,
|
||||||
|
hasComment,
|
||||||
isCallExpression,
|
isCallExpression,
|
||||||
isMemberish,
|
isMemberish,
|
||||||
isStringLiteral,
|
isStringLiteral,
|
||||||
@@ -15,8 +16,7 @@ import { printFunctionTypeParameters, printOptionalToken } from "./misc.js";
|
|||||||
|
|
||||||
function printCallExpression(path, options, print) {
|
function printCallExpression(path, options, print) {
|
||||||
const { node } = path;
|
const { node } = path;
|
||||||
const isNew = node.type === "NewExpression";
|
const isNewExpression = node.type === "NewExpression";
|
||||||
const isDynamicImport = node.type === "ImportExpression";
|
|
||||||
|
|
||||||
const optional = printOptionalToken(path);
|
const optional = printOptionalToken(path);
|
||||||
const args = getCallArguments(node);
|
const args = getCallArguments(node);
|
||||||
@@ -26,6 +26,8 @@ function printCallExpression(path, options, print) {
|
|||||||
|
|
||||||
if (
|
if (
|
||||||
isTemplateLiteralSingleArg ||
|
isTemplateLiteralSingleArg ||
|
||||||
|
// Don't break simple `import()` with long module name
|
||||||
|
isSimpleModuleImport(path) ||
|
||||||
// Dangling comments are not handled, all these special cases should have arguments #9668
|
// Dangling comments are not handled, all these special cases should have arguments #9668
|
||||||
// We want to keep CommonJS- and AMD-style require calls, and AMD-style
|
// We want to keep CommonJS- and AMD-style require calls, and AMD-style
|
||||||
// define calls, as a unit.
|
// define calls, as a unit.
|
||||||
@@ -41,7 +43,7 @@ function printCallExpression(path, options, print) {
|
|||||||
});
|
});
|
||||||
if (!(isTemplateLiteralSingleArg && printed[0].label?.embed)) {
|
if (!(isTemplateLiteralSingleArg && printed[0].label?.embed)) {
|
||||||
return [
|
return [
|
||||||
isNew ? "new " : "",
|
isNewExpression ? "new " : "",
|
||||||
printCallee(path, print),
|
printCallee(path, print),
|
||||||
optional,
|
optional,
|
||||||
printFunctionTypeParameters(path, options, print),
|
printFunctionTypeParameters(path, options, print),
|
||||||
@@ -52,11 +54,14 @@ function printCallExpression(path, options, print) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const isDynamicImport =
|
||||||
|
node.type === "ImportExpression" || node.type === "TSImportType";
|
||||||
|
|
||||||
// We detect calls on member lookups and possibly print them in a
|
// We detect calls on member lookups and possibly print them in a
|
||||||
// special chain format. See `printMemberChain` for more info.
|
// special chain format. See `printMemberChain` for more info.
|
||||||
if (
|
if (
|
||||||
!isDynamicImport &&
|
!isDynamicImport &&
|
||||||
!isNew &&
|
!isNewExpression &&
|
||||||
isMemberish(node.callee) &&
|
isMemberish(node.callee) &&
|
||||||
!path.call(
|
!path.call(
|
||||||
(path) => pathNeedsParens(path, options),
|
(path) => pathNeedsParens(path, options),
|
||||||
@@ -68,7 +73,7 @@ function printCallExpression(path, options, print) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const contents = [
|
const contents = [
|
||||||
isNew ? "new " : "",
|
isNewExpression ? "new " : "",
|
||||||
printCallee(path, print),
|
printCallee(path, print),
|
||||||
optional,
|
optional,
|
||||||
printFunctionTypeParameters(path, options, print),
|
printFunctionTypeParameters(path, options, print),
|
||||||
@@ -87,13 +92,50 @@ function printCallExpression(path, options, print) {
|
|||||||
function printCallee(path, print) {
|
function printCallee(path, print) {
|
||||||
const { node } = path;
|
const { node } = path;
|
||||||
|
|
||||||
if (node.type === "ImportExpression") {
|
if (node.type === "ImportExpression" || node.type === "TSImportType") {
|
||||||
return `import${node.phase ? `.${node.phase}` : ""}`;
|
return `import${node.phase ? `.${node.phase}` : ""}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
return print("callee");
|
return print("callee");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isSimpleModuleImport(path) {
|
||||||
|
const { node } = path;
|
||||||
|
|
||||||
|
if (
|
||||||
|
!(
|
||||||
|
// `import("foo")`
|
||||||
|
(
|
||||||
|
node.type === "ImportExpression" ||
|
||||||
|
// `type foo = import("foo")`
|
||||||
|
node.type === "TSImportType" ||
|
||||||
|
// `require("foo")`
|
||||||
|
(node.type === "CallExpression" &&
|
||||||
|
!node.optional &&
|
||||||
|
node.callee.type === "Identifier" &&
|
||||||
|
node.callee.name === "require")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
function isCommonsJsOrAmdModuleDefinition(path) {
|
function isCommonsJsOrAmdModuleDefinition(path) {
|
||||||
const { node } = path;
|
const { node } = path;
|
||||||
|
|
||||||
|
|||||||
@@ -91,13 +91,21 @@ function printAbstractToken({ node }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function printFunctionTypeParameters(path, options, print) {
|
function printFunctionTypeParameters(path, options, print) {
|
||||||
const fun = path.node;
|
const { node } = path;
|
||||||
if (fun.typeArguments) {
|
|
||||||
|
// `TSImportType.typeArguments` is after `qualifier`, not before the "arguments"
|
||||||
|
if (node.type === "TSImportType") {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (node.typeArguments) {
|
||||||
return print("typeArguments");
|
return print("typeArguments");
|
||||||
}
|
}
|
||||||
if (fun.typeParameters) {
|
|
||||||
|
if (node.typeParameters) {
|
||||||
return print("typeParameters");
|
return print("typeParameters");
|
||||||
}
|
}
|
||||||
|
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,7 +15,7 @@ import {
|
|||||||
import isTsKeywordType from "../utils/is-ts-keyword-type.js";
|
import isTsKeywordType from "../utils/is-ts-keyword-type.js";
|
||||||
import { printArray } from "./array.js";
|
import { printArray } from "./array.js";
|
||||||
import { printBlock } from "./block.js";
|
import { printBlock } from "./block.js";
|
||||||
import printCallArguments from "./call-arguments.js";
|
import { printCallExpression } from "./call-expression.js";
|
||||||
import { printBinaryCastExpression } from "./cast-expression.js";
|
import { printBinaryCastExpression } from "./cast-expression.js";
|
||||||
import { printClassMethod, printClassProperty } from "./class.js";
|
import { printClassMethod, printClassProperty } from "./class.js";
|
||||||
import {
|
import {
|
||||||
@@ -215,8 +215,7 @@ function printTypescript(path, options, print) {
|
|||||||
return [print("expression"), "!"];
|
return [print("expression"), "!"];
|
||||||
case "TSImportType":
|
case "TSImportType":
|
||||||
return [
|
return [
|
||||||
"import",
|
printCallExpression(path, options, print),
|
||||||
printCallArguments(path, options, print),
|
|
||||||
!node.qualifier ? "" : [".", print("qualifier")],
|
!node.qualifier ? "" : [".", print("qualifier")],
|
||||||
printTypeParameters(
|
printTypeParameters(
|
||||||
path,
|
path,
|
||||||
|
|||||||
@@ -24,6 +24,8 @@ const plugin2 = require(
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const plugin3 = require('./a/long/long/long/long/long/long/long/long/long/long/long/path/to/module')
|
||||||
|
|
||||||
=====================================output=====================================
|
=====================================output=====================================
|
||||||
const {
|
const {
|
||||||
one,
|
one,
|
||||||
@@ -63,5 +65,7 @@ const plugin2 = require(
|
|||||||
path.join(__dirname, global.STANDALONE ? "../standalone.js" : ".."),
|
path.join(__dirname, global.STANDALONE ? "../standalone.js" : ".."),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const plugin3 = require("./a/long/long/long/long/long/long/long/long/long/long/long/path/to/module");
|
||||||
|
|
||||||
================================================================================
|
================================================================================
|
||||||
`;
|
`;
|
||||||
@@ -15,3 +15,5 @@ const plugin2 = require(
|
|||||||
global.STANDALONE ? "../standalone.js" : '..'
|
global.STANDALONE ? "../standalone.js" : '..'
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const plugin3 = require('./a/long/long/long/long/long/long/long/long/long/long/long/path/to/module')
|
||||||
Reference in new issue
Block a user