/
assumptions.js
250 lines (205 loc) 路 6.31 KB
/
assumptions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import path from "path";
import { fileURLToPath } from "url";
import { loadOptions as loadOptionsOrig, transformSync } from "../lib/index.js";
import pluginCommonJS from "@babel/plugin-transform-modules-commonjs";
const cwd = path.dirname(fileURLToPath(import.meta.url));
function loadOptions(opts) {
return loadOptionsOrig({ cwd, ...opts });
}
function withAssumptions(assumptions) {
return loadOptions({ assumptions });
}
describe("assumptions", () => {
it("throws if invalid name", () => {
expect(() => withAssumptions({ foo: true })).toThrow(
`.assumptions["foo"] is not a supported assumption.`,
);
expect(() => withAssumptions({ setPublicClassFields: true })).not.toThrow();
});
it("throws if not boolean", () => {
expect(() => withAssumptions({ setPublicClassFields: "yes" })).toThrow(
`.assumptions["setPublicClassFields"] must be a boolean.`,
);
expect(() => withAssumptions({ setPublicClassFields: true })).not.toThrow();
expect(() =>
withAssumptions({ setPublicClassFields: false }),
).not.toThrow();
});
it("can be enabled by presets", () => {
expect(
loadOptions({
assumptions: {
setPublicClassFields: true,
},
presets: [() => ({ assumptions: { mutableTemplateObject: true } })],
}).assumptions,
).toEqual({
setPublicClassFields: true,
mutableTemplateObject: true,
});
});
it("cannot be disabled by presets", () => {
expect(() =>
loadOptions({
presets: [() => ({ assumptions: { mutableTemplateObject: false } })],
}),
).toThrow(
` .assumptions["mutableTemplateObject"] cannot be set to 'false' inside presets.`,
);
});
it("can be queried from plugins", () => {
let setPublicClassFields;
let unknownAssumption;
transformSync("", {
configFile: false,
browserslistConfigFile: false,
assumptions: {
setPublicClassFields: true,
},
plugins: [
api => {
setPublicClassFields = api.assumption("setPublicClassFields");
// Unknown assumptions don't throw, so that plugins can keep compat
// with older @babel/core versions when they introduce support for
// a new assumption.
unknownAssumption = api.assumption("unknownAssumption");
return {};
},
],
});
expect(setPublicClassFields).toBe(true);
expect(unknownAssumption).toBe(undefined);
});
it("cannot be queried from presets", () => {
let assumptionFn;
transformSync("", {
configFile: false,
browserslistConfigFile: false,
presets: [
api => {
assumptionFn = api.assumption;
return {};
},
],
});
expect(assumptionFn).toBeUndefined();
});
// https://github.com/babel/babel/issues/13316
describe("assumptions set in presets are visible from plugins - #13316", () => {
function presetEnumerableModuleMeta() {
return { assumptions: { enumerableModuleMeta: true } };
}
function pluginExtractEnumerableModuleMeta(api, options) {
options.enumerableModuleMeta = api.assumption("enumerableModuleMeta");
return { visitor: {} };
}
it("plugin defined outside preset", () => {
const ref = {};
loadOptions({
configFile: false,
presets: [presetEnumerableModuleMeta],
plugins: [[pluginExtractEnumerableModuleMeta, ref]],
});
expect(ref.enumerableModuleMeta).toBe(true);
});
it("plugin defined inside preset", () => {
const ref = {};
loadOptions({
configFile: false,
presets: [
() => ({
assumptions: { enumerableModuleMeta: true },
plugins: [[pluginExtractEnumerableModuleMeta, ref]],
}),
],
});
expect(ref.enumerableModuleMeta).toBe(true);
});
it("integration", () => {
const { code } = transformSync(`export const foo = 1;`, {
configFile: false,
presets: [presetEnumerableModuleMeta],
plugins: [pluginCommonJS],
});
expect(code).toMatchInlineSnapshot(`
"\\"use strict\\";
exports.__esModule = true;
exports.foo = void 0;
const foo = 1;
exports.foo = foo;"
`);
});
});
describe("plugin cache", () => {
const makePlugin = () =>
jest.fn(api => {
api.assumption("setPublicClassFields");
return {};
});
const run = (plugin, assumptions) =>
transformSync("", {
assumptions,
configFile: false,
browserslistConfigFile: false,
plugins: [plugin],
});
it("is not invalidated when assumptions don't change", () => {
const plugin = makePlugin();
run(plugin, {
setPublicClassFields: true,
mutableTemplateObject: false,
});
run(plugin, {
setPublicClassFields: true,
mutableTemplateObject: false,
});
expect(plugin).toHaveBeenCalledTimes(1);
});
it("is not invalidated when unused assumptions change", () => {
const plugin = makePlugin();
run(plugin, {
setPublicClassFields: true,
mutableTemplateObject: false,
});
run(plugin, {
setPublicClassFields: true,
mutableTemplateObject: true,
});
expect(plugin).toHaveBeenCalledTimes(1);
});
it("is invalidated when used assumptions change", () => {
const plugin = makePlugin();
run(plugin, {
setPublicClassFields: true,
mutableTemplateObject: false,
});
run(plugin, {
setPublicClassFields: false,
mutableTemplateObject: true,
});
expect(plugin).toHaveBeenCalledTimes(2);
});
it("is invalidated when used assumptions are added", () => {
const plugin = makePlugin();
run(plugin, {
mutableTemplateObject: false,
});
run(plugin, {
mutableTemplateObject: false,
setPublicClassFields: true,
});
expect(plugin).toHaveBeenCalledTimes(2);
});
it("is invalidated when used assumptions are removed", () => {
const plugin = makePlugin();
run(plugin, {
setPublicClassFields: true,
mutableTemplateObject: false,
});
run(plugin, {
mutableTemplateObject: true,
});
expect(plugin).toHaveBeenCalledTimes(2);
});
});
});