@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])
parses the provided code
as an entire ECMAScript program, whileparseExpression()
tries to parse a single Expression with performance inmind. When in doubt, use .parse()
.
allowImportExportEverywhere: By default,
import
andexport
declarations can only appear at a program's top level. Setting thisoption totrue
allows them anywhere where a statement is allowed.allowAwaitOutsideFunction: By default,
await
use is not allowedoutside of an async function. Set this totrue
to accept suchcode.allowReturnOutsideFunction: By default, a return statement atthe top level raises an error. Set this to
true
to accept suchcode.allowSuperOutsideMethod: By default,
super
use is not allowedoutside of class and object methods. Set this totrue
to accept suchcode.sourceType: Indicate the mode the code should be parsed in. Can beone of
"script"
,"module"
, or"unambiguous"
. Defaults to"script"
."unambiguous"
will make @babel/parser attempt to guess, based on the presence of ES6import
orexport
statements. Files with ES6import
s andexport
s are considered"module"
and are otherwise"script"
.startLine: 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.
plugins: Array containing the plugins that you want to enable.
ranges: Adds a
ranges
property to each node:[node.start, node.end]
tokens: Adds all parsed tokens to a
tokens
property on theFile
node
Output
The Babel parser generates AST according to Babel AST format.It is based on with the following deviations:
- Literal token is replaced with , NumericLiteral, , NullLiteral,
- Property token is replaced with and ObjectMethod
- is replaced with ClassMethod
- and BlockStatement contain additional
directives
field with and DirectiveLiteral - , ObjectProperty, and value property's properties in FunctionExpression is coerced/brought into the main method node.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 proposals
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 optionallows to choose which one to use.See What's Happening With the Pipeline (|>) Proposal? 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 to always parse files as if// @flow
was specified.
FAQ
Will the Babel parser support a plugin system?
Previous issues: , #6694.
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.
To consume your custom parser, you can add a plugin to your to call the parser via its npm package name or require it if using JavaScript,