forked from eslint/typescript-eslint-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
186 lines (155 loc) · 5.23 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
/**
* @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"),
commentAttachment = require("./lib/comment-attachment"),
// TokenTranslator = require("./lib/token-translator"),
ts = require("typescript");
// var lookahead;
var extra;
// var lastToken;
/**
* 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: {}
};
}
//------------------------------------------------------------------------------
// 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;
// var acornOptions = {
// ecmaVersion: 5
// };
// var translator;
if (typeof code !== "string" && !(code instanceof String)) {
code = toString(code);
}
resetExtra();
commentAttachment.reset();
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 = [];
// translator = new TokenTranslator(tt, code);
}
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 = [];
commentAttachment.reset();
}
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) {
// acornOptions.onComment = function() {
// var comment = convertAcornCommentToEsprimaComment.apply(this, arguments);
// extra.comments.push(comment);
//
// if (extra.attachComment) {
// commentAttachment.addComment(comment);
// }
// };
}
}
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;
}());