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