diff --git a/src/language-js/comments/handle-comments.js b/src/language-js/comments/handle-comments.js index 6b51021e0..0fe5dc995 100644 --- a/src/language-js/comments/handle-comments.js +++ b/src/language-js/comments/handle-comments.js @@ -1092,6 +1092,9 @@ function handleParenthesizedExpressionTrailingComment({ enclosingNode.init === precedingNode) || (enclosingNode.type === "ReturnStatement" && enclosingNode.argument === precedingNode) || + (isSequence && + enclosingNode.type === "ExpressionStatement" && + enclosingNode.expression === precedingNode) || (isSequence && enclosingNode.type === "AssignmentExpression" && enclosingNode.right === precedingNode)) && diff --git a/src/language-js/location/overrides.js b/src/language-js/location/overrides.js index 437b5b382..82ef7fdcb 100644 --- a/src/language-js/location/overrides.js +++ b/src/language-js/location/overrides.js @@ -27,6 +27,7 @@ const overrideBreakOrContinueEnd = const getContentEnd = (node) => node.__contentEnd ?? locEndWithFullText(node); const nodeTypesWithContentEnd = /** @type {const} */ ([ + "ExpressionStatement", "Directive", "ImportDeclaration", "ExportDefaultDeclaration", @@ -45,8 +46,6 @@ const overrides = ["DebuggerStatement", (node) => locStart(node) + DEBUGGER_KEYWORD_LENGTH], // @ts-expect-error -- ignore ["VariableDeclaration", (node) => locEnd(node.declarations.at(-1))], - // @ts-expect-error -- ignore - ["ExpressionStatement", (node) => locEnd(node.expression)], ...nodeTypesWithContentEnd.map((type) => [type, getContentEnd]), ]); @@ -63,8 +62,7 @@ const shouldIgnoredNodePrintSemicolon = (node) => { type === "BreakStatement" || type === "ContinueStatement" || type === "DebuggerStatement" || - type === "VariableDeclaration" || - type === "ExpressionStatement" + type === "VariableDeclaration" ) { return true; } diff --git a/tests/config/format-test/failed-format-tests.js b/tests/config/format-test/failed-format-tests.js index 4eb6d77a9..a99242a90 100644 --- a/tests/config/format-test/failed-format-tests.js +++ b/tests/config/format-test/failed-format-tests.js @@ -4,8 +4,10 @@ import { FORMAT_TEST_DIRECTORY } from "./constants.js"; // TODO: these test files need fix const unstableTests = new Map( [ - "js/ignore/semi/class-expression-decorator.js", - ["js/identifier/parentheses/let.js", (options) => options.semi === false], + [ + "js/ignore/semi/class-expression-decorator.js", + (options) => options.semi === false, + ], ["js/ignore/semi/head-ignored.js", (options) => options.semi === false], "js/comments/return-statement.js", "js/comments/tagged-template-literal.js", diff --git a/tests/format/js/ignore/__snapshots__/format.test.js.snap b/tests/format/js/ignore/__snapshots__/format.test.js.snap index df96c70e6..e1d3a16e2 100644 --- a/tests/format/js/ignore/__snapshots__/format.test.js.snap +++ b/tests/format/js/ignore/__snapshots__/format.test.js.snap @@ -317,7 +317,7 @@ function HelloWorld(x) { // eslint-disable-next-line x.a | x.b - ).call(null); + ).call(null) } ================================================================================ @@ -401,3 +401,18 @@ let y = ================================================================================ `; + +exports[`parenthesized-expression.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +// prettier-ignore +(a = 1); + +=====================================output===================================== +// prettier-ignore +(a = 1); + +================================================================================ +`; diff --git a/tests/format/js/ignore/parenthesized-expression.js b/tests/format/js/ignore/parenthesized-expression.js new file mode 100644 index 000000000..a665615fb --- /dev/null +++ b/tests/format/js/ignore/parenthesized-expression.js @@ -0,0 +1,2 @@ +// prettier-ignore +(a = 1); diff --git a/tests/format/js/sequence-expression/__snapshots__/format.test.js.snap b/tests/format/js/sequence-expression/__snapshots__/format.test.js.snap index 7a09a413b..2aedc70df 100644 --- a/tests/format/js/sequence-expression/__snapshots__/format.test.js.snap +++ b/tests/format/js/sequence-expression/__snapshots__/format.test.js.snap @@ -136,7 +136,7 @@ const x1 = (a, b, c /* comment */); const x2 = (a = c /* comment */); (a, b, c /* comment */); -(a = c /* comment */); +// (a = c /* comment */); assigned = (a, b, c /* comment */); // assigned = (a = c /* comment */); @@ -150,8 +150,8 @@ function f() { const x1 = (a, b, c /* comment */); const x2 = (a = c /* comment */); -(a, b, c); /* comment */ -a = c; /* comment */ +(a, b, c /* comment */); +// (a = c /* comment */); assigned = (a, b, c /* comment */); // assigned = (a = c /* comment */); @@ -169,12 +169,16 @@ exports[`parenthesized-trailing-comment-unstable.js format 1`] = ` parsers: ["babel", "flow", "typescript"] printWidth: 80 (default) | =====================================input====================================== -// TODO: Enable test in parenthesized-trailing-comment.js when it's stable +// TODO: Enable tests in parenthesized-trailing-comment.js when they are stable + +(a = c /* comment */); assigned = (a = c /* comment */); =====================================output===================================== -// TODO: Enable test in parenthesized-trailing-comment.js when it's stable +// TODO: Enable tests in parenthesized-trailing-comment.js when they are stable + +a = c /* comment */; assigned = a = c /* comment */; diff --git a/tests/format/js/sequence-expression/parenthesized-trailing-comment-unstable.js b/tests/format/js/sequence-expression/parenthesized-trailing-comment-unstable.js index 1618c00f9..659db507c 100644 --- a/tests/format/js/sequence-expression/parenthesized-trailing-comment-unstable.js +++ b/tests/format/js/sequence-expression/parenthesized-trailing-comment-unstable.js @@ -1,3 +1,5 @@ -// TODO: Enable test in parenthesized-trailing-comment.js when it's stable +// TODO: Enable tests in parenthesized-trailing-comment.js when they are stable + +(a = c /* comment */); assigned = (a = c /* comment */); diff --git a/tests/format/js/sequence-expression/parenthesized-trailing-comment.js b/tests/format/js/sequence-expression/parenthesized-trailing-comment.js index cadc833dc..92ac2f5b4 100644 --- a/tests/format/js/sequence-expression/parenthesized-trailing-comment.js +++ b/tests/format/js/sequence-expression/parenthesized-trailing-comment.js @@ -2,7 +2,7 @@ const x1 = (a, b, c /* comment */); const x2 = (a = c /* comment */); (a, b, c /* comment */); -(a = c /* comment */); +// (a = c /* comment */); assigned = (a, b, c /* comment */); // assigned = (a = c /* comment */); diff --git a/tests/format/markdown/markdown/__snapshots__/format.test.js.snap b/tests/format/markdown/markdown/__snapshots__/format.test.js.snap index f825a760a..27f83c9d0 100644 --- a/tests/format/markdown/markdown/__snapshots__/format.test.js.snap +++ b/tests/format/markdown/markdown/__snapshots__/format.test.js.snap @@ -1530,7 +1530,7 @@ matrix( 1, 0, 0, 0, 1, 0, 0, 0, 1 -); +) \`\`\` will be transformed to: @@ -1543,7 +1543,7 @@ matrix( 1, 0, 0, 0, 1, 0, 0, 0, 1 -); +) \`\`\` ## Options @@ -3501,7 +3501,7 @@ matrix( 1, 0, 0, 0, 1, 0, 0, 0, 1 -); +) \`\`\` will be transformed to: @@ -3514,7 +3514,7 @@ matrix( 1, 0, 0, 0, 1, 0, 0, 0, 1 -); +) \`\`\` ## Options