parse.js 4.0 KB

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