|
| 1 | +--- |
| 2 | +# Don't edit this file directly, it was copied using scripts/download-readmes.js: |
| 3 | +id: version-6.x-babylon |
| 4 | +title: babylon |
| 5 | +sidebar_label: babylon |
| 6 | +original_id: babylon |
| 7 | +--- |
| 8 | + |
| 9 | +<p align="center"> |
| 10 | + Babylon is a JavaScript parser used in <a href="https://github.com/babel/babel">Babel</a>. |
| 11 | +</p> |
| 12 | + |
| 13 | +<p align="center"> |
| 14 | + <a href="https://travis-ci.org/babel/babylon"><img alt="Travis Status" src="https://img.shields.io/travis/babel/babylon/master.svg?style=flat&label=travis"></a> |
| 15 | + <a href="https://codecov.io/gh/babel/babylon"><img alt="Codecov Status" src="https://img.shields.io/codecov/c/github/babel/babylon/master.svg?style=flat"></a> |
| 16 | +</p> |
| 17 | + |
| 18 | + - The latest ECMAScript version enabled by default (ES2017). |
| 19 | + - Comment attachment. |
| 20 | + - Support for JSX and Flow. |
| 21 | + - Support for experimental language proposals (accepting PRs for anything at least [stage-0](https://github.com/tc39/proposals/blob/master/stage-0-proposals.md)). |
| 22 | + |
| 23 | +## Credits |
| 24 | + |
| 25 | +Heavily based on [acorn](https://github.com/marijnh/acorn) and [acorn-jsx](https://github.com/RReverser/acorn-jsx), |
| 26 | +thanks to the awesome work of [@RReverser](https://github.com/RReverser) and [@marijnh](https://github.com/marijnh). |
| 27 | + |
| 28 | +Significant diversions are expected to occur in the future such as streaming, EBNF definitions, sweet.js integration, interspatial parsing and more. |
| 29 | + |
| 30 | +## API |
| 31 | + |
| 32 | +### `babylon.parse(code, [options])` |
| 33 | + |
| 34 | +### `babylon.parseExpression(code, [options])` |
| 35 | + |
| 36 | +`parse()` parses the provided `code` as an entire ECMAScript program, while |
| 37 | +`parseExpression()` tries to parse a single Expression with performance in |
| 38 | +mind. When in doubt, use `.parse()`. |
| 39 | + |
| 40 | +### Options |
| 41 | + |
| 42 | +- **allowImportExportEverywhere**: By default, `import` and `export` |
| 43 | + declarations can only appear at a program's top level. Setting this |
| 44 | + option to `true` allows them anywhere where a statement is allowed. |
| 45 | + |
| 46 | +- **allowReturnOutsideFunction**: By default, a return statement at |
| 47 | + the top level raises an error. Set this to `true` to accept such |
| 48 | + code. |
| 49 | + |
| 50 | +- **allowSuperOutsideMethod**: TODO |
| 51 | + |
| 52 | +- **sourceType**: Indicate the mode the code should be parsed in. Can be |
| 53 | + either `"script"` or `"module"`. |
| 54 | + |
| 55 | +- **sourceFilename**: Correlate output AST nodes with their source filename. Useful when generating code and source maps from the ASTs of multiple input files. |
| 56 | + |
| 57 | +- **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. |
| 58 | + |
| 59 | +- **plugins**: Array containing the plugins that you want to enable. |
| 60 | + |
| 61 | +- **strictMode**: TODO |
| 62 | + |
| 63 | +### Output |
| 64 | + |
| 65 | +Babylon generates AST according to [Babel AST format][]. |
| 66 | +It is based on [ESTree spec][] with the following deviations: |
| 67 | + |
| 68 | +> There is now an `estree` plugin which reverts these deviations |
| 69 | +
|
| 70 | +- [Literal][] token is replaced with [StringLiteral][], [NumericLiteral][], [BooleanLiteral][], [NullLiteral][], [RegExpLiteral][] |
| 71 | +- [Property][] token is replaced with [ObjectProperty][] and [ObjectMethod][] |
| 72 | +- [MethodDefinition][] is replaced with [ClassMethod][] |
| 73 | +- [Program][] and [BlockStatement][] contain additional `directives` field with [Directive][] and [DirectiveLiteral][] |
| 74 | +- [ClassMethod][], [ObjectProperty][], and [ObjectMethod][] value property's properties in [FunctionExpression][] is coerced/brought into the main method node. |
| 75 | + |
| 76 | +AST for JSX code is based on [Facebook JSX AST][] with the addition of one node type: |
| 77 | + |
| 78 | +- `JSXText` |
| 79 | + |
| 80 | +[Babel AST format]: https://github.com/babel/babylon/blob/master/ast/spec.md |
| 81 | +[ESTree spec]: https://github.com/estree/estree |
| 82 | + |
| 83 | +[Literal]: https://github.com/estree/estree/blob/master/es5.md#literal |
| 84 | +[Property]: https://github.com/estree/estree/blob/master/es5.md#property |
| 85 | +[MethodDefinition]: https://github.com/estree/estree/blob/master/es2015.md#methoddefinition |
| 86 | + |
| 87 | +[StringLiteral]: https://github.com/babel/babylon/blob/master/ast/spec.md#stringliteral |
| 88 | +[NumericLiteral]: https://github.com/babel/babylon/blob/master/ast/spec.md#numericliteral |
| 89 | +[BooleanLiteral]: https://github.com/babel/babylon/blob/master/ast/spec.md#booleanliteral |
| 90 | +[NullLiteral]: https://github.com/babel/babylon/blob/master/ast/spec.md#nullliteral |
| 91 | +[RegExpLiteral]: https://github.com/babel/babylon/blob/master/ast/spec.md#regexpliteral |
| 92 | +[ObjectProperty]: https://github.com/babel/babylon/blob/master/ast/spec.md#objectproperty |
| 93 | +[ObjectMethod]: https://github.com/babel/babylon/blob/master/ast/spec.md#objectmethod |
| 94 | +[ClassMethod]: https://github.com/babel/babylon/blob/master/ast/spec.md#classmethod |
| 95 | +[Program]: https://github.com/babel/babylon/blob/master/ast/spec.md#programs |
| 96 | +[BlockStatement]: https://github.com/babel/babylon/blob/master/ast/spec.md#blockstatement |
| 97 | +[Directive]: https://github.com/babel/babylon/blob/master/ast/spec.md#directive |
| 98 | +[DirectiveLiteral]: https://github.com/babel/babylon/blob/master/ast/spec.md#directiveliteral |
| 99 | +[FunctionExpression]: https://github.com/babel/babylon/blob/master/ast/spec.md#functionexpression |
| 100 | + |
| 101 | +[Facebook JSX AST]: https://github.com/facebook/jsx/blob/master/AST.md |
| 102 | + |
| 103 | +### Semver |
| 104 | + |
| 105 | +Babylon follows semver in most situations. The only thing to note is that some spec-compliancy bug fixes may be released under patch versions. |
| 106 | + |
| 107 | +For example: We push a fix to early error on something like [#107](https://github.com/babel/babylon/pull/107) - multiple default exports per file. That would be considered a bug fix even though it would cause a build to fail. |
| 108 | + |
| 109 | +### Example |
| 110 | + |
| 111 | +```javascript |
| 112 | +require("babylon").parse("code", { |
| 113 | + // parse in strict mode and allow module declarations |
| 114 | + sourceType: "module", |
| 115 | + |
| 116 | + plugins: [ |
| 117 | + // enable jsx and flow syntax |
| 118 | + "jsx", |
| 119 | + "flow" |
| 120 | + ] |
| 121 | +}); |
| 122 | +``` |
| 123 | + |
| 124 | +### Plugins |
| 125 | + |
| 126 | + - `estree` |
| 127 | + - `jsx` |
| 128 | + - `flow` |
| 129 | + - `doExpressions` |
| 130 | + - `objectRestSpread` |
| 131 | + - `decorators` (Based on an outdated version of the Decorators proposal. Will be removed in a future version of `Babylon`) |
| 132 | + - `classProperties` |
| 133 | + - `exportExtensions` |
| 134 | + - `asyncGenerators` |
| 135 | + - `functionBind` |
| 136 | + - `functionSent` |
| 137 | + - `dynamicImport` |
| 138 | + - `templateInvalidEscapes` |
| 139 | + |
0 commit comments