1 | <h1 align="center">TypeScript ESTree</h1>
|
2 |
|
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 |
|
5 | <p align="center">
|
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>
|
7 | <a href="https://github.com/JamesHenry/typescript-estree/blob/master/LICENSE"><img src="https://img.shields.io/npm/l/typescript-estree.svg?style=flat-square" alt="GitHub license" /></a>
|
8 | <a href="https://www.npmjs.com/package/typescript-estree"><img src="https://img.shields.io/npm/v/typescript-estree.svg?style=flat-square" alt="NPM Version" /></a>
|
9 | <a href="https://www.npmjs.com/package/typescript-estree"><img src="https://img.shields.io/npm/dt/typescript-estree.svg?style=flat-square" alt="NPM Downloads" /></a>
|
10 | <a href="http://commitizen.github.io/cz-cli/"><img src="https://img.shields.io/badge/commitizen-friendly-brightgreen.svg" alt="Commitizen friendly" /></a>
|
11 | <a href="https://github.com/semantic-release/semantic-release"><img src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square" alt="semantic-release" /></a>
|
12 | </p>
|
13 |
|
14 | <br>
|
15 |
|
16 | ## About
|
17 |
|
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-compatible AST.
|
19 |
|
20 | In fact, it is already used within these hyper-popular open-source projects to power their TypeScript support:
|
21 |
|
22 | - [ESLint](https://eslint.org), the pluggable linting utility for JavaScript and JSX
|
23 | - See [typescript-eslint-parser](https://github.com/eslint/typescript-eslint-parser) for more details
|
24 | - [Prettier](https://prettier.io), an opinionated code formatter
|
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 | const ast = parser.parse(code, {
|
81 | range: true,
|
82 | loc: true
|
83 | });
|
84 | ```
|
85 |
|
86 | ### version
|
87 |
|
88 | Exposes the current version of typescript-estree as specified in package.json.
|
89 |
|
90 | Example usage:
|
91 |
|
92 | ```javascript
|
93 | const parser = require('typescript-estree');
|
94 | const version = parser.version;
|
95 | ```
|
96 |
|
97 | ### AST_NODE_TYPES
|
98 |
|
99 | Exposes an object that contains the AST node types produced by the parser.
|
100 |
|
101 | Example usage:
|
102 |
|
103 | ```javascript
|
104 | const parser = require('typescript-estree');
|
105 | const astNodeTypes = parser.AST_NODE_TYPES;
|
106 | ```
|
107 |
|
108 | ## Supported TypeScript Version
|
109 |
|
110 | We will always endeavor to support the latest stable version of TypeScript.
|
111 |
|
112 | The version of TypeScript currently supported by this parser is `~3.2.1`. This is reflected in the `devDependency` requirement within the package.json file, and it is what the tests will be run against. We have an open `peerDependency` requirement in order to allow for experimentation on newer/beta versions of TypeScript.
|
113 |
|
114 | If you use a non-supported version of TypeScript, the parser will log a warning to the console.
|
115 |
|
116 | **Please ensure that you are using a supported version before submitting any issues/bug reports.**
|
117 |
|
118 | ## Reporting Issues
|
119 |
|
120 | Please check the current list of open and known issues and ensure the issue has not been reported before. When creating a new issue provide as much information about your environment as possible. This includes:
|
121 |
|
122 | - TypeScript version
|
123 | - The `typescript-estree` version
|
124 |
|
125 | ## AST Alignment Tests
|
126 |
|
127 | A couple of years after work on this parser began, the TypeScript Team at Microsoft began [officially supporting TypeScript parsing via Babel](https://blogs.msdn.microsoft.com/typescript/2018/08/27/typescript-and-babel-7/).
|
128 |
|
129 | I work closely with TypeScript Team and we are gradually aliging the AST of this project with the one produced by Babel's parser. To that end, I have created a full test harness to compare the ASTs of the two projects which runs on every PR, please see the code for more details.
|
130 |
|
131 | ## Build/Test Commands
|
132 |
|
133 | - `npm test` - run all tests
|
134 | - `npm run unit-tests` - run only unit tests
|
135 | - `npm run ast-alignment-tests` - run only Babylon AST alignment tests
|
136 |
|
137 | ## License
|
138 |
|
139 | TypeScript ESTree inherits from the the original TypeScript ESLint Parser license, as the majority of the work began there. It is licensed under a permissive BSD 2-clause license.
|