configuration.cjs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
  2. function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
  3. const babel = require("./babel-core.cjs");
  4. const ESLINT_VERSION = require("../utils/eslint-version.cjs");
  5. function getParserPlugins(babelOptions) {
  6. var _babelOptions$parserO, _babelOptions$parserO2;
  7. const babelParserPlugins = (_babelOptions$parserO = (_babelOptions$parserO2 = babelOptions.parserOpts) == null ? void 0 : _babelOptions$parserO2.plugins) != null ? _babelOptions$parserO : [];
  8. const estreeOptions = {
  9. classFeatures: ESLINT_VERSION >= 8
  10. };
  11. for (const plugin of babelParserPlugins) {
  12. if (Array.isArray(plugin) && plugin[0] === "estree") {
  13. Object.assign(estreeOptions, plugin[1]);
  14. break;
  15. }
  16. }
  17. return [["estree", estreeOptions], ...babelParserPlugins];
  18. }
  19. function normalizeParserOptions(options) {
  20. var _options$allowImportE, _options$ecmaFeatures, _options$ecmaFeatures2;
  21. return Object.assign({
  22. sourceType: options.sourceType,
  23. filename: options.filePath
  24. }, options.babelOptions, {
  25. parserOpts: Object.assign({}, {
  26. allowImportExportEverywhere: (_options$allowImportE = options.allowImportExportEverywhere) != null ? _options$allowImportE : false,
  27. allowSuperOutsideMethod: true
  28. }, {
  29. allowReturnOutsideFunction: (_options$ecmaFeatures = (_options$ecmaFeatures2 = options.ecmaFeatures) == null ? void 0 : _options$ecmaFeatures2.globalReturn) != null ? _options$ecmaFeatures : true
  30. }, options.babelOptions.parserOpts, {
  31. plugins: getParserPlugins(options.babelOptions),
  32. attachComment: false,
  33. ranges: true,
  34. tokens: true
  35. }),
  36. caller: Object.assign({
  37. name: "@babel/eslint-parser"
  38. }, options.babelOptions.caller)
  39. });
  40. }
  41. function validateResolvedConfig(config, options, parseOptions) {
  42. if (config !== null) {
  43. if (options.requireConfigFile !== false) {
  44. if (!config.hasFilesystemConfig()) {
  45. let error = `No Babel config file detected for ${config.options.filename}. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files.`;
  46. if (config.options.filename.includes("node_modules")) {
  47. error += `\nIf you have a .babelrc.js file or use package.json#babel, keep in mind that it's not used when parsing dependencies. If you want your config to be applied to your whole app, consider using babel.config.js or babel.config.json instead.`;
  48. }
  49. throw new Error(error);
  50. }
  51. }
  52. if (config.options) return config.options;
  53. }
  54. return getDefaultParserOptions(parseOptions);
  55. }
  56. function getDefaultParserOptions(options) {
  57. return Object.assign({
  58. plugins: []
  59. }, options, {
  60. babelrc: false,
  61. configFile: false,
  62. browserslistConfigFile: false,
  63. ignore: null,
  64. only: null
  65. });
  66. }
  67. exports.normalizeBabelParseConfig = function () {
  68. var _ref = _asyncToGenerator(function* (options) {
  69. const parseOptions = normalizeParserOptions(options);
  70. const config = yield babel.loadPartialConfigAsync(parseOptions);
  71. return validateResolvedConfig(config, options, parseOptions);
  72. });
  73. return function (_x) {
  74. return _ref.apply(this, arguments);
  75. };
  76. }();
  77. exports.normalizeBabelParseConfigSync = function (options) {
  78. const parseOptions = normalizeParserOptions(options);
  79. const config = babel.loadPartialConfigSync(parseOptions);
  80. return validateResolvedConfig(config, options, parseOptions);
  81. };