Debian Codegraph @

packages/babel-core/src/config/validation/plugins.ts

raw
1 import {
2 assertString,
3 assertFunction,
4 assertObject,
5 msg,
6 } from "./option-assertions";
7
8 import type {
9 ValidatorSet,
10 Validator,
11 OptionPath,
12 RootPath,
13 } from "./option-assertions";
14 import type { ParserOptions } from "@babel/parser";
15 import type { Visitor } from "@babel/traverse";
16 import type { ValidatedOptions } from "./options";
17 import type { File, PluginPass } from "../../index";
18
19 // Note: The casts here are just meant to be static assertions to make sure
20 // that the assertion functions actually assert that the value's type matches
21 // the declared types.
22 const VALIDATORS: ValidatorSet = {
23 name: assertString as Validator<PluginObject["name"]>,
24 manipulateOptions: assertFunction as Validator<
25 PluginObject["manipulateOptions"]
26 >,
27 pre: assertFunction as Validator<PluginObject["pre"]>,
28 post: assertFunction as Validator<PluginObject["post"]>,
29 inherits: assertFunction as Validator<PluginObject["inherits"]>,
30 visitor: assertVisitorMap as Validator<PluginObject["visitor"]>,
31
32 parserOverride: assertFunction as Validator<PluginObject["parserOverride"]>,
33 generatorOverride: assertFunction as Validator<
34 PluginObject["generatorOverride"]
35 >,
36 };
37
38 function assertVisitorMap(loc: OptionPath, value: unknown): Visitor {
39 const obj = assertObject(loc, value);
40 if (obj) {
41 Object.keys(obj).forEach(prop => assertVisitorHandler(prop, obj[prop]));
42
43 if (obj.enter || obj.exit) {
44 throw new Error(
45 `${msg(
46 loc,
47 )} cannot contain catch-all "enter" or "exit" handlers. Please target individual nodes.`,
48 );
49 }
50 }
51 return obj as Visitor;
52 }
53
54 function assertVisitorHandler(
55 key: string,
56 value: unknown,
57 ): VisitorHandler | void {
58 if (value && typeof value === "object") {
59 Object.keys(value).forEach((handler: string) => {
60 if (handler !== "enter" && handler !== "exit") {
61 throw new Error(
62 `.visitor["${key}"] may only have .enter and/or .exit handlers.`,
63 );
64 }
65 });
66 } else if (typeof value !== "function") {
67 throw new Error(`.visitor["${key}"] must be a function`);
68 }
69
70 return value as any;
71 }
72
73 type VisitorHandler =
74 | Function
75 | {
76 enter?: Function;
77 exit?: Function;
78 };
79
80 export type PluginObject<S extends PluginPass = PluginPass> = {
81 name?: string;
82 manipulateOptions?: (
83 options: ValidatedOptions,
84 parserOpts: ParserOptions,
85 ) => void;
86 pre?: (this: S, file: File) => void;
87 post?: (this: S, file: File) => void;
88 inherits?: Function;
89 visitor?: Visitor<S>;
90 parserOverride?: Function;
91 generatorOverride?: Function;
92 };
93
94 export function validatePluginObject(obj: {
95 [key: string]: unknown;
96 }): PluginObject {
97 const rootPath: RootPath = {
98 type: "root",
99 source: "plugin",
100 };
101 Object.keys(obj).forEach((key: string) => {
102 const validator = VALIDATORS[key];
103
104 if (validator) {
105 const optLoc: OptionPath = {
106 type: "option",
107 name: key,
108 parent: rootPath,
109 };
110 validator(optLoc, obj[key]);
111 } else {
112 const invalidPluginPropertyError = new Error(
113 `.${key} is not a valid Plugin property`,
114 );
115 // @ts-expect-error todo(flow->ts) consider additing BabelConfigError with code field
116 invalidPluginPropertyError.code = "BABEL_UNKNOWN_PLUGIN_PROPERTY";
117 throw invalidPluginPropertyError;
118 }
119 });
120
121 return obj as any;
122 }