| 1 |
import * as t from "@babel/types"; |
| 2 |
|
| 3 |
import _generator from "../../babel-generator/lib/index.js"; |
| 4 |
import _template from "../lib/index.js"; |
| 5 |
const generator = _generator.default || _generator; |
| 6 |
const template = _template.default || _template; |
| 7 |
|
| 8 |
const comments = "// Sum two numbers\nconst add = (a, b) => a + b;"; |
| 9 |
|
| 10 |
describe("@babel/template", function () { |
| 11 |
it("import statements are allowed by default", function () { |
| 12 |
expect(function () { |
| 13 |
template("import foo from 'foo'")({}); |
| 14 |
}).not.toThrow(); |
| 15 |
}); |
| 16 |
|
| 17 |
it("with statements are allowed with sourceType: script", function () { |
| 18 |
expect(function () { |
| 19 |
template("with({}){}", { sourceType: "script" })({}); |
| 20 |
}).not.toThrow(); |
| 21 |
}); |
| 22 |
|
| 23 |
it("should strip comments by default", function () { |
| 24 |
const code = "const add = (a, b) => a + b;"; |
| 25 |
const output = template(comments)(); |
| 26 |
expect(generator(output).code).toBe(code); |
| 27 |
}); |
| 28 |
|
| 29 |
it("should preserve comments with a flag", function () { |
| 30 |
const output = template(comments, { preserveComments: true })(); |
| 31 |
expect(generator(output).code).toBe(comments); |
| 32 |
}); |
| 33 |
|
| 34 |
it("should preserve comments with a flag when using .ast", function () { |
| 35 |
const output1 = template.ast(comments, { preserveComments: true }); |
| 36 |
const output2 = template({ preserveComments: true }).ast(comments); |
| 37 |
expect(generator(output1).code).toBe(comments); |
| 38 |
expect(generator(output2).code).toBe(comments); |
| 39 |
}); |
| 40 |
|
| 41 |
describe("string-based", () => { |
| 42 |
it("should handle replacing values from an object", () => { |
| 43 |
const value = t.stringLiteral("some string value"); |
| 44 |
const result = template(` |
| 45 |
if (SOME_VAR === "") {} |
| 46 |
`)({ |
| 47 |
SOME_VAR: value, |
| 48 |
}); |
| 49 |
|
| 50 |
expect(result.type).toBe("IfStatement"); |
| 51 |
expect(result.test.type).toBe("BinaryExpression"); |
| 52 |
expect(result.test.left).toBe(value); |
| 53 |
}); |
| 54 |
|
| 55 |
it("should handle replacing values given an array", () => { |
| 56 |
const value = t.stringLiteral("some string value"); |
| 57 |
const result = template(` |
| 58 |
if ($0 === "") {} |
| 59 |
`)([value]); |
| 60 |
|
| 61 |
expect(result.type).toBe("IfStatement"); |
| 62 |
expect(result.test.type).toBe("BinaryExpression"); |
| 63 |
expect(result.test.left).toBe(value); |
| 64 |
}); |
| 65 |
|
| 66 |
it("should handle replacing values with null to remove them", () => { |
| 67 |
const result = template(` |
| 68 |
callee(ARG); |
| 69 |
`)({ ARG: null }); |
| 70 |
|
| 71 |
expect(result.type).toBe("ExpressionStatement"); |
| 72 |
expect(result.expression.type).toBe("CallExpression"); |
| 73 |
expect(result.expression.arguments).toEqual([]); |
| 74 |
}); |
| 75 |
|
| 76 |
it("should handle replacing values that are string content", () => { |
| 77 |
const result = template(` |
| 78 |
("ARG"); |
| 79 |
`)({ ARG: "some new content" }); |
| 80 |
|
| 81 |
expect(result.type).toBe("ExpressionStatement"); |
| 82 |
expect(result.expression.type).toBe("StringLiteral"); |
| 83 |
expect(result.expression.value).toBe("some new content"); |
| 84 |
}); |
| 85 |
|
| 86 |
it("should automatically clone nodes if they are injected twice", () => { |
| 87 |
const id = t.identifier("someIdent"); |
| 88 |
|
| 89 |
const result = template(` |
| 90 |
ID; |
| 91 |
ID; |
| 92 |
`)({ ID: id }); |
| 93 |
|
| 94 |
expect(result[0].type).toBe("ExpressionStatement"); |
| 95 |
expect(result[0].expression).toBe(id); |
| 96 |
expect(result[1].type).toBe("ExpressionStatement"); |
| 97 |
expect(result[1].expression).not.toBe(id); |
| 98 |
expect(result[1].expression).toEqual(id); |
| 99 |
}); |
| 100 |
|
| 101 |
it("should allow passing in a whitelist of replacement names", () => { |
| 102 |
const id = t.identifier("someIdent"); |
| 103 |
const result = template( |
| 104 |
` |
| 105 |
some_id; |
| 106 |
`, |
| 107 |
{ placeholderWhitelist: new Set(["some_id"]) }, |
| 108 |
)({ some_id: id }); |
| 109 |
|
| 110 |
expect(result.type).toBe("ExpressionStatement"); |
| 111 |
expect(result.expression).toBe(id); |
| 112 |
}); |
| 113 |
|
| 114 |
it("should allow passing in a RegExp to match replacement patterns", () => { |
| 115 |
const id = t.identifier("someIdent"); |
| 116 |
const result = template( |
| 117 |
` |
| 118 |
ID; |
| 119 |
ANOTHER_ID; |
| 120 |
`, |
| 121 |
{ placeholderPattern: /^ID$/ }, |
| 122 |
)({ ID: id }); |
| 123 |
|
| 124 |
expect(result[0].type).toBe("ExpressionStatement"); |
| 125 |
expect(result[0].expression).toBe(id); |
| 126 |
expect(result[1].type).toBe("ExpressionStatement"); |
| 127 |
expect(result[1].expression.type).toBe("Identifier"); |
| 128 |
expect(result[1].expression.name).toBe("ANOTHER_ID"); |
| 129 |
}); |
| 130 |
|
| 131 |
it("should throw if unknown replacements are provided", () => { |
| 132 |
expect(() => { |
| 133 |
template(` |
| 134 |
ID; |
| 135 |
`)({ ID: t.identifier("someIdent"), ANOTHER_ID: null }); |
| 136 |
}).toThrow('Unknown substitution "ANOTHER_ID" given'); |
| 137 |
}); |
| 138 |
|
| 139 |
it("should throw if placeholders are not given explicit values", () => { |
| 140 |
expect(() => { |
| 141 |
template(` |
| 142 |
ID; |
| 143 |
ANOTHER_ID; |
| 144 |
`)({ ID: t.identifier("someIdent") }); |
| 145 |
}).toThrow( |
| 146 |
`Error: No substitution given for "ANOTHER_ID". If this is not meant to be a |
| 147 |
placeholder you may want to consider passing one of the following options to @babel/template: |
| 148 |
- { placeholderPattern: false, placeholderWhitelist: new Set(['ANOTHER_ID'])} |
| 149 |
- { placeholderPattern: /^ANOTHER_ID$/ }`, |
| 150 |
); |
| 151 |
}); |
| 152 |
|
| 153 |
it("should return the AST directly when using .ast", () => { |
| 154 |
const result = template.ast(` |
| 155 |
if ("some string value" === "") {} |
| 156 |
`); |
| 157 |
|
| 158 |
expect(result.type).toBe("IfStatement"); |
| 159 |
expect(result.test.type).toBe("BinaryExpression"); |
| 160 |
expect(result.test.left.type).toBe("StringLiteral"); |
| 161 |
expect(result.test.left.value).toBe("some string value"); |
| 162 |
}); |
| 163 |
}); |
| 164 |
|
| 165 |
describe("literal-based", () => { |
| 166 |
it("should handle replacing values from an object", () => { |
| 167 |
const value = t.stringLiteral("some string value"); |
| 168 |
const result = template` |
| 169 |
if (${value} === "") {} |
| 170 |
`(); |
| 171 |
|
| 172 |
expect(result.type).toBe("IfStatement"); |
| 173 |
expect(result.test.type).toBe("BinaryExpression"); |
| 174 |
expect(result.test.left).toBe(value); |
| 175 |
}); |
| 176 |
|
| 177 |
it("should handle replacing values with null to remove them", () => { |
| 178 |
const result = template` |
| 179 |
callee(${null}); |
| 180 |
`(); |
| 181 |
|
| 182 |
expect(result.type).toBe("ExpressionStatement"); |
| 183 |
expect(result.expression.type).toBe("CallExpression"); |
| 184 |
expect(result.expression.arguments).toEqual([]); |
| 185 |
}); |
| 186 |
|
| 187 |
it("should handle replacing values that are string content", () => { |
| 188 |
const result = template` |
| 189 |
("${"some new content"}"); |
| 190 |
`(); |
| 191 |
|
| 192 |
expect(result.type).toBe("ExpressionStatement"); |
| 193 |
expect(result.expression.type).toBe("StringLiteral"); |
| 194 |
expect(result.expression.value).toBe("some new content"); |
| 195 |
}); |
| 196 |
|
| 197 |
it("should allow setting options by passing an object", () => { |
| 198 |
const result = template({ sourceType: "script" })` |
| 199 |
with({}){} |
| 200 |
`(); |
| 201 |
|
| 202 |
expect(result.type).toBe("WithStatement"); |
| 203 |
}); |
| 204 |
|
| 205 |
it("should return the AST directly when using .ast", () => { |
| 206 |
const value = t.stringLiteral("some string value"); |
| 207 |
const result = template.ast` |
| 208 |
if (${value} === "") {} |
| 209 |
`; |
| 210 |
|
| 211 |
expect(result.type).toBe("IfStatement"); |
| 212 |
expect(result.test.type).toBe("BinaryExpression"); |
| 213 |
expect(result.test.left).toBe(value); |
| 214 |
}); |
| 215 |
|
| 216 |
it("should return assertions in ImportDeclaration when using .ast", () => { |
| 217 |
const result = template.ast( |
| 218 |
`import json from "./foo.json" assert { type: "json" };`, |
| 219 |
{ |
| 220 |
plugins: ["importAssertions"], |
| 221 |
}, |
| 222 |
); |
| 223 |
|
| 224 |
expect(result.assertions[0].type).toBe("ImportAttribute"); |
| 225 |
}); |
| 226 |
|
| 227 |
it("should return assertions in ExportNamedDeclaration when using .ast", () => { |
| 228 |
const result = template.ast( |
| 229 |
`export { default as foo2 } from "foo.json" assert { type: "json" };`, |
| 230 |
{ |
| 231 |
plugins: ["importAssertions"], |
| 232 |
}, |
| 233 |
); |
| 234 |
|
| 235 |
expect(result.assertions[0].type).toBe("ImportAttribute"); |
| 236 |
}); |
| 237 |
|
| 238 |
it("should return assertions in ExportDefaultDeclaration when using .ast", () => { |
| 239 |
const result = template.ast( |
| 240 |
`export foo2 from "foo.json" assert { type: "json" };`, |
| 241 |
{ |
| 242 |
plugins: ["importAssertions", "exportDefaultFrom"], |
| 243 |
}, |
| 244 |
); |
| 245 |
|
| 246 |
expect(result.assertions[0].type).toBe("ImportAttribute"); |
| 247 |
}); |
| 248 |
|
| 249 |
it("should return assertions in ExportAllDeclaration when using .ast", () => { |
| 250 |
const result = template.ast( |
| 251 |
`export * from "foo.json" assert { type: "json" };`, |
| 252 |
{ |
| 253 |
plugins: ["importAssertions"], |
| 254 |
}, |
| 255 |
); |
| 256 |
|
| 257 |
expect(result.assertions[0].type).toBe("ImportAttribute"); |
| 258 |
}); |
| 259 |
|
| 260 |
it("should replace JSX placeholder", () => { |
| 261 |
const result = template.expression( |
| 262 |
` |
| 263 |
<TAG>{'content'}</TAG> |
| 264 |
`, |
| 265 |
{ |
| 266 |
plugins: ["jsx"], |
| 267 |
}, |
| 268 |
)({ |
| 269 |
TAG: t.jsxIdentifier("div"), |
| 270 |
}); |
| 271 |
|
| 272 |
expect(generator(result).code).toEqual("<div>{'content'}</div>"); |
| 273 |
}); |
| 274 |
}); |
| 275 |
|
| 276 |
describe(".syntacticPlaceholders", () => { |
| 277 |
it("works in function body", () => { |
| 278 |
const output = template(`function f() %%A%%`)({ |
| 279 |
A: t.blockStatement([]), |
| 280 |
}); |
| 281 |
expect(generator(output).code).toMatchInlineSnapshot(`"function f() {}"`); |
| 282 |
}); |
| 283 |
|
| 284 |
it("works in class body", () => { |
| 285 |
const output = template(`class C %%A%%`)({ |
| 286 |
A: t.classBody([]), |
| 287 |
}); |
| 288 |
expect(generator(output).code).toMatchInlineSnapshot(`"class C {}"`); |
| 289 |
}); |
| 290 |
|
| 291 |
it("replaces lowercase names", () => { |
| 292 |
const output = template(`%%foo%%`)({ |
| 293 |
foo: t.numericLiteral(1), |
| 294 |
}); |
| 295 |
expect(generator(output).code).toMatchInlineSnapshot(`"1;"`); |
| 296 |
}); |
| 297 |
|
| 298 |
it("pattern", () => { |
| 299 |
expect(() => { |
| 300 |
template(`%%A%% + %%B%%`, { |
| 301 |
placeholderPattern: /B/, |
| 302 |
})(); |
| 303 |
}).toThrow(/aren't compatible with '.syntacticPlaceholders: true'/); |
| 304 |
}); |
| 305 |
|
| 306 |
it("whitelist", () => { |
| 307 |
expect(() => { |
| 308 |
template(`%%A%% + %%B%%`, { |
| 309 |
placeholderPattern: false, |
| 310 |
placeholderWhitelist: new Set(["B"]), |
| 311 |
})(); |
| 312 |
}).toThrow(/aren't compatible with '.syntacticPlaceholders: true'/); |
| 313 |
}); |
| 314 |
|
| 315 |
describe("option value", () => { |
| 316 |
describe("true", () => { |
| 317 |
it("allows placeholders", () => { |
| 318 |
const output = template(`%%FOO%%`, { syntacticPlaceholders: true })({ |
| 319 |
FOO: t.numericLiteral(1), |
| 320 |
}); |
| 321 |
expect(generator(output).code).toMatchInlineSnapshot(`"1;"`); |
| 322 |
}); |
| 323 |
|
| 324 |
it("doesn't replace identifiers", () => { |
| 325 |
expect(() => { |
| 326 |
template(`FOO`, { syntacticPlaceholders: true })({ |
| 327 |
FOO: t.numericLiteral(1), |
| 328 |
}); |
| 329 |
}).toThrow(/Unknown substitution/); |
| 330 |
}); |
| 331 |
}); |
| 332 |
|
| 333 |
describe("false", () => { |
| 334 |
it("disallow placeholders", () => { |
| 335 |
expect(() => { |
| 336 |
template(`%%FOO%%`, { syntacticPlaceholders: false })({ |
| 337 |
FOO: t.numericLiteral(1), |
| 338 |
}); |
| 339 |
}).toThrow(/Unexpected token.*/); |
| 340 |
}); |
| 341 |
|
| 342 |
it("disallow manually enabled placeholders", () => { |
| 343 |
expect(() => { |
| 344 |
template(`%%FOO%%`, { |
| 345 |
syntacticPlaceholders: false, |
| 346 |
plugins: ["placeholders"], |
| 347 |
})({ |
| 348 |
FOO: t.numericLiteral(1), |
| 349 |
}); |
| 350 |
}).toThrow(/%%.*placeholders can't be used/); |
| 351 |
}); |
| 352 |
|
| 353 |
it("replaces identifiers", () => { |
| 354 |
const output = template(`FOO`, { syntacticPlaceholders: false })({ |
| 355 |
FOO: t.numericLiteral(1), |
| 356 |
}); |
| 357 |
expect(generator(output).code).toMatchInlineSnapshot(`"1;"`); |
| 358 |
}); |
| 359 |
|
| 360 |
it("allows v8intrinsics", () => { |
| 361 |
const output = template(`%DebugPrint(1)`, { |
| 362 |
syntacticPlaceholders: false, |
| 363 |
plugins: ["v8intrinsic"], |
| 364 |
})(); |
| 365 |
expect(generator(output).code).toMatchInlineSnapshot( |
| 366 |
`"%DebugPrint(1);"`, |
| 367 |
); |
| 368 |
}); |
| 369 |
}); |
| 370 |
|
| 371 |
describe("undefined", () => { |
| 372 |
it("allows placeholders", () => { |
| 373 |
const output = template(`%%FOO%%`)({ |
| 374 |
FOO: t.numericLiteral(1), |
| 375 |
}); |
| 376 |
expect(generator(output).code).toMatchInlineSnapshot(`"1;"`); |
| 377 |
}); |
| 378 |
|
| 379 |
it("replaces identifiers", () => { |
| 380 |
const output = template(`FOO`)({ |
| 381 |
FOO: t.numericLiteral(1), |
| 382 |
}); |
| 383 |
expect(generator(output).code).toMatchInlineSnapshot(`"1;"`); |
| 384 |
}); |
| 385 |
|
| 386 |
it("doesn't mix placeholder styles", () => { |
| 387 |
const output = template(`FOO + %%FOO%%`)({ |
| 388 |
FOO: t.numericLiteral(1), |
| 389 |
}); |
| 390 |
expect(generator(output).code).toMatchInlineSnapshot(`"FOO + 1;"`); |
| 391 |
}); |
| 392 |
}); |
| 393 |
}); |
| 394 |
|
| 395 |
it("works in var declaration", () => { |
| 396 |
const output = template("var %%LHS%% = %%RHS%%")({ |
| 397 |
LHS: t.identifier("x"), |
| 398 |
RHS: t.numericLiteral(7), |
| 399 |
}); |
| 400 |
expect(generator(output).code).toMatchInlineSnapshot(`"var x = 7;"`); |
| 401 |
}); |
| 402 |
|
| 403 |
it("works in const declaration", () => { |
| 404 |
const output = template("const %%LHS%% = %%RHS%%")({ |
| 405 |
LHS: t.identifier("x"), |
| 406 |
RHS: t.numericLiteral(7), |
| 407 |
}); |
| 408 |
expect(generator(output).code).toMatchInlineSnapshot(`"const x = 7;"`); |
| 409 |
}); |
| 410 |
|
| 411 |
it("works in let declaration", () => { |
| 412 |
const output = template("let %%LHS%% = %%RHS%%")({ |
| 413 |
LHS: t.identifier("x"), |
| 414 |
RHS: t.numericLiteral(7), |
| 415 |
}); |
| 416 |
expect(generator(output).code).toMatchInlineSnapshot(`"let x = 7;"`); |
| 417 |
}); |
| 418 |
}); |
| 419 |
}); |