/
assumptions.js
180 lines (148 loc) 路 4.44 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
import { loadOptions as loadOptionsOrig, transformSync } from "../lib";
function loadOptions(opts) {
return loadOptionsOrig({ cwd: __dirname, ...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 set by presets", () => {
expect(
loadOptions({
assumptions: {
setPublicClassFields: true,
},
presets: [() => ({ assumptions: { mutableTemplateObject: true } })],
}).assumptions,
).toEqual({
setPublicClassFields: true,
mutableTemplateObject: true,
// This is enabled by default
newableArrowFunctions: true,
});
});
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 default to "false" and 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(false);
});
it("cannot be queried from presets", () => {
let assumptionFn;
transformSync("", {
configFile: false,
browserslistConfigFile: false,
presets: [
api => {
assumptionFn = api.assumption;
return {};
},
],
});
expect(assumptionFn).toBeUndefined();
});
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);
});
});
});