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

Commit 256ca39

Browse files
author
Kai Cataldo
committed
feat: standardize and document configuration options
1 parent c3ea1d2 commit 256ca39

File tree

11 files changed

+424
-356
lines changed

11 files changed

+424
-356
lines changed

README.md

Lines changed: 87 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,100 @@
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+
tolerant: false,
56+
57+
source: null,
58+
59+
/*
60+
* The JSX AST changed the node type for string literals
61+
* inside a JSX Element from `Literal` to `JSXText`.
62+
* When value is `true`, these nodes will be parsed as type `JSXText`.
63+
* When value is `false`, these nodes will be parsed as type `Literal`.
64+
*/
65+
useJSXTextNode: false,
66+
67+
// Cause the parser to error if it encounters an unknown AST node type (useful for testing)
68+
errorOnUnknownASTType: false,
69+
70+
/*
71+
* Allows overriding of function used for logging.
72+
* When value is `false`, no logging will occur.
73+
* When value is not provided, `console.log()` will be used.
74+
*/
75+
loggerFn: undefined
76+
}
77+
```
78+
79+
Example usage:
80+
```javascript
81+
const parser = require("typescript-estree");
82+
const code = "const hello: string = 'world';";
83+
// Optional second options argument with the following default settings
84+
const ast = parser.parse(code, {
85+
range: true,
86+
loc: true
87+
});
88+
```
89+
90+
### version
91+
92+
Exposes the current version of typescript-estree as specified in package.json.
93+
94+
Example usage:
95+
```javascript
96+
const parser = require("typescript-estree");
97+
const version = parser.version;
98+
```
99+
100+
### AST_NODE_TYPES
101+
102+
Exposes an object that contains the AST node types produced by the parser.
103+
104+
Example usage:
105+
```javascript
106+
const parser = require("typescript-estree");
107+
const astNodeTypes = parser.AST_NODE_TYPES;
108+
```
109+
26110
## Supported TypeScript Version
27111

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

parser.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function resetExtra() {
3838
tolerant: false,
3939
errors: [],
4040
strict: false,
41-
ecmaFeatures: {},
41+
jsx: false,
4242
useJSXTextNode: false,
4343
log: console.log
4444
};
@@ -82,9 +82,8 @@ function generateAST(code, options) {
8282
extra.errors = [];
8383
}
8484

85-
if (options.ecmaFeatures && typeof options.ecmaFeatures === 'object') {
86-
// pass through jsx option
87-
extra.ecmaFeatures.jsx = options.ecmaFeatures.jsx;
85+
if (typeof options.jsx === 'boolean' && options.jsx) {
86+
extra.jsx = options.jsx;
8887
}
8988

9089
/**
@@ -126,7 +125,7 @@ function generateAST(code, options) {
126125

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

131130
const compilerHost = {
132131
fileExists() {
@@ -165,7 +164,7 @@ function generateAST(code, options) {
165164
{
166165
noResolve: true,
167166
target: ts.ScriptTarget.Latest,
168-
jsx: extra.ecmaFeatures.jsx ? 'preserve' : undefined
167+
jsx: extra.jsx ? 'preserve' : undefined
169168
},
170169
compilerHost
171170
);

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)