forked from eslint/typescript-eslint-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
250 lines (209 loc) · 7.07 KB
/
parser.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* @fileoverview Parser that converts TypeScript into ESTree format.
* @author Nicholas C. Zakas
* @copyright jQuery Foundation and other contributors, https://jquery.org/
* MIT License
*/
/* eslint no-undefined:0, no-use-before-define: 0 */
"use strict";
var astNodeTypes = require("./lib/ast-node-types"),
ts = require("typescript");
var extra;
/**
* Resets the extra config object
* @returns {void}
*/
function resetExtra() {
extra = {
tokens: null,
range: false,
loc: false,
comment: false,
comments: [],
tolerant: false,
errors: [],
strict: false,
ecmaFeatures: {}
};
}
/**
* Converts a TypeScript comment to an Esprima comment.
* @param {boolean} block True if it's a block comment, false if not.
* @param {string} text The text of the comment.
* @param {int} start The index at which the comment starts.
* @param {int} end The index at which the comment ends.
* @param {Location} startLoc The location at which the comment starts.
* @param {Location} endLoc The location at which the comment ends.
* @returns {Object} The comment object.
* @private
*/
function convertTypeScriptCommentToEsprimaComment(block, text, start, end, startLoc, endLoc) {
var comment = {
type: block ? "Block" : "Line",
value: text
};
if (typeof start === "number") {
comment.range = [start, end];
}
if (typeof startLoc === "object") {
comment.loc = {
start: startLoc,
end: endLoc
};
}
return comment;
}
/**
* Returns line and column data for the given start and end positions,
* for the given AST
* @param {Object} start start data
* @param {Object} end end data
* @param {Object} ast the AST object
* @returns {Object} the loc data
*/
function getLocFor(start, end, ast) {
var startLoc = ast.getLineAndCharacterOfPosition(start),
endLoc = ast.getLineAndCharacterOfPosition(end);
return {
start: {
line: startLoc.line + 1,
column: startLoc.character
},
end: {
line: endLoc.line + 1,
column: endLoc.character
}
};
}
//------------------------------------------------------------------------------
// Parser
//------------------------------------------------------------------------------
/**
* Parses the given source code to produce a valid AST
* @param {mixed} code TypeScript code
* @param {object} options configuration object for the parser
* @returns {object} the AST
*/
function parse(code, options) {
var program,
toString = String;
if (typeof code !== "string" && !(code instanceof String)) {
code = toString(code);
}
resetExtra();
if (typeof options !== "undefined") {
extra.range = (typeof options.range === "boolean") && options.range;
extra.loc = (typeof options.loc === "boolean") && options.loc;
extra.attachComment = (typeof options.attachComment === "boolean") && options.attachComment;
if (extra.loc && options.source !== null && options.source !== undefined) {
extra.source = toString(options.source);
}
if (typeof options.tokens === "boolean" && options.tokens) {
extra.tokens = [];
}
if (typeof options.comment === "boolean" && options.comment) {
extra.comment = true;
extra.comments = [];
}
if (typeof options.tolerant === "boolean" && options.tolerant) {
extra.errors = [];
}
if (extra.attachComment) {
extra.range = true;
extra.comments = [];
}
if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") {
// pass through jsx option
extra.ecmaFeatures.jsx = options.ecmaFeatures.jsx;
}
}
// Even if jsx option is set in typescript compiler, filename still has to
// contain .tsx file extension
var FILENAME = (extra.ecmaFeatures.jsx) ? "eslint.tsx" : "eslint.ts";
var compilerHost = {
fileExists: function() {
return true;
},
getCanonicalFileName: function() {
return FILENAME;
},
getCurrentDirectory: function() {
return "";
},
getDefaultLibFileName: function() {
return "lib.d.ts";
},
// TODO: Support Windows CRLF
getNewLine: function() {
return "\n";
},
getSourceFile: function(filename) {
return ts.createSourceFile(filename, code, ts.ScriptTarget.Latest, true);
},
readFile: function() {
return null;
},
useCaseSensitiveFileNames: function() {
return true;
},
writeFile: function() {
return null;
}
};
program = ts.createProgram([FILENAME], {
noResolve: true,
target: ts.ScriptTarget.Latest,
jsx: extra.ecmaFeatures.jsx ? "preserve" : undefined
}, compilerHost);
var ast = program.getSourceFile(FILENAME);
if (extra.attachComment || extra.comment) {
/**
* Create a TypeScript Scanner, with skipTrivia set to false so that
* we can parse the comments
*/
var triviaScanner = ts.createScanner(ast.languageVersion, false, 0, code);
var kind = triviaScanner.scan();
while (kind !== ts.SyntaxKind.EndOfFileToken) {
if (kind !== ts.SyntaxKind.SingleLineCommentTrivia && kind !== ts.SyntaxKind.MultiLineCommentTrivia) {
kind = triviaScanner.scan();
continue;
}
var isBlock = (kind === ts.SyntaxKind.MultiLineCommentTrivia);
var range = {
pos: triviaScanner.getTokenPos(),
end: triviaScanner.getTextPos(),
kind: triviaScanner.getToken()
};
var comment = code.substring(range.pos, range.end);
var text = comment.replace("//", "").replace("/*", "").replace("*/", "");
var loc = getLocFor(range.pos, range.end, ast);
var esprimaComment = convertTypeScriptCommentToEsprimaComment(isBlock, text, range.pos, range.end, loc.start, loc.end);
extra.comments.push(esprimaComment);
kind = triviaScanner.scan();
}
}
var convert = require("./lib/ast-converter");
return convert(ast, extra);
}
//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------
exports.version = require("./package.json").version;
exports.parse = parse;
// Deep copy.
/* istanbul ignore next */
exports.Syntax = (function() {
var name, types = {};
if (typeof Object.create === "function") {
types = Object.create(null);
}
for (name in astNodeTypes) {
if (astNodeTypes.hasOwnProperty(name)) {
types[name] = astNodeTypes[name];
}
}
if (typeof Object.freeze === "function") {
Object.freeze(types);
}
return types;
}());