1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.parseForESLint = exports.parse = void 0;
|
7 | const scope_manager_1 = require("@typescript-eslint/scope-manager");
|
8 | const typescript_estree_1 = require("@typescript-eslint/typescript-estree");
|
9 | const visitor_keys_1 = require("@typescript-eslint/visitor-keys");
|
10 | const debug_1 = __importDefault(require("debug"));
|
11 | const typescript_1 = require("typescript");
|
12 | const log = (0, debug_1.default)('typescript-eslint:parser:parser');
|
13 | function validateBoolean(value, fallback = false) {
|
14 | if (typeof value !== 'boolean') {
|
15 | return fallback;
|
16 | }
|
17 | return value;
|
18 | }
|
19 | const LIB_FILENAME_REGEX = /lib\.(.+)\.d\.[cm]?ts$/;
|
20 | function getLib(compilerOptions) {
|
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 = compilerOptions.target ?? typescript_1.ScriptTarget.ES5;
|
31 |
|
32 | switch (target) {
|
33 | case typescript_1.ScriptTarget.ESNext:
|
34 | return ['esnext.full'];
|
35 | case typescript_1.ScriptTarget.ES2022:
|
36 | return ['es2022.full'];
|
37 | case typescript_1.ScriptTarget.ES2021:
|
38 | return ['es2021.full'];
|
39 | case typescript_1.ScriptTarget.ES2020:
|
40 | return ['es2020.full'];
|
41 | case typescript_1.ScriptTarget.ES2019:
|
42 | return ['es2019.full'];
|
43 | case typescript_1.ScriptTarget.ES2018:
|
44 | return ['es2018.full'];
|
45 | case typescript_1.ScriptTarget.ES2017:
|
46 | return ['es2017.full'];
|
47 | case typescript_1.ScriptTarget.ES2016:
|
48 | return ['es2016.full'];
|
49 | case typescript_1.ScriptTarget.ES2015:
|
50 | return ['es6'];
|
51 | default:
|
52 | return ['lib'];
|
53 | }
|
54 | }
|
55 | function parse(code, options) {
|
56 | return parseForESLint(code, options).ast;
|
57 | }
|
58 | exports.parse = parse;
|
59 | function parseForESLint(code, options) {
|
60 | if (!options || typeof options !== 'object') {
|
61 | options = {};
|
62 | }
|
63 | else {
|
64 | options = { ...options };
|
65 | }
|
66 |
|
67 |
|
68 | if (options.sourceType !== 'module' && options.sourceType !== 'script') {
|
69 | options.sourceType = 'script';
|
70 | }
|
71 | if (typeof options.ecmaFeatures !== 'object') {
|
72 | options.ecmaFeatures = {};
|
73 | }
|
74 | const parserOptions = {};
|
75 | Object.assign(parserOptions, options, {
|
76 | jsx: validateBoolean(options.ecmaFeatures.jsx),
|
77 | });
|
78 | const analyzeOptions = {
|
79 | globalReturn: options.ecmaFeatures.globalReturn,
|
80 | jsxPragma: options.jsxPragma,
|
81 | jsxFragmentName: options.jsxFragmentName,
|
82 | lib: options.lib,
|
83 | sourceType: options.sourceType,
|
84 | };
|
85 | |
86 |
|
87 |
|
88 |
|
89 | const warnOnUnsupportedTypeScriptVersion = validateBoolean(options.warnOnUnsupportedTypeScriptVersion, true);
|
90 | if (!warnOnUnsupportedTypeScriptVersion) {
|
91 | parserOptions.loggerFn = false;
|
92 | }
|
93 | const { ast, services } = (0, typescript_estree_1.parseAndGenerateServices)(code, parserOptions);
|
94 | ast.sourceType = options.sourceType;
|
95 | let emitDecoratorMetadata = options.emitDecoratorMetadata === true;
|
96 | if (services.program) {
|
97 |
|
98 | const compilerOptions = services.program.getCompilerOptions();
|
99 | if (analyzeOptions.lib == null) {
|
100 | analyzeOptions.lib = getLib(compilerOptions);
|
101 | log('Resolved libs from program: %o', analyzeOptions.lib);
|
102 | }
|
103 | if (analyzeOptions.jsxPragma === undefined &&
|
104 | compilerOptions.jsxFactory != null) {
|
105 |
|
106 | const factory = compilerOptions.jsxFactory.split('.')[0].trim();
|
107 | analyzeOptions.jsxPragma = factory;
|
108 | log('Resolved jsxPragma from program: %s', analyzeOptions.jsxPragma);
|
109 | }
|
110 | if (analyzeOptions.jsxFragmentName === undefined &&
|
111 | compilerOptions.jsxFragmentFactory != null) {
|
112 |
|
113 | const fragFactory = compilerOptions.jsxFragmentFactory
|
114 | .split('.')[0]
|
115 | .trim();
|
116 | analyzeOptions.jsxFragmentName = fragFactory;
|
117 | log('Resolved jsxFragmentName from program: %s', analyzeOptions.jsxFragmentName);
|
118 | }
|
119 | if (compilerOptions.emitDecoratorMetadata === true) {
|
120 | emitDecoratorMetadata = true;
|
121 | }
|
122 | }
|
123 | if (emitDecoratorMetadata) {
|
124 | analyzeOptions.emitDecoratorMetadata = true;
|
125 | }
|
126 | const scopeManager = (0, scope_manager_1.analyze)(ast, analyzeOptions);
|
127 | return { ast, services, scopeManager, visitorKeys: visitor_keys_1.visitorKeys };
|
128 | }
|
129 | exports.parseForESLint = parseForESLint;
|
130 |
|
\ | No newline at end of file |