forked from eslint/typescript-eslint-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
57 lines (47 loc) · 1.59 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
/**
* @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 visitorKeys = require("./visitor-keys");
//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------
exports.version = require("./package.json").version;
exports.parseForESLint = function parseForESLint(code, options) {
const ast = parse(code, options);
traverser.traverse(ast, {
enter: node => {
if (node.type === "DeclareFunction" || node.type === "FunctionExpression" || node.type === "FunctionDeclaration") {
if (!node.body) {
node.type = `TSEmptyBody${node.type}`;
}
}
}
});
return { ast, visitorKeys };
};
// 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;
}());