fad1559ee970c94041f112c0f554bbf849b83771f77404151fb374104d6579d260b5995ce3c899df871aa1a369a2f6878e10067bd0172e2fa30dcf57481632 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.access = access;
  6. exports.assertArray = assertArray;
  7. exports.assertAssumptions = assertAssumptions;
  8. exports.assertBabelrcSearch = assertBabelrcSearch;
  9. exports.assertBoolean = assertBoolean;
  10. exports.assertCallerMetadata = assertCallerMetadata;
  11. exports.assertCompact = assertCompact;
  12. exports.assertConfigApplicableTest = assertConfigApplicableTest;
  13. exports.assertConfigFileSearch = assertConfigFileSearch;
  14. exports.assertFunction = assertFunction;
  15. exports.assertIgnoreList = assertIgnoreList;
  16. exports.assertInputSourceMap = assertInputSourceMap;
  17. exports.assertObject = assertObject;
  18. exports.assertPluginList = assertPluginList;
  19. exports.assertRootMode = assertRootMode;
  20. exports.assertSourceMaps = assertSourceMaps;
  21. exports.assertSourceType = assertSourceType;
  22. exports.assertString = assertString;
  23. exports.assertTargets = assertTargets;
  24. exports.msg = msg;
  25. function _helperCompilationTargets() {
  26. const data = require("@babel/helper-compilation-targets");
  27. _helperCompilationTargets = function () {
  28. return data;
  29. };
  30. return data;
  31. }
  32. var _options = require("./options.js");
  33. function msg(loc) {
  34. switch (loc.type) {
  35. case "root":
  36. return ``;
  37. case "env":
  38. return `${msg(loc.parent)}.env["${loc.name}"]`;
  39. case "overrides":
  40. return `${msg(loc.parent)}.overrides[${loc.index}]`;
  41. case "option":
  42. return `${msg(loc.parent)}.${loc.name}`;
  43. case "access":
  44. return `${msg(loc.parent)}[${JSON.stringify(loc.name)}]`;
  45. default:
  46. throw new Error(`Assertion failure: Unknown type ${loc.type}`);
  47. }
  48. }
  49. function access(loc, name) {
  50. return {
  51. type: "access",
  52. name,
  53. parent: loc
  54. };
  55. }
  56. function assertRootMode(loc, value) {
  57. if (value !== undefined && value !== "root" && value !== "upward" && value !== "upward-optional") {
  58. throw new Error(`${msg(loc)} must be a "root", "upward", "upward-optional" or undefined`);
  59. }
  60. return value;
  61. }
  62. function assertSourceMaps(loc, value) {
  63. if (value !== undefined && typeof value !== "boolean" && value !== "inline" && value !== "both") {
  64. throw new Error(`${msg(loc)} must be a boolean, "inline", "both", or undefined`);
  65. }
  66. return value;
  67. }
  68. function assertCompact(loc, value) {
  69. if (value !== undefined && typeof value !== "boolean" && value !== "auto") {
  70. throw new Error(`${msg(loc)} must be a boolean, "auto", or undefined`);
  71. }
  72. return value;
  73. }
  74. function assertSourceType(loc, value) {
  75. if (value !== undefined && value !== "module" && value !== "script" && value !== "unambiguous") {
  76. throw new Error(`${msg(loc)} must be "module", "script", "unambiguous", or undefined`);
  77. }
  78. return value;
  79. }
  80. function assertCallerMetadata(loc, value) {
  81. const obj = assertObject(loc, value);
  82. if (obj) {
  83. if (typeof obj.name !== "string") {
  84. throw new Error(`${msg(loc)} set but does not contain "name" property string`);
  85. }
  86. for (const prop of Object.keys(obj)) {
  87. const propLoc = access(loc, prop);
  88. const value = obj[prop];
  89. if (value != null && typeof value !== "boolean" && typeof value !== "string" && typeof value !== "number") {
  90. throw new Error(`${msg(propLoc)} must be null, undefined, a boolean, a string, or a number.`);
  91. }
  92. }
  93. }
  94. return value;
  95. }
  96. function assertInputSourceMap(loc, value) {
  97. if (value !== undefined && typeof value !== "boolean" && (typeof value !== "object" || !value)) {
  98. throw new Error(`${msg(loc)} must be a boolean, object, or undefined`);
  99. }
  100. return value;
  101. }
  102. function assertString(loc, value) {
  103. if (value !== undefined && typeof value !== "string") {
  104. throw new Error(`${msg(loc)} must be a string, or undefined`);
  105. }
  106. return value;
  107. }
  108. function assertFunction(loc, value) {
  109. if (value !== undefined && typeof value !== "function") {
  110. throw new Error(`${msg(loc)} must be a function, or undefined`);
  111. }
  112. return value;
  113. }
  114. function assertBoolean(loc, value) {
  115. if (value !== undefined && typeof value !== "boolean") {
  116. throw new Error(`${msg(loc)} must be a boolean, or undefined`);
  117. }
  118. return value;
  119. }
  120. function assertObject(loc, value) {
  121. if (value !== undefined && (typeof value !== "object" || Array.isArray(value) || !value)) {
  122. throw new Error(`${msg(loc)} must be an object, or undefined`);
  123. }
  124. return value;
  125. }
  126. function assertArray(loc, value) {
  127. if (value != null && !Array.isArray(value)) {
  128. throw new Error(`${msg(loc)} must be an array, or undefined`);
  129. }
  130. return value;
  131. }
  132. function assertIgnoreList(loc, value) {
  133. const arr = assertArray(loc, value);
  134. arr == null || arr.forEach((item, i) => assertIgnoreItem(access(loc, i), item));
  135. return arr;
  136. }
  137. function assertIgnoreItem(loc, value) {
  138. if (typeof value !== "string" && typeof value !== "function" && !(value instanceof RegExp)) {
  139. throw new Error(`${msg(loc)} must be an array of string/Function/RegExp values, or undefined`);
  140. }
  141. return value;
  142. }
  143. function assertConfigApplicableTest(loc, value) {
  144. if (value === undefined) {
  145. return value;
  146. }
  147. if (Array.isArray(value)) {
  148. value.forEach((item, i) => {
  149. if (!checkValidTest(item)) {
  150. throw new Error(`${msg(access(loc, i))} must be a string/Function/RegExp.`);
  151. }
  152. });
  153. } else if (!checkValidTest(value)) {
  154. throw new Error(`${msg(loc)} must be a string/Function/RegExp, or an array of those`);
  155. }
  156. return value;
  157. }
  158. function checkValidTest(value) {
  159. return typeof value === "string" || typeof value === "function" || value instanceof RegExp;
  160. }
  161. function assertConfigFileSearch(loc, value) {
  162. if (value !== undefined && typeof value !== "boolean" && typeof value !== "string") {
  163. throw new Error(`${msg(loc)} must be a undefined, a boolean, a string, ` + `got ${JSON.stringify(value)}`);
  164. }
  165. return value;
  166. }
  167. function assertBabelrcSearch(loc, value) {
  168. if (value === undefined || typeof value === "boolean") {
  169. return value;
  170. }
  171. if (Array.isArray(value)) {
  172. value.forEach((item, i) => {
  173. if (!checkValidTest(item)) {
  174. throw new Error(`${msg(access(loc, i))} must be a string/Function/RegExp.`);
  175. }
  176. });
  177. } else if (!checkValidTest(value)) {
  178. throw new Error(`${msg(loc)} must be a undefined, a boolean, a string/Function/RegExp ` + `or an array of those, got ${JSON.stringify(value)}`);
  179. }
  180. return value;
  181. }
  182. function assertPluginList(loc, value) {
  183. const arr = assertArray(loc, value);
  184. if (arr) {
  185. arr.forEach((item, i) => assertPluginItem(access(loc, i), item));
  186. }
  187. return arr;
  188. }
  189. function assertPluginItem(loc, value) {
  190. if (Array.isArray(value)) {
  191. if (value.length === 0) {
  192. throw new Error(`${msg(loc)} must include an object`);
  193. }
  194. if (value.length > 3) {
  195. throw new Error(`${msg(loc)} may only be a two-tuple or three-tuple`);
  196. }
  197. assertPluginTarget(access(loc, 0), value[0]);
  198. if (value.length > 1) {
  199. const opts = value[1];
  200. if (opts !== undefined && opts !== false && (typeof opts !== "object" || Array.isArray(opts) || opts === null)) {
  201. throw new Error(`${msg(access(loc, 1))} must be an object, false, or undefined`);
  202. }
  203. }
  204. if (value.length === 3) {
  205. const name = value[2];
  206. if (name !== undefined && typeof name !== "string") {
  207. throw new Error(`${msg(access(loc, 2))} must be a string, or undefined`);
  208. }
  209. }
  210. } else {
  211. assertPluginTarget(loc, value);
  212. }
  213. return value;
  214. }
  215. function assertPluginTarget(loc, value) {
  216. if ((typeof value !== "object" || !value) && typeof value !== "string" && typeof value !== "function") {
  217. throw new Error(`${msg(loc)} must be a string, object, function`);
  218. }
  219. return value;
  220. }
  221. function assertTargets(loc, value) {
  222. if ((0, _helperCompilationTargets().isBrowsersQueryValid)(value)) return value;
  223. if (typeof value !== "object" || !value || Array.isArray(value)) {
  224. throw new Error(`${msg(loc)} must be a string, an array of strings or an object`);
  225. }
  226. const browsersLoc = access(loc, "browsers");
  227. const esmodulesLoc = access(loc, "esmodules");
  228. assertBrowsersList(browsersLoc, value.browsers);
  229. assertBoolean(esmodulesLoc, value.esmodules);
  230. for (const key of Object.keys(value)) {
  231. const val = value[key];
  232. const subLoc = access(loc, key);
  233. if (key === "esmodules") assertBoolean(subLoc, val);else if (key === "browsers") assertBrowsersList(subLoc, val);else if (!hasOwnProperty.call(_helperCompilationTargets().TargetNames, key)) {
  234. const validTargets = Object.keys(_helperCompilationTargets().TargetNames).join(", ");
  235. throw new Error(`${msg(subLoc)} is not a valid target. Supported targets are ${validTargets}`);
  236. } else assertBrowserVersion(subLoc, val);
  237. }
  238. return value;
  239. }
  240. function assertBrowsersList(loc, value) {
  241. if (value !== undefined && !(0, _helperCompilationTargets().isBrowsersQueryValid)(value)) {
  242. throw new Error(`${msg(loc)} must be undefined, a string or an array of strings`);
  243. }
  244. }
  245. function assertBrowserVersion(loc, value) {
  246. if (typeof value === "number" && Math.round(value) === value) return;
  247. if (typeof value === "string") return;
  248. throw new Error(`${msg(loc)} must be a string or an integer number`);
  249. }
  250. function assertAssumptions(loc, value) {
  251. if (value === undefined) return;
  252. if (typeof value !== "object" || value === null) {
  253. throw new Error(`${msg(loc)} must be an object or undefined.`);
  254. }
  255. let root = loc;
  256. do {
  257. root = root.parent;
  258. } while (root.type !== "root");
  259. const inPreset = root.source === "preset";
  260. for (const name of Object.keys(value)) {
  261. const subLoc = access(loc, name);
  262. if (!_options.assumptionsNames.has(name)) {
  263. throw new Error(`${msg(subLoc)} is not a supported assumption.`);
  264. }
  265. if (typeof value[name] !== "boolean") {
  266. throw new Error(`${msg(subLoc)} must be a boolean.`);
  267. }
  268. if (inPreset && value[name] === false) {
  269. throw new Error(`${msg(subLoc)} cannot be set to 'false' inside presets.`);
  270. }
  271. }
  272. return value;
  273. }
  274. 0 && 0;
  275. //# sourceMappingURL=option-assertions.js.map