Skip to content
This repository was archived by the owner on Jan 14, 2019. It is now read-only.

Commit 4ca858c

Browse files
author
Kai Cataldo
committed
feat: document API and standardize config options
1 parent 928b087 commit 4ca858c

File tree

11 files changed

+428
-376
lines changed

11 files changed

+428
-376
lines changed

README.md

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<h1 align="center">TypeScript ESTree</h1>
22

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>
44

55
<p align="center">
66
<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,16 +13,99 @@
1313

1414
<br>
1515

16-
## Usage
16+
## About
1717

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.
1919

2020
In fact, it is already used within these hyper-popular open-source projects to power their TypeScript support:
2121

2222
- [ESLint](https://eslint.org), the pluggable linting utility for JavaScript and JSX
2323
- See [typescript-eslint-parser](https://github.com/eslint/typescript-eslint-parser) for more details
2424
- [Prettier](https://prettier.io), an opinionated code formatter
2525

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+
26109
## Supported TypeScript Version
27110

28111
We will always endeavor to support the latest stable version of TypeScript.

parser.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,8 @@ function resetExtra() {
3535
loc: false,
3636
comment: false,
3737
comments: [],
38-
tolerant: false,
39-
errors: [],
4038
strict: false,
41-
ecmaFeatures: {},
39+
jsx: false,
4240
useJSXTextNode: false,
4341
log: console.log
4442
};
@@ -67,31 +65,27 @@ function generateAST(code, options) {
6765
extra.range = typeof options.range === 'boolean' && options.range;
6866
extra.loc = typeof options.loc === 'boolean' && options.loc;
6967

70-
if (extra.loc && options.source !== null && options.source !== undefined) {
71-
extra.source = toString(options.source);
72-
}
73-
7468
if (typeof options.tokens === 'boolean' && options.tokens) {
7569
extra.tokens = [];
7670
}
71+
7772
if (typeof options.comment === 'boolean' && options.comment) {
7873
extra.comment = true;
7974
extra.comments = [];
8075
}
81-
if (typeof options.tolerant === 'boolean' && options.tolerant) {
82-
extra.errors = [];
83-
}
8476

85-
if (options.ecmaFeatures && typeof options.ecmaFeatures === 'object') {
86-
// pass through jsx option
87-
extra.ecmaFeatures.jsx = options.ecmaFeatures.jsx;
77+
if (typeof options.jsx === 'boolean' && options.jsx) {
78+
extra.jsx = true;
8879
}
8980

9081
/**
9182
* Allow the user to cause the parser to error if it encounters an unknown AST Node Type
9283
* (used in testing).
9384
*/
94-
if (options.errorOnUnknownASTType) {
85+
if (
86+
typeof options.errorOnUnknownASTType === 'boolean' &&
87+
options.errorOnUnknownASTType
88+
) {
9589
extra.errorOnUnknownASTType = true;
9690
}
9791

@@ -126,7 +120,7 @@ function generateAST(code, options) {
126120

127121
// Even if jsx option is set in typescript compiler, filename still has to
128122
// contain .tsx file extension
129-
const FILENAME = extra.ecmaFeatures.jsx ? 'estree.tsx' : 'estree.ts';
123+
const FILENAME = extra.jsx ? 'estree.tsx' : 'estree.ts';
130124

131125
const compilerHost = {
132126
fileExists() {
@@ -165,7 +159,7 @@ function generateAST(code, options) {
165159
{
166160
noResolve: true,
167161
target: ts.ScriptTarget.Latest,
168-
jsx: extra.ecmaFeatures.jsx ? 'preserve' : undefined
162+
jsx: extra.jsx ? 'preserve' : undefined
169163
},
170164
compilerHost
171165
);

tests/ast-alignment/parse.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ function parseWithTypeScriptESTree(text, parserOptions) {
5454
comment: false,
5555
useJSXTextNode: true,
5656
errorOnUnknownASTType: true,
57-
ecmaFeatures: {
58-
jsx: true
59-
}
57+
jsx: true
6058
},
6159
parserOptions
6260
)

0 commit comments

Comments
 (0)