UNPKG

3.23 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const typescript_estree_1 = require("@typescript-eslint/typescript-estree");
4const analyze_scope_1 = require("./analyze-scope");
5// note - cannot migrate this to an import statement because it will make TSC copy the package.json to the dist folder
6const packageJSON = require('../package.json');
7function validateBoolean(value, fallback = false) {
8 if (typeof value !== 'boolean') {
9 return fallback;
10 }
11 return value;
12}
13//------------------------------------------------------------------------------
14// Public
15//------------------------------------------------------------------------------
16exports.version = packageJSON.version;
17exports.Syntax = Object.freeze(typescript_estree_1.AST_NODE_TYPES);
18function parse(code, options) {
19 return parseForESLint(code, options).ast;
20}
21exports.parse = parse;
22function parseForESLint(code, options) {
23 if (!options || typeof options !== 'object') {
24 options = {};
25 }
26 // https://eslint.org/docs/user-guide/configuring#specifying-parser-options
27 // if sourceType is not provided by default eslint expect that it will be set to "script"
28 if (options.sourceType !== 'module' && options.sourceType !== 'script') {
29 options.sourceType = 'script';
30 }
31 if (typeof options.ecmaFeatures !== 'object') {
32 options.ecmaFeatures = {};
33 }
34 const parserOptions = {};
35 Object.assign(parserOptions, options, {
36 useJSXTextNode: validateBoolean(options.useJSXTextNode, true),
37 jsx: validateBoolean(options.ecmaFeatures.jsx),
38 });
39 if (typeof options.filePath === 'string') {
40 const tsx = options.filePath.endsWith('.tsx');
41 if (tsx || options.filePath.endsWith('.ts')) {
42 parserOptions.jsx = tsx;
43 }
44 }
45 /**
46 * Allow the user to suppress the warning from typescript-estree if they are using an unsupported
47 * version of TypeScript
48 */
49 const warnOnUnsupportedTypeScriptVersion = validateBoolean(options.warnOnUnsupportedTypeScriptVersion, true);
50 if (!warnOnUnsupportedTypeScriptVersion) {
51 parserOptions.loggerFn = false;
52 }
53 const { ast, services } = typescript_estree_1.parseAndGenerateServices(code, parserOptions);
54 ast.sourceType = options.sourceType;
55 typescript_estree_1.simpleTraverse(ast, {
56 enter(node) {
57 switch (node.type) {
58 // Function#body cannot be null in ESTree spec.
59 case typescript_estree_1.AST_NODE_TYPES.FunctionExpression:
60 if (!node.body) {
61 // eslint-disable-next-line @typescript-eslint/no-explicit-any
62 node.type = `TSEmptyBody${node.type}`;
63 }
64 break;
65 // no default
66 }
67 },
68 });
69 const scopeManager = analyze_scope_1.analyzeScope(ast, options);
70 return { ast, services, scopeManager, visitorKeys: typescript_estree_1.visitorKeys };
71}
72exports.parseForESLint = parseForESLint;
73var typescript_estree_2 = require("@typescript-eslint/typescript-estree");
74exports.clearCaches = typescript_estree_2.clearCaches;
75//# sourceMappingURL=parser.js.map
\No newline at end of file