forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
normalize-options.skip-bundled.js
255 lines (226 loc) 路 7.92 KB
/
normalize-options.skip-bundled.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
251
252
253
254
255
import _normalizeOptions, {
checkDuplicateIncludeExcludes,
validateModulesOption,
validateUseBuiltInsOption,
normalizePluginName,
} from "../lib/normalize-options.js";
const normalizeOptions = _normalizeOptions.default || _normalizeOptions;
describe("normalize-options", () => {
describe("normalizeOptions", () => {
it("should return normalized `include` and `exclude`", () => {
const normalized = normalizeOptions({
include: [
"babel-plugin-transform-spread",
"transform-classes",
"@babel/plugin-transform-unicode-regex",
"@babel/transform-block-scoping",
],
exclude: [
"babel-plugin-transform-for-of",
"transform-parameters",
"@babel/plugin-transform-regenerator",
"@babel/transform-new-target",
],
});
expect(normalized.include).toEqual([
"transform-spread",
"transform-classes",
"transform-unicode-regex",
"transform-block-scoping",
]);
expect(normalized.exclude).toEqual([
"transform-for-of",
"transform-parameters",
"transform-regenerator",
"transform-new-target",
]);
});
it("should not normalize babel-plugin with prefix", () => {
const normalized = normalizePluginName("prefix-babel-plugin-postfix");
expect(normalized).toBe("prefix-babel-plugin-postfix");
});
test.each`
include | exclude
${["babel-plugin-transform-spread"]} | ${["transform-spread"]}
${["@babel/plugin-transform-spread"]} | ${["transform-spread"]}
${["transform-spread"]} | ${["babel-plugin-transform-spread"]}
${["transform-spread"]} | ${["@babel/plugin-transform-spread"]}
${["babel-plugin-transform-spread"]} | ${["@babel/plugin-transform-spread"]}
${["@babel/plugin-transform-spread"]} | ${["babel-plugin-transform-spread"]}
${["@babel/plugin-transform-spread"]} | ${["@babel/transform-spread"]}
${["@babel/transform-spread"]} | ${["@babel/plugin-transform-spread"]}
${["babel-plugin-transform-spread"]} | ${["@babel/transform-spread"]}
${["@babel/transform-spread"]} | ${["babel-plugin-transform-spread"]}
`(
"should throw if with includes $include and excludes $exclude",
({ include, exclude }) => {
expect(() => normalizeOptions({ include, exclude })).toThrowError(
/were found in both/,
);
},
);
it("should not throw if corejs version is valid", () => {
[2, 2.1, 3, 3.5].forEach(corejs => {
["entry", "usage"].forEach(useBuiltIns => {
expect(() =>
normalizeOptions({ useBuiltIns, corejs }),
).not.toThrowError();
});
});
});
it("should throw if corejs version is invalid", () => {
[1, 1.2, 4, 4.5].forEach(corejs => {
["entry", "usage"].forEach(useBuiltIns => {
expect(() => normalizeOptions({ useBuiltIns, corejs })).toThrowError(
/The version passed to `corejs` is invalid./,
);
});
});
});
it("throws when including module plugins", () => {
expect(() =>
normalizeOptions({ include: ["proposal-dynamic-import"] }),
).toThrow();
expect(() =>
normalizeOptions({ include: ["transform-modules-amd"] }),
).toThrow();
});
it("allows exclusion of module plugins", () => {
expect(() =>
normalizeOptions({ exclude: ["proposal-dynamic-import"] }),
).not.toThrow();
expect(() =>
normalizeOptions({ exclude: ["transform-modules-commonjs"] }),
).not.toThrow();
});
});
describe("Config format validation", () => {
it("should throw if top-level option not found", () => {
const unknownTopLevelOption = () => {
normalizeOptions({ unknown: "option" });
};
expect(unknownTopLevelOption).toThrow();
});
});
describe("RegExp include/exclude", () => {
it("should not allow invalid plugins in `include` and `exclude`", () => {
const normalizeWithNonExistingPlugin = () => {
normalizeOptions({
include: ["non-existing-plugin"],
});
};
expect(normalizeWithNonExistingPlugin).toThrow(Error);
});
it("should expand regular expressions in `include` and `exclude`", () => {
const normalized = normalizeOptions({
include: ["^[a-z]*-spread", "babel-plugin-transform-classes"],
});
expect(normalized.include).toEqual([
"transform-spread",
"transform-classes",
]);
});
it("should expand regular expressions in `include` and `exclude`", () => {
const normalized = normalizeOptions({
useBuiltIns: "entry",
corejs: 3,
exclude: ["es.math.log.*"],
});
expect(normalized.exclude).toEqual([
"es.math.log10",
"es.math.log1p",
"es.math.log2",
]);
});
it("should not allow the same modules in `include` and `exclude`", () => {
const normalizeWithNonExistingPlugin = () => {
normalizeOptions({
useBuiltIns: "entry",
corejs: 3,
include: ["es.math.log2"],
exclude: ["es.math.log.*"],
});
};
expect(normalizeWithNonExistingPlugin).toThrow(Error);
});
it("should not do partial match if not explicitly defined `include` and `exclude`", () => {
const normalized = normalizeOptions({
useBuiltIns: "entry",
corejs: 3,
include: ["es.reflect.set-prototype-of"],
exclude: ["es.reflect.set"],
});
expect(normalized.include).toEqual(["es.reflect.set-prototype-of"]);
expect(normalized.exclude).toEqual(["es.reflect.set"]);
});
});
describe("checkDuplicateIncludeExcludes", function () {
it("should throw if duplicate names in both", function () {
expect(() => {
checkDuplicateIncludeExcludes(
["transform-regenerator", "map"],
["transform-regenerator", "map"],
);
}).toThrow();
});
it("should not throw if no duplicate names in both", function () {
expect(() => {
checkDuplicateIncludeExcludes(["transform-regenerator"], ["map"]);
}).not.toThrow();
});
});
describe("validateModulesOption", () => {
it("`undefined` option returns auto", () => {
expect(validateModulesOption()).toBe("auto");
});
it("`false` option returns false", () => {
expect(validateModulesOption(false)).toBe(false);
});
it("auto option is valid", () => {
expect(validateModulesOption("auto")).toBe("auto");
});
it("commonjs option is valid", () => {
expect(validateModulesOption("commonjs")).toBe("commonjs");
});
it("systemjs option is valid", () => {
expect(validateModulesOption("systemjs")).toBe("systemjs");
});
it("amd option is valid", () => {
expect(validateModulesOption("amd")).toBe("amd");
});
it("umd option is valid", () => {
expect(validateModulesOption("umd")).toBe("umd");
});
it("`true` option is invalid", () => {
expect(() => {
validateModulesOption(true);
}).toThrow();
});
it("`'false'` option is invalid", () => {
expect(() => {
validateModulesOption("false");
}).toThrow();
});
it("array option is invalid", () => {
expect(() => {
validateModulesOption([]);
}).toThrow();
});
});
describe("validateUseBuiltInsOptions", () => {
it("usage option is valid", () => {
expect(validateUseBuiltInsOption("usage")).toBe("usage");
});
it("entry option is valid", () => {
expect(validateUseBuiltInsOption("entry")).toBe("entry");
});
it("`false` option returns false", () => {
expect(validateUseBuiltInsOption(false)).toBe(false);
});
it("`'false'` option is invalid", () => {
expect(() => {
validateUseBuiltInsOption("false");
}).toThrow();
});
});
});