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

Commit 85de855

Browse files
author
Kai Cataldo
committed
Standardize and document configuration options
1 parent c3ea1d2 commit 85de855

File tree

2 files changed

+67
-9
lines changed

2 files changed

+67
-9
lines changed

README.md

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,75 @@
1313

1414
<br>
1515

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

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

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

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+
2685
## Supported TypeScript Version
2786

2887
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
);

0 commit comments

Comments
 (0)