From 968c1a62c8dce7f72a4fed477b2a5b260f5f31cf Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 12 Sep 2026 11:47:48 +0800 Subject: [PATCH] Allow Babel parse in "script" mode (#20041) --- src/language-js/parentheses/identifier.js | 17 ++ src/language-js/parse/babel.js | 14 +- .../await/__snapshots__/format.test.js.snap | 10 +- .../__snapshots__/format.test.js.snap | 58 ----- .../html-like-comments/format.test.js | 14 -- .../__snapshots__/format.test.js.snap | 6 +- .../__snapshots__/format.test.js.snap | 81 ++++-- .../js/comments/html-like/format.test.js | 3 - .../html-like}/html-like-comments.js | 0 .../await/__snapshots__/format.test.js.snap | 235 ++++++++++++++++++ .../format/js/identifier/await/format.test.js | 37 +++ .../__snapshots__/format.test.js.snap | 2 +- .../__snapshots__/format.test.js.snap | 21 -- .../format/jsx/jsx-test-suite/format.test.js | 3 - .../__snapshots__/format.test.js.snap | 112 ++++----- .../format.test.js | 10 +- .../__snapshots__/format.test.js.snap | 2 +- 17 files changed, 418 insertions(+), 207 deletions(-) delete mode 100644 tests/format/js/_errors_/html-like-comments/__snapshots__/format.test.js.snap delete mode 100644 tests/format/js/_errors_/html-like-comments/format.test.js rename tests/format/js/{_errors_/html-like-comments => comments/html-like}/html-like-comments.js (100%) create mode 100644 tests/format/js/identifier/await/__snapshots__/format.test.js.snap create mode 100644 tests/format/js/identifier/await/format.test.js diff --git a/src/language-js/parentheses/identifier.js b/src/language-js/parentheses/identifier.js index 979df088d..e0c2da173 100644 --- a/src/language-js/parentheses/identifier.js +++ b/src/language-js/parentheses/identifier.js @@ -93,6 +93,23 @@ function shouldAddParenthesesToIdentifier(path) { } } + if (node.name === "await") { + const statement = path.findAncestor( + (node) => node.type === "ExpressionStatement", + ); + if (statement) { + const { expression } = statement; + if ( + startsWithNoLookaheadToken( + expression, + (leftmostNode) => leftmostNode === node, + ) + ) { + return true; + } + } + } + // `(type) satisfies never;` and similar cases if ( key === "expression" && diff --git a/src/language-js/parse/babel.js b/src/language-js/parse/babel.js index 2861305db..b2f550ecd 100644 --- a/src/language-js/parse/babel.js +++ b/src/language-js/parse/babel.js @@ -7,8 +7,8 @@ import createBabelParseError from "./utilities/create-babel-parse-error.js"; import createParser from "./utilities/create-parser.js"; import { getSourceType, + SOURCE_TYPE_COMBINATIONS, SOURCE_TYPE_COMMONJS, - SOURCE_TYPE_MODULE, } from "./utilities/source-types.js"; import wrapExpression from "./utilities/wrap-expression.js"; @@ -22,7 +22,6 @@ const createBabelParser = (options) => createParser(createParse(options)); /** @type {ParserOptions} */ const parseOptions = { - sourceType: SOURCE_TYPE_MODULE, allowImportExportEverywhere: true, allowReturnOutsideFunction: true, allowNewTargetOutsideFunction: true, @@ -132,8 +131,11 @@ function createParse({ isExpression = false, optionsCombinations }) { let combinations = optionsCombinations; const sourceType = options.__babelSourceType ?? getSourceType(filepath); - if (sourceType && sourceType !== SOURCE_TYPE_MODULE) { - combinations = combinations.map((options) => ({ + + combinations = ( + sourceType ? [sourceType] : SOURCE_TYPE_COMBINATIONS + ).flatMap((sourceType) => + combinations.map((options) => ({ ...options, sourceType, // `sourceType: "commonjs"` does not allow these two properties @@ -143,8 +145,8 @@ function createParse({ isExpression = false, optionsCombinations }) { allowNewTargetOutsideFunction: undefined, } : undefined), - })); - } + })), + ); const shouldEnableV8intrinsicPlugin = /%[A-Z]/.test(text); if (text.includes("|>")) { diff --git a/tests/format/flow/await/__snapshots__/format.test.js.snap b/tests/format/flow/await/__snapshots__/format.test.js.snap index ca984fea4..973bdb524 100644 --- a/tests/format/flow/await/__snapshots__/format.test.js.snap +++ b/tests/format/flow/await/__snapshots__/format.test.js.snap @@ -43,7 +43,7 @@ export class C1 { p = await(0); } -await(0); +(await)(0); async function foo() { function bar(x = await(2)) {} @@ -55,7 +55,7 @@ export class C2 { } function foo(promise) { - await(promise); + (await)(promise); } function a() { @@ -63,18 +63,18 @@ function a() { } () => { - await(x); + (await)(x); }; function foo() { - await(foo); + (await)(foo); } export class C { p = await(0); } -await(0); +(await)(0); ================================================================================ `; diff --git a/tests/format/js/_errors_/html-like-comments/__snapshots__/format.test.js.snap b/tests/format/js/_errors_/html-like-comments/__snapshots__/format.test.js.snap deleted file mode 100644 index c28ec72b4..000000000 --- a/tests/format/js/_errors_/html-like-comments/__snapshots__/format.test.js.snap +++ /dev/null @@ -1,58 +0,0 @@ -// Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing - -exports[`html-like-comments.js format[babel] 1`] = ` -"Unexpected token (2:1) - 1 | alert( -> 2 | -Cause: Unexpected token (1:0)" -`; - -exports[`comment.js format[babel] 1`] = ` -"Unexpected token (1:1) -> 1 | -Cause: Unexpected token (1:0)" -`; - -exports[`comment.js format[babel-ts] 1`] = ` -"Unexpected token (1:1) -> 1 | -Cause: Unexpected token (1:0)" -`; - exports[`comment.js format[flow] 1`] = ` "Unexpected token \`!\`, expected the token \`>\` (1:2) > 1 | Cause: Expression expected." `; + +exports[`html-like-comments.js format 1`] = ` +====================================options===================================== +parsers: ["babel", "flow", "typescript"] + printWidth: 80 (default) | +=====================================input====================================== +alert( +