diff --git a/changelog_unreleased/typescript/18522.md b/changelog_unreleased/typescript/18522.md new file mode 100644 index 000000000..38548d5f0 --- /dev/null +++ b/changelog_unreleased/typescript/18522.md @@ -0,0 +1,35 @@ +#### Fix line comments jumping past member expression lookups (#18522 by @ulrichstark) + +Line comments between a member expression object and property were incorrectly moved past subsequent content like `as` expressions. + + +```tsx +// Input +function getClassNameFromPrototypeMethod(container) { + return ((container // a + .left as PropertyAccessExpression) // b + .expression as PropertyAccessExpression) // c + .expression; // d +} + +// Prettier stable +function getClassNameFromPrototypeMethod(container) { + return ( + ( + container.left as PropertyAccessExpression // a + ).expression as PropertyAccessExpression // b + ).expression; // c // d +} + +// Prettier main +function getClassNameFromPrototypeMethod(container) { + return ( + ( + container // a + .left as PropertyAccessExpression + ) // b + .expression as PropertyAccessExpression + ) // c + .expression; // d +} +``` diff --git a/src/language-js/print/member.js b/src/language-js/print/member.js index 45773b085..ecc90a4ac 100644 --- a/src/language-js/print/member.js +++ b/src/language-js/print/member.js @@ -1,4 +1,10 @@ -import { group, indent, label, softline } from "../../document/index.js"; +import { + group, + indent, + label, + lineSuffixBoundary, + softline, +} from "../../document/index.js"; import { getCallArguments, isCallExpression, @@ -62,6 +68,7 @@ function printMemberExpression(path, options, print) { return label(objectDoc.label, [ objectDoc, + lineSuffixBoundary, shouldInline ? lookupDoc : group(indent([softline, lookupDoc])), ]); } diff --git a/tests/format/js/template-literals/__snapshots__/format.test.js.snap b/tests/format/js/template-literals/__snapshots__/format.test.js.snap index f4fa69117..ff0a6ae01 100644 --- a/tests/format/js/template-literals/__snapshots__/format.test.js.snap +++ b/tests/format/js/template-literals/__snapshots__/format.test.js.snap @@ -220,7 +220,8 @@ a = \`\${[[1, 2, 3], [4, 5, 6]]}\` =====================================output===================================== const long1 = \`long \${ - a.b //comment + a //comment + .b } long longlong \${a.b.c.d.e} long longlong \${a.b.c.d.e} long longlong \${a.b.c.d.e} long long\`; const long2 = \`long \${a.b.c.d.e} long longlong \${loooooooooooooooooong} long longlong \${loooooooooooooooooong} long longlong \${loooooooooooooooooong} long long\`; diff --git a/tests/format/typescript/as/comments/17407.ts b/tests/format/typescript/as/comments/17407.ts new file mode 100644 index 000000000..b9dde36fb --- /dev/null +++ b/tests/format/typescript/as/comments/17407.ts @@ -0,0 +1,6 @@ +function getClassNameFromPrototypeMethod(container) { + return ((container // a + .left as PropertyAccessExpression) // b + .expression as PropertyAccessExpression) // c + .expression; // d +} diff --git a/tests/format/typescript/as/comments/__snapshots__/format.test.js.snap b/tests/format/typescript/as/comments/__snapshots__/format.test.js.snap index a1d914a13..57bf4733c 100644 --- a/tests/format/typescript/as/comments/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/as/comments/__snapshots__/format.test.js.snap @@ -1,5 +1,33 @@ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing +exports[`17407.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript", "flow"] +printWidth: 80 + | printWidth +=====================================input====================================== +function getClassNameFromPrototypeMethod(container) { + return ((container // a + .left as PropertyAccessExpression) // b + .expression as PropertyAccessExpression) // c + .expression; // d +} + +=====================================output===================================== +function getClassNameFromPrototypeMethod(container) { + return ( + ( + container // a + .left as PropertyAccessExpression + ) // b + .expression as PropertyAccessExpression + ) // c + .expression; // d +} + +================================================================================ +`; + exports[`18160.ts [hermes] format 1`] = ` "';' expected (7:2) 5 | comment