diff --git a/Gulpfile.mjs b/Gulpfile.mjs index a702dde443651..b0edbd5cdc6e6 100644 --- a/Gulpfile.mjs +++ b/Gulpfile.mjs @@ -621,6 +621,7 @@ function buildRollupDts(packages) { "packages/babel-parser/typings/babel-parser.source.d.ts", "packages/babel-parser/typings/babel-parser.d.ts", "// This file is auto-generated! Do not modify it directly.\n" + + "// Run `yarn gulp bundle-dts` to re-generate it.\n" + // @typescript-eslint/no-redundant-type-constituents can be removed once we drop the IF_BABEL_7 type "/* eslint-disable @typescript-eslint/consistent-type-imports, @typescript-eslint/no-redundant-type-constituents */", "packages/babel-parser" diff --git a/packages/babel-core/src/parser/index.ts b/packages/babel-core/src/parser/index.ts index ec2d0fce6d4bb..3c070d9aa463e 100644 --- a/packages/babel-core/src/parser/index.ts +++ b/packages/babel-core/src/parser/index.ts @@ -1,5 +1,5 @@ import type { Handler } from "gensync"; -import { parse, type File as ParseResult } from "@babel/parser"; +import { parse, type ParseResult } from "@babel/parser"; import { codeFrameColumns } from "@babel/code-frame"; import generateMissingPluginMessage from "./util/missing-plugin-helper.ts"; import type { PluginPasses } from "../config/index.ts"; diff --git a/packages/babel-core/src/transformation/normalize-file.ts b/packages/babel-core/src/transformation/normalize-file.ts index c75148fd9f741..07ccd982357c4 100644 --- a/packages/babel-core/src/transformation/normalize-file.ts +++ b/packages/babel-core/src/transformation/normalize-file.ts @@ -46,7 +46,6 @@ export default function* normalizeFile( ast = cloneDeep(ast); } } else { - // @ts-expect-error todo: use babel-types ast typings in Babel parser ast = yield* parser(pluginPasses, options, code); } @@ -101,7 +100,7 @@ export default function* normalizeFile( return new File(options, { code, - ast: ast as t.File, + ast: ast, inputMap, }); } diff --git a/packages/babel-parser/package.json b/packages/babel-parser/package.json index ca1966e831e0c..f341a32d92b6e 100644 --- a/packages/babel-parser/package.json +++ b/packages/babel-parser/package.json @@ -49,6 +49,7 @@ "conditions": { "BABEL_8_BREAKING": [ { + "types": null, "engines": { "node": "^18.20.0 || ^20.17.0 || >=22.8.0" } diff --git a/packages/babel-parser/src/index.ts b/packages/babel-parser/src/index.ts index d32695cf4bc09..4b34b595eac36 100644 --- a/packages/babel-parser/src/index.ts +++ b/packages/babel-parser/src/index.ts @@ -4,11 +4,13 @@ import { mixinPluginNames, mixinPlugins, } from "./plugin-utils.ts"; -import type { +export type { PluginConfig as ParserPlugin, + DecoratorsPluginOptions, FlowPluginOptions, - RecordAndTuplePluginOptions, PipelineOperatorPluginOptions, + RecordAndTuplePluginOptions, + TypeScriptPluginOptions, } from "./typings.ts"; import Parser, { type PluginsMap } from "./parser/index.ts"; @@ -20,10 +22,28 @@ import { } from "./tokenizer/types.ts"; export type { Token } from "./tokenizer/index.ts"; -import type { Expression, File } from "./types.ts"; +// TODO: Rather than type-casting the internal AST definitions to the +// @babel/types one, we should actually unify them. +import type { Expression, File } from "@babel/types"; export type { Expression, File }; -export function parse(input: string, options?: Options): File { +export type ParserOptions = Partial; + +export interface ParseError { + code: string; + reasonCode: string; +} +export type ParseResult = Result & { + errors: null | ParseError[]; +}; + +/** + * Parse the provided code as an entire ECMAScript program. + */ +export function parse( + input: string, + options?: ParserOptions, +): ParseResult { if (options?.sourceType === "unambiguous") { options = { ...options, @@ -34,7 +54,7 @@ export function parse(input: string, options?: Options): File { const ast = parser.parse(); if (parser.sawUnambiguousESM) { - return ast; + return ast as unknown as ParseResult; } if (parser.ambiguousScriptDifferentAst) { @@ -45,7 +65,10 @@ export function parse(input: string, options?: Options): File { // can be parsed either as an AwaitExpression, or as two ExpressionStatements. try { options.sourceType = "script"; - return getParser(options, input).parse(); + return getParser( + options, + input, + ).parse() as unknown as ParseResult; } catch {} } else { // This is both a valid module and a valid script, but @@ -53,26 +76,32 @@ export function parse(input: string, options?: Options): File { ast.program.sourceType = "script"; } - return ast; + return ast as unknown as ParseResult; } catch (moduleError) { try { options.sourceType = "script"; - return getParser(options, input).parse(); + return getParser( + options, + input, + ).parse() as unknown as ParseResult; } catch {} throw moduleError; } } else { - return getParser(options, input).parse(); + return getParser(options, input).parse() as unknown as ParseResult; } } -export function parseExpression(input: string, options?: Options): Expression { +export function parseExpression( + input: string, + options?: ParserOptions, +): ParseResult { const parser = getParser(options, input); if (parser.options.strictMode) { parser.state.strict = true; } - return parser.getExpression(); + return parser.getExpression() as unknown as ParseResult; } function generateExportedTokenTypes( @@ -135,11 +164,3 @@ function getParserClass( } return cls; } - -export type { - FlowPluginOptions, - ParserPlugin, - PipelineOperatorPluginOptions, - RecordAndTuplePluginOptions, -}; -export type ParserOptions = Partial; diff --git a/packages/babel-parser/src/options.ts b/packages/babel-parser/src/options.ts index 002cf0afa663f..7e5a243ec25fd 100644 --- a/packages/babel-parser/src/options.ts +++ b/packages/babel-parser/src/options.ts @@ -1,4 +1,4 @@ -import type { PluginList } from "./plugin-utils.ts"; +import type { Plugin } from "./plugin-utils.ts"; // A second optional argument can be given to further configure // the parser process. These options are recognized: @@ -6,26 +6,133 @@ import type { PluginList } from "./plugin-utils.ts"; export type SourceType = "script" | "module" | "unambiguous"; export interface Options { - sourceType?: SourceType; - sourceFilename?: string; - startIndex?: number; - startColumn?: number; - startLine?: number; + /** + * By default, import and export declarations can only appear at a program's top level. + * Setting this option to true allows them anywhere where a statement is allowed. + */ + allowImportExportEverywhere?: boolean; + + /** + * By default, await use is not allowed outside of an async function. + * Set this to true to accept such code. + */ allowAwaitOutsideFunction?: boolean; + + /** + * By default, a return statement at the top level raises an error. + * Set this to true to accept such code. + */ allowReturnOutsideFunction?: boolean; + + /** + * By default, new.target use is not allowed outside of a function or class. + * Set this to true to accept such code. + */ allowNewTargetOutsideFunction?: boolean; - allowImportExportEverywhere?: boolean; + allowSuperOutsideMethod?: boolean; + + /** + * By default, exported identifiers must refer to a declared variable. + * Set this to true to allow export statements to reference undeclared variables. + */ allowUndeclaredExports?: boolean; - plugins?: PluginList; - strictMode?: boolean | undefined | null; + + /** + * By default, Babel parser JavaScript code according to Annex B syntax. + * Set this to `false` to disable such behavior. + */ + annexB?: boolean; + + /** + * By default, Babel attaches comments to adjacent AST nodes. + * When this option is set to false, comments are not attached. + * It can provide up to 30% performance improvement when the input code has many comments. + * @babel/eslint-parser will set it for you. + * It is not recommended to use attachComment: false with Babel transform, + * as doing so removes all the comments in output code, and renders annotations such as + * /* istanbul ignore next *\/ nonfunctional. + */ + attachComment?: boolean; + + /** + * By default, Babel always throws an error when it finds some invalid code. + * When this option is set to true, it will store the parsing error and + * try to continue parsing the invalid input file. + */ + errorRecovery?: boolean; + + /** + * Indicate the mode the code should be parsed in. + * Can be one of "script", "module", or "unambiguous". Defaults to "script". + * "unambiguous" will make @babel/parser attempt to guess, based on the presence + * of ES6 import or export statements. + * Files with ES6 imports and exports are considered "module" and are otherwise "script". + */ + sourceType?: "script" | "module" | "unambiguous"; + + /** + * Correlate output AST nodes with their source filename. + * Useful when generating code and source maps from the ASTs of multiple input files. + */ + sourceFilename?: string; + + /** + * By default, all source indexes start from 0. + * You can provide a start index to alternatively start with. + * Useful for integration with other source tools. + */ + startIndex?: number; + + /** + * By default, the first line of code parsed is treated as line 1. + * You can provide a line number to alternatively start with. + * Useful for integration with other source tools. + */ + startLine?: number; + + /** + * By default, the parsed code is treated as if it starts from line 1, column 0. + * You can provide a column number to alternatively start with. + * Useful for integration with other source tools. + */ + startColumn?: number; + + /** + * Array containing the plugins that you want to enable. + */ + plugins?: Plugin[]; + + /** + * Should the parser work in strict mode. + * Defaults to true if sourceType === 'module'. Otherwise, false. + */ + strictMode?: boolean; + + /** + * Adds a ranges property to each node: [node.start, node.end] + */ ranges?: boolean; + + /** + * Adds all parsed tokens to a tokens property on the File node. + */ tokens?: boolean; - createImportExpressions?: boolean; + + /** + * By default, the parser adds information about parentheses by setting + * `extra.parenthesized` to `true` as needed. + * When this option is `true` the parser creates `ParenthesizedExpression` + * AST nodes instead of using the `extra` property. + */ createParenthesizedExpressions?: boolean; - errorRecovery?: boolean; - attachComment?: boolean; - annexB?: boolean; + + /** + * The default is false in Babel 7 and true in Babel 8 + * Set this to true to parse it as an `ImportExpression` node. + * Otherwise `import(foo)` is parsed as `CallExpression(Import, [Identifier(foo)])`. + */ + createImportExpressions?: boolean; } export const enum OptionFlags { diff --git a/packages/babel-parser/src/plugin-utils.ts b/packages/babel-parser/src/plugin-utils.ts index 2840d1f29154c..e56c174c5b1b3 100644 --- a/packages/babel-parser/src/plugin-utils.ts +++ b/packages/babel-parser/src/plugin-utils.ts @@ -3,8 +3,6 @@ import type { PluginConfig } from "./typings.ts"; export type Plugin = PluginConfig; -export type PluginList = PluginConfig[]; - export type MixinPlugin = ( superClass: new (...args: any) => Parser, ) => new (...args: any) => Parser; diff --git a/packages/babel-parser/tsconfig.json b/packages/babel-parser/tsconfig.json index d00d96d6102e3..d4bd4e340167b 100644 --- a/packages/babel-parser/tsconfig.json +++ b/packages/babel-parser/tsconfig.json @@ -8,8 +8,7 @@ "./src/**/*.ts", "./src/**/*.cts", "../../lib/globals.d.ts", - "../../scripts/repo-utils/*.d.ts", - "../../packages/babel-parser/typings/*.d.ts" + "../../scripts/repo-utils/*.d.ts" ], "references": [ { diff --git a/packages/babel-parser/typings/babel-parser.d.ts b/packages/babel-parser/typings/babel-parser.d.ts index 7478632b162d0..33828dc0bb39b 100644 --- a/packages/babel-parser/typings/babel-parser.d.ts +++ b/packages/babel-parser/typings/babel-parser.d.ts @@ -1,11 +1,12 @@ // This file is auto-generated! Do not modify it directly. +// Run `yarn gulp bundle-dts` to re-generate it. /* eslint-disable @typescript-eslint/consistent-type-imports, @typescript-eslint/no-redundant-type-constituents */ -import * as _babel_types from '@babel/types'; +import { File, Expression } from '@babel/types'; type BABEL_8_BREAKING = false; type IF_BABEL_7 = false extends BABEL_8_BREAKING ? V : never; -type Plugin = +type Plugin$1 = | "asyncDoExpressions" | IF_BABEL_7<"asyncGenerators"> | IF_BABEL_7<"bigInt"> @@ -62,7 +63,7 @@ type ParserPluginWithOptions = | ["flow", FlowPluginOptions] | ["typescript", TypeScriptPluginOptions]; -type PluginConfig = Plugin | ParserPluginWithOptions; +type PluginConfig = Plugin$1 | ParserPluginWithOptions; interface DecoratorsPluginOptions { decoratorsBeforeExport?: boolean; @@ -94,174 +95,136 @@ interface TypeScriptPluginOptions { disallowAmbiguousJSXLike?: boolean; } -// Type definitions for @babel/parser -// Project: https://github.com/babel/babel/tree/main/packages/babel-parser -// Definitions by: Troy Gerwien -// Marvin Hagemeister -// Avi Vahl -// TypeScript Version: 2.9 +type Plugin = PluginConfig; + +interface Options { + /** + * By default, import and export declarations can only appear at a program's top level. + * Setting this option to true allows them anywhere where a statement is allowed. + */ + allowImportExportEverywhere?: boolean; + /** + * By default, await use is not allowed outside of an async function. + * Set this to true to accept such code. + */ + allowAwaitOutsideFunction?: boolean; + /** + * By default, a return statement at the top level raises an error. + * Set this to true to accept such code. + */ + allowReturnOutsideFunction?: boolean; + /** + * By default, new.target use is not allowed outside of a function or class. + * Set this to true to accept such code. + */ + allowNewTargetOutsideFunction?: boolean; + allowSuperOutsideMethod?: boolean; + /** + * By default, exported identifiers must refer to a declared variable. + * Set this to true to allow export statements to reference undeclared variables. + */ + allowUndeclaredExports?: boolean; + /** + * By default, Babel parser JavaScript code according to Annex B syntax. + * Set this to `false` to disable such behavior. + */ + annexB?: boolean; + /** + * By default, Babel attaches comments to adjacent AST nodes. + * When this option is set to false, comments are not attached. + * It can provide up to 30% performance improvement when the input code has many comments. + * @babel/eslint-parser will set it for you. + * It is not recommended to use attachComment: false with Babel transform, + * as doing so removes all the comments in output code, and renders annotations such as + * /* istanbul ignore next *\/ nonfunctional. + */ + attachComment?: boolean; + /** + * By default, Babel always throws an error when it finds some invalid code. + * When this option is set to true, it will store the parsing error and + * try to continue parsing the invalid input file. + */ + errorRecovery?: boolean; + /** + * Indicate the mode the code should be parsed in. + * Can be one of "script", "module", or "unambiguous". Defaults to "script". + * "unambiguous" will make @babel/parser attempt to guess, based on the presence + * of ES6 import or export statements. + * Files with ES6 imports and exports are considered "module" and are otherwise "script". + */ + sourceType?: "script" | "module" | "unambiguous"; + /** + * Correlate output AST nodes with their source filename. + * Useful when generating code and source maps from the ASTs of multiple input files. + */ + sourceFilename?: string; + /** + * By default, all source indexes start from 0. + * You can provide a start index to alternatively start with. + * Useful for integration with other source tools. + */ + startIndex?: number; + /** + * By default, the first line of code parsed is treated as line 1. + * You can provide a line number to alternatively start with. + * Useful for integration with other source tools. + */ + startLine?: number; + /** + * By default, the parsed code is treated as if it starts from line 1, column 0. + * You can provide a column number to alternatively start with. + * Useful for integration with other source tools. + */ + startColumn?: number; + /** + * Array containing the plugins that you want to enable. + */ + plugins?: Plugin[]; + /** + * Should the parser work in strict mode. + * Defaults to true if sourceType === 'module'. Otherwise, false. + */ + strictMode?: boolean; + /** + * Adds a ranges property to each node: [node.start, node.end] + */ + ranges?: boolean; + /** + * Adds all parsed tokens to a tokens property on the File node. + */ + tokens?: boolean; + /** + * By default, the parser adds information about parentheses by setting + * `extra.parenthesized` to `true` as needed. + * When this option is `true` the parser creates `ParenthesizedExpression` + * AST nodes instead of using the `extra` property. + */ + createParenthesizedExpressions?: boolean; + /** + * The default is false in Babel 7 and true in Babel 8 + * Set this to true to parse it as an `ImportExpression` node. + * Otherwise `import(foo)` is parsed as `CallExpression(Import, [Identifier(foo)])`. + */ + createImportExpressions?: boolean; +} +type ParserOptions = Partial; +interface ParseError { + code: string; + reasonCode: string; +} +type ParseResult = Result & { + errors: null | ParseError[]; +}; /** * Parse the provided code as an entire ECMAScript program. */ -declare function parse( - input: string, - options?: ParserOptions -): ParseResult<_babel_types.File>; - -/** - * Parse the provided code as a single expression. - */ -declare function parseExpression( - input: string, - options?: ParserOptions -): ParseResult<_babel_types.Expression>; - -interface ParserOptions { - /** - * By default, import and export declarations can only appear at a program's top level. - * Setting this option to true allows them anywhere where a statement is allowed. - */ - allowImportExportEverywhere?: boolean; - - /** - * By default, await use is not allowed outside of an async function. - * Set this to true to accept such code. - */ - allowAwaitOutsideFunction?: boolean; - - /** - * By default, a return statement at the top level raises an error. - * Set this to true to accept such code. - */ - allowReturnOutsideFunction?: boolean; - - /** - * By default, new.target use is not allowed outside of a function or class. - * Set this to true to accept such code. - */ - allowNewTargetOutsideFunction?: boolean; - - allowSuperOutsideMethod?: boolean; - - /** - * By default, exported identifiers must refer to a declared variable. - * Set this to true to allow export statements to reference undeclared variables. - */ - allowUndeclaredExports?: boolean; - - /** - * By default, Babel parser JavaScript code according to Annex B syntax. - * Set this to `false` to disable such behavior. - */ - annexB?: boolean; - - /** - * By default, Babel attaches comments to adjacent AST nodes. - * When this option is set to false, comments are not attached. - * It can provide up to 30% performance improvement when the input code has many comments. - * @babel/eslint-parser will set it for you. - * It is not recommended to use attachComment: false with Babel transform, - * as doing so removes all the comments in output code, and renders annotations such as - * /* istanbul ignore next *\/ nonfunctional. - */ - attachComment?: boolean; - - /** - * By default, Babel always throws an error when it finds some invalid code. - * When this option is set to true, it will store the parsing error and - * try to continue parsing the invalid input file. - */ - errorRecovery?: boolean; - - /** - * Indicate the mode the code should be parsed in. - * Can be one of "script", "module", or "unambiguous". Defaults to "script". - * "unambiguous" will make @babel/parser attempt to guess, based on the presence - * of ES6 import or export statements. - * Files with ES6 imports and exports are considered "module" and are otherwise "script". - */ - sourceType?: "script" | "module" | "unambiguous"; - - /** - * Correlate output AST nodes with their source filename. - * Useful when generating code and source maps from the ASTs of multiple input files. - */ - sourceFilename?: string; - - /** - * By default, all source indexes start from 0. - * You can provide a start index to alternatively start with. - * Useful for integration with other source tools. - */ - startIndex?: number; - - /** - * By default, the first line of code parsed is treated as line 1. - * You can provide a line number to alternatively start with. - * Useful for integration with other source tools. - */ - startLine?: number; - - /** - * By default, the parsed code is treated as if it starts from line 1, column 0. - * You can provide a column number to alternatively start with. - * Useful for integration with other source tools. - */ - startColumn?: number; - - /** - * Array containing the plugins that you want to enable. - */ - plugins?: ParserPlugin[]; - - /** - * Should the parser work in strict mode. - * Defaults to true if sourceType === 'module'. Otherwise, false. - */ - strictMode?: boolean; - - /** - * Adds a ranges property to each node: [node.start, node.end] - */ - ranges?: boolean; - - /** - * Adds all parsed tokens to a tokens property on the File node. - */ - tokens?: boolean; - - /** - * By default, the parser adds information about parentheses by setting - * `extra.parenthesized` to `true` as needed. - * When this option is `true` the parser creates `ParenthesizedExpression` - * AST nodes instead of using the `extra` property. - */ - createParenthesizedExpressions?: boolean; - - /** - * The default is false in Babel 7 and true in Babel 8 - * Set this to true to parse it as an `ImportExpression` node. - * Otherwise `import(foo)` is parsed as `CallExpression(Import, [Identifier(foo)])`. - */ - createImportExpressions?: boolean; -} - -type ParserPlugin = PluginConfig; - +declare function parse(input: string, options?: ParserOptions): ParseResult; +declare function parseExpression(input: string, options?: ParserOptions): ParseResult; declare const tokTypes: { // todo(flow->ts) real token type [name: string]: any; }; -interface ParseError { - code: string; - reasonCode: string; -} - -type ParseResult = Result & { - errors: ParseError[]; -}; - -export { DecoratorsPluginOptions, FlowPluginOptions, ParseError, ParseResult, ParserOptions, ParserPlugin, ParserPluginWithOptions, PipelineOperatorPluginOptions, RecordAndTuplePluginOptions, TypeScriptPluginOptions, parse, parseExpression, tokTypes }; +export { DecoratorsPluginOptions, FlowPluginOptions, ParseError, ParseResult, ParserOptions, PluginConfig as ParserPlugin, ParserPluginWithOptions, PipelineOperatorPluginOptions, RecordAndTuplePluginOptions, TypeScriptPluginOptions, parse, parseExpression, tokTypes }; diff --git a/packages/babel-parser/typings/babel-parser.source.d.ts b/packages/babel-parser/typings/babel-parser.source.d.ts index 353d434de7a11..421e8d2a3ccfc 100644 --- a/packages/babel-parser/typings/babel-parser.source.d.ts +++ b/packages/babel-parser/typings/babel-parser.source.d.ts @@ -1,177 +1,24 @@ -// Type definitions for @babel/parser -// Project: https://github.com/babel/babel/tree/main/packages/babel-parser -// Definitions by: Troy Gerwien -// Marvin Hagemeister -// Avi Vahl -// TypeScript Version: 2.9 - -/** - * Parse the provided code as an entire ECMAScript program. - */ -export function parse( - input: string, - options?: ParserOptions -): ParseResult; - -/** - * Parse the provided code as a single expression. - */ -export function parseExpression( - input: string, - options?: ParserOptions -): ParseResult; - -export interface ParserOptions { - /** - * By default, import and export declarations can only appear at a program's top level. - * Setting this option to true allows them anywhere where a statement is allowed. - */ - allowImportExportEverywhere?: boolean; - - /** - * By default, await use is not allowed outside of an async function. - * Set this to true to accept such code. - */ - allowAwaitOutsideFunction?: boolean; - - /** - * By default, a return statement at the top level raises an error. - * Set this to true to accept such code. - */ - allowReturnOutsideFunction?: boolean; - - /** - * By default, new.target use is not allowed outside of a function or class. - * Set this to true to accept such code. - */ - allowNewTargetOutsideFunction?: boolean; - - allowSuperOutsideMethod?: boolean; - - /** - * By default, exported identifiers must refer to a declared variable. - * Set this to true to allow export statements to reference undeclared variables. - */ - allowUndeclaredExports?: boolean; - - /** - * By default, Babel parser JavaScript code according to Annex B syntax. - * Set this to `false` to disable such behavior. - */ - annexB?: boolean; - - /** - * By default, Babel attaches comments to adjacent AST nodes. - * When this option is set to false, comments are not attached. - * It can provide up to 30% performance improvement when the input code has many comments. - * @babel/eslint-parser will set it for you. - * It is not recommended to use attachComment: false with Babel transform, - * as doing so removes all the comments in output code, and renders annotations such as - * /* istanbul ignore next *\/ nonfunctional. - */ - attachComment?: boolean; - - /** - * By default, Babel always throws an error when it finds some invalid code. - * When this option is set to true, it will store the parsing error and - * try to continue parsing the invalid input file. - */ - errorRecovery?: boolean; - - /** - * Indicate the mode the code should be parsed in. - * Can be one of "script", "module", or "unambiguous". Defaults to "script". - * "unambiguous" will make @babel/parser attempt to guess, based on the presence - * of ES6 import or export statements. - * Files with ES6 imports and exports are considered "module" and are otherwise "script". - */ - sourceType?: "script" | "module" | "unambiguous"; - - /** - * Correlate output AST nodes with their source filename. - * Useful when generating code and source maps from the ASTs of multiple input files. - */ - sourceFilename?: string; - - /** - * By default, all source indexes start from 0. - * You can provide a start index to alternatively start with. - * Useful for integration with other source tools. - */ - startIndex?: number; - - /** - * By default, the first line of code parsed is treated as line 1. - * You can provide a line number to alternatively start with. - * Useful for integration with other source tools. - */ - startLine?: number; - - /** - * By default, the parsed code is treated as if it starts from line 1, column 0. - * You can provide a column number to alternatively start with. - * Useful for integration with other source tools. - */ - startColumn?: number; - - /** - * Array containing the plugins that you want to enable. - */ - plugins?: ParserPlugin[]; - - /** - * Should the parser work in strict mode. - * Defaults to true if sourceType === 'module'. Otherwise, false. - */ - strictMode?: boolean; - - /** - * Adds a ranges property to each node: [node.start, node.end] - */ - ranges?: boolean; - - /** - * Adds all parsed tokens to a tokens property on the File node. - */ - tokens?: boolean; - - /** - * By default, the parser adds information about parentheses by setting - * `extra.parenthesized` to `true` as needed. - * When this option is `true` the parser creates `ParenthesizedExpression` - * AST nodes instead of using the `extra` property. - */ - createParenthesizedExpressions?: boolean; - - /** - * The default is false in Babel 7 and true in Babel 8 - * Set this to true to parse it as an `ImportExpression` node. - * Otherwise `import(foo)` is parsed as `CallExpression(Import, [Identifier(foo)])`. - */ - createImportExpressions?: boolean; -} - -export type ParserPlugin = import("../src/typings").PluginConfig; +export type { + parse, + parseExpression, +} from "../../../dts/packages/babel-parser/src/index.d.ts"; export type { - ParserPluginWithOptions, + ParserOptions, + ParserPlugin, DecoratorsPluginOptions, PipelineOperatorPluginOptions, RecordAndTuplePluginOptions, FlowPluginOptions, TypeScriptPluginOptions, -} from "../src/typings"; + ParseError, + ParseResult, +} from "../../../dts/packages/babel-parser/src/index.d.ts"; + +/** @deprecated Will be removed in Babel 8 */ +export type { ParserPluginWithOptions } from "../../../dts/packages/babel-parser/src/typings.d.ts"; export const tokTypes: { // todo(flow->ts) real token type [name: string]: any; }; - -export interface ParseError { - code: string; - reasonCode: string; -} - -export type ParseResult = Result & { - errors: ParseError[]; -}; diff --git a/packages/babel-template/src/parse.ts b/packages/babel-template/src/parse.ts index 7c8ef5e7b2cdf..5c0e989db21c8 100644 --- a/packages/babel-template/src/parse.ts +++ b/packages/babel-template/src/parse.ts @@ -208,7 +208,6 @@ function parseWithCodeFrame( }; try { - // @ts-expect-error todo: use babel-types ast typings in Babel parser return parse(code, parserOpts); } catch (err) { const loc = err.loc; diff --git a/packages/babel-traverse/src/path/replacement.ts b/packages/babel-traverse/src/path/replacement.ts index 2c9846740e6ab..06edd51a16618 100644 --- a/packages/babel-traverse/src/path/replacement.ts +++ b/packages/babel-traverse/src/path/replacement.ts @@ -84,7 +84,6 @@ export function replaceWithSourceString(this: NodePath, replacement: string) { try { replacement = `(${replacement})`; - // @ts-expect-error todo: use babel-types ast typings in Babel parser ast = parse(replacement); } catch (err) { const loc = err.loc; diff --git a/scripts/generators/tsconfig.js b/scripts/generators/tsconfig.js index 7ba19041c218b..e60a35861a8a2 100644 --- a/scripts/generators/tsconfig.js +++ b/scripts/generators/tsconfig.js @@ -257,9 +257,9 @@ function buildTSConfig(pkgs, allDeps, hasOverrides) { "./src/**/*.cts", "../../lib/globals.d.ts", "../../scripts/repo-utils/*.d.ts", - pkgs.some(p => p.name === "@babel/parser") - ? "../../packages/babel-parser/typings/*.d.ts" - : null, + //pkgs.some(p => p.name === "@babel/parser") + // ? "../../packages/babel-parser/typings/*.d.ts" + // : null, ].filter(Boolean), references: Array.from(referencePaths, path => ({ path })), };