From 0926587a5fc4134b5773e54a2739cc5576c4dd23 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Tue, 24 Jun 2025 00:26:42 +0800 Subject: [PATCH] Allow const without initializer in `oxc-ts` parser (#17654) --- changelog_unreleased/typescript/17650.md | 7 +++- packages/plugin-oxc/package.json | 2 +- src/language-js/parse/oxc.js | 32 ++++++++----------- tests/config/run-format-test.js | 6 +++- .../__snapshots__/format.test.js.snap | 12 +------ .../types/functions/format.test.js | 1 - .../__snapshots__/format.test.js.snap | 16 ---------- .../typescript/d-ts-files/format.test.js | 9 +----- .../__snapshots__/format.test.js.snap | 30 ----------------- .../typescript/trailing-comma/format.test.js | 10 ++---- 10 files changed, 31 insertions(+), 94 deletions(-) diff --git a/changelog_unreleased/typescript/17650.md b/changelog_unreleased/typescript/17650.md index 2896ecabd..9f4612433 100644 --- a/changelog_unreleased/typescript/17650.md +++ b/changelog_unreleased/typescript/17650.md @@ -1,4 +1,4 @@ -#### Allow const without initializer (#17650 by @fisker) +#### Allow const without initializer (#17650, #17654 by @fisker) ```jsx @@ -10,6 +10,11 @@ SyntaxError: Unexpected token (1:21) > 1 | export const version: string; | ^ +// Prettier stable (--parser=oxc-ts) +SyntaxError: Missing initializer in const declaration (1:14) +> 1 | export const version: string; + | ^^^^^^^^^^^^^^^ + // Prettier main export const version: string; ``` diff --git a/packages/plugin-oxc/package.json b/packages/plugin-oxc/package.json index b5eb05a0f..e06e30b12 100644 --- a/packages/plugin-oxc/package.json +++ b/packages/plugin-oxc/package.json @@ -1,6 +1,6 @@ { "name": "@prettier/plugin-oxc", - "version": "0.0.2", + "version": "0.0.3", "description": "Prettier Oxc plugin.", "type": "module", "exports": { diff --git a/src/language-js/parse/oxc.js b/src/language-js/parse/oxc.js index d365d8d8b..158abfab9 100644 --- a/src/language-js/parse/oxc.js +++ b/src/language-js/parse/oxc.js @@ -84,36 +84,32 @@ async function parseJs(text, options) { } async function parseTs(text, options) { - let filepath = options?.filepath; + const filepath = options?.filepath; const sourceType = getSourceType(filepath); /** @type {ParserOptions} */ const parseOptions = { sourceType, astType: "ts" }; const isKnownJsx = typeof filepath === "string" && /\.(?:jsx|tsx)$/iu.test(filepath); + const isDtsFile = + typeof filepath === "string" && filepath.toLowerCase().endsWith(".d.ts"); - /** @type {ParserOptions[]} */ - let parseOptionsCombinations = []; - if (isKnownJsx) { - parseOptionsCombinations = [{ ...parseOptions, lang: "tsx" }]; - } else { + /** @type {string[]} */ + let filenameCombinations = [isDtsFile ? "prettier.d.ts" : "prettier.tsx"]; + if (!isDtsFile && !isKnownJsx) { const shouldEnableJsx = jsxRegexp.test(text); - parseOptionsCombinations = [shouldEnableJsx, !shouldEnableJsx].map( - (shouldEnableJsx) => ({ - ...parseOptions, - lang: shouldEnableJsx ? "tsx" : "ts", - }), - ); - } - - if (typeof filepath !== "string") { - filepath = "prettier.tsx"; + filenameCombinations = [ + ...[shouldEnableJsx, !shouldEnableJsx].map((shouldEnableJsx) => + shouldEnableJsx ? "prettier.tsx" : "prettier.ts", + ), + "prettier.d.ts", + ]; } let result; try { result = await tryCombinationsAsync( - parseOptionsCombinations.map( - (parseOptions) => () => parseWithOptions(filepath, text, parseOptions), + filenameCombinations.map( + (filename) => () => parseWithOptions(filename, text, parseOptions), ), ); } catch ({ diff --git a/tests/config/run-format-test.js b/tests/config/run-format-test.js index 369487dd6..e5d7fa1e5 100644 --- a/tests/config/run-format-test.js +++ b/tests/config/run-format-test.js @@ -85,7 +85,11 @@ const babelTsDisabledTests = new Set( ), ); const oxcDisabledTests = new Set(); -const oxcTsDisabledTests = new Set(); +const oxcTsDisabledTests = new Set( + [ + "typescript/conformance/types/functions/functionOverloadErrorsSyntax.ts", + ].map((file) => path.join(__dirname, "../format", file)), +); const hermesDisabledTests = new Set([ ...commentClosureTypecaseTests, ...[ diff --git a/tests/format/typescript/conformance/types/functions/__snapshots__/format.test.js.snap b/tests/format/typescript/conformance/types/functions/__snapshots__/format.test.js.snap index c4efac423..001a93414 100644 --- a/tests/format/typescript/conformance/types/functions/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/conformance/types/functions/__snapshots__/format.test.js.snap @@ -1,4 +1,4 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP +// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing exports[`TSFunctionTypeNoUnnecessaryParentheses.ts format 1`] = ` ====================================options===================================== @@ -603,16 +603,6 @@ exports[`functionOverloadErrorsSyntax.ts [babel-ts] format 1`] = ` Cause: Rest element must be last element. (9:35)" `; -exports[`functionOverloadErrorsSyntax.ts [oxc-ts] format 1`] = ` -"A rest parameter must be last in a parameter list (9:25) - 7 | - 8 | //Function overload signature with rest param followed by non-optional parameter -> 9 | function fn5(x: string, ...y: any[], z: string); - | ^^^^^^^^^^^ - 10 | function fn5() { } - 11 |" -`; - exports[`functionOverloadErrorsSyntax.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/conformance/types/functions/format.test.js b/tests/format/typescript/conformance/types/functions/format.test.js index 3d703a140..d0f135e2e 100644 --- a/tests/format/typescript/conformance/types/functions/format.test.js +++ b/tests/format/typescript/conformance/types/functions/format.test.js @@ -1,6 +1,5 @@ runFormatTest(import.meta, ["typescript"], { errors: { "babel-ts": ["functionOverloadErrorsSyntax.ts"], - "oxc-ts": ["functionOverloadErrorsSyntax.ts"], }, }); diff --git a/tests/format/typescript/d-ts-files/__snapshots__/format.test.js.snap b/tests/format/typescript/d-ts-files/__snapshots__/format.test.js.snap index 5f9767219..32ad22b79 100644 --- a/tests/format/typescript/d-ts-files/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/d-ts-files/__snapshots__/format.test.js.snap @@ -1,12 +1,5 @@ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing -exports[`const-without-initializer.d.ts [oxc-ts] format 1`] = ` -"Missing initializer in const declaration (1:17) -> 1 | export const version: string; - | ^^^^^^^^^^^^^^^ - 2 |" -`; - exports[`const-without-initializer.d.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -21,15 +14,6 @@ export const version: string; ================================================================================ `; -exports[`const-without-initializer-in-namespace.d.ts [oxc-ts] format 1`] = ` -"Missing initializer in const declaration (2:12) - 1 | export namespace builders { -> 2 | const breakParent: string; - | ^^^^^^^^^^^^^^^^^^^ - 3 | } - 4 |" -`; - exports[`const-without-initializer-in-namespace.d.ts format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/d-ts-files/format.test.js b/tests/format/typescript/d-ts-files/format.test.js index 23c95ac1d..116dcbd78 100644 --- a/tests/format/typescript/d-ts-files/format.test.js +++ b/tests/format/typescript/d-ts-files/format.test.js @@ -1,8 +1 @@ -runFormatTest(import.meta, ["typescript"], { - errors: { - "oxc-ts": [ - "const-without-initializer.d.ts", - "const-without-initializer-in-namespace.d.ts", - ], - }, -}); +runFormatTest(import.meta, ["typescript"]); diff --git a/tests/format/typescript/trailing-comma/__snapshots__/format.test.js.snap b/tests/format/typescript/trailing-comma/__snapshots__/format.test.js.snap index c46f526f3..b846f5f59 100644 --- a/tests/format/typescript/trailing-comma/__snapshots__/format.test.js.snap +++ b/tests/format/typescript/trailing-comma/__snapshots__/format.test.js.snap @@ -63,16 +63,6 @@ const f2 = < ================================================================================ `; -exports[`trailing.ts - {"trailingComma":"all"} [oxc-ts] format 1`] = ` -"A rest element must be last in a destructuring pattern (15:3) - 13 | longKeySoThisWillGoOnMultipleLines2, - 14 | longKeySoThisWillGoOnMultipleLines3, -> 15 | ...rest, - | ^^^^^^^ - 16 | } = something; - 17 |" -`; - exports[`trailing.ts - {"trailingComma":"all"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -117,16 +107,6 @@ const { ================================================================================ `; -exports[`trailing.ts - {"trailingComma":"es5"} [oxc-ts] format 1`] = ` -"A rest element must be last in a destructuring pattern (15:3) - 13 | longKeySoThisWillGoOnMultipleLines2, - 14 | longKeySoThisWillGoOnMultipleLines3, -> 15 | ...rest, - | ^^^^^^^ - 16 | } = something; - 17 |" -`; - exports[`trailing.ts - {"trailingComma":"es5"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] @@ -171,16 +151,6 @@ const { ================================================================================ `; -exports[`trailing.ts - {"trailingComma":"none"} [oxc-ts] format 1`] = ` -"A rest element must be last in a destructuring pattern (15:3) - 13 | longKeySoThisWillGoOnMultipleLines2, - 14 | longKeySoThisWillGoOnMultipleLines3, -> 15 | ...rest, - | ^^^^^^^ - 16 | } = something; - 17 |" -`; - exports[`trailing.ts - {"trailingComma":"none"} format 1`] = ` ====================================options===================================== parsers: ["typescript"] diff --git a/tests/format/typescript/trailing-comma/format.test.js b/tests/format/typescript/trailing-comma/format.test.js index 0e2f7e8f3..255526bea 100644 --- a/tests/format/typescript/trailing-comma/format.test.js +++ b/tests/format/typescript/trailing-comma/format.test.js @@ -1,7 +1,3 @@ -const errors = { - "oxc-ts": ["trailing.ts"], -}; - -runFormatTest(import.meta, ["typescript"], { trailingComma: "all", errors }); -runFormatTest(import.meta, ["typescript"], { trailingComma: "es5", errors }); -runFormatTest(import.meta, ["typescript"], { trailingComma: "none", errors }); +runFormatTest(import.meta, ["typescript"], { trailingComma: "all" }); +runFormatTest(import.meta, ["typescript"], { trailingComma: "es5" }); +runFormatTest(import.meta, ["typescript"], { trailingComma: "none" });