UNPKG

1.98 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.validatePluginObject = validatePluginObject;
7
8var _optionAssertions = require("./option-assertions");
9
10const VALIDATORS = {
11 name: _optionAssertions.assertString,
12 manipulateOptions: _optionAssertions.assertFunction,
13 pre: _optionAssertions.assertFunction,
14 post: _optionAssertions.assertFunction,
15 inherits: _optionAssertions.assertFunction,
16 visitor: assertVisitorMap,
17 parserOverride: _optionAssertions.assertFunction,
18 generatorOverride: _optionAssertions.assertFunction
19};
20
21function assertVisitorMap(loc, value) {
22 const obj = (0, _optionAssertions.assertObject)(loc, value);
23
24 if (obj) {
25 Object.keys(obj).forEach(prop => assertVisitorHandler(prop, obj[prop]));
26
27 if (obj.enter || obj.exit) {
28 throw new Error(`${(0, _optionAssertions.msg)(loc)} cannot contain catch-all "enter" or "exit" handlers. Please target individual nodes.`);
29 }
30 }
31
32 return obj;
33}
34
35function assertVisitorHandler(key, value) {
36 if (value && typeof value === "object") {
37 Object.keys(value).forEach(handler => {
38 if (handler !== "enter" && handler !== "exit") {
39 throw new Error(`.visitor["${key}"] may only have .enter and/or .exit handlers.`);
40 }
41 });
42 } else if (typeof value !== "function") {
43 throw new Error(`.visitor["${key}"] must be a function`);
44 }
45
46 return value;
47}
48
49function validatePluginObject(obj) {
50 const rootPath = {
51 type: "root",
52 source: "plugin"
53 };
54 Object.keys(obj).forEach(key => {
55 const validator = VALIDATORS[key];
56
57 if (validator) {
58 const optLoc = {
59 type: "option",
60 name: key,
61 parent: rootPath
62 };
63 validator(optLoc, obj[key]);
64 } else {
65 const invalidPluginPropertyError = new Error(`.${key} is not a valid Plugin property`);
66 invalidPluginPropertyError.code = "BABEL_UNKNOWN_PLUGIN_PROPERTY";
67 throw invalidPluginPropertyError;
68 }
69 });
70 return obj;
71}
\No newline at end of file