@babel/parser
A JavaScript parser
Last updated 6 years ago by loganfsmyth .
MIT · Repository · Original npm · Tarball · package.json
$ npm install @babel/parser 
SYNC missed versions from official npm registry.

@babel/parser

The Babel parser (previously Babylon) is a JavaScript parser used in Babel.

  • 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).

Credits

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

API

babelParser.parse(code, [options])

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().

Options

  • 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 not allowed outside of an async function. Set this to true to accept such code.

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

  • allowSuperOutsideMethod: TODO

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

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

  • strictMode: TODO

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

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

Output

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

There is now an estree plugin which reverts these deviations

AST for JSX code is based on Facebook JSX AST.

Semver

The Babel Parser follows semver in most situations. The only thing to note is that some spec-compliancy bug fixes may be released under patch versions.

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

require("@babel/parser").parse("code", {
  // parse in strict mode and allow module declarations
  sourceType: "module",

  plugins: [
    // enable jsx and flow syntax
    "jsx",
    "flow"
  ]
});

Plugins

Name Code Example
estree (repo) n/a
jsx (repo) <a attr="b">{s}</a>
flow (repo) var a: string = "";
flowComments (docs) /*:: type Foo = {...}; */
typescript (repo) var a: string = "";
doExpressions var a = do { if (true) { 'hi'; } };
objectRestSpread (proposal) var a = { b, ...c };
decorators (Stage 2 proposal) and decorators-legacy (Stage 1) @a class A {}
classProperties (proposal) class A { b = 1; }
classPrivateProperties (proposal) class A { #b = 1; }
classPrivateMethods (proposal) class A { #c() {} }
exportDefaultFrom (proposal) export v from "mod"
exportNamespaceFrom (proposal) export * as ns from "mod"
asyncGenerators (proposal) async function*() {}, for await (let a of b) {}
functionBind (proposal) a::b, ::console.log
functionSent function.sent
dynamicImport (proposal) import('./guy').then(a)
numericSeparator (proposal) 1_000_000
optionalChaining (proposal) a?.b
importMeta (proposal) import.meta.url
bigInt (proposal) 100n
optionalCatchBinding (proposal) try {throw 0;} catch{do();}
throwExpressions (proposal) () => throw new Error("")
pipelineOperator (proposal) a \|> b
nullishCoalescingOperator (proposal) a ?? b

Plugins options

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

  • decorators:
    • decoratorsBeforeExport (boolean)
      // decoratorsBeforeExport: true
      @dec
      export class C {}
      
      // decoratorsBeforeExport: false
      export @dec class C {}
      
  • flow:
    • all (boolean)

FAQ

Will the Babel parser support a plugin system?

Previous issues: #1351, #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.

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

To consume your custom parser, you can add to your .babelrc via its npm package name or require it if using JavaScript,

{
  "parserOpts": {
    "parser": "custom-fork-of-babel-parser-on-npm-here"
  }
}

Current Tags

  • 7.21.4-esm.4                                ...           esm (2 years ago)
  • 7.26.2                                ...           latest (15 days ago)
  • 8.0.0-alpha.13                                ...           next (21 days ago)

211 Versions

  • 7.26.2                                ...           15 days ago
  • 7.26.1                                ...           20 days ago
  • 8.0.0-alpha.13                                ...           21 days ago
  • 7.26.0                                ...           21 days ago
  • 7.25.9                                ...           23 days ago
  • 7.25.8                                ...           a month ago
  • 7.25.7                                ...           a month ago
  • 7.25.6                                ...           3 months ago
  • 7.25.4                                ...           3 months ago
  • 7.25.3                                ...           3 months ago
  • 8.0.0-alpha.12                                ...           4 months ago
  • 7.25.0                                ...           4 months ago
  • 7.24.8                                ...           4 months ago
  • 8.0.0-alpha.11                                ...           5 months ago
  • 7.24.7                                ...           5 months ago
  • 8.0.0-alpha.10                                ...           5 months ago
  • 8.0.0-alpha.9                                ...           5 months ago
  • 7.24.6                                ...           6 months ago
  • 7.24.5                                ...           7 months ago
  • 8.0.0-alpha.8                                ...           7 months ago
  • 7.24.4                                ...           7 months ago
  • 7.24.1                                ...           8 months ago
  • 8.0.0-alpha.7                                ...           9 months ago
  • 7.24.0                                ...           9 months ago
  • 8.0.0-alpha.6                                ...           10 months ago
  • 7.23.9                                ...           10 months ago
  • 8.0.0-alpha.5                                ...           a year ago
  • 7.23.6                                ...           a year ago
  • 7.23.5                                ...           a year ago
  • 7.23.4                                ...           a year ago
  • 7.23.3                                ...           a year ago
  • 8.0.0-alpha.4                                ...           a year ago
  • 8.0.0-alpha.3                                ...           a year ago
  • 7.23.0                                ...           a year ago
  • 7.22.16                                ...           a year ago
  • 7.22.15                                ...           a year ago
  • 7.22.14                                ...           a year ago
  • 7.22.13                                ...           a year ago
  • 7.22.11                                ...           a year ago
  • 8.0.0-alpha.2                                ...           a year ago
  • 7.22.10                                ...           a year ago
  • 8.0.0-alpha.1                                ...           a year ago
  • 8.0.0-alpha.0                                ...           a year ago
  • 7.22.7                                ...           a year ago
  • 7.22.6                                ...           a year ago
  • 7.22.5                                ...           a year ago
  • 7.22.4                                ...           a year ago
  • 7.22.3                                ...           a year ago
  • 7.22.0                                ...           a year ago
  • 7.21.9                                ...           a year ago
  • 7.21.8                                ...           2 years ago
  • 7.21.5                                ...           2 years ago
  • 7.21.4-esm.4                                ...           2 years ago
  • 7.21.4-esm.3                                ...           2 years ago
  • 7.21.4-esm.2                                ...           2 years ago
  • 7.21.4-esm.1                                ...           2 years ago
  • 7.21.4-esm                                ...           2 years ago
  • 7.21.4                                ...           2 years ago
  • 7.21.3                                ...           2 years ago
  • 7.21.2                                ...           2 years ago
  • 7.21.1                                ...           2 years ago
  • 7.21.0                                ...           2 years ago
  • 7.20.15                                ...           2 years ago
  • 7.20.13                                ...           2 years ago
  • 7.20.7                                ...           2 years ago
  • 7.20.5                                ...           2 years ago
  • 7.20.3                                ...           2 years ago
  • 7.20.2                                ...           2 years ago
  • 7.20.1                                ...           2 years ago
  • 7.20.0                                ...           2 years ago
  • 7.19.6                                ...           2 years ago
  • 7.19.4                                ...           2 years ago
  • 7.19.3                                ...           2 years ago
  • 7.19.1                                ...           2 years ago
  • 7.19.0                                ...           2 years ago
  • 7.18.13                                ...           2 years ago
  • 7.18.11                                ...           2 years ago
  • 7.18.10                                ...           2 years ago
  • 7.18.9                                ...           2 years ago
  • 7.18.8                                ...           2 years ago
  • 7.18.6                                ...           2 years ago
  • 7.18.5                                ...           2 years ago
  • 7.18.4                                ...           2 years ago
  • 7.18.3                                ...           2 years ago
  • 7.18.0                                ...           2 years ago
  • 7.17.12                                ...           2 years ago
  • 7.17.10                                ...           3 years ago
  • 7.17.9                                ...           3 years ago
  • 7.17.8                                ...           3 years ago
  • 7.17.7                                ...           3 years ago
  • 7.17.3                                ...           3 years ago
  • 7.17.0                                ...           3 years ago
  • 7.16.12                                ...           3 years ago
  • 7.16.10                                ...           3 years ago
  • 7.16.8                                ...           3 years ago
  • 7.16.7                                ...           3 years ago
  • 7.16.6                                ...           3 years ago
  • 7.16.5                                ...           3 years ago
  • 7.16.4                                ...           3 years ago
  • 7.16.3                                ...           3 years ago
  • 7.16.2                                ...           3 years ago
  • 7.16.0                                ...           3 years ago
  • 7.15.8                                ...           3 years ago
  • 7.15.7                                ...           3 years ago
  • 7.15.6                                ...           3 years ago
  • 7.15.5                                ...           3 years ago
  • 7.15.4                                ...           3 years ago
  • 7.15.3                                ...           3 years ago
  • 7.15.2                                ...           3 years ago
  • 7.15.0                                ...           3 years ago
  • 7.14.9                                ...           3 years ago
  • 7.14.8                                ...           3 years ago
  • 7.14.7                                ...           3 years ago
  • 7.14.6                                ...           3 years ago
  • 7.14.5                                ...           3 years ago
  • 7.14.4                                ...           3 years ago
  • 7.14.3                                ...           3 years ago
  • 7.14.2                                ...           4 years ago
  • 7.14.1                                ...           4 years ago
  • 7.14.0                                ...           4 years ago
  • 7.13.16                                ...           4 years ago
  • 7.13.15                                ...           4 years ago
  • 7.13.13                                ...           4 years ago
  • 7.13.12                                ...           4 years ago
  • 7.13.11                                ...           4 years ago
  • 7.13.10                                ...           4 years ago
  • 7.13.9                                ...           4 years ago
  • 7.13.4                                ...           4 years ago
  • 7.13.0                                ...           4 years ago
  • 7.12.17                                ...           4 years ago
  • 7.12.16                                ...           4 years ago
  • 7.12.15                                ...           4 years ago
  • 7.12.14                                ...           4 years ago
  • 7.12.13                                ...           4 years ago
  • 7.12.11                                ...           4 years ago
  • 7.12.10                                ...           4 years ago
  • 7.12.7                                ...           4 years ago
  • 7.12.5                                ...           4 years ago
  • 7.12.3                                ...           4 years ago
  • 7.12.2                                ...           4 years ago
  • 7.12.1                                ...           4 years ago
  • 7.12.0                                ...           4 years ago
  • 7.11.5                                ...           4 years ago
  • 7.11.4                                ...           4 years ago
  • 7.11.3                                ...           4 years ago
  • 7.11.2                                ...           4 years ago
  • 7.11.1                                ...           4 years ago
  • 7.11.0                                ...           4 years ago
  • 7.10.5                                ...           4 years ago
  • 7.10.4                                ...           4 years ago
  • 7.10.3                                ...           4 years ago
  • 7.10.2                                ...           4 years ago
  • 7.10.1                                ...           4 years ago
  • 7.10.0                                ...           4 years ago
  • 7.9.6                                ...           5 years ago
  • 7.9.4                                ...           5 years ago
  • 7.9.3                                ...           5 years ago
  • 7.9.2                                ...           5 years ago
  • 7.9.0                                ...           5 years ago
  • 7.8.8                                ...           5 years ago
  • 7.8.7                                ...           5 years ago
  • 7.8.6                                ...           5 years ago
  • 7.8.4                                ...           5 years ago
  • 7.8.3                                ...           5 years ago
  • 7.8.0                                ...           5 years ago
  • 7.7.7                                ...           5 years ago
  • 7.7.5                                ...           5 years ago
  • 7.7.4                                ...           5 years ago
  • 7.7.3                                ...           5 years ago
  • 7.7.2                                ...           5 years ago
  • 7.7.0                                ...           5 years ago
  • 7.6.4                                ...           5 years ago
  • 7.6.3                                ...           5 years ago
  • 7.6.2                                ...           5 years ago
  • 7.6.0                                ...           5 years ago
  • 7.5.5                                ...           5 years ago
  • 7.5.0                                ...           5 years ago
  • 7.4.5                                ...           5 years ago
  • 7.4.4                                ...           6 years ago
  • 7.4.3                                ...           6 years ago
  • 7.4.2                                ...           6 years ago
  • 7.4.0                                ...           6 years ago
  • 7.3.4                                ...           6 years ago
  • 7.3.3                                ...           6 years ago
  • 7.3.2                                ...           6 years ago
  • 7.3.1                                ...           6 years ago
  • 7.3.0                                ...           6 years ago
  • 7.2.3                                ...           6 years ago
  • 7.2.2                                ...           6 years ago
  • 7.2.0                                ...           6 years ago
  • 7.1.6                                ...           6 years ago
  • 7.1.5                                ...           6 years ago
  • 7.1.3                                ...           6 years ago
  • 7.1.2                                ...           6 years ago
  • 7.1.1                                ...           6 years ago
  • 7.1.0                                ...           6 years ago
  • 7.0.0                                ...           6 years ago
  • 7.0.0-rc.4                                ...           6 years ago
  • 7.0.0-rc.3                                ...           6 years ago
  • 7.0.0-rc.2                                ...           6 years ago
  • 7.0.0-rc.1                                ...           6 years ago
  • 7.0.0-rc.0                                ...           6 years ago
  • 7.0.0-beta.56                                ...           6 years ago
  • 7.0.0-beta.55                                ...           6 years ago
  • 7.0.0-beta.54                                ...           6 years ago
  • 7.0.0-beta.53                                ...           6 years ago
  • 7.0.0-beta.52                                ...           6 years ago
  • 7.0.0-beta.51                                ...           6 years ago
  • 7.0.0-beta.50                                ...           6 years ago
  • 7.0.0-beta.49                                ...           6 years ago
  • 7.0.0-beta.48                                ...           6 years ago
Downloads
Total 15
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 1
Dependencies (0)
None
Dev Dependencies (3)

© 2010 - cnpmjs.org x YWFE | Home | YWFE