fd814fbc9a67432e6f2c9ba643b9e900492db245b677bb9e8732d934910197ae4ceb3068b2ad319efb25901acefec012a3b457c2c8a9ad9d204e453252a761 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.assumptionsNames = void 0;
  6. exports.checkNoUnwrappedItemOptionPairs = checkNoUnwrappedItemOptionPairs;
  7. exports.validate = validate;
  8. var _removed = require("./removed.js");
  9. var _optionAssertions = require("./option-assertions.js");
  10. var _configError = require("../../errors/config-error.js");
  11. const ROOT_VALIDATORS = {
  12. cwd: _optionAssertions.assertString,
  13. root: _optionAssertions.assertString,
  14. rootMode: _optionAssertions.assertRootMode,
  15. configFile: _optionAssertions.assertConfigFileSearch,
  16. caller: _optionAssertions.assertCallerMetadata,
  17. filename: _optionAssertions.assertString,
  18. filenameRelative: _optionAssertions.assertString,
  19. code: _optionAssertions.assertBoolean,
  20. ast: _optionAssertions.assertBoolean,
  21. cloneInputAst: _optionAssertions.assertBoolean,
  22. envName: _optionAssertions.assertString
  23. };
  24. const BABELRC_VALIDATORS = {
  25. babelrc: _optionAssertions.assertBoolean,
  26. babelrcRoots: _optionAssertions.assertBabelrcSearch
  27. };
  28. const NONPRESET_VALIDATORS = {
  29. extends: _optionAssertions.assertString,
  30. ignore: _optionAssertions.assertIgnoreList,
  31. only: _optionAssertions.assertIgnoreList,
  32. targets: _optionAssertions.assertTargets,
  33. browserslistConfigFile: _optionAssertions.assertConfigFileSearch,
  34. browserslistEnv: _optionAssertions.assertString
  35. };
  36. const COMMON_VALIDATORS = {
  37. inputSourceMap: _optionAssertions.assertInputSourceMap,
  38. presets: _optionAssertions.assertPluginList,
  39. plugins: _optionAssertions.assertPluginList,
  40. passPerPreset: _optionAssertions.assertBoolean,
  41. assumptions: _optionAssertions.assertAssumptions,
  42. env: assertEnvSet,
  43. overrides: assertOverridesList,
  44. test: _optionAssertions.assertConfigApplicableTest,
  45. include: _optionAssertions.assertConfigApplicableTest,
  46. exclude: _optionAssertions.assertConfigApplicableTest,
  47. retainLines: _optionAssertions.assertBoolean,
  48. comments: _optionAssertions.assertBoolean,
  49. shouldPrintComment: _optionAssertions.assertFunction,
  50. compact: _optionAssertions.assertCompact,
  51. minified: _optionAssertions.assertBoolean,
  52. auxiliaryCommentBefore: _optionAssertions.assertString,
  53. auxiliaryCommentAfter: _optionAssertions.assertString,
  54. sourceType: _optionAssertions.assertSourceType,
  55. wrapPluginVisitorMethod: _optionAssertions.assertFunction,
  56. highlightCode: _optionAssertions.assertBoolean,
  57. sourceMaps: _optionAssertions.assertSourceMaps,
  58. sourceMap: _optionAssertions.assertSourceMaps,
  59. sourceFileName: _optionAssertions.assertString,
  60. sourceRoot: _optionAssertions.assertString,
  61. parserOpts: _optionAssertions.assertObject,
  62. generatorOpts: _optionAssertions.assertObject
  63. };
  64. {
  65. Object.assign(COMMON_VALIDATORS, {
  66. getModuleId: _optionAssertions.assertFunction,
  67. moduleRoot: _optionAssertions.assertString,
  68. moduleIds: _optionAssertions.assertBoolean,
  69. moduleId: _optionAssertions.assertString
  70. });
  71. }
  72. const knownAssumptions = ["arrayLikeIsIterable", "constantReexports", "constantSuper", "enumerableModuleMeta", "ignoreFunctionLength", "ignoreToPrimitiveHint", "iterableIsArray", "mutableTemplateObject", "noClassCalls", "noDocumentAll", "noIncompleteNsImportDetection", "noNewArrows", "noUninitializedPrivateFieldAccess", "objectRestNoSymbols", "privateFieldsAsSymbols", "privateFieldsAsProperties", "pureGetters", "setClassMethods", "setComputedProperties", "setPublicClassFields", "setSpreadProperties", "skipForOfIteratorClosing", "superIsCallableConstructor"];
  73. const assumptionsNames = exports.assumptionsNames = new Set(knownAssumptions);
  74. function getSource(loc) {
  75. return loc.type === "root" ? loc.source : getSource(loc.parent);
  76. }
  77. function validate(type, opts, filename) {
  78. try {
  79. return validateNested({
  80. type: "root",
  81. source: type
  82. }, opts);
  83. } catch (error) {
  84. const configError = new _configError.default(error.message, filename);
  85. if (error.code) configError.code = error.code;
  86. throw configError;
  87. }
  88. }
  89. function validateNested(loc, opts) {
  90. const type = getSource(loc);
  91. assertNoDuplicateSourcemap(opts);
  92. Object.keys(opts).forEach(key => {
  93. const optLoc = {
  94. type: "option",
  95. name: key,
  96. parent: loc
  97. };
  98. if (type === "preset" && NONPRESET_VALIDATORS[key]) {
  99. throw new Error(`${(0, _optionAssertions.msg)(optLoc)} is not allowed in preset options`);
  100. }
  101. if (type !== "arguments" && ROOT_VALIDATORS[key]) {
  102. throw new Error(`${(0, _optionAssertions.msg)(optLoc)} is only allowed in root programmatic options`);
  103. }
  104. if (type !== "arguments" && type !== "configfile" && BABELRC_VALIDATORS[key]) {
  105. if (type === "babelrcfile" || type === "extendsfile") {
  106. throw new Error(`${(0, _optionAssertions.msg)(optLoc)} is not allowed in .babelrc or "extends"ed files, only in root programmatic options, ` + `or babel.config.js/config file options`);
  107. }
  108. throw new Error(`${(0, _optionAssertions.msg)(optLoc)} is only allowed in root programmatic options, or babel.config.js/config file options`);
  109. }
  110. const validator = COMMON_VALIDATORS[key] || NONPRESET_VALIDATORS[key] || BABELRC_VALIDATORS[key] || ROOT_VALIDATORS[key] || throwUnknownError;
  111. validator(optLoc, opts[key]);
  112. });
  113. return opts;
  114. }
  115. function throwUnknownError(loc) {
  116. const key = loc.name;
  117. if (_removed.default[key]) {
  118. const {
  119. message,
  120. version = 5
  121. } = _removed.default[key];
  122. throw new Error(`Using removed Babel ${version} option: ${(0, _optionAssertions.msg)(loc)} - ${message}`);
  123. } else {
  124. const unknownOptErr = new Error(`Unknown option: ${(0, _optionAssertions.msg)(loc)}. Check out https://babeljs.io/docs/en/babel-core/#options for more information about options.`);
  125. unknownOptErr.code = "BABEL_UNKNOWN_OPTION";
  126. throw unknownOptErr;
  127. }
  128. }
  129. function assertNoDuplicateSourcemap(opts) {
  130. if (hasOwnProperty.call(opts, "sourceMap") && hasOwnProperty.call(opts, "sourceMaps")) {
  131. throw new Error(".sourceMap is an alias for .sourceMaps, cannot use both");
  132. }
  133. }
  134. function assertEnvSet(loc, value) {
  135. if (loc.parent.type === "env") {
  136. throw new Error(`${(0, _optionAssertions.msg)(loc)} is not allowed inside of another .env block`);
  137. }
  138. const parent = loc.parent;
  139. const obj = (0, _optionAssertions.assertObject)(loc, value);
  140. if (obj) {
  141. for (const envName of Object.keys(obj)) {
  142. const env = (0, _optionAssertions.assertObject)((0, _optionAssertions.access)(loc, envName), obj[envName]);
  143. if (!env) continue;
  144. const envLoc = {
  145. type: "env",
  146. name: envName,
  147. parent
  148. };
  149. validateNested(envLoc, env);
  150. }
  151. }
  152. return obj;
  153. }
  154. function assertOverridesList(loc, value) {
  155. if (loc.parent.type === "env") {
  156. throw new Error(`${(0, _optionAssertions.msg)(loc)} is not allowed inside an .env block`);
  157. }
  158. if (loc.parent.type === "overrides") {
  159. throw new Error(`${(0, _optionAssertions.msg)(loc)} is not allowed inside an .overrides block`);
  160. }
  161. const parent = loc.parent;
  162. const arr = (0, _optionAssertions.assertArray)(loc, value);
  163. if (arr) {
  164. for (const [index, item] of arr.entries()) {
  165. const objLoc = (0, _optionAssertions.access)(loc, index);
  166. const env = (0, _optionAssertions.assertObject)(objLoc, item);
  167. if (!env) throw new Error(`${(0, _optionAssertions.msg)(objLoc)} must be an object`);
  168. const overridesLoc = {
  169. type: "overrides",
  170. index,
  171. parent
  172. };
  173. validateNested(overridesLoc, env);
  174. }
  175. }
  176. return arr;
  177. }
  178. function checkNoUnwrappedItemOptionPairs(items, index, type, e) {
  179. if (index === 0) return;
  180. const lastItem = items[index - 1];
  181. const thisItem = items[index];
  182. if (lastItem.file && lastItem.options === undefined && typeof thisItem.value === "object") {
  183. e.message += `\n- Maybe you meant to use\n` + `"${type}s": [\n ["${lastItem.file.request}", ${JSON.stringify(thisItem.value, undefined, 2)}]\n]\n` + `To be a valid ${type}, its name and options should be wrapped in a pair of brackets`;
  184. }
  185. }
  186. 0 && 0;
  187. //# sourceMappingURL=options.js.map