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