parse.js 8.1 KB

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