parse.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. 'use strict';
  2. exports.__esModule = true;
  3. const moduleRequire = require('./module-require').default;
  4. const extname = require('path').extname;
  5. const log = require('debug')('eslint-plugin-import:parse');
  6. exports.default = function parse(path, content, context) {
  7. if (context == null) throw new Error('need context to parse properly');
  8. let parserOptions = context.parserOptions;
  9. const parserPath = getParserPath(path, context);
  10. if (!parserPath) throw new Error('parserPath is required!');
  11. // hack: espree blows up with frozen options
  12. parserOptions = Object.assign({}, parserOptions);
  13. parserOptions.ecmaFeatures = Object.assign({}, parserOptions.ecmaFeatures);
  14. // always include comments and tokens (for doc parsing)
  15. parserOptions.comment = true;
  16. parserOptions.attachComment = true; // keeping this for backward-compat with older parsers
  17. parserOptions.tokens = true;
  18. // attach node locations
  19. parserOptions.loc = true;
  20. parserOptions.range = true;
  21. // provide the `filePath` like eslint itself does, in `parserOptions`
  22. // https://github.com/eslint/eslint/blob/3ec436ee/lib/linter.js#L637
  23. parserOptions.filePath = path;
  24. // @typescript-eslint/parser will parse the entire project with typechecking if you provide
  25. // "project" or "projects" in parserOptions. Removing these options means the parser will
  26. // only parse one file in isolate mode, which is much, much faster.
  27. // https://github.com/benmosher/eslint-plugin-import/issues/1408#issuecomment-509298962
  28. delete parserOptions.project;
  29. delete parserOptions.projects;
  30. // require the parser relative to the main module (i.e., ESLint)
  31. const parser = moduleRequire(parserPath);
  32. if (typeof parser.parseForESLint === 'function') {
  33. let ast;
  34. try {
  35. ast = parser.parseForESLint(content, parserOptions).ast;
  36. } catch (e) {
  37. console.warn();
  38. console.warn('Error while parsing ' + parserOptions.filePath);
  39. console.warn('Line ' + e.lineNumber + ', column ' + e.column + ': ' + e.message);
  40. }
  41. if (!ast || typeof ast !== 'object') {
  42. console.warn(
  43. '`parseForESLint` from parser `' +
  44. parserPath +
  45. '` is invalid and will just be ignored'
  46. );
  47. } else {
  48. return ast;
  49. }
  50. }
  51. return parser.parse(content, parserOptions);
  52. };
  53. function getParserPath(path, context) {
  54. const parsers = context.settings['import/parsers'];
  55. if (parsers != null) {
  56. const extension = extname(path);
  57. for (const parserPath in parsers) {
  58. if (parsers[parserPath].indexOf(extension) > -1) {
  59. // use this alternate parser
  60. log('using alt parser:', parserPath);
  61. return parserPath;
  62. }
  63. }
  64. }
  65. // default to use ESLint parser
  66. return context.parserPath;
  67. }