Debian Codegraph @

packages/babel-helper-plugin-utils/src/index.ts

raw
1 import type {
2 PluginAPI,
3 PluginObject,
4 PluginPass,
5 PresetAPI,
6 PresetObject,
7 } from "@babel/core";
8
9 type APIPolyfillFactory<T extends keyof PluginAPI> = (
10 api: PluginAPI,
11 ) => PluginAPI[T];
12
13 type APIPolyfills = {
14 assertVersion: APIPolyfillFactory<"assertVersion">;
15 targets: APIPolyfillFactory<"targets">;
16 assumption: APIPolyfillFactory<"assumption">;
17 };
18
19 const apiPolyfills: APIPolyfills = {
20 // Not supported by Babel 7 and early versions of Babel 7 beta.
21 // It's important that this is polyfilled for older Babel versions
22 // since it's needed to report the version mismatch.
23 assertVersion: (api: PluginAPI) => (range: number | string) => {
24 throwVersionError(range, api.version);
25 },
26 // This is supported starting from Babel 7.13
27 // TODO(Babel 8): Remove this polyfill
28 targets: () => () => {
29 return {};
30 },
31 // This is supported starting from Babel 7.13
32 // TODO(Babel 8): Remove this polyfill
33 assumption: () => () => {
34 return undefined;
35 },
36 };
37
38 export function declare<State = {}, Option = {}>(
39 builder: (
40 api: PluginAPI,
41 options: Option,
42 dirname: string,
43 ) => PluginObject<State & PluginPass>,
44 ): (
45 api: PluginAPI,
46 options: Option,
47 dirname: string,
48 ) => PluginObject<State & PluginPass> {
49 return (api, options: Option, dirname: string) => {
50 let clonedApi: PluginAPI;
51
52 for (const name of Object.keys(
53 apiPolyfills,
54 ) as (keyof typeof apiPolyfills)[]) {
55 if (api[name]) continue;
56
57 // TODO: Use ??= when flow lets us to do so
58 clonedApi = clonedApi ?? copyApiObject(api);
59 // @ts-expect-error The shape of API polyfill is guaranteed by APIPolyfillFactory
60 clonedApi[name] = apiPolyfills[name](clonedApi);
61 }
62
63 // @ts-expect-error options || {} may not be assigned to Options
64 return builder(clonedApi ?? api, options || {}, dirname);
65 };
66 }
67
68 export const declarePreset = declare as <Option = {}>(
69 builder: (api: PresetAPI, options: Option, dirname: string) => PresetObject,
70 ) => (api: PresetAPI, options: Option, dirname: string) => PresetObject;
71
72 function copyApiObject(api: PluginAPI): PluginAPI {
73 // Babel >= 7 <= beta.41 passed the API as a new object that had
74 // babel/core as the prototype. While slightly faster, it also
75 // means that the Object.assign copy below fails. Rather than
76 // keep complexity, the Babel 6 behavior has been reverted and this
77 // normalizes all that for Babel 7.
78 let proto = null;
79 if (typeof api.version === "string" && /^7\./.test(api.version)) {
80 proto = Object.getPrototypeOf(api);
81 if (
82 proto &&
83 (!has(proto, "version") ||
84 !has(proto, "transform") ||
85 !has(proto, "template") ||
86 !has(proto, "types"))
87 ) {
88 proto = null;
89 }
90 }
91
92 return {
93 ...proto,
94 ...api,
95 };
96 }
97
98 function has(obj: {}, key: string) {
99 return Object.prototype.hasOwnProperty.call(obj, key);
100 }
101
102 function throwVersionError(range: string | number, version: string) {
103 if (typeof range === "number") {
104 if (!Number.isInteger(range)) {
105 throw new Error("Expected string or integer value.");
106 }
107 range = `^${range}.0.0-0`;
108 }
109 if (typeof range !== "string") {
110 throw new Error("Expected string or integer value.");
111 }
112
113 const limit = Error.stackTraceLimit;
114
115 if (typeof limit === "number" && limit < 25) {
116 // Bump up the limit if needed so that users are more likely
117 // to be able to see what is calling Babel.
118 Error.stackTraceLimit = 25;
119 }
120
121 let err;
122 if (version.slice(0, 2) === "7.") {
123 err = new Error(
124 `Requires Babel "^7.0.0-beta.41", but was loaded with "${version}". ` +
125 `You'll need to update your @babel/core version.`,
126 );
127 } else {
128 err = new Error(
129 `Requires Babel "${range}", but was loaded with "${version}". ` +
130 `If you are sure you have a compatible version of @babel/core, ` +
131 `it is likely that something in your build process is loading the ` +
132 `wrong version. Inspect the stack trace of this error to look for ` +
133 `the first entry that doesn't mention "@babel/core" or "babel-core" ` +
134 `to see what is calling Babel.`,
135 );
136 }
137
138 if (typeof limit === "number") {
139 Error.stackTraceLimit = limit;
140 }
141
142 throw Object.assign(err, {
143 code: "BABEL_VERSION_UNSUPPORTED",
144 version,
145 range,
146 } as any);
147 }