parse.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. 'use strict';
  2. exports.__esModule = true;
  3. const moduleRequire = require('./module-require').default;
  4. const extname = require('path').extname;
  5. const fs = require('fs');
  6. const log = require('debug')('eslint-plugin-import:parse');
  7. function getBabelEslintVisitorKeys(parserPath) {
  8. if (parserPath.endsWith('index.js')) {
  9. const hypotheticalLocation = parserPath.replace('index.js', 'visitor-keys.js');
  10. if (fs.existsSync(hypotheticalLocation)) {
  11. const keys = moduleRequire(hypotheticalLocation);
  12. return keys.default || keys;
  13. }
  14. }
  15. return null;
  16. }
  17. function keysFromParser(parserPath, parserInstance, parsedResult) {
  18. // Exposed by @typescript-eslint/parser and @babel/eslint-parser
  19. if (parsedResult && parsedResult.visitorKeys) {
  20. return parsedResult.visitorKeys;
  21. }
  22. if (typeof parserPath === 'string' && (/.*espree.*/).test(parserPath)) {
  23. return parserInstance.VisitorKeys;
  24. }
  25. if (typeof parserPath === 'string' && (/.*babel-eslint.*/).test(parserPath)) {
  26. return getBabelEslintVisitorKeys(parserPath);
  27. }
  28. return null;
  29. }
  30. // this exists to smooth over the unintentional breaking change in v2.7.
  31. // TODO, semver-major: avoid mutating `ast` and return a plain object instead.
  32. function makeParseReturn(ast, visitorKeys) {
  33. if (ast) {
  34. ast.visitorKeys = visitorKeys;
  35. ast.ast = ast;
  36. }
  37. return ast;
  38. }
  39. function stripUnicodeBOM(text) {
  40. return text.charCodeAt(0) === 0xFEFF ? text.slice(1) : text;
  41. }
  42. function transformHashbang(text) {
  43. return text.replace(/^#!([^\r\n]+)/u, (_, captured) => `//${captured}`);
  44. }
  45. exports.default = function parse(path, content, context) {
  46. if (context == null) { throw new Error('need context to parse properly'); }
  47. // ESLint in "flat" mode only sets context.languageOptions.parserOptions
  48. let parserOptions = context.languageOptions && context.languageOptions.parserOptions || context.parserOptions;
  49. const parserOrPath = getParser(path, context);
  50. if (!parserOrPath) { throw new Error('parserPath or languageOptions.parser is required!'); }
  51. // hack: espree blows up with frozen options
  52. parserOptions = Object.assign({}, parserOptions);
  53. parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures);
  54. // always include comments and tokens (for doc parsing)
  55. parserOptions.comment = true;
  56. parserOptions.attachComment = true; // keeping this for backward-compat with older parsers
  57. parserOptions.tokens = true;
  58. // attach node locations
  59. parserOptions.loc = true;
  60. parserOptions.range = true;
  61. // provide the `filePath` like eslint itself does, in `parserOptions`
  62. // https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
  63. parserOptions.filePath = path;
  64. // @typescript-eslint/parser will parse the entire project with typechecking if you provide
  65. // "project" or "projects" in parserOptions. Removing these options means the parser will
  66. // only parse one file in isolate mode, which is much, much faster.
  67. // https://github.com/import-js/eslint-plugin-import/issues/1408#issuecomment-509298962
  68. delete parserOptions.project;
  69. delete parserOptions.projects;
  70. // require the parser relative to the main module (i.e., ESLint)
  71. const parser = typeof parserOrPath === 'string' ? moduleRequire(parserOrPath) : parserOrPath;
  72. // replicate bom strip and hashbang transform of ESLint
  73. // https://github.com/eslint/eslint/blob/b93af98b3c417225a027cabc964c38e779adb945/lib/linter/linter.js#L779
  74. content = transformHashbang(stripUnicodeBOM(String(content)));
  75. if (typeof parser.parseForESLint === 'function') {
  76. let ast;
  77. try {
  78. const parserRaw = parser.parseForESLint(content, parserOptions);
  79. ast = parserRaw.ast;
  80. return makeParseReturn(ast, keysFromParser(parserOrPath, parser, parserRaw));
  81. } catch (e) {
  82. console.warn();
  83. console.warn('Error while parsing ' + parserOptions.filePath);
  84. console.warn('Line ' + e.lineNumber + ', column ' + e.column + ': ' + e.message);
  85. }
  86. if (!ast || typeof ast !== 'object') {
  87. console.warn(
  88. // Can only be invalid for custom parser per imports/parser
  89. '`parseForESLint` from parser `' + (typeof parserOrPath === 'string' ? parserOrPath : '`context.languageOptions.parser`') + '` is invalid and will just be ignored'
  90. );
  91. } else {
  92. return makeParseReturn(ast, keysFromParser(parserOrPath, parser, undefined));
  93. }
  94. }
  95. const ast = parser.parse(content, parserOptions);
  96. return makeParseReturn(ast, keysFromParser(parserOrPath, parser, undefined));
  97. };
  98. function getParser(path, context) {
  99. const parserPath = getParserPath(path, context);
  100. if (parserPath) {
  101. return parserPath;
  102. }
  103. const isFlat = context.languageOptions
  104. && context.languageOptions.parser
  105. && typeof context.languageOptions.parser !== 'string'
  106. && (
  107. typeof context.languageOptions.parser.parse === 'function'
  108. || typeof context.languageOptions.parser.parseForESLint === 'function'
  109. );
  110. return isFlat ? context.languageOptions.parser : null;
  111. }
  112. function getParserPath(path, context) {
  113. const parsers = context.settings['import/parsers'];
  114. if (parsers != null) {
  115. const extension = extname(path);
  116. for (const parserPath in parsers) {
  117. if (parsers[parserPath].indexOf(extension) > -1) {
  118. // use this alternate parser
  119. log('using alt parser:', parserPath);
  120. return parserPath;
  121. }
  122. }
  123. }
  124. // default to use ESLint parser
  125. return context.parserPath;
  126. }