πŸ“ƒ A JavaScript parser
JavaScript Makefile
Permalink
Failed to load latest commit information.
.github Correct repo [skip ci] Nov 1, 2016
ast AST spec: fix casing of `RegExpLiteral` (#318) [skip ci] Jan 20, 2017
bin Update eslint-config-babel to the latest version πŸš€ (#299) Jan 15, 2017
scripts Run tests of flow with babylon (#225) Nov 14, 2016
src Disallow import type { type a } from … (#305) Jan 17, 2017
test Disallow import type { type a } from … (#305) Jan 17, 2017
.babelrc User external-helpers (#254) Jan 2, 2017
.editorconfig Add .gitattributes and .editorconfig for LF line endings (#179) Oct 15, 2016
.eslintignore Readd missin .eslinignore for IDEs Oct 15, 2016
.eslintrc Remove kcheck (#173) Oct 14, 2016
.flowconfig Disallow import type { type a } from … (#305) Jan 17, 2017
.gitattributes Fix .gitattributes line ending setting (#191) Oct 17, 2016
.gitignore User external-helpers (#254) Jan 2, 2017
.travis.yml Fix syntax error in .travis.yml Jan 10, 2017
AUTHORS add acorn license - cc @RReverser Jul 30, 2015
CHANGELOG.md Changelog 6.15.0 (#283) Jan 10, 2017
CODE_OF_CONDUCT.md first commit Mar 10, 2016
CONTRIBUTING.md Fix Contributing guidelines [skip ci] Sep 15, 2016
LICENSE add acorn license - cc @RReverser Jul 30, 2015
Makefile Run tests of flow with babylon (#225) Nov 14, 2016
README.md Add some test coverage for decorators stage-0 plugin (#250) Jan 5, 2017
codecov.yml Enable partial code coverage (#109) Sep 6, 2016
package.json chore(package): update flow-bin to version 0.38.0 (#313) Jan 18, 2017
rollup.config.js Use rollup for bundling to speed up startup time (#190) Oct 26, 2016
yarn.lock Update yarn.lock Jan 18, 2017

README.md

babylon

Babylon is a JavaScript parser used in Babel.

Travis Status Codecov Status

  • The latest ECMAScript version enabled by default (ES2017).
  • Comment attachment.
  • Support for JSX and Flow.
  • 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.

Significant diversions are expected to occur in the future such as streaming, EBNF definitions, sweet.js integration, interspatial parsing and more.

API

babylon.parse(code, [options])

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.

  • 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 either "script" or "module".

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

  • plugins: Array containing the plugins that you want to enable.

Output

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

AST for JSX code is based on Facebook JSX AST with the addition of one node type:

  • JSXText

Semver

Babylon 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("babylon").parse("code", {
  // parse in strict mode and allow module declarations
  sourceType: "module",

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

Plugins

  • jsx
  • flow
  • doExpressions
  • objectRestSpread
  • decorators (Based on an outdated version of the Decorators proposal. Will be removed in a future version of Babylon)
  • classProperties
  • exportExtensions
  • asyncGenerators
  • functionBind
  • functionSent
  • dynamicImport