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;
9 var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10 Object.defineProperty(o, "default", { enumerable: true, value: v });
14 var __importStar = (this && this.__importStar) || function (mod) {
15 if (mod && mod.__esModule) return mod;
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);
21 var __importDefault = (this && this.__importDefault) || function (mod) {
22 return (mod && mod.__esModule) ? mod : { "default": mod };
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');
41 * This needs to be kept in sync with the top-level README.md in the
42 * typescript-eslint monorepo
44 const SUPPORTED_TYPESCRIPT_VERSIONS = '>=3.3.1 <4.1.0';
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
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)
55 let warnedAboutTSVersion = false;
56 function enforceString(code) {
58 * Ensure the source code is a string
60 if (typeof code !== 'string') {
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
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));
80 * Compute the filename based on the parser options.
82 * Even if jsx option is set in typescript compiler, filename still has to
83 * contain .tsx file extension.
85 * @param options Parser options
87 function getFileName({ jsx } = {}) {
88 return jsx ? 'estree.tsx' : 'estree.ts';
91 * Resets the extra config object
93 function resetExtra() {
98 createDefaultProgram: false,
99 debugLevel: new Set(),
100 errorOnTypeScriptSyntacticAndSemanticIssues: false,
101 errorOnUnknownASTType: false,
102 extraFileExtensions: [],
103 filePath: getFileName(),
107 preserveNodeMaps: true,
112 tsconfigRootDir: process.cwd(),
113 useJSXTextNode: false,
117 * Normalizes, sanitizes, resolves and filters the provided
119 function prepareAndTransformProjects(projectsInput, ignoreListInput) {
121 // Normalize and sanitize the project paths
122 if (typeof projectsInput === 'string') {
123 projects.push(projectsInput);
125 else if (Array.isArray(projectsInput)) {
126 for (const project of projectsInput) {
127 if (typeof project === 'string') {
128 projects.push(project);
132 if (projects.length === 0) {
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,
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);
148 else if (typeof ignore === 'string') {
149 ignoreRegexes.push(new RegExp(ignore));
154 ignoreRegexes.push(/\/node_modules\//);
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)) {
165 log('parserOptions.project matched projects: %s', projects);
166 log('ignore list applied to parserOptions.project: %s', filtered);
169 function applyParserOptionsToExtra(options) {
171 * Configure Debug logging
173 if (options.debugLevel === true) {
174 extra.debugLevel = new Set(['typescript-eslint']);
176 else if (Array.isArray(options.debugLevel)) {
177 extra.debugLevel = new Set(options.debugLevel);
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:*');
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');
191 debug_1.default.enable(namespaces.join(','));
194 * Track range information in the AST
196 extra.range = typeof options.range === 'boolean' && options.range;
197 extra.loc = typeof options.loc === 'boolean' && options.loc;
199 * Track tokens in the AST
201 if (typeof options.tokens === 'boolean' && options.tokens) {
205 * Track comments in the AST
207 if (typeof options.comment === 'boolean' && options.comment) {
208 extra.comment = true;
212 * Enable JSX - note the applicable file extension is still required
214 if (typeof options.jsx === 'boolean' && options.jsx) {
220 if (typeof options.filePath === 'string' && options.filePath !== '<input>') {
221 extra.filePath = options.filePath;
224 extra.filePath = getFileName(extra);
227 * The JSX AST changed the node type for string literals
228 * inside a JSX Element from `Literal` to `JSXText`.
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`.
233 if (typeof options.useJSXTextNode === 'boolean' && options.useJSXTextNode) {
234 extra.useJSXTextNode = true;
237 * Allow the user to cause the parser to error if it encounters an unknown AST Node Type
240 if (typeof options.errorOnUnknownASTType === 'boolean' &&
241 options.errorOnUnknownASTType) {
242 extra.errorOnUnknownASTType = true;
245 * Allow the user to override the function used for logging
247 if (typeof options.loggerFn === 'function') {
248 extra.log = options.loggerFn;
250 else if (options.loggerFn === false) {
251 extra.log = () => { };
253 if (typeof options.tsconfigRootDir === 'string') {
254 extra.tsconfigRootDir = options.tsconfigRootDir;
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;
265 * Allow the user to enable or disable the preservation of the AST node maps
266 * during the conversion process.
268 if (typeof options.preserveNodeMaps === 'boolean') {
269 extra.preserveNodeMaps = options.preserveNodeMaps;
271 extra.createDefaultProgram =
272 typeof options.createDefaultProgram === 'boolean' &&
273 options.createDefaultProgram;
275 function warnAboutTSVersion() {
277 if (!isRunningSupportedTypeScriptVersion && !warnedAboutTSVersion) {
278 const isTTY = typeof process === undefined ? false : (_a = process.stdout) === null || _a === void 0 ? void 0 : _a.isTTY;
280 const border = '=============';
281 const versionWarning = [
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.',
290 extra.log(versionWarning.join('\n\n'));
292 warnedAboutTSVersion = true;
295 function parse(code, options) {
297 * Reset the parse configuration
301 * Ensure users do not attempt to use parse() when they need parseAndGenerateServices()
303 if (options === null || options === void 0 ? void 0 : options.errorOnTypeScriptSyntacticAndSemanticIssues) {
304 throw new Error(`"errorOnTypeScriptSyntacticAndSemanticIssues" is only supported for parseAndGenerateServices()`);
307 * Ensure the source code is a string, and store a reference to it
309 code = enforceString(code);
312 * Apply the given parser options
314 if (typeof options !== 'undefined') {
315 applyParserOptionsToExtra(options);
318 * Warn if the user is using an unsupported version of TypeScript
320 warnAboutTSVersion();
322 * Create a ts.SourceFile directly, no ts.Program is needed for a simple
325 const ast = createSourceFile_1.createSourceFile(code, extra);
327 * Convert the TypeScript AST to an ESTree-compatible one
329 const { estree } = ast_converter_1.astConverter(ast, extra, false);
332 exports.parse = parse;
333 function parseAndGenerateServices(code, options) {
335 * Reset the parse configuration
339 * Ensure the source code is a string, and store a reference to it
341 code = enforceString(code);
344 * Apply the given parser options
346 if (typeof options !== 'undefined') {
347 applyParserOptionsToExtra(options);
348 if (typeof options.errorOnTypeScriptSyntacticAndSemanticIssues ===
350 options.errorOnTypeScriptSyntacticAndSemanticIssues) {
351 extra.errorOnTypeScriptSyntacticAndSemanticIssues = true;
355 * Warn if the user is using an unsupported version of TypeScript
357 warnAboutTSVersion();
359 * Generate a full ts.Program in order to be able to provide parser
360 * services, such as type-checking
362 const shouldProvideParserServices = extra.projects && extra.projects.length > 0;
363 const { ast, program } = getProgramAndAST(code, shouldProvideParserServices, extra.createDefaultProgram);
365 * Convert the TypeScript AST to an ESTree-compatible one, and optionally preserve
366 * mappings between converted and original AST nodes
368 const preserveNodeMaps = typeof extra.preserveNodeMaps === 'boolean' ? extra.preserveNodeMaps : true;
369 const { estree, astMaps } = ast_converter_1.astConverter(ast, extra, preserveNodeMaps);
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.
374 if (program && extra.errorOnTypeScriptSyntacticAndSemanticIssues) {
375 const error = semantic_or_syntactic_errors_1.getFirstSemanticOrSyntacticError(program, ast);
377 throw convert_1.convertError(error);
381 * Return the converted AST and additional parser services
386 hasFullTypeInformation: shouldProvideParserServices,
388 esTreeNodeToTSNodeMap: astMaps.esTreeNodeToTSNodeMap,
389 tsNodeToESTreeNodeMap: astMaps.tsNodeToESTreeNodeMap,
393 exports.parseAndGenerateServices = parseAndGenerateServices;
394 //# sourceMappingURL=parser.js.map