diff --git a/changelog_unreleased/typescript/18738.md b/changelog_unreleased/typescript/18738.md new file mode 100644 index 000000000..bd96e5acc --- /dev/null +++ b/changelog_unreleased/typescript/18738.md @@ -0,0 +1,19 @@ +#### Print semicolon before type assertion in `no-semi` mode (#18738 by @fisker) + + +```tsx +// Input +a; +c; + +// Prettier stable (First format, --no-semi) +a +c + +// Prettier stable (Second format, --no-semi) +a < b > c + +// Prettier main +a +;c +``` diff --git a/src/language-js/print/type-assertion.js b/src/language-js/print/type-assertion.js index d7c09b87a..9869df5e5 100644 --- a/src/language-js/print/type-assertion.js +++ b/src/language-js/print/type-assertion.js @@ -7,6 +7,9 @@ import { } from "../../document/index.js"; import { isArrayExpression, isObjectExpression } from "../utilities/index.js"; +/* +- `TSTypeAssertion` (TypeScript) +*/ function printTypeAssertion(path, options, print) { const { node } = path; const shouldBreakAfterCast = !( diff --git a/src/language-js/semicolon/semicolon.js b/src/language-js/semicolon/semicolon.js index b6c488035..ed62a76c5 100644 --- a/src/language-js/semicolon/semicolon.js +++ b/src/language-js/semicolon/semicolon.js @@ -39,12 +39,14 @@ function expressionNeedsASIProtection(path, options) { switch (node.type) { case "ParenthesizedExpression": case "TypeCastExpression": + case "TSTypeAssertion": case "ArrayExpression": case "ArrayPattern": case "TemplateLiteral": case "TemplateElement": case "RegExpLiteral": return true; + case "ArrowFunctionExpression": if (!shouldPrintParamsWithoutParens(path, options)) { return true; diff --git a/tests/format/typescript/no-semi/__snapshots__/format.test.js.snap b/tests/format/typescript/no-semi/__snapshots__/format.test.js.snap index e13af981f..2d94c08d5 100644 --- a/tests/format/typescript/no-semi/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/no-semi/__snapshots__/format.test.js.snap @@ -105,3 +105,64 @@ const el = ReactDOM.findDOMNode(ref); ================================================================================ `; + +exports[`type-assertion.ts - {"semi":false} format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 +semi: false + | printWidth +=====================================input====================================== +// #14039 +a; +c; + +a; +(c).foo(); + +a; +c + 1; + +=====================================output===================================== +// #14039 +a +;c + +a +;(c).foo() + +a +;c + 1 + +================================================================================ +`; + +exports[`type-assertion.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +// #14039 +a; +c; + +a; +(c).foo(); + +a; +c + 1; + +=====================================output===================================== +// #14039 +a; +c; + +a; +(c).foo(); + +a; +c + 1; + +================================================================================ +`; diff --git a/tests/format/typescript/no-semi/type-assertion.ts b/tests/format/typescript/no-semi/type-assertion.ts new file mode 100644 index 000000000..11d85be4b --- /dev/null +++ b/tests/format/typescript/no-semi/type-assertion.ts @@ -0,0 +1,9 @@ +// #14039 +a; +c; + +a; +(c).foo(); + +a; +c + 1;