Giant blob of minor changes
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / @typescript-eslint / typescript-estree / dist / parser.js
1 "use strict";
2 var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3     if (k2 === undefined) k2 = k;
4     Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5 }) : (function(o, m, k, k2) {
6     if (k2 === undefined) k2 = k;
7     o[k2] = m[k];
8 }));
9 var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10     Object.defineProperty(o, "default", { enumerable: true, value: v });
11 }) : function(o, v) {
12     o["default"] = v;
13 });
14 var __importStar = (this && this.__importStar) || function (mod) {
15     if (mod && mod.__esModule) return mod;
16     var result = {};
17     if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18     __setModuleDefault(result, mod);
19     return result;
20 };
21 var __importDefault = (this && this.__importDefault) || function (mod) {
22     return (mod && mod.__esModule) ? mod : { "default": mod };
23 };
24 Object.defineProperty(exports, "__esModule", { value: true });
25 exports.parseAndGenerateServices = exports.parse = void 0;
26 const debug_1 = __importDefault(require("debug"));
27 const glob_1 = require("glob");
28 const is_glob_1 = __importDefault(require("is-glob"));
29 const semver_1 = __importDefault(require("semver"));
30 const ts = __importStar(require("typescript"));
31 const ast_converter_1 = require("./ast-converter");
32 const convert_1 = require("./convert");
33 const createDefaultProgram_1 = require("./create-program/createDefaultProgram");
34 const createIsolatedProgram_1 = require("./create-program/createIsolatedProgram");
35 const createProjectProgram_1 = require("./create-program/createProjectProgram");
36 const createSourceFile_1 = require("./create-program/createSourceFile");
37 const semantic_or_syntactic_errors_1 = require("./semantic-or-syntactic-errors");
38 const shared_1 = require("./create-program/shared");
39 const log = debug_1.default('typescript-eslint:typescript-estree:parser');
40 /**
41  * This needs to be kept in sync with the top-level README.md in the
42  * typescript-eslint monorepo
43  */
44 const SUPPORTED_TYPESCRIPT_VERSIONS = '>=3.3.1 <4.1.0';
45 /*
46  * The semver package will ignore prerelease ranges, and we don't want to explicitly document every one
47  * List them all separately here, so we can automatically create the full string
48  */
49 const SUPPORTED_PRERELEASE_RANGES = [];
50 const ACTIVE_TYPESCRIPT_VERSION = ts.version;
51 const isRunningSupportedTypeScriptVersion = semver_1.default.satisfies(ACTIVE_TYPESCRIPT_VERSION, [SUPPORTED_TYPESCRIPT_VERSIONS]
52     .concat(SUPPORTED_PRERELEASE_RANGES)
53     .join(' || '));
54 let extra;
55 let warnedAboutTSVersion = false;
56 function enforceString(code) {
57     /**
58      * Ensure the source code is a string
59      */
60     if (typeof code !== 'string') {
61         return String(code);
62     }
63     return code;
64 }
65 /**
66  * @param code The code of the file being linted
67  * @param shouldProvideParserServices True if the program should be attempted to be calculated from provided tsconfig files
68  * @param shouldCreateDefaultProgram True if the program should be created from compiler host
69  * @returns Returns a source file and program corresponding to the linted code
70  */
71 function getProgramAndAST(code, shouldProvideParserServices, shouldCreateDefaultProgram) {
72     return ((shouldProvideParserServices &&
73         createProjectProgram_1.createProjectProgram(code, shouldCreateDefaultProgram, extra)) ||
74         (shouldProvideParserServices &&
75             shouldCreateDefaultProgram &&
76             createDefaultProgram_1.createDefaultProgram(code, extra)) ||
77         createIsolatedProgram_1.createIsolatedProgram(code, extra));
78 }
79 /**
80  * Compute the filename based on the parser options.
81  *
82  * Even if jsx option is set in typescript compiler, filename still has to
83  * contain .tsx file extension.
84  *
85  * @param options Parser options
86  */
87 function getFileName({ jsx } = {}) {
88     return jsx ? 'estree.tsx' : 'estree.ts';
89 }
90 /**
91  * Resets the extra config object
92  */
93 function resetExtra() {
94     extra = {
95         code: '',
96         comment: false,
97         comments: [],
98         createDefaultProgram: false,
99         debugLevel: new Set(),
100         errorOnTypeScriptSyntacticAndSemanticIssues: false,
101         errorOnUnknownASTType: false,
102         extraFileExtensions: [],
103         filePath: getFileName(),
104         jsx: false,
105         loc: false,
106         log: console.log,
107         preserveNodeMaps: true,
108         projects: [],
109         range: false,
110         strict: false,
111         tokens: null,
112         tsconfigRootDir: process.cwd(),
113         useJSXTextNode: false,
114     };
115 }
116 /**
117  * Normalizes, sanitizes, resolves and filters the provided
118  */
119 function prepareAndTransformProjects(projectsInput, ignoreListInput) {
120     let projects = [];
121     // Normalize and sanitize the project paths
122     if (typeof projectsInput === 'string') {
123         projects.push(projectsInput);
124     }
125     else if (Array.isArray(projectsInput)) {
126         for (const project of projectsInput) {
127             if (typeof project === 'string') {
128                 projects.push(project);
129             }
130         }
131     }
132     if (projects.length === 0) {
133         return projects;
134     }
135     // Transform glob patterns into paths
136     projects = projects.reduce((projects, project) => projects.concat(is_glob_1.default(project)
137         ? glob_1.sync(project, {
138             cwd: extra.tsconfigRootDir,
139         })
140         : project), []);
141     // Normalize and sanitize the ignore regex list
142     const ignoreRegexes = [];
143     if (Array.isArray(ignoreListInput)) {
144         for (const ignore of ignoreListInput) {
145             if (ignore instanceof RegExp) {
146                 ignoreRegexes.push(ignore);
147             }
148             else if (typeof ignore === 'string') {
149                 ignoreRegexes.push(new RegExp(ignore));
150             }
151         }
152     }
153     else {
154         ignoreRegexes.push(/\/node_modules\//);
155     }
156     // Remove any paths that match the ignore list
157     const filtered = projects.filter(project => {
158         for (const ignore of ignoreRegexes) {
159             if (ignore.test(project)) {
160                 return false;
161             }
162         }
163         return true;
164     });
165     log('parserOptions.project matched projects: %s', projects);
166     log('ignore list applied to parserOptions.project: %s', filtered);
167     return filtered;
168 }
169 function applyParserOptionsToExtra(options) {
170     /**
171      * Configure Debug logging
172      */
173     if (options.debugLevel === true) {
174         extra.debugLevel = new Set(['typescript-eslint']);
175     }
176     else if (Array.isArray(options.debugLevel)) {
177         extra.debugLevel = new Set(options.debugLevel);
178     }
179     if (extra.debugLevel.size > 0) {
180         // debug doesn't support multiple `enable` calls, so have to do it all at once
181         const namespaces = [];
182         if (extra.debugLevel.has('typescript-eslint')) {
183             namespaces.push('typescript-eslint:*');
184         }
185         if (extra.debugLevel.has('eslint') ||
186             // make sure we don't turn off the eslint debug if it was enabled via --debug
187             debug_1.default.enabled('eslint:*')) {
188             // https://github.com/eslint/eslint/blob/9dfc8501fb1956c90dc11e6377b4cb38a6bea65d/bin/eslint.js#L25
189             namespaces.push('eslint:*,-eslint:code-path');
190         }
191         debug_1.default.enable(namespaces.join(','));
192     }
193     /**
194      * Track range information in the AST
195      */
196     extra.range = typeof options.range === 'boolean' && options.range;
197     extra.loc = typeof options.loc === 'boolean' && options.loc;
198     /**
199      * Track tokens in the AST
200      */
201     if (typeof options.tokens === 'boolean' && options.tokens) {
202         extra.tokens = [];
203     }
204     /**
205      * Track comments in the AST
206      */
207     if (typeof options.comment === 'boolean' && options.comment) {
208         extra.comment = true;
209         extra.comments = [];
210     }
211     /**
212      * Enable JSX - note the applicable file extension is still required
213      */
214     if (typeof options.jsx === 'boolean' && options.jsx) {
215         extra.jsx = true;
216     }
217     /**
218      * Get the file path
219      */
220     if (typeof options.filePath === 'string' && options.filePath !== '<input>') {
221         extra.filePath = options.filePath;
222     }
223     else {
224         extra.filePath = getFileName(extra);
225     }
226     /**
227      * The JSX AST changed the node type for string literals
228      * inside a JSX Element from `Literal` to `JSXText`.
229      *
230      * When value is `true`, these nodes will be parsed as type `JSXText`.
231      * When value is `false`, these nodes will be parsed as type `Literal`.
232      */
233     if (typeof options.useJSXTextNode === 'boolean' && options.useJSXTextNode) {
234         extra.useJSXTextNode = true;
235     }
236     /**
237      * Allow the user to cause the parser to error if it encounters an unknown AST Node Type
238      * (used in testing)
239      */
240     if (typeof options.errorOnUnknownASTType === 'boolean' &&
241         options.errorOnUnknownASTType) {
242         extra.errorOnUnknownASTType = true;
243     }
244     /**
245      * Allow the user to override the function used for logging
246      */
247     if (typeof options.loggerFn === 'function') {
248         extra.log = options.loggerFn;
249     }
250     else if (options.loggerFn === false) {
251         extra.log = () => { };
252     }
253     if (typeof options.tsconfigRootDir === 'string') {
254         extra.tsconfigRootDir = options.tsconfigRootDir;
255     }
256     // NOTE - ensureAbsolutePath relies upon having the correct tsconfigRootDir in extra
257     extra.filePath = shared_1.ensureAbsolutePath(extra.filePath, extra);
258     // NOTE - prepareAndTransformProjects relies upon having the correct tsconfigRootDir in extra
259     extra.projects = prepareAndTransformProjects(options.project, options.projectFolderIgnoreList);
260     if (Array.isArray(options.extraFileExtensions) &&
261         options.extraFileExtensions.every(ext => typeof ext === 'string')) {
262         extra.extraFileExtensions = options.extraFileExtensions;
263     }
264     /**
265      * Allow the user to enable or disable the preservation of the AST node maps
266      * during the conversion process.
267      */
268     if (typeof options.preserveNodeMaps === 'boolean') {
269         extra.preserveNodeMaps = options.preserveNodeMaps;
270     }
271     extra.createDefaultProgram =
272         typeof options.createDefaultProgram === 'boolean' &&
273             options.createDefaultProgram;
274 }
275 function warnAboutTSVersion() {
276     var _a;
277     if (!isRunningSupportedTypeScriptVersion && !warnedAboutTSVersion) {
278         const isTTY = typeof process === undefined ? false : (_a = process.stdout) === null || _a === void 0 ? void 0 : _a.isTTY;
279         if (isTTY) {
280             const border = '=============';
281             const versionWarning = [
282                 border,
283                 'WARNING: You are currently running a version of TypeScript which is not officially supported by @typescript-eslint/typescript-estree.',
284                 'You may find that it works just fine, or you may not.',
285                 `SUPPORTED TYPESCRIPT VERSIONS: ${SUPPORTED_TYPESCRIPT_VERSIONS}`,
286                 `YOUR TYPESCRIPT VERSION: ${ACTIVE_TYPESCRIPT_VERSION}`,
287                 'Please only submit bug reports when using the officially supported version.',
288                 border,
289             ];
290             extra.log(versionWarning.join('\n\n'));
291         }
292         warnedAboutTSVersion = true;
293     }
294 }
295 function parse(code, options) {
296     /**
297      * Reset the parse configuration
298      */
299     resetExtra();
300     /**
301      * Ensure users do not attempt to use parse() when they need parseAndGenerateServices()
302      */
303     if (options === null || options === void 0 ? void 0 : options.errorOnTypeScriptSyntacticAndSemanticIssues) {
304         throw new Error(`"errorOnTypeScriptSyntacticAndSemanticIssues" is only supported for parseAndGenerateServices()`);
305     }
306     /**
307      * Ensure the source code is a string, and store a reference to it
308      */
309     code = enforceString(code);
310     extra.code = code;
311     /**
312      * Apply the given parser options
313      */
314     if (typeof options !== 'undefined') {
315         applyParserOptionsToExtra(options);
316     }
317     /**
318      * Warn if the user is using an unsupported version of TypeScript
319      */
320     warnAboutTSVersion();
321     /**
322      * Create a ts.SourceFile directly, no ts.Program is needed for a simple
323      * parse
324      */
325     const ast = createSourceFile_1.createSourceFile(code, extra);
326     /**
327      * Convert the TypeScript AST to an ESTree-compatible one
328      */
329     const { estree } = ast_converter_1.astConverter(ast, extra, false);
330     return estree;
331 }
332 exports.parse = parse;
333 function parseAndGenerateServices(code, options) {
334     /**
335      * Reset the parse configuration
336      */
337     resetExtra();
338     /**
339      * Ensure the source code is a string, and store a reference to it
340      */
341     code = enforceString(code);
342     extra.code = code;
343     /**
344      * Apply the given parser options
345      */
346     if (typeof options !== 'undefined') {
347         applyParserOptionsToExtra(options);
348         if (typeof options.errorOnTypeScriptSyntacticAndSemanticIssues ===
349             'boolean' &&
350             options.errorOnTypeScriptSyntacticAndSemanticIssues) {
351             extra.errorOnTypeScriptSyntacticAndSemanticIssues = true;
352         }
353     }
354     /**
355      * Warn if the user is using an unsupported version of TypeScript
356      */
357     warnAboutTSVersion();
358     /**
359      * Generate a full ts.Program in order to be able to provide parser
360      * services, such as type-checking
361      */
362     const shouldProvideParserServices = extra.projects && extra.projects.length > 0;
363     const { ast, program } = getProgramAndAST(code, shouldProvideParserServices, extra.createDefaultProgram);
364     /**
365      * Convert the TypeScript AST to an ESTree-compatible one, and optionally preserve
366      * mappings between converted and original AST nodes
367      */
368     const preserveNodeMaps = typeof extra.preserveNodeMaps === 'boolean' ? extra.preserveNodeMaps : true;
369     const { estree, astMaps } = ast_converter_1.astConverter(ast, extra, preserveNodeMaps);
370     /**
371      * Even if TypeScript parsed the source code ok, and we had no problems converting the AST,
372      * there may be other syntactic or semantic issues in the code that we can optionally report on.
373      */
374     if (program && extra.errorOnTypeScriptSyntacticAndSemanticIssues) {
375         const error = semantic_or_syntactic_errors_1.getFirstSemanticOrSyntacticError(program, ast);
376         if (error) {
377             throw convert_1.convertError(error);
378         }
379     }
380     /**
381      * Return the converted AST and additional parser services
382      */
383     return {
384         ast: estree,
385         services: {
386             hasFullTypeInformation: shouldProvideParserServices,
387             program,
388             esTreeNodeToTSNodeMap: astMaps.esTreeNodeToTSNodeMap,
389             tsNodeToESTreeNodeMap: astMaps.tsNodeToESTreeNodeMap,
390         },
391     };
392 }
393 exports.parseAndGenerateServices = parseAndGenerateServices;
394 //# sourceMappingURL=parser.js.map