Use printCallExpression to print TSExternalModuleReference (#18035)
This commit is contained in:
1 parent
fc7da89a54
commit
05b1a783d1
9 files changed
+109
-33
No files matched your search
@@ -8,28 +8,13 @@ require(
|
||||
"foo"
|
||||
);
|
||||
|
||||
call(
|
||||
// Comment
|
||||
"foo"
|
||||
);
|
||||
|
||||
// Prettier stable
|
||||
require(// Comment
|
||||
"foo");
|
||||
|
||||
call(
|
||||
// Comment
|
||||
"foo",
|
||||
);
|
||||
|
||||
// Prettier main
|
||||
require(
|
||||
// Comment
|
||||
"foo",
|
||||
);
|
||||
|
||||
call(
|
||||
// Comment
|
||||
"foo",
|
||||
);
|
||||
```
|
||||
@@ -0,0 +1,20 @@
|
||||
#### Improve CommonJS module `require()` with comments (#18035 by @fisker)
|
||||
|
||||
<!-- prettier-ignore -->
|
||||
```tsx
|
||||
// Input
|
||||
import foo = require(
|
||||
// Comment
|
||||
"foo"
|
||||
);
|
||||
|
||||
// Prettier stable
|
||||
import foo = require(// Comment
|
||||
"foo");
|
||||
|
||||
// Prettier main
|
||||
import foo = require(
|
||||
// Comment
|
||||
"foo"
|
||||
);
|
||||
```
|
||||
@@ -37,6 +37,14 @@ import {
|
||||
} from "../utils/index.js";
|
||||
import { isConciselyPrintedArray } from "./array.js";
|
||||
|
||||
/*
|
||||
- `NewExpression`
|
||||
- `ImportExpression`
|
||||
- `OptionalCallExpression`
|
||||
- `CallExpression`
|
||||
- `TSImportType` (TypeScript)
|
||||
- `TSExternalModuleReference` (TypeScript)
|
||||
*/
|
||||
function printCallArguments(path, options, print) {
|
||||
const { node } = path;
|
||||
|
||||
@@ -84,6 +92,7 @@ function printCallArguments(path, options, print) {
|
||||
// Dynamic imports cannot have trailing commas
|
||||
node.type !== "ImportExpression" &&
|
||||
node.type !== "TSImportType" &&
|
||||
node.type !== "TSExternalModuleReference" &&
|
||||
shouldPrintComma(options, "all")
|
||||
? ","
|
||||
: "";
|
||||
|
||||
@@ -21,6 +21,7 @@ import { printOptionalToken } from "./misc.js";
|
||||
- `OptionalCallExpression`
|
||||
- `CallExpression`
|
||||
- `TSImportType` (TypeScript)
|
||||
- `TSExternalModuleReference` (TypeScript)
|
||||
*/
|
||||
function printCallExpression(path, options, print) {
|
||||
const { node } = path;
|
||||
@@ -67,13 +68,15 @@ function printCallExpression(path, options, print) {
|
||||
}
|
||||
}
|
||||
|
||||
const isDynamicImport =
|
||||
node.type === "ImportExpression" || node.type === "TSImportType";
|
||||
const isDynamicImportLike =
|
||||
node.type === "ImportExpression" ||
|
||||
node.type === "TSImportType" ||
|
||||
node.type === "TSExternalModuleReference";
|
||||
|
||||
// We detect calls on member lookups and possibly print them in a
|
||||
// special chain format. See `printMemberChain` for more info.
|
||||
if (
|
||||
!isDynamicImport &&
|
||||
!isDynamicImportLike &&
|
||||
!isNewExpression &&
|
||||
isMemberish(node.callee) &&
|
||||
!path.call(
|
||||
@@ -95,7 +98,7 @@ function printCallExpression(path, options, print) {
|
||||
|
||||
// We group here when the callee is itself a call expression.
|
||||
// See `isLongCurriedCallExpression` for more info.
|
||||
if (isDynamicImport || isCallExpression(node.callee)) {
|
||||
if (isDynamicImportLike || isCallExpression(node.callee)) {
|
||||
return group(contents);
|
||||
}
|
||||
|
||||
@@ -109,6 +112,10 @@ function printCallee(path, print) {
|
||||
return `import${node.phase ? `.${node.phase}` : ""}`;
|
||||
}
|
||||
|
||||
if (node.type === "TSExternalModuleReference") {
|
||||
return "require";
|
||||
}
|
||||
|
||||
return print("callee");
|
||||
}
|
||||
|
||||
@@ -128,6 +135,8 @@ function isSimpleModuleImport(path) {
|
||||
node.type === "ImportExpression" ||
|
||||
// `type foo = import("foo")`
|
||||
node.type === "TSImportType" ||
|
||||
// `import type A = require("foo")`
|
||||
node.type === "TSExternalModuleReference" ||
|
||||
// `require("foo")`
|
||||
// `require.resolve("foo")`
|
||||
// `require.resolve.paths("foo")`
|
||||
|
||||
@@ -278,7 +278,7 @@ function printTypescript(path, options, print) {
|
||||
options.semi ? ";" : "",
|
||||
];
|
||||
case "TSExternalModuleReference":
|
||||
return ["require(", print("expression"), ")"];
|
||||
return printCallExpression(path, options, print);
|
||||
case "TSModuleDeclaration": {
|
||||
const parts = [];
|
||||
const { parent } = path;
|
||||
|
||||
@@ -855,7 +855,7 @@ function getFunctionParameters(node) {
|
||||
function iterateFunctionParametersPath(path, iteratee) {
|
||||
const { node } = path;
|
||||
let index = 0;
|
||||
const callback = (childPath) => iteratee(childPath, index++);
|
||||
const callback = () => iteratee(path, index++);
|
||||
if (node.this) {
|
||||
path.call(callback, "this");
|
||||
}
|
||||
@@ -877,13 +877,17 @@ function getCallArguments(node) {
|
||||
return getCallArguments(node.expression);
|
||||
}
|
||||
|
||||
let args = node.arguments;
|
||||
let args;
|
||||
if (node.type === "ImportExpression" || node.type === "TSImportType") {
|
||||
args = [node.type === "ImportExpression" ? node.source : node.argument];
|
||||
|
||||
if (node.options) {
|
||||
args.push(node.options);
|
||||
}
|
||||
} else if (node.type === "TSExternalModuleReference") {
|
||||
args = [node.expression];
|
||||
} else {
|
||||
args = node.arguments;
|
||||
}
|
||||
|
||||
callArgumentsCache.set(node, args);
|
||||
@@ -902,13 +906,15 @@ function iterateCallArgumentsPath(path, iteratee) {
|
||||
|
||||
if (node.type === "ImportExpression" || node.type === "TSImportType") {
|
||||
path.call(
|
||||
(sourcePath) => iteratee(sourcePath, 0),
|
||||
() => iteratee(path, 0),
|
||||
node.type === "ImportExpression" ? "source" : "argument",
|
||||
);
|
||||
|
||||
if (node.options) {
|
||||
path.call((sourcePath) => iteratee(sourcePath, 1), "options");
|
||||
path.call(() => iteratee(path, 1), "options");
|
||||
}
|
||||
} else if (node.type === "TSExternalModuleReference") {
|
||||
path.call(() => iteratee(path, 0), "expression");
|
||||
} else {
|
||||
path.each(iteratee, "arguments");
|
||||
}
|
||||
@@ -932,15 +938,21 @@ function getCallArgumentSelector(node, index) {
|
||||
return [...selectors, "options"];
|
||||
}
|
||||
throw new RangeError("Invalid argument index");
|
||||
} else if (node.type === "TSExternalModuleReference") {
|
||||
if (index === 0 || index === -1) {
|
||||
return [...selectors, "expression"];
|
||||
}
|
||||
} else {
|
||||
if (index < 0) {
|
||||
index = node.arguments.length + index;
|
||||
}
|
||||
if (index >= 0 && index < node.arguments.length) {
|
||||
return [...selectors, "arguments", index];
|
||||
}
|
||||
}
|
||||
if (index < 0) {
|
||||
index = node.arguments.length + index;
|
||||
}
|
||||
/* c8 ignore next 3 */
|
||||
if (index < 0 || index >= node.arguments.length) {
|
||||
throw new RangeError("Invalid argument index");
|
||||
}
|
||||
return [...selectors, "arguments", index];
|
||||
|
||||
/* c8 ignore next */
|
||||
throw new RangeError("Invalid argument index");
|
||||
}
|
||||
|
||||
function isPrettierIgnoreComment(comment) {
|
||||
|
||||
@@ -1,6 +1,38 @@
|
||||
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
|
||||
|
||||
exports[`import_require.ts format 1`] = `
|
||||
exports[`comments.ts format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["typescript"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
import A1 = require("./a/long/long/long/long/long/long/long/long/long/long/long/long/path/to/module");
|
||||
const A2 = require("./a/long/long/long/long/long/long/long/long/long/long/long/long/path/to/module");
|
||||
|
||||
import B1 = require(
|
||||
// 1
|
||||
"b");
|
||||
const B2 = require(
|
||||
// 2
|
||||
"b");
|
||||
|
||||
=====================================output=====================================
|
||||
import A1 = require("./a/long/long/long/long/long/long/long/long/long/long/long/long/path/to/module");
|
||||
const A2 = require("./a/long/long/long/long/long/long/long/long/long/long/long/long/path/to/module");
|
||||
|
||||
import B1 = require(
|
||||
// 1
|
||||
"b"
|
||||
);
|
||||
const B2 = require(
|
||||
// 2
|
||||
"b",
|
||||
);
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
exports[`import-require.ts format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["typescript"]
|
||||
printWidth: 80
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import A1 = require("./a/long/long/long/long/long/long/long/long/long/long/long/long/path/to/module");
|
||||
const A2 = require("./a/long/long/long/long/long/long/long/long/long/long/long/long/path/to/module");
|
||||
|
||||
import B1 = require(
|
||||
// 1
|
||||
"b");
|
||||
const B2 = require(
|
||||
// 2
|
||||
"b");
|
||||
File renamed without changes.
Reference in new issue
Block a user