Improve comments around type arguments print (#18620)
This commit is contained in:
1 parent
1b36f27d8e
commit
e636454118
9 files changed
+250
-19
No files matched your search
@@ -9,6 +9,7 @@ import hasNewline from "../../utilities/has-newline.js";
|
||||
import hasNewlineInRange from "../../utilities/has-newline-in-range.js";
|
||||
import isNonEmptyArray from "../../utilities/is-non-empty-array.js";
|
||||
import { locEnd, locStart } from "../loc.js";
|
||||
import { isInsideCallExpressionParentheses } from "../utilities/call-expression-parentheses.js";
|
||||
import getTextWithoutComments from "../utilities/get-text-without-comments.js";
|
||||
import {
|
||||
createTypeCheckFunction,
|
||||
@@ -780,12 +781,13 @@ function handleCallExpressionComments({
|
||||
comment,
|
||||
precedingNode,
|
||||
enclosingNode,
|
||||
options,
|
||||
}) {
|
||||
if (
|
||||
isCallExpression(enclosingNode) &&
|
||||
precedingNode &&
|
||||
enclosingNode.callee === precedingNode &&
|
||||
enclosingNode.arguments.length > 0
|
||||
enclosingNode.arguments.length > 0 &&
|
||||
isInsideCallExpressionParentheses(enclosingNode, comment, options)
|
||||
) {
|
||||
addLeadingComment(enclosingNode.arguments[0], comment);
|
||||
return true;
|
||||
|
||||
@@ -32,7 +32,7 @@ function printCallExpression(path, options, print) {
|
||||
// `TSImportType.typeArguments` is after `qualifier`, not before the "arguments"
|
||||
const typeArgumentsDoc =
|
||||
node.type !== "TSImportType" && node.typeArguments
|
||||
? print("typeArguments")
|
||||
? [print("typeArguments"), lineSuffixBoundary]
|
||||
: "";
|
||||
|
||||
const isTemplateLiteralSingleArg =
|
||||
@@ -57,7 +57,6 @@ function printCallExpression(path, options, print) {
|
||||
});
|
||||
if (!(isTemplateLiteralSingleArg && printed[0].label?.embed)) {
|
||||
return [
|
||||
isNewExpression ? "new " : "",
|
||||
printCallee(path, print),
|
||||
optional,
|
||||
typeArgumentsDoc,
|
||||
@@ -89,11 +88,9 @@ function printCallExpression(path, options, print) {
|
||||
}
|
||||
|
||||
const contents = [
|
||||
isNewExpression ? "new " : "",
|
||||
printCallee(path, print),
|
||||
optional,
|
||||
typeArgumentsDoc,
|
||||
lineSuffixBoundary,
|
||||
printCallArguments(path, options, print),
|
||||
];
|
||||
|
||||
@@ -121,7 +118,11 @@ function printCallee(path, print) {
|
||||
return "require";
|
||||
}
|
||||
|
||||
return print("callee");
|
||||
return [
|
||||
node.type === "NewExpression" ? "new " : "",
|
||||
print("callee"),
|
||||
lineSuffixBoundary,
|
||||
];
|
||||
}
|
||||
|
||||
const moduleImportCallees = [
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
import { locEnd, locStart } from "../loc.js";
|
||||
import getTextWithoutComments from "../utilities/get-text-without-comments.js";
|
||||
|
||||
function getCallExpressionClosingParenthesisIndex(callExpression, options) {
|
||||
const closingParenthesisIndex = locEnd(callExpression) - 1;
|
||||
|
||||
/* c8 ignore next 6 */
|
||||
if (options.originalText[closingParenthesisIndex] !== ")") {
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
throw new Error(
|
||||
`Unable to get '${callExpression.type}' closing parenthesis.`,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
return closingParenthesisIndex;
|
||||
}
|
||||
|
||||
function getCallExpressionOpeningParenthesisIndex(callExpression, options) {
|
||||
const closingParenthesisIndex = getCallExpressionClosingParenthesisIndex(
|
||||
callExpression,
|
||||
options,
|
||||
);
|
||||
|
||||
/* c8 ignore next 3 */
|
||||
if (closingParenthesisIndex === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
const start = locEnd(callExpression.typeArguments ?? callExpression.callee);
|
||||
const text = getTextWithoutComments(options, start, closingParenthesisIndex);
|
||||
const openingParenthesisIndex = text.indexOf("(");
|
||||
|
||||
/* c8 ignore next 6 */
|
||||
if (openingParenthesisIndex === -1) {
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
throw new Error(
|
||||
`Unable to get '${callExpression.type}' opening parenthesis.`,
|
||||
);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
return start + openingParenthesisIndex;
|
||||
}
|
||||
|
||||
function isInsideCallExpressionParentheses(
|
||||
callExpression,
|
||||
nodeOrComment,
|
||||
options,
|
||||
) {
|
||||
const closingParenthesisIndex = getCallExpressionClosingParenthesisIndex(
|
||||
callExpression,
|
||||
options,
|
||||
);
|
||||
|
||||
/* c8 ignore next 3 */
|
||||
if (closingParenthesisIndex === undefined) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (locEnd(nodeOrComment) > closingParenthesisIndex) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const openingParenthesisIndex = getCallExpressionOpeningParenthesisIndex(
|
||||
callExpression,
|
||||
options,
|
||||
);
|
||||
|
||||
/* c8 ignore next 3 */
|
||||
if (openingParenthesisIndex === undefined) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return locStart(nodeOrComment) > openingParenthesisIndex;
|
||||
}
|
||||
|
||||
export { isInsideCallExpressionParentheses };
|
||||
@@ -52,6 +52,7 @@ const unstableTests = new Map(
|
||||
"typescript/method-chain/object/issue-17239.ts",
|
||||
(options) => options.objectWrap !== "collapse",
|
||||
],
|
||||
"typescript/call/callee-comments.ts",
|
||||
].map((fixture) => {
|
||||
const [file, isUnstable = () => true] = Array.isArray(fixture)
|
||||
? fixture
|
||||
|
||||
@@ -77,12 +77,12 @@ call(
|
||||
call(
|
||||
// 105
|
||||
);
|
||||
call?. // 106
|
||||
();
|
||||
call?. // 107
|
||||
();
|
||||
call?. // 108
|
||||
();
|
||||
call // 106
|
||||
?.();
|
||||
call // 107
|
||||
?.();
|
||||
call // 108
|
||||
?.();
|
||||
|
||||
call /* 201 */();
|
||||
call
|
||||
@@ -93,16 +93,16 @@ call(/* 204 */);
|
||||
call(/* 205 */);
|
||||
call /* 206 */?.();
|
||||
call /* 207 */?.();
|
||||
call?.
|
||||
call
|
||||
/* 208 */
|
||||
();
|
||||
?.();
|
||||
|
||||
call /* 209 */?.();
|
||||
call /* 210 */?.();
|
||||
call /* 211 */?.();
|
||||
call?.
|
||||
call
|
||||
/* 212 */
|
||||
();
|
||||
?.();
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
|
||||
@@ -103,8 +103,8 @@ if (
|
||||
);
|
||||
|
||||
if (
|
||||
Boolean(
|
||||
// comment
|
||||
Boolean // comment
|
||||
(
|
||||
node.type === "ImportExpression" ||
|
||||
node.type === "TSImportType" ||
|
||||
node.type === "TSExternalModuleReference",
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
|
||||
|
||||
exports[`callee-comments.ts format 1`] = `
|
||||
====================================options=====================================
|
||||
parsers: ["typescript"]
|
||||
printWidth: 80
|
||||
| printWidth
|
||||
=====================================input======================================
|
||||
foo// 11
|
||||
<string>(1)
|
||||
foo
|
||||
// 12
|
||||
<string>(1)
|
||||
foo
|
||||
<string>// 13
|
||||
(1)
|
||||
foo
|
||||
<string>
|
||||
// 14
|
||||
(1)
|
||||
|
||||
foo// 21
|
||||
<string>()
|
||||
foo
|
||||
// 22
|
||||
<string>()
|
||||
foo
|
||||
<string>// 23
|
||||
()
|
||||
foo
|
||||
<string>
|
||||
// 24
|
||||
()
|
||||
|
||||
foo/* 31 */
|
||||
<string>(1)
|
||||
foo
|
||||
/* 32 */
|
||||
<string>(1)
|
||||
foo/* 33 */<string>(1)
|
||||
foo<string>/* 34 */(1)
|
||||
foo<string>
|
||||
/* 35 */
|
||||
(1)
|
||||
|
||||
foo/* 41 */
|
||||
<string>()
|
||||
foo
|
||||
/* 42 */
|
||||
<string>()
|
||||
foo/* 43 */<string>()
|
||||
foo<string>/* 44 */()
|
||||
foo<string>
|
||||
/* 45 */
|
||||
()
|
||||
|
||||
=====================================output=====================================
|
||||
foo // 11
|
||||
<string>(1);
|
||||
foo// 12
|
||||
<string>(1);
|
||||
foo<string> // 13
|
||||
(1);
|
||||
foo<string>(
|
||||
// 14
|
||||
1,
|
||||
);
|
||||
|
||||
foo // 21
|
||||
<string>();
|
||||
foo// 22
|
||||
<string>();
|
||||
foo<string> // 23
|
||||
();
|
||||
foo<string>
|
||||
// 24
|
||||
();
|
||||
|
||||
foo /* 31 */<string>(1);
|
||||
foo/* 32 */
|
||||
<string>(1);
|
||||
foo/* 33 */ <string>(1);
|
||||
foo<string>(/* 34 */ 1);
|
||||
foo<string>(
|
||||
/* 35 */
|
||||
1,
|
||||
);
|
||||
|
||||
foo /* 41 */<string>();
|
||||
foo/* 42 */
|
||||
<string>();
|
||||
foo/* 43 */ <string>();
|
||||
foo<string> /* 44 */();
|
||||
foo<string>
|
||||
/* 45 */
|
||||
();
|
||||
|
||||
================================================================================
|
||||
`;
|
||||
@@ -0,0 +1,47 @@
|
||||
foo// 11
|
||||
<string>(1)
|
||||
foo
|
||||
// 12
|
||||
<string>(1)
|
||||
foo
|
||||
<string>// 13
|
||||
(1)
|
||||
foo
|
||||
<string>
|
||||
// 14
|
||||
(1)
|
||||
|
||||
foo// 21
|
||||
<string>()
|
||||
foo
|
||||
// 22
|
||||
<string>()
|
||||
foo
|
||||
<string>// 23
|
||||
()
|
||||
foo
|
||||
<string>
|
||||
// 24
|
||||
()
|
||||
|
||||
foo/* 31 */
|
||||
<string>(1)
|
||||
foo
|
||||
/* 32 */
|
||||
<string>(1)
|
||||
foo/* 33 */<string>(1)
|
||||
foo<string>/* 34 */(1)
|
||||
foo<string>
|
||||
/* 35 */
|
||||
(1)
|
||||
|
||||
foo/* 41 */
|
||||
<string>()
|
||||
foo
|
||||
/* 42 */
|
||||
<string>()
|
||||
foo/* 43 */<string>()
|
||||
foo<string>/* 44 */()
|
||||
foo<string>
|
||||
/* 45 */
|
||||
()
|
||||
@@ -0,0 +1 @@
|
||||
runFormatTest(import.meta, ["typescript"]);
|
||||
Reference in new issue
Block a user