diff --git a/changelog_unreleased/typescript/18507.md b/changelog_unreleased/typescript/18507.md new file mode 100644 index 000000000..c1c0061fc --- /dev/null +++ b/changelog_unreleased/typescript/18507.md @@ -0,0 +1,45 @@ +#### Fix formatting for new expression with type cast (#18507 by @fisker) + + +```tsx +// Input +new (require("./webpack/plugins/next-trace-entrypoints-plugin") + .TraceEntryPointsPlugin as P)({ + rootDir: dir, +}); + +( + require("./webpack/plugins/next-trace-entrypoints-plugin") + .TraceEntryPointsPlugin as P +)({ + rootDir: dir, +}); + +// Prettier stable +new (require("./webpack/plugins/next-trace-entrypoints-plugin") + .TraceEntryPointsPlugin as P)({ + rootDir: dir, +}); + +( + require("./webpack/plugins/next-trace-entrypoints-plugin") + .TraceEntryPointsPlugin as P +)({ + rootDir: dir, +}); + +// Prettier main +new ( + require("./webpack/plugins/next-trace-entrypoints-plugin") + .TraceEntryPointsPlugin as P +)({ + rootDir: dir, +}); + +( + require("./webpack/plugins/next-trace-entrypoints-plugin") + .TraceEntryPointsPlugin as P +)({ + rootDir: dir, +}); +``` diff --git a/src/language-js/print/cast-expression.js b/src/language-js/print/cast-expression.js index a411c9dc7..6a9e61213 100644 --- a/src/language-js/print/cast-expression.js +++ b/src/language-js/print/cast-expression.js @@ -26,7 +26,8 @@ function printBinaryCastExpression(path, options, print) { ]; if ( - (key === "callee" && isCallExpression(parent)) || + (key === "callee" && + (isCallExpression(parent) || parent.type === "NewExpression")) || (key === "object" && isMemberExpression(parent)) ) { return group([indent([softline, ...parts]), softline]); diff --git a/tests/format/typescript/cast/18406.ts b/tests/format/typescript/cast/18406.ts new file mode 100644 index 000000000..ed4b9dbfe --- /dev/null +++ b/tests/format/typescript/cast/18406.ts @@ -0,0 +1,14 @@ +new (require("./webpack/plugins/next-trace-entrypoints-plugin") + .TraceEntryPointsPlugin as P)({ + rootDir: dir, +}); + +(require("./webpack/plugins/next-trace-entrypoints-plugin") + .TraceEntryPointsPlugin as P)({ + rootDir: dir, +}); + +(require("./webpack/plugins/next-trace-entrypoints-plugin") + ?.TraceEntryPointsPlugin as P)?.({ + rootDir: dir, +}); diff --git a/tests/format/typescript/cast/__snapshots__/format.test.js.snap b/tests/format/typescript/cast/__snapshots__/format.test.js.snap index da5f8ed9a..114676247 100644 --- a/tests/format/typescript/cast/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/cast/__snapshots__/format.test.js.snap @@ -1,8 +1,54 @@ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing +exports[`18406.ts format 1`] = ` +====================================options===================================== +parsers: ["typescript"] +printWidth: 80 + | printWidth +=====================================input====================================== +new (require("./webpack/plugins/next-trace-entrypoints-plugin") + .TraceEntryPointsPlugin as P)({ + rootDir: dir, +}); + +(require("./webpack/plugins/next-trace-entrypoints-plugin") + .TraceEntryPointsPlugin as P)({ + rootDir: dir, +}); + +(require("./webpack/plugins/next-trace-entrypoints-plugin") + ?.TraceEntryPointsPlugin as P)?.({ + rootDir: dir, +}); + +=====================================output===================================== +new ( + require("./webpack/plugins/next-trace-entrypoints-plugin") + .TraceEntryPointsPlugin as P +)({ + rootDir: dir, +}); + +( + require("./webpack/plugins/next-trace-entrypoints-plugin") + .TraceEntryPointsPlugin as P +)({ + rootDir: dir, +}); + +( + require("./webpack/plugins/next-trace-entrypoints-plugin") + ?.TraceEntryPointsPlugin as P +)?.({ + rootDir: dir, +}); + +================================================================================ +`; + exports[`array-and-object.ts format 1`] = ` ====================================options===================================== -parsers: ["babel-ts", "typescript"] +parsers: ["typescript"] printWidth: 80 | printWidth =====================================input====================================== @@ -22,7 +68,7 @@ testObjLiteral = | undefined>{ prop1: "myPropVal" }; exports[`as-const.ts format 1`] = ` ====================================options===================================== -parsers: ["babel-ts", "typescript"] +parsers: ["typescript"] printWidth: 80 | printWidth =====================================input====================================== @@ -44,7 +90,7 @@ let x = "123" as const; exports[`assert-and-assign.ts format 1`] = ` ====================================options===================================== -parsers: ["babel-ts", "typescript"] +parsers: ["typescript"] printWidth: 80 | printWidth =====================================input====================================== @@ -59,7 +105,7 @@ printWidth: 80 exports[`generic-cast.ts format 1`] = ` ====================================options===================================== -parsers: ["babel-ts", "typescript"] +parsers: ["typescript"] printWidth: 80 | printWidth =====================================input====================================== @@ -312,7 +358,7 @@ function x() { exports[`hug-args.ts format 1`] = ` ====================================options===================================== -parsers: ["babel-ts", "typescript"] +parsers: ["typescript"] printWidth: 80 | printWidth =====================================input====================================== @@ -362,7 +408,7 @@ postMessages([ exports[`parenthesis.ts format 1`] = ` ====================================options===================================== -parsers: ["babel-ts", "typescript"] +parsers: ["typescript"] printWidth: 80 | printWidth =====================================input====================================== diff --git a/tests/format/typescript/cast/format.test.js b/tests/format/typescript/cast/format.test.js index 083abcf04..116dcbd78 100644 --- a/tests/format/typescript/cast/format.test.js +++ b/tests/format/typescript/cast/format.test.js @@ -1 +1 @@ -runFormatTest(import.meta, ["babel-ts", "typescript"]); +runFormatTest(import.meta, ["typescript"]);