Simplify getNextNonSpaceNonCommentCharacter (#14318)

This commit is contained in:
fisker Cheung authored and GitHub committed 2023-02-09 16:05:14 +08:00
1 parent 62827ab4d5
commit c05723e7b0
3 files changed
+16 -19

No files matched your search

+7 -7
View File
@@ -107,17 +107,17 @@ function getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd) {
}
/**
* @template N
* @param {string} text
* @param {N} node
* @param {(node: N) => number} locEnd
* @param {number} startIndex
* @returns {string}
*/
function getNextNonSpaceNonCommentCharacter(text, node, locEnd) {
return text.charAt(
// @ts-expect-error => TBD: can return false, should we define a fallback?
getNextNonSpaceNonCommentCharacterIndex(text, node, locEnd)
function getNextNonSpaceNonCommentCharacter(text, startIndex) {
const index = getNextNonSpaceNonCommentCharacterIndexWithStartIndex(
text,
startIndex
);
return index === false ? "" : text.charAt(index);
}
// Not using, but it's public utils
+8 -10
View File
@@ -179,8 +179,7 @@ function handleIfStatementComments({
// it is a ).
const nextCharacter = getNextNonSpaceNonCommentCharacter(
text,
comment,
locEnd
locEnd(comment)
);
if (nextCharacter === ")") {
addTrailingComment(precedingNode, comment);
@@ -260,8 +259,7 @@ function handleWhileComments({
// it is a ).
const nextCharacter = getNextNonSpaceNonCommentCharacter(
text,
comment,
locEnd
locEnd(comment)
);
if (nextCharacter === ")") {
addTrailingComment(precedingNode, comment);
@@ -461,7 +459,7 @@ function handleMethodNameComments({
if (
enclosingNode &&
precedingNode &&
getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === "(" &&
getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === "(" &&
// "MethodDefinition" is handled in getCommentChildNodes
(enclosingNode.type === "Property" ||
enclosingNode.type === "TSDeclareMethod" ||
@@ -470,7 +468,7 @@ function handleMethodNameComments({
enclosingNode.key === precedingNode &&
// special Property case: { key: /*comment*/(value) };
// comment should be attached to value instead of key
getNextNonSpaceNonCommentCharacter(text, precedingNode, locEnd) !== ":"
getNextNonSpaceNonCommentCharacter(text, locEnd(precedingNode)) !== ":"
) {
addTrailingComment(precedingNode, comment);
return true;
@@ -502,7 +500,7 @@ function handleFunctionNameComments({
enclosingNode,
text,
}) {
if (getNextNonSpaceNonCommentCharacter(text, comment, locEnd) !== "(") {
if (getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) !== "(") {
return false;
}
if (precedingNode && functionLikeNodeTypes.has(enclosingNode?.type)) {
@@ -527,7 +525,7 @@ function handleCommentAfterArrowParams({ comment, enclosingNode, text }) {
}
function handleCommentInEmptyParens({ comment, enclosingNode, text }) {
if (getNextNonSpaceNonCommentCharacter(text, comment, locEnd) !== ")") {
if (getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) !== ")") {
return false;
}
@@ -577,7 +575,7 @@ function handleLastFunctionArgComments({
precedingNode?.type === "AssignmentPattern") &&
enclosingNode &&
isRealFunctionLikeNode(enclosingNode) &&
getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === ")"
getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === ")"
) {
addTrailingComment(precedingNode, comment);
return true;
@@ -829,7 +827,7 @@ function handleTSFunctionTrailingComments({
(enclosingNode?.type === "TSMethodSignature" ||
enclosingNode?.type === "TSDeclareFunction" ||
enclosingNode?.type === "TSAbstractMethodDefinition") &&
getNextNonSpaceNonCommentCharacter(text, comment, locEnd) === ";"
getNextNonSpaceNonCommentCharacter(text, locEnd(comment)) === ";"
) {
addTrailingComment(enclosingNode, comment);
return true;
+1 -2
View File
@@ -51,8 +51,7 @@ function printFunctionParameters(
filter: (comment) =>
getNextNonSpaceNonCommentCharacter(
options.originalText,
comment,
locEnd
locEnd(comment)
) === ")",
}),
")",