@babel/parser

    • The latest ECMAScript version enabled by default (ES2017).
    • Comment attachment.
    • Support for JSX, Flow, Typescript.
    • Support for experimental language proposals (accepting PRs for anything at least stage-0).

    Heavily based on and acorn-jsx, thanks to the awesome work of and @marijnh.

    API

    babelParser.parseExpression(code, [options])

    parse() parses the provided code as an entire ECMAScript program, while parseExpression() tries to parse a single Expression with performance in mind. When in doubt, use .parse().

    • allowImportExportEverywhere: 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.

    • allowAwaitOutsideFunction: By default, await use is only allowed inside of an async function or, when the topLevelAwait plugin is enabled, in the top-level scope of modules. Set this to true to also accept it in the top-level scope of scripts.

    • allowReturnOutsideFunction: By default, a return statement at the top level raises an error. Set this to true to accept such code.

    • allowSuperOutsideMethod: By default, super use is not allowed outside of class and object methods. Set this to true to accept such code.

    • allowUndeclaredExports: By default, exporting an identifier that was not declared in the current module scope will raise an error. While this behavior is required by the ECMAScript modules specification, Babel’s parser cannot anticipate transforms later in the plugin pipeline that might insert the appropriate declarations, so it is sometimes important to set this option to true to prevent the parser from prematurely complaining about undeclared exports that will be added later.

    • createParenthesizedExpressions: By default, the parser sets extra.parenthesized on the expression nodes. When this option is set to true, ParenthesizedExpression AST nodes are created instead.

    • errorRecovery: 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. The resulting AST will have an errors property representing an array of all the parsing errors. Note that even when this option is enabled, @babel/parser could throw for unrecoverable errors.

    • sourceType: 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".

    • sourceFilename: Correlate output AST nodes with their source filename. Useful when generating code and source maps from the ASTs of multiple input files.

    • strictMode: By default, ECMAScript code is parsed as strict only if a "use strict"; directive is present or if the parsed file is an ECMAScript module. Set this option to true to always parse files in strict mode.

    • ranges: Adds a range property to each node: [node.start, node.end]

    • tokens: Adds all parsed tokens to a tokens property on the node

    Output

    The Babel parser generates AST according to Babel AST format. It is based on with the following deviations:

    AST for JSX code is based on .

    For example: We push a fix to early error on something like #107 - multiple default exports per file. That would be considered a bug fix even though it would cause a build to fail.

    Example

    Miscellaneous

    Language extensions

    ECMAScript

    Plugins options

    NOTE: When a plugin is specified multiple times, only the first options are considered.

    • decorators:

      • decoratorsBeforeExport (boolean)

    • pipelineOperator:

      • proposal (required, accepted values: minimal, smart) There are different proposals for the pipeline operator. This option allows to choose which one to use. See for more information.
    • flow:

      • all (boolean, default: false) Some code has different meaning in Flow and in vanilla JavaScript. For example, foo<T>(x) is parsed as a call expression with a type argument in Flow, but as a comparison (foo < T > x) accordingly to the ECMAScript specification. By default, babel-parser parses those ambiguous constructs as Flow types only if the file starts with a // @flow pragma. Set this option to true to always parse files as if // @flow was specified.

    FAQ

    Will the Babel parser support a plugin system?

    Previous issues: #1351, .

    We currently aren’t willing to commit to supporting the API for plugins or the resulting ecosystem (there is already enough work maintaining Babel’s own plugin system). It’s not clear how to make that API effective, and it would limit our ability to refactor and optimize the codebase.

    Our current recommendation for those that want to create their own custom syntax is for users to fork the parser.