From 8d74ddeccdd5e224056e449b0eccefef070c6765 Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sun, 1 Feb 2026 09:57:39 +0800 Subject: [PATCH] Work after babel upgrade to v8.0.0-rc1 (#18823) --- src/language-js/massage-ast/index.js | 15 ----------- src/language-js/parse/babel.js | 30 +++++++++++++--------- src/language-js/parse/postprocess/index.js | 12 ++++----- src/language-js/types/estree.d.ts | 1 + 4 files changed, 25 insertions(+), 33 deletions(-) diff --git a/src/language-js/massage-ast/index.js b/src/language-js/massage-ast/index.js index 0ba53128d..a39835811 100644 --- a/src/language-js/massage-ast/index.js +++ b/src/language-js/massage-ast/index.js @@ -189,21 +189,6 @@ function massageAstNode(original, cloned, parent) { if (original.type === "TemplateLiteral") { removeTemplateElementsValue(cloned); } - - // https://github.com/babel/babel/issues/17719 - if ( - (original.type === "ClassDeclaration" || - original.type === "ClassExpression") && - !original.superTypeArguments && - original.superClass?.type === "TSInstantiationExpression" && - original.superClass.typeArguments - ) { - const { - superClass: { typeArguments, expression }, - } = cloned; - cloned.superTypeArguments = typeArguments; - cloned.superClass = expression; - } } massageAstNode.ignoredProperties = ignoredProperties; diff --git a/src/language-js/parse/babel.js b/src/language-js/parse/babel.js index 018708292..6a7e11816 100644 --- a/src/language-js/parse/babel.js +++ b/src/language-js/parse/babel.js @@ -14,7 +14,7 @@ import wrapExpression from "./utilities/wrap-expression.js"; const createBabelParser = (options) => createParser(createParse(options)); -/** @import {ParserOptions, ParserPlugin} from "@babel/parser" */ +/** @import {ParserOptions, ParserPlugin, ParseError} from "@babel/parser" */ /** * @typedef {typeof babelParse | typeof parseExpression} Parse @@ -39,7 +39,7 @@ const parseOptions = { "functionBind", "functionSent", "throwExpressions", - "partialApplication", + ["partialApplication", { version: "2018-07" }], "decorators", "moduleBlocks", "asyncDoExpressions", @@ -59,12 +59,17 @@ const parseOptions = { /** @type {ParserPlugin} */ const v8intrinsicPlugin = "v8intrinsic"; -/** @type {Array} */ +/** @type {ParserPlugin[]} */ const pipelineOperatorPlugins = [ ["pipelineOperator", { proposal: "hack", topicToken: "%" }], ["pipelineOperator", { proposal: "fsharp" }], ]; +/** +@param {ParserPlugin[]} plugins +@param {ParserOptions} options +@returns {ParserOptions} +*/ const appendPlugins = (plugins, options = parseOptions) => ({ ...options, plugins: [...options.plugins, ...plugins], @@ -104,7 +109,13 @@ function parseWithOptions(parse, text, options) { return ast; } +/** +@param {{isExpression: boolean, optionsCombinations: ParserOptions[]}} param0 +*/ function createParse({ isExpression = false, optionsCombinations }) { + /** + @param {string} text + */ return (text, options = {}) => { let { filepath } = options; if (typeof filepath !== "string") { @@ -177,12 +188,8 @@ function createParse({ isExpression = false, optionsCombinations }) { }; } -// Error codes are defined in -// - https://github.com/babel/babel/tree/v7.23.6/packages/babel-parser/src/parse-error -// - https://github.com/babel/babel/blob/v7.23.6/packages/babel-parser/src/plugins/typescript/index.ts#L73-L223 -// - https://github.com/babel/babel/blob/v7.23.6/packages/babel-parser/src/plugins/flow/index.ts#L47-L224 -// - https://github.com/babel/babel/blob/v7.23.6/packages/babel-parser/src/plugins/jsx/index.ts#L23-L44 -const allowedReasonCodes = new Set([ +/** @type {ParseError["reasonCode"][]} */ +const allowedReasonCodesArray = [ "StrictNumericEscape", "StrictWith", "StrictOctalLiteral", @@ -192,9 +199,7 @@ const allowedReasonCodes = new Set([ "StrictFunction", "ForInOfLoopInitializer", - "DecoratorExportClass", "ParamDupe", - "InvalidDecimal", "RestTrailingComma", "UnsupportedParameterDecorator", "UnterminatedJsxContent", @@ -218,7 +223,8 @@ const allowedReasonCodes = new Set([ ``` */ "DeclarationMissingInitializer", -]); +]; +const allowedReasonCodes = new Set(allowedReasonCodesArray); const babelParserOptionsCombinations = [appendPlugins(["jsx"])]; const babel = /* @__PURE__ */ createBabelParser({ diff --git a/src/language-js/parse/postprocess/index.js b/src/language-js/parse/postprocess/index.js index b5bd169d8..0d7abfcae 100644 --- a/src/language-js/parse/postprocess/index.js +++ b/src/language-js/parse/postprocess/index.js @@ -13,7 +13,7 @@ import { mergeNestledJsdocComments } from "./merge-nestled-jsdoc-comments.js"; import visitNode from "./visit-node.js"; /** -@import {Node, Comment} from "../../types/estree.js" +@import {Node, Comment, NodeMap} from "../../types/estree.js" */ const isNodeWithRaw = createTypeCheckFunction([ @@ -38,11 +38,11 @@ const isNodeWithRaw = createTypeCheckFunction([ ]); /** - * @param {{ - * text: string, - * astType?: "espree" | "flow" | "hermes" | "meriyah" | "oxc-js" | "oxc-ts" | "typescript", - * }} options - */ +@param {{ + text: string, + astType?: "espree" | "flow" | "hermes" | "meriyah" | "oxc-js" | "oxc-ts" | "typescript", +}} options +*/ function postprocess(ast, options) { const { text, astType } = options; const isOxcTs = astType === "oxc-ts"; diff --git a/src/language-js/types/estree.d.ts b/src/language-js/types/estree.d.ts index b8c399f0d..8dc0a46d9 100644 --- a/src/language-js/types/estree.d.ts +++ b/src/language-js/types/estree.d.ts @@ -30,6 +30,7 @@ type FlowAdditionalNode = { type PrettierNode = { type: "JsExpressionRoot"; node: Babel.Expression }; export type Comment = ( + | Babel.InterpreterDirective | Babel.Comment | TSESTree.Comment | MeriyahESTree.Comment