UNPKG

5.69 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.parseForESLint = exports.parse = void 0;
7const typescript_estree_1 = require("@typescript-eslint/typescript-estree");
8const scope_manager_1 = require("@typescript-eslint/scope-manager");
9const debug_1 = __importDefault(require("debug"));
10const typescript_1 = require("typescript");
11const log = (0, debug_1.default)('typescript-eslint:parser:parser');
12function validateBoolean(value, fallback = false) {
13 if (typeof value !== 'boolean') {
14 return fallback;
15 }
16 return value;
17}
18const LIB_FILENAME_REGEX = /lib\.(.+)\.d\.ts$/;
19function getLib(compilerOptions) {
20 var _a;
21 if (compilerOptions.lib) {
22 return compilerOptions.lib.reduce((acc, lib) => {
23 const match = LIB_FILENAME_REGEX.exec(lib.toLowerCase());
24 if (match) {
25 acc.push(match[1]);
26 }
27 return acc;
28 }, []);
29 }
30 const target = (_a = compilerOptions.target) !== null && _a !== void 0 ? _a : typescript_1.ScriptTarget.ES5;
31 // https://github.com/Microsoft/TypeScript/blob/59ad375234dc2efe38d8ee0ba58414474c1d5169/src/compiler/utilitiesPublic.ts#L13-L32
32 switch (target) {
33 case typescript_1.ScriptTarget.ESNext:
34 return ['esnext.full'];
35 case typescript_1.ScriptTarget.ES2020:
36 return ['es2020.full'];
37 case typescript_1.ScriptTarget.ES2019:
38 return ['es2019.full'];
39 case typescript_1.ScriptTarget.ES2018:
40 return ['es2018.full'];
41 case typescript_1.ScriptTarget.ES2017:
42 return ['es2017.full'];
43 case typescript_1.ScriptTarget.ES2016:
44 return ['es2016.full'];
45 case typescript_1.ScriptTarget.ES2015:
46 return ['es6'];
47 default:
48 return ['lib'];
49 }
50}
51function parse(code, options) {
52 return parseForESLint(code, options).ast;
53}
54exports.parse = parse;
55function parseForESLint(code, options) {
56 if (!options || typeof options !== 'object') {
57 options = {};
58 }
59 else {
60 options = Object.assign({}, options);
61 }
62 // https://eslint.org/docs/user-guide/configuring#specifying-parser-options
63 // if sourceType is not provided by default eslint expect that it will be set to "script"
64 if (options.sourceType !== 'module' && options.sourceType !== 'script') {
65 options.sourceType = 'script';
66 }
67 if (typeof options.ecmaFeatures !== 'object') {
68 options.ecmaFeatures = {};
69 }
70 const parserOptions = {};
71 Object.assign(parserOptions, options, {
72 useJSXTextNode: validateBoolean(options.useJSXTextNode, true),
73 jsx: validateBoolean(options.ecmaFeatures.jsx),
74 });
75 const analyzeOptions = {
76 ecmaVersion: options.ecmaVersion === 'latest' ? 1e8 : options.ecmaVersion,
77 globalReturn: options.ecmaFeatures.globalReturn,
78 jsxPragma: options.jsxPragma,
79 jsxFragmentName: options.jsxFragmentName,
80 lib: options.lib,
81 sourceType: options.sourceType,
82 };
83 if (typeof options.filePath === 'string') {
84 const tsx = options.filePath.endsWith('.tsx');
85 if (tsx || options.filePath.endsWith('.ts')) {
86 parserOptions.jsx = tsx;
87 }
88 }
89 /**
90 * Allow the user to suppress the warning from typescript-estree if they are using an unsupported
91 * version of TypeScript
92 */
93 const warnOnUnsupportedTypeScriptVersion = validateBoolean(options.warnOnUnsupportedTypeScriptVersion, true);
94 if (!warnOnUnsupportedTypeScriptVersion) {
95 parserOptions.loggerFn = false;
96 }
97 const { ast, services } = (0, typescript_estree_1.parseAndGenerateServices)(code, parserOptions);
98 ast.sourceType = options.sourceType;
99 if (services.hasFullTypeInformation) {
100 // automatically apply the options configured for the program
101 const compilerOptions = services.program.getCompilerOptions();
102 if (analyzeOptions.lib == null) {
103 analyzeOptions.lib = getLib(compilerOptions);
104 log('Resolved libs from program: %o', analyzeOptions.lib);
105 }
106 if (parserOptions.jsx === true) {
107 if (analyzeOptions.jsxPragma === undefined &&
108 compilerOptions.jsxFactory != null) {
109 // in case the user has specified something like "preact.h"
110 const factory = compilerOptions.jsxFactory.split('.')[0].trim();
111 analyzeOptions.jsxPragma = factory;
112 log('Resolved jsxPragma from program: %s', analyzeOptions.jsxPragma);
113 }
114 if (analyzeOptions.jsxFragmentName === undefined &&
115 compilerOptions.jsxFragmentFactory != null) {
116 // in case the user has specified something like "preact.Fragment"
117 const fragFactory = compilerOptions.jsxFragmentFactory
118 .split('.')[0]
119 .trim();
120 analyzeOptions.jsxFragmentName = fragFactory;
121 log('Resolved jsxFragmentName from program: %s', analyzeOptions.jsxFragmentName);
122 }
123 }
124 if (compilerOptions.emitDecoratorMetadata === true) {
125 analyzeOptions.emitDecoratorMetadata =
126 compilerOptions.emitDecoratorMetadata;
127 }
128 }
129 const scopeManager = (0, scope_manager_1.analyze)(ast, analyzeOptions);
130 return { ast, services, scopeManager, visitorKeys: typescript_estree_1.visitorKeys };
131}
132exports.parseForESLint = parseForESLint;
133//# sourceMappingURL=parser.js.map
\No newline at end of file