|
1 |
| -# Acorn |
| 1 | +# Acorn-JSX |
2 | 2 |
|
3 |
| -[](https://travis-ci.org/marijnh/acorn) |
| 3 | +[](https://travis-ci.org/RReverser/acorn-jsx) |
4 | 4 |
|
5 |
| -This is modification of [Acorn](http://marijnhaverbeke.nl/acorn/) - a tiny, fast JavaScript parser, written completely in JavaScript. |
| 5 | +This is plugin for [Acorn](http://marijnhaverbeke.nl/acorn/) - a tiny, fast JavaScript parser, written completely in JavaScript. |
6 | 6 |
|
7 |
| -It was forked to create experimental alternative, faster [React.js JSX](http://facebook.github.io/react/docs/jsx-in-depth.html) parser by integrating pieces of code from official parser, modified to match Acorn's parsing logic. |
| 7 | +It was created as an experimental alternative, faster [React.js JSX](http://facebook.github.io/react/docs/jsx-in-depth.html) parser. |
8 | 8 |
|
9 | 9 | According to [benchmarks](https://github.com/RReverser/acorn-jsx/blob/master/test/bench.html), Acorn-JSX is 2x faster than official [Esprima-based parser](https://github.com/facebook/esprima) when location tracking is turned on in both (call it "source maps enabled mode"). At the same time, it consumes all the ES6+JSX syntax that can be consumed by Esprima-FB (this is proved by [official tests](https://github.com/RReverser/acorn-jsx/blob/master/test/tests-jsx.js)).
|
10 | 10 |
|
| 11 | +**UPDATE [14-Apr-2015]**: Facebook implementation started [deprecation process](https://github.com/facebook/esprima/issues/111) in favor of Acorn + Acorn-JSX + Babel for parsing and transpiling JSX syntax. |
| 12 | + |
11 | 13 | ## Transpiler
|
12 | 14 |
|
13 | 15 | Please note that this tool only parses source code to JSX AST, which is useful for various language tools and services. If you want to transpile your code to regular ES5-compliant JavaScript with source map, check out the [babel transpiler](https://babeljs.io/) which uses `acorn-jsx` under the hood.
|
14 | 16 |
|
15 |
| -## Installation |
16 |
| - |
17 |
| -The easiest way to install acorn is with [`npm`][npm]. |
18 |
| - |
19 |
| -[npm]: http://npmjs.org |
20 |
| - |
21 |
| -```sh |
22 |
| -npm install acorn |
23 |
| -``` |
| 17 | +## Usage |
24 | 18 |
|
25 |
| -Alternately, download the source. |
| 19 | +You can use module directly in order to get Acorn instance with plugin installed: |
26 | 20 |
|
27 |
| -```sh |
28 |
| -git clone https://github.com/marijnh/acorn.git |
| 21 | +```javascript |
| 22 | +var acorn = require('acorn-jsx'); |
29 | 23 | ```
|
30 | 24 |
|
31 |
| -## Components |
32 |
| - |
33 |
| -When run in a CommonJS (node.js) or AMD environment, exported values |
34 |
| -appear in the interfaces exposed by the individual files, as usual. |
35 |
| -When loaded in the browser (Acorn works in any JS-enabled browser more |
36 |
| -recent than IE5) without any kind of module management, a single |
37 |
| -global object `acorn` will be defined, and all the exported properties |
38 |
| -will be added to that. |
39 |
| - |
40 |
| -### acorn.js |
41 |
| - |
42 |
| -This file contains the actual parser (and is what you get when you |
43 |
| -`require("acorn")` in node.js). |
44 |
| - |
45 |
| -**parse**`(input, options)` is used to parse a JavaScript program. |
46 |
| -The `input` parameter is a string, `options` can be undefined or an |
47 |
| -object setting some of the options listed below. The return value will |
48 |
| -be an abstract syntax tree object as specified by the |
49 |
| -[Mozilla Parser API][mozapi]. |
50 |
| - |
51 |
| -When encountering a syntax error, the parser will raise a |
52 |
| -`SyntaxError` object with a meaningful message. The error object will |
53 |
| -have a `pos` property that indicates the character offset at which the |
54 |
| -error occurred, and a `loc` object that contains a `{line, column}` |
55 |
| -object referring to that same position. |
56 |
| - |
57 |
| -[mozapi]: https://developer.mozilla.org/en-US/docs/SpiderMonkey/Parser_API |
58 |
| - |
59 |
| -- **ecmaVersion**: Indicates the ECMAScript version to parse. Must be |
60 |
| - either 3, 5, or 6. This influences support for strict mode, the set |
61 |
| - of reserved words, and support for new syntax features. Default is 5. |
62 |
| - |
63 |
| -- **strictSemicolons**: If `true`, prevents the parser from doing |
64 |
| - automatic semicolon insertion, and statements that do not end with |
65 |
| - a semicolon will generate an error. Defaults to `false`. |
66 |
| - |
67 |
| -- **allowTrailingCommas**: If `false`, the parser will not allow |
68 |
| - trailing commas in array and object literals. Default is `true`. |
69 |
| - |
70 |
| -- **forbidReserved**: If `true`, using a reserved word will generate |
71 |
| - an error. Defaults to `false`. When given the value `"everywhere"`, |
72 |
| - reserved words and keywords can also not be used as property names |
73 |
| - (as in Internet Explorer's old parser). |
74 |
| - |
75 |
| -- **allowReturnOutsideFunction**: By default, a return statement at |
76 |
| - the top level raises an error. Set this to `true` to accept such |
77 |
| - code. |
78 |
| - |
79 |
| -- **allowImportExportEverywhere**: By default, `import` and `export` |
80 |
| - declarations can only appear at a program's top level. Setting this |
81 |
| - option to `true` allows them anywhere where a statement is allowed. |
82 |
| - |
83 |
| -- **allowHashBang**: When this is enabled (off by default), if the |
84 |
| - code starts with the characters `#!` (as in a shellscript), the |
85 |
| - first line will be treated as a comment. |
86 |
| - |
87 |
| -- **locations**: When `true`, each node has a `loc` object attached |
88 |
| - with `start` and `end` subobjects, each of which contains the |
89 |
| - one-based line and zero-based column numbers in `{line, column}` |
90 |
| - form. Default is `false`. |
91 |
| - |
92 |
| -- **onToken**: If a function is passed for this option, each found |
93 |
| - token will be passed in same format as `tokenize()` returns. |
94 |
| - |
95 |
| - If array is passed, each found token is pushed to it. |
96 |
| - |
97 |
| - Note that you are not allowed to call the parser from the |
98 |
| - callback—that will corrupt its internal state. |
99 |
| - |
100 |
| -- **onComment**: If a function is passed for this option, whenever a |
101 |
| - comment is encountered the function will be called with the |
102 |
| - following parameters: |
103 |
| - |
104 |
| - - `block`: `true` if the comment is a block comment, false if it |
105 |
| - is a line comment. |
106 |
| - - `text`: The content of the comment. |
107 |
| - - `start`: Character offset of the start of the comment. |
108 |
| - - `end`: Character offset of the end of the comment. |
109 |
| - |
110 |
| - When the `locations` options is on, the `{line, column}` locations |
111 |
| - of the comment’s start and end are passed as two additional |
112 |
| - parameters. |
113 |
| - |
114 |
| - If array is passed for this option, each found comment is pushed |
115 |
| - to it as object in Esprima format: |
116 |
| - |
117 |
| - ```javascript |
118 |
| - { |
119 |
| - "type": "Line" | "Block", |
120 |
| - "value": "comment text", |
121 |
| - "range": ..., |
122 |
| - "loc": ... |
123 |
| - } |
124 |
| - ``` |
125 |
| - |
126 |
| - Note that you are not allowed to call the parser from the |
127 |
| - callback—that will corrupt its internal state. |
128 |
| - |
129 |
| -- **ranges**: Nodes have their start and end characters offsets |
130 |
| - recorded in `start` and `end` properties (directly on the node, |
131 |
| - rather than the `loc` object, which holds line/column data. To also |
132 |
| - add a [semi-standardized][range] "range" property holding a |
133 |
| - `[start, end]` array with the same numbers, set the `ranges` option |
134 |
| - to `true`. |
135 |
| - |
136 |
| -- **program**: It is possible to parse multiple files into a single |
137 |
| - AST by passing the tree produced by parsing the first file as the |
138 |
| - `program` option in subsequent parses. This will add the toplevel |
139 |
| - forms of the parsed file to the "Program" (top) node of an existing |
140 |
| - parse tree. |
141 |
| - |
142 |
| -- **sourceFile**: When the `locations` option is `true`, you can pass |
143 |
| - this option to add a `source` attribute in every node’s `loc` |
144 |
| - object. Note that the contents of this option are not examined or |
145 |
| - processed in any way; you are free to use whatever format you |
146 |
| - choose. |
147 |
| - |
148 |
| -- **directSourceFile**: Like `sourceFile`, but a `sourceFile` property |
149 |
| - will be added directly to the nodes, rather than the `loc` object. |
150 |
| - |
151 |
| -- **preserveParens**: If this option is `true`, parenthesized expressions |
152 |
| - are represented by (non-standard) `ParenthesizedExpression` nodes |
153 |
| - that have a single `expression` property containing the expression |
154 |
| - inside parentheses. |
155 |
| - |
156 |
| -[range]: https://bugzilla.mozilla.org/show_bug.cgi?id=745678 |
157 |
| - |
158 |
| -**parseExpressionAt**`(input, offset, options)` will parse a single |
159 |
| -expression in a string, and return its AST. It will not complain if |
160 |
| -there is more of the string left after the expression. |
161 |
| - |
162 |
| -**getLineInfo**`(input, offset)` can be used to get a `{line, |
163 |
| -column}` object for a given program string and character offset. |
164 |
| - |
165 |
| -**tokenize**`(input, options)` exports a primitive interface to |
166 |
| -Acorn's tokenizer. The function takes an input string and options |
167 |
| -similar to `parse` (though only some options are meaningful here), and |
168 |
| -returns a function that can be called repeatedly to read a single |
169 |
| -token, and returns a `{start, end, type, value}` object (with added |
170 |
| -`loc` property when the `locations` option is enabled and `range` |
171 |
| -property when the `ranges` option is enabled). |
172 |
| - |
173 |
| -In ES6 environment, returned result can be used as any other protocol-compliant iterable: |
| 25 | +Or you can use `inject.js` for injecting plugin into your own version of Acorn like following: |
174 | 26 |
|
175 | 27 | ```javascript
|
176 |
| -for (let token of acorn.tokenize(str)) { |
177 |
| - // iterate over the tokens |
178 |
| -} |
179 |
| - |
180 |
| -// transform code to array of tokens: |
181 |
| -var tokens = [...acorn.tokenize(str)]; |
| 28 | +var acorn = require('acorn-jsx/inject')(require('./custom-acorn')); |
182 | 29 | ```
|
183 | 30 |
|
184 |
| -**tokTypes** holds an object mapping names to the token type objects |
185 |
| -that end up in the `type` properties of tokens. |
186 |
| - |
187 |
| -#### Note on using with [Escodegen][escodegen] |
188 |
| - |
189 |
| -Escodegen supports generating comments from AST, attached in |
190 |
| -Esprima-specific format. In order to simulate same format in |
191 |
| -Acorn, consider following example: |
| 31 | +Then, use `plugins` option whenever you need to support JSX while parsing: |
192 | 32 |
|
193 | 33 | ```javascript
|
194 |
| -var comments = [], tokens = []; |
195 |
| - |
196 |
| -var ast = acorn.parse('var x = 42; // answer', { |
197 |
| - // collect ranges for each node |
198 |
| - ranges: true, |
199 |
| - // collect comments in Esprima's format |
200 |
| - onComment: comments, |
201 |
| - // collect token ranges |
202 |
| - onToken: tokens |
| 34 | +var ast = acorn.parse(code, { |
| 35 | + plugins: { jsx: true } |
203 | 36 | });
|
204 |
| - |
205 |
| -// attach comments using collected information |
206 |
| -escodegen.attachComments(ast, comments, tokens); |
207 |
| - |
208 |
| -// generate code |
209 |
| -console.log(escodegen.generate(ast, {comment: true})); |
210 |
| -// > 'var x = 42; // answer' |
211 | 37 | ```
|
212 | 38 |
|
213 |
| -[escodegen]: https://github.com/Constellation/escodegen |
214 |
| - |
215 |
| -#### Using Acorn in an environment with a Content Security Policy |
216 |
| - |
217 |
| -Some contexts, such as Chrome Web Apps, disallow run-time code evaluation. |
218 |
| -Acorn uses `new Function` to generate fast functions that test whether |
219 |
| -a word is in a given set, and will trigger a security error when used |
220 |
| -in a context with such a |
221 |
| -[Content Security Policy](http://www.html5rocks.com/en/tutorials/security/content-security-policy/#eval-too) |
222 |
| -(see [#90](https://github.com/marijnh/acorn/issues/90) and |
223 |
| -[#123](https://github.com/marijnh/acorn/issues/123)). |
224 |
| - |
225 |
| -The `bin/without_eval` script can be used to generate a version of |
226 |
| -`acorn.js` that has the generated code inlined, and can thus run |
227 |
| -without evaluating anything. In versions of this library downloaded |
228 |
| -from NPM, this script will be available as `acorn_csp.js`. |
229 |
| - |
230 |
| -### acorn_loose.js ### |
231 |
| - |
232 |
| -This file implements an error-tolerant parser. It exposes a single |
233 |
| -function. |
234 |
| - |
235 |
| -**parse_dammit**`(input, options)` takes the same arguments and |
236 |
| -returns the same syntax tree as the `parse` function in `acorn.js`, |
237 |
| -but never raises an error, and will do its best to parse syntactically |
238 |
| -invalid code in as meaningful a way as it can. It'll insert identifier |
239 |
| -nodes with name `"✖"` as placeholders in places where it can't make |
240 |
| -sense of the input. Depends on `acorn.js`, because it uses the same |
241 |
| -tokenizer. |
242 |
| - |
243 |
| -### util/walk.js ### |
244 |
| - |
245 |
| -Implements an abstract syntax tree walker. Will store its interface in |
246 |
| -`acorn.walk` when used without a module system. |
247 |
| - |
248 |
| -**simple**`(node, visitors, base, state)` does a 'simple' walk over |
249 |
| -a tree. `node` should be the AST node to walk, and `visitors` an |
250 |
| -object with properties whose names correspond to node types in the |
251 |
| -[Mozilla Parser API][mozapi]. The properties should contain functions |
252 |
| -that will be called with the node object and, if applicable the state |
253 |
| -at that point. The last two arguments are optional. `base` is a walker |
254 |
| -algorithm, and `state` is a start state. The default walker will |
255 |
| -simply visit all statements and expressions and not produce a |
256 |
| -meaningful state. (An example of a use of state it to track scope at |
257 |
| -each point in the tree.) |
258 |
| - |
259 |
| -**ancestor**`(node, visitors, base, state)` does a 'simple' walk over |
260 |
| -a tree, building up an array of ancestor nodes (including the current node) |
261 |
| -and passing the array to callbacks in the `state` parameter. |
262 |
| - |
263 |
| -**recursive**`(node, state, functions, base)` does a 'recursive' |
264 |
| -walk, where the walker functions are responsible for continuing the |
265 |
| -walk on the child nodes of their target node. `state` is the start |
266 |
| -state, and `functions` should contain an object that maps node types |
267 |
| -to walker functions. Such functions are called with `(node, state, c)` |
268 |
| -arguments, and can cause the walk to continue on a sub-node by calling |
269 |
| -the `c` argument on it with `(node, state)` arguments. The optional |
270 |
| -`base` argument provides the fallback walker functions for node types |
271 |
| -that aren't handled in the `functions` object. If not given, the |
272 |
| -default walkers will be used. |
273 |
| - |
274 |
| -**make**`(functions, base)` builds a new walker object by using the |
275 |
| -walker functions in `functions` and filling in the missing ones by |
276 |
| -taking defaults from `base`. |
277 |
| - |
278 |
| -**findNodeAt**`(node, start, end, test, base, state)` tries to |
279 |
| -locate a node in a tree at the given start and/or end offsets, which |
280 |
| -satisfies the predicate `test`. `start` end `end` can be either `null` |
281 |
| -(as wildcard) or a number. `test` may be a string (indicating a node |
282 |
| -type) or a function that takes `(nodeType, node)` arguments and |
283 |
| -returns a boolean indicating whether this node is interesting. `base` |
284 |
| -and `state` are optional, and can be used to specify a custom walker. |
285 |
| -Nodes are tested from inner to outer, so if two nodes match the |
286 |
| -boundaries, the inner one will be preferred. |
287 |
| - |
288 |
| -**findNodeAround**`(node, pos, test, base, state)` is a lot like |
289 |
| -`findNodeAt`, but will match any node that exists 'around' (spanning) |
290 |
| -the given position. |
291 |
| - |
292 |
| -**findNodeAfter**`(node, pos, test, base, state)` is similar to |
293 |
| -`findNodeAround`, but will match all nodes *after* the given position |
294 |
| -(testing outer nodes before inner nodes). |
295 |
| - |
296 |
| -## Command line interface |
297 |
| - |
298 |
| -The `bin/acorn` utility can be used to parse a file from the command |
299 |
| -line. It accepts as arguments its input file and the following |
300 |
| -options: |
301 |
| - |
302 |
| -- `--ecma3|--ecma5|--ecma6`: Sets the ECMAScript version to parse. Default is |
303 |
| - version 5. |
304 |
| - |
305 |
| -- `--strictSemicolons`: Prevents the parser from doing automatic |
306 |
| - semicolon insertion. Statements that do not end in semicolons will |
307 |
| - generate an error. |
308 |
| - |
309 |
| -- `--locations`: Attaches a "loc" object to each node with "start" and |
310 |
| - "end" subobjects, each of which contains the one-based line and |
311 |
| - zero-based column numbers in `{line, column}` form. |
312 |
| - |
313 |
| -- `--compact`: No whitespace is used in the AST output. |
314 |
| - |
315 |
| -- `--silent`: Do not output the AST, just return the exit status. |
316 |
| - |
317 |
| -- `--help`: Print the usage information and quit. |
| 39 | +## License |
318 | 40 |
|
319 |
| -The utility spits out the syntax tree as JSON data. |
| 41 | +This plugin is issued under the [MIT license](./LICENSE). |
0 commit comments