parse.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 (/.*espree.*/.test(parserPath)) {
  23. return parserInstance.VisitorKeys;
  24. }
  25. if (/.*babel-eslint.*/.test(parserPath)) {
  26. return getBabelEslintVisitorKeys(parserPath);
  27. }
  28. return null;
  29. }
  30. exports.default = function parse(path, content, context) {
  31. if (context == null) throw new Error('need context to parse properly');
  32. let parserOptions = context.parserOptions;
  33. const parserPath = getParserPath(path, context);
  34. if (!parserPath) throw new Error('parserPath is required!');
  35. // hack: espree blows up with frozen options
  36. parserOptions = Object.assign({}, parserOptions);
  37. parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures);
  38. // always include comments and tokens (for doc parsing)
  39. parserOptions.comment = true;
  40. parserOptions.attachComment = true; // keeping this for backward-compat with older parsers
  41. parserOptions.tokens = true;
  42. // attach node locations
  43. parserOptions.loc = true;
  44. parserOptions.range = true;
  45. // provide the `filePath` like eslint itself does, in `parserOptions`
  46. // https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
  47. parserOptions.filePath = path;
  48. // @typescript-eslint/parser will parse the entire project with typechecking if you provide
  49. // "project" or "projects" in parserOptions. Removing these options means the parser will
  50. // only parse one file in isolate mode, which is much, much faster.
  51. // https://github.com/import-js/eslint-plugin-import/issues/1408#issuecomment-509298962
  52. delete parserOptions.project;
  53. delete parserOptions.projects;
  54. // require the parser relative to the main module (i.e., ESLint)
  55. const parser = moduleRequire(parserPath);
  56. if (typeof parser.parseForESLint === 'function') {
  57. let ast;
  58. try {
  59. const parserRaw = parser.parseForESLint(content, parserOptions);
  60. ast = parserRaw.ast;
  61. return {
  62. ast,
  63. visitorKeys: keysFromParser(parserPath, parser, parserRaw),
  64. };
  65. } catch (e) {
  66. console.warn();
  67. console.warn('Error while parsing ' + parserOptions.filePath);
  68. console.warn('Line ' + e.lineNumber + ', column ' + e.column + ': ' + e.message);
  69. }
  70. if (!ast || typeof ast !== 'object') {
  71. console.warn(
  72. '`parseForESLint` from parser `' +
  73. parserPath +
  74. '` is invalid and will just be ignored'
  75. );
  76. } else {
  77. return {
  78. ast,
  79. visitorKeys: keysFromParser(parserPath, parser, undefined),
  80. };
  81. }
  82. }
  83. const keys = keysFromParser(parserPath, parser, undefined);
  84. return {
  85. ast: parser.parse(content, parserOptions),
  86. visitorKeys: keys,
  87. };
  88. };
  89. function getParserPath(path, context) {
  90. const parsers = context.settings['import/parsers'];
  91. if (parsers != null) {
  92. const extension = extname(path);
  93. for (const parserPath in parsers) {
  94. if (parsers[parserPath].indexOf(extension) > -1) {
  95. // use this alternate parser
  96. log('using alt parser:', parserPath);
  97. return parserPath;
  98. }
  99. }
  100. }
  101. // default to use ESLint parser
  102. return context.parserPath;
  103. }