/
parser.ts
108 lines (92 loc) · 3.1 KB
/
parser.ts
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
import { TSESLint } from '@typescript-eslint/experimental-utils';
import {
AST_NODE_TYPES,
parseAndGenerateServices,
ParserServices,
TSESTreeOptions,
} from '@typescript-eslint/typescript-estree';
import { analyzeScope } from './analyze-scope';
import { simpleTraverse } from './simple-traverse';
import { visitorKeys } from './visitor-keys';
type ParserOptions = TSESLint.ParserOptions;
// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
const packageJSON = require('../package.json');
interface ParseForESLintResult {
ast: any;
services: ParserServices;
visitorKeys: typeof visitorKeys;
scopeManager: ReturnType<typeof analyzeScope>;
}
function validateBoolean(
value: boolean | undefined,
fallback: boolean = false,
): boolean {
if (typeof value !== 'boolean') {
return fallback;
}
return value;
}
//------------------------------------------------------------------------------
// Public
//------------------------------------------------------------------------------
export const version = packageJSON.version;
export const Syntax = Object.freeze(AST_NODE_TYPES);
export function parse(code: string, options?: ParserOptions) {
return parseForESLint(code, options).ast;
}
export function parseForESLint(
code: string,
options?: ParserOptions | null,
): ParseForESLintResult {
if (!options || typeof options !== 'object') {
options = {};
}
// https://eslint.org/docs/user-guide/configuring#specifying-parser-options
// if sourceType is not provided by default eslint expect that it will be set to "script"
if (options.sourceType !== 'module' && options.sourceType !== 'script') {
options.sourceType = 'script';
}
if (typeof options.ecmaFeatures !== 'object') {
options.ecmaFeatures = {};
}
const parserOptions: TSESTreeOptions = {};
Object.assign(parserOptions, options, {
useJSXTextNode: validateBoolean(options.useJSXTextNode, true),
jsx: validateBoolean(options.ecmaFeatures.jsx),
});
if (typeof options.filePath === 'string') {
const tsx = options.filePath.endsWith('.tsx');
if (tsx || options.filePath.endsWith('.ts')) {
parserOptions.jsx = tsx;
}
}
/**
* Allow the user to suppress the warning from typescript-estree if they are using an unsupported
* version of TypeScript
*/
const warnOnUnsupportedTypeScriptVersion = validateBoolean(
options.warnOnUnsupportedTypeScriptVersion,
true,
);
if (!warnOnUnsupportedTypeScriptVersion) {
parserOptions.loggerFn = false;
}
const { ast, services } = parseAndGenerateServices(code, parserOptions);
ast.sourceType = options.sourceType;
simpleTraverse(ast, {
enter(node) {
switch (node.type) {
// Function#body cannot be null in ESTree spec.
case 'FunctionExpression':
if (!node.body) {
node.type = `TSEmptyBody${node.type}` as any;
}
break;
// no default
}
},
});
const scopeManager = analyzeScope(ast, options);
return { ast, services, scopeManager, visitorKeys };
}
export { ParserServices, ParserOptions };