|
1 | 1 | <h1 align="center">TypeScript ESTree</h1>
|
2 | 2 |
|
3 |
| -<p align="center">A parser that converts TypeScript source code into an ESTree-compatible form: https://github.com/estree/estree</p> |
| 3 | +<p align="center">A parser that converts TypeScript source code into an <a href="https://github.com/estree/estree">ESTree</a>-compatible form</p> |
4 | 4 |
|
5 | 5 | <p align="center">
|
6 | 6 | <a href="https://travis-ci.org/JamesHenry/typescript-estree"><img src="https://img.shields.io/travis/JamesHenry/typescript-estree.svg?style=flat-square" alt="Travis"/></a>
|
|
13 | 13 |
|
14 | 14 | <br>
|
15 | 15 |
|
16 |
| -## Usage |
| 16 | +## About |
17 | 17 |
|
18 |
| -This parser is somewhat generic and robust, it could be used to power any use-case which requires taking TypeScript source code and producing an ESTree-compatiable AST. |
| 18 | +This parser is somewhat generic and robust, and could be used to power any use-case which requires taking TypeScript source code and producing an ESTree-compatiable AST. |
19 | 19 |
|
20 | 20 | In fact, it is already used within these hyper-popular open-source projects to power their TypeScript support:
|
21 | 21 |
|
22 | 22 | - [ESLint](https://eslint.org), the pluggable linting utility for JavaScript and JSX
|
23 | 23 | - See [typescript-eslint-parser](https://github.com/eslint/typescript-eslint-parser) for more details
|
24 | 24 | - [Prettier](https://prettier.io), an opinionated code formatter
|
25 | 25 |
|
| 26 | +## Installation |
| 27 | + |
| 28 | +``` |
| 29 | +npm install --save typescript-estree |
| 30 | +``` |
| 31 | + |
| 32 | +## API |
| 33 | + |
| 34 | +### parse(code, options) |
| 35 | + |
| 36 | +Parses the given string of code with the options provided and returns an ESTree-compatible AST. The options object has the following properties: |
| 37 | + |
| 38 | +```javascript |
| 39 | +{ |
| 40 | + // attach range information to each node |
| 41 | + range: false, |
| 42 | + |
| 43 | + // attach line/column location information to each node |
| 44 | + loc: false, |
| 45 | + |
| 46 | + // create a top-level tokens array containing all tokens |
| 47 | + tokens: false, |
| 48 | + |
| 49 | + // create a top-level comments array containing all comments |
| 50 | + comment: false, |
| 51 | + |
| 52 | + // enable parsing JSX. For more details, see https://www.typescriptlang.org/docs/handbook/jsx.html |
| 53 | + jsx: false, |
| 54 | + |
| 55 | + /* |
| 56 | + * The JSX AST changed the node type for string literals |
| 57 | + * inside a JSX Element from `Literal` to `JSXText`. |
| 58 | + * When value is `true`, these nodes will be parsed as type `JSXText`. |
| 59 | + * When value is `false`, these nodes will be parsed as type `Literal`. |
| 60 | + */ |
| 61 | + useJSXTextNode: false, |
| 62 | + |
| 63 | + // Cause the parser to error if it encounters an unknown AST node type (useful for testing) |
| 64 | + errorOnUnknownASTType: false, |
| 65 | + |
| 66 | + /* |
| 67 | + * Allows overriding of function used for logging. |
| 68 | + * When value is `false`, no logging will occur. |
| 69 | + * When value is not provided, `console.log()` will be used. |
| 70 | + */ |
| 71 | + loggerFn: undefined |
| 72 | +} |
| 73 | +``` |
| 74 | + |
| 75 | +Example usage: |
| 76 | + |
| 77 | +```javascript |
| 78 | +const parser = require('typescript-estree'); |
| 79 | +const code = "const hello: string = 'world';"; |
| 80 | +// Optional second options argument with the following default settings |
| 81 | +const ast = parser.parse(code, { |
| 82 | + range: true, |
| 83 | + loc: true |
| 84 | +}); |
| 85 | +``` |
| 86 | + |
| 87 | +### version |
| 88 | + |
| 89 | +Exposes the current version of typescript-estree as specified in package.json. |
| 90 | + |
| 91 | +Example usage: |
| 92 | + |
| 93 | +```javascript |
| 94 | +const parser = require('typescript-estree'); |
| 95 | +const version = parser.version; |
| 96 | +``` |
| 97 | + |
| 98 | +### AST_NODE_TYPES |
| 99 | + |
| 100 | +Exposes an object that contains the AST node types produced by the parser. |
| 101 | + |
| 102 | +Example usage: |
| 103 | + |
| 104 | +```javascript |
| 105 | +const parser = require('typescript-estree'); |
| 106 | +const astNodeTypes = parser.AST_NODE_TYPES; |
| 107 | +``` |
| 108 | + |
26 | 109 | ## Supported TypeScript Version
|
27 | 110 |
|
28 | 111 | We will always endeavor to support the latest stable version of TypeScript.
|
|
0 commit comments