This repository was archived by the owner on Jan 19, 2019. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathparser.js
114 lines (94 loc) · 3.45 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
/**
* @fileoverview Parser that converts TypeScript into ESTree format.
* @author Nicholas C. Zakas
* @author James Henry <https://github.com/JamesHenry>
* @copyright jQuery Foundation and other contributors, https://jquery.org/
* MIT License
*/
"use strict";
const parse = require("typescript-estree").parse;
const astNodeTypes = require("typescript-estree").AST_NODE_TYPES;
const traverser = require("eslint/lib/util/traverser");
const analyzeScope = require("./analyze-scope");
const visitorKeys = require("./visitor-keys");
//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------
exports.version = require("./package.json").version;
exports.parseForESLint = function parseForESLint(code, inputOptions) {
const options = Object.assign({
useJSXTextNode: true,
// typescript-estree doesn't respect ecmaFeatures object
jsx: false
}, inputOptions);
if (typeof options.useJSXTextNode !== "boolean") {
options.useJSXTextNode = true;
}
options.jsx = (options.ecmaFeatures || {}).jsx;
if (typeof options.jsx !== "boolean") {
inputOptions.jsx = false;
}
// override the jsx option depending on the file path
if (typeof inputOptions.filePath === "string") {
const tsx = inputOptions.filePath.endsWith(".tsx");
if (tsx || inputOptions.filePath.endsWith(".ts")) {
options.jsx = tsx;
}
}
const ast = parse(code, options);
const extraOptions = {
sourceType: ast.sourceType
};
traverser.traverse(ast, {
enter: node => {
switch (node.type) {
// Just for backward compatibility.
case "DeclareFunction":
if (!node.body) {
node.type = `TSEmptyBody${node.type}`;
}
break;
// Function#body cannot be null in ESTree spec.
case "FunctionExpression":
case "FunctionDeclaration":
if (!node.body) {
node.type = `TSEmptyBody${node.type}`;
}
break;
// Import/Export declarations cannot appear in script.
// But if those appear only in namespace/module blocks, `ast.sourceType` was `"script"`.
// This doesn't modify `ast.sourceType` directly for backward compatibility.
case "ImportDeclaration":
case "ExportAllDeclaration":
case "ExportDefaultDeclaration":
case "ExportNamedDeclaration":
extraOptions.sourceType = "module";
break;
// no default
}
}
});
const scopeManager = analyzeScope(ast, options, extraOptions);
return { ast, scopeManager, visitorKeys };
};
exports.parse = function(code, options) {
return this.parseForESLint(code, options).ast;
};
// Deep copy.
/* istanbul ignore next */
exports.Syntax = (function() {
let 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;
}());