|
13 | 13 |
|
14 | 14 | <br>
|
15 | 15 |
|
16 |
| -## Usage |
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. |
| 16 | +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 | 17 |
|
20 | 18 | In fact, it is already used within these hyper-popular open-source projects to power their TypeScript support:
|
21 | 19 |
|
22 | 20 | - [ESLint](https://eslint.org), the pluggable linting utility for JavaScript and JSX
|
23 | 21 | - See [typescript-eslint-parser](https://github.com/eslint/typescript-eslint-parser) for more details
|
24 | 22 | - [Prettier](https://prettier.io), an opinionated code formatter
|
25 | 23 |
|
| 24 | +## Usage |
| 25 | + |
| 26 | +Install: |
| 27 | + |
| 28 | +``` |
| 29 | +npm i typescript-estree --save |
| 30 | +``` |
| 31 | + |
| 32 | +And in your Node.js code: |
| 33 | + |
| 34 | +```javascript |
| 35 | +const parser = require("typescript-estree"); |
| 36 | +const ast = parser.parse(code); |
| 37 | +``` |
| 38 | + |
| 39 | +There is a second argument to `parse()` that allows you to specify various options: |
| 40 | + |
| 41 | +```javascript |
| 42 | +const parser = require("typescript-estree"); |
| 43 | +// Optional second options argument with the following default settings |
| 44 | +const ast = parser.parse(code, { |
| 45 | + |
| 46 | + // attach range information to each node |
| 47 | + range: false, |
| 48 | + |
| 49 | + // attach line/column location information to each node |
| 50 | + loc: false, |
| 51 | + |
| 52 | + // create a top-level tokens array containing all tokens |
| 53 | + tokens: false, |
| 54 | + |
| 55 | + // create a top-level comments array containing all comments |
| 56 | + comment: false, |
| 57 | + |
| 58 | + // enable parsing JSX. For more details, see https://www.typescriptlang.org/docs/handbook/jsx.html |
| 59 | + jsx: false, |
| 60 | + |
| 61 | + tolerant: false, |
| 62 | + |
| 63 | + source: null, |
| 64 | + |
| 65 | + /* |
| 66 | + * The JSX AST changed the node type for string literals |
| 67 | + * inside a JSX Element from `Literal` to `JSXText`. |
| 68 | + * When value is `true`, these nodes will be parsed as type `JSXText`. |
| 69 | + * When value is `false`, these nodes will be parsed as type `Literal`. |
| 70 | + */ |
| 71 | + useJSXTextNode: false, |
| 72 | + |
| 73 | + // Cause the parser to error if it encounters an unknown AST node type (useful for testing) |
| 74 | + errorOnUnknownASTType: false, |
| 75 | + |
| 76 | + /* |
| 77 | + * Allows overriding of function used for logging. |
| 78 | + * When value is `false`, no logging will occur. |
| 79 | + * When value is not provided, `console.log()` will be used. |
| 80 | + */ |
| 81 | + loggerFn: undefined |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
26 | 85 | ## Supported TypeScript Version
|
27 | 86 |
|
28 | 87 | We will always endeavor to support the latest stable version of TypeScript.
|
|
0 commit comments