UNPKG

8.2 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5/** @typedef {`.${string}`} Extension */
6/** @typedef {NonNullable<import('eslint').Rule.RuleContext['settings']> & { 'import/extensions'?: Extension[], 'import/parsers'?: { [k: string]: Extension[] }, 'import/cache'?: { lifetime: number | '∞' | 'Infinity' } }} ESLintSettings */
7
8const moduleRequire = require('./module-require').default;
9const extname = require('path').extname;
10const fs = require('fs');
11
12const log = require('debug')('eslint-plugin-import:parse');
13
14/** @type {(parserPath: NonNullable<import('eslint').Rule.RuleContext['parserPath']>) => unknown} */
15function getBabelEslintVisitorKeys(parserPath) {
16 if (parserPath.endsWith('index.js')) {
17 const hypotheticalLocation = parserPath.replace('index.js', 'visitor-keys.js');
18 if (fs.existsSync(hypotheticalLocation)) {
19 const keys = moduleRequire(hypotheticalLocation);
20 return keys.default || keys;
21 }
22 }
23 return null;
24}
25
26/** @type {(parserPath: import('eslint').Rule.RuleContext['parserPath'], parserInstance: { VisitorKeys: unknown }, parsedResult?: { visitorKeys?: unknown }) => unknown} */
27function keysFromParser(parserPath, parserInstance, parsedResult) {
28 // Exposed by @typescript-eslint/parser and @babel/eslint-parser
29 if (parsedResult && parsedResult.visitorKeys) {
30 return parsedResult.visitorKeys;
31 }
32 // The old babel parser doesn't have a `parseForESLint` eslint function, so we don't end
33 // up with a `parsedResult` here. It also doesn't expose the visitor keys on the parser itself,
34 // so we have to try and infer the visitor-keys module from the parserPath.
35 // This is NOT supported in flat config!
36 if (typeof parserPath === 'string' && (/.*babel-eslint.*/).test(parserPath)) {
37 return getBabelEslintVisitorKeys(parserPath);
38 }
39 // The espree parser doesn't have the `parseForESLint` function, so we don't end up with a
40 // `parsedResult` here, but it does expose the visitor keys on the parser instance that we can use.
41 if (parserInstance && parserInstance.VisitorKeys) {
42 return parserInstance.VisitorKeys;
43 }
44 return null;
45}
46
47// this exists to smooth over the unintentional breaking change in v2.7.
48// TODO, semver-major: avoid mutating `ast` and return a plain object instead.
49/** @type {<T extends import('eslint').AST.Program>(ast: T, visitorKeys: unknown) => T} */
50function makeParseReturn(ast, visitorKeys) {
51 if (ast) {
52 // @ts-expect-error see TODO
53 ast.visitorKeys = visitorKeys;
54 // @ts-expect-error see TODO
55 ast.ast = ast;
56 }
57 return ast;
58}
59
60/** @type {(text: string) => string} */
61function stripUnicodeBOM(text) {
62 return text.charCodeAt(0) === 0xFEFF ? text.slice(1) : text;
63}
64
65/** @type {(text: string) => string} */
66function transformHashbang(text) {
67 return text.replace(/^#!([^\r\n]+)/u, (_, captured) => `//${captured}`);
68}
69
70/** @type {(path: string, context: import('eslint').Rule.RuleContext & { settings?: ESLintSettings }) => import('eslint').Rule.RuleContext['parserPath']} */
71function getParserPath(path, context) {
72 const parsers = context.settings['import/parsers'];
73 if (parsers != null) {
74 // eslint-disable-next-line no-extra-parens
75 const extension = /** @type {Extension} */ (extname(path));
76 for (const parserPath in parsers) {
77 if (parsers[parserPath].indexOf(extension) > -1) {
78 // use this alternate parser
79 log('using alt parser:', parserPath);
80 return parserPath;
81 }
82 }
83 }
84 // default to use ESLint parser
85 return context.parserPath;
86}
87
88/** @type {(path: string, context: import('eslint').Rule.RuleContext) => string | null | (import('eslint').Linter.ParserModule)} */
89function getParser(path, context) {
90 const parserPath = getParserPath(path, context);
91 if (parserPath) {
92 return parserPath;
93 }
94 if (
95 !!context.languageOptions
96 && !!context.languageOptions.parser
97 && typeof context.languageOptions.parser !== 'string'
98 && (
99 // @ts-expect-error TODO: figure out a better type
100 typeof context.languageOptions.parser.parse === 'function'
101 // @ts-expect-error TODO: figure out a better type
102 || typeof context.languageOptions.parser.parseForESLint === 'function'
103 )
104 ) {
105 return context.languageOptions.parser;
106 }
107
108 return null;
109}
110
111/** @type {import('./parse').default} */
112exports.default = function parse(path, content, context) {
113 if (context == null) { throw new Error('need context to parse properly'); }
114
115 // ESLint in "flat" mode only sets context.languageOptions.parserOptions
116 const languageOptions = context.languageOptions;
117 let parserOptions = languageOptions && languageOptions.parserOptions || context.parserOptions;
118 const parserOrPath = getParser(path, context);
119
120 if (!parserOrPath) { throw new Error('parserPath or languageOptions.parser is required!'); }
121
122 // hack: espree blows up with frozen options
123 parserOptions = Object.assign({}, parserOptions);
124 parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures);
125
126 // always include comments and tokens (for doc parsing)
127 parserOptions.comment = true;
128 parserOptions.attachComment = true; // keeping this for backward-compat with older parsers
129 parserOptions.tokens = true;
130
131 // attach node locations
132 parserOptions.loc = true;
133 parserOptions.range = true;
134
135 // provide the `filePath` like eslint itself does, in `parserOptions`
136 // https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
137 parserOptions.filePath = path;
138
139 // @typescript-eslint/parser will parse the entire project with typechecking if you provide
140 // "project" or "projects" in parserOptions. Removing these options means the parser will
141 // only parse one file in isolate mode, which is much, much faster.
142 // https://github.com/import-js/eslint-plugin-import/issues/1408#issuecomment-509298962
143 delete parserOptions.EXPERIMENTAL_useProjectService;
144 delete parserOptions.projectService;
145 delete parserOptions.project;
146 delete parserOptions.projects;
147
148 // If this is a flat config, we need to add ecmaVersion and sourceType (if present) from languageOptions
149 if (languageOptions && languageOptions.ecmaVersion) {
150 parserOptions.ecmaVersion = languageOptions.ecmaVersion;
151 }
152 if (languageOptions && languageOptions.sourceType) {
153 // @ts-expect-error languageOptions is from the flatConfig Linter type in 8.57 while parserOptions is not.
154 // Non-flat config parserOptions.sourceType doesn't have "commonjs" in the type. Once upgraded to v9 types,
155 // they'll be the same and this expect-error should be removed.
156 parserOptions.sourceType = languageOptions.sourceType;
157 }
158
159 // require the parser relative to the main module (i.e., ESLint)
160 const parser = typeof parserOrPath === 'string' ? moduleRequire(parserOrPath) : parserOrPath;
161
162 // replicate bom strip and hashbang transform of ESLint
163 // https://github.com/eslint/eslint/blob/b93af98b3c417225a027cabc964c38e779adb945/lib/linter/linter.js#L779
164 content = transformHashbang(stripUnicodeBOM(String(content)));
165
166 if (typeof parser.parseForESLint === 'function') {
167 let ast;
168 try {
169 const parserRaw = parser.parseForESLint(content, parserOptions);
170 ast = parserRaw.ast;
171 // @ts-expect-error TODO: FIXME
172 return makeParseReturn(ast, keysFromParser(parserOrPath, parser, parserRaw));
173 } catch (e) {
174 console.warn();
175 console.warn('Error while parsing ' + parserOptions.filePath);
176 // @ts-expect-error e is almost certainly an Error here
177 console.warn('Line ' + e.lineNumber + ', column ' + e.column + ': ' + e.message);
178 }
179 if (!ast || typeof ast !== 'object') {
180 console.warn(
181 // Can only be invalid for custom parser per imports/parser
182 '`parseForESLint` from parser `' + (typeof parserOrPath === 'string' ? parserOrPath : 'context.languageOptions.parser') + '` is invalid and will just be ignored'
183 );
184 } else {
185 // @ts-expect-error TODO: FIXME
186 return makeParseReturn(ast, keysFromParser(parserOrPath, parser, undefined));
187 }
188 }
189
190 const ast = parser.parse(content, parserOptions);
191 // @ts-expect-error TODO: FIXME
192 return makeParseReturn(ast, keysFromParser(parserOrPath, parser, undefined));
193};