parse.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // 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. exports.default = function parse(path, content, context) {
  40. if (context == null) throw new Error('need context to parse properly');
  41. let parserOptions = context.parserOptions;
  42. const parserPath = getParserPath(path, context);
  43. if (!parserPath) throw new Error('parserPath is required!');
  44. // hack: espree blows up with frozen options
  45. parserOptions = Object.assign({}, parserOptions);
  46. parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures);
  47. // always include comments and tokens (for doc parsing)
  48. parserOptions.comment = true;
  49. parserOptions.attachComment = true; // keeping this for backward-compat with older parsers
  50. parserOptions.tokens = true;
  51. // attach node locations
  52. parserOptions.loc = true;
  53. parserOptions.range = true;
  54. // provide the `filePath` like eslint itself does, in `parserOptions`
  55. // https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
  56. parserOptions.filePath = path;
  57. // @typescript-eslint/parser will parse the entire project with typechecking if you provide
  58. // "project" or "projects" in parserOptions. Removing these options means the parser will
  59. // only parse one file in isolate mode, which is much, much faster.
  60. // https://github.com/import-js/eslint-plugin-import/issues/1408#issuecomment-509298962
  61. delete parserOptions.project;
  62. delete parserOptions.projects;
  63. // require the parser relative to the main module (i.e., ESLint)
  64. const parser = moduleRequire(parserPath);
  65. if (typeof parser.parseForESLint === 'function') {
  66. let ast;
  67. try {
  68. const parserRaw = parser.parseForESLint(content, parserOptions);
  69. ast = parserRaw.ast;
  70. return makeParseReturn(ast, keysFromParser(parserPath, parser, parserRaw));
  71. } catch (e) {
  72. console.warn();
  73. console.warn('Error while parsing ' + parserOptions.filePath);
  74. console.warn('Line ' + e.lineNumber + ', column ' + e.column + ': ' + e.message);
  75. }
  76. if (!ast || typeof ast !== 'object') {
  77. console.warn(
  78. '`parseForESLint` from parser `' +
  79. parserPath +
  80. '` is invalid and will just be ignored'
  81. );
  82. } else {
  83. return makeParseReturn(ast, keysFromParser(parserPath, parser, undefined));
  84. }
  85. }
  86. const ast = parser.parse(content, parserOptions);
  87. return makeParseReturn(ast, keysFromParser(parserPath, parser, undefined));
  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. }