UNPKG

5.47 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 scope_manager_1 = require("@typescript-eslint/scope-manager");
8const typescript_estree_1 = require("@typescript-eslint/typescript-estree");
9const visitor_keys_1 = require("@typescript-eslint/visitor-keys");
10const debug_1 = __importDefault(require("debug"));
11const typescript_1 = require("typescript");
12const log = (0, debug_1.default)('typescript-eslint:parser:parser');
13function validateBoolean(value, fallback = false) {
14 if (typeof value !== 'boolean') {
15 return fallback;
16 }
17 return value;
18}
19const LIB_FILENAME_REGEX = /lib\.(.+)\.d\.[cm]?ts$/;
20function 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 // https://github.com/microsoft/TypeScript/blob/ae582a22ee1bb052e19b7c1bc4cac60509b574e0/src/compiler/utilitiesPublic.ts#L13-L36
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}
55function parse(code, options) {
56 return parseForESLint(code, options).ast;
57}
58exports.parse = parse;
59function parseForESLint(code, options) {
60 if (!options || typeof options !== 'object') {
61 options = {};
62 }
63 else {
64 options = { ...options };
65 }
66 // https://eslint.org/docs/user-guide/configuring#specifying-parser-options
67 // if sourceType is not provided by default eslint expect that it will be set to "script"
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 * Allow the user to suppress the warning from typescript-estree if they are using an unsupported
87 * version of TypeScript
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 // automatically apply the options configured for the program
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 // in case the user has specified something like "preact.h"
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 // in case the user has specified something like "preact.Fragment"
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}
129exports.parseForESLint = parseForESLint;
130//# sourceMappingURL=parser.js.map
\No newline at end of file