| 1 |
import { parse } from "@babel/parser"; |
| 2 |
import * as t from "@babel/types"; |
| 3 |
import fs from "fs"; |
| 4 |
import path from "path"; |
| 5 |
import fixtures from "@babel/helper-fixtures"; |
| 6 |
import { TraceMap, originalPositionFor } from "@jridgewell/trace-mapping"; |
| 7 |
import { fileURLToPath } from "url"; |
| 8 |
|
| 9 |
import _generate, { CodeGenerator } from "../lib/index.js"; |
| 10 |
const generate = _generate.default || _generate; |
| 11 |
|
| 12 |
describe("generation", function () { |
| 13 |
it("multiple sources", function () { |
| 14 |
const sources = { |
| 15 |
"a.js": "function hi (msg) { console.log(msg); }\n", |
| 16 |
"b.js": "hi('hello');\n", |
| 17 |
}; |
| 18 |
const parsed = Object.keys(sources).reduce(function (_parsed, filename) { |
| 19 |
_parsed[filename] = parse(sources[filename], { |
| 20 |
sourceFilename: filename, |
| 21 |
}); |
| 22 |
return _parsed; |
| 23 |
}, {}); |
| 24 |
|
| 25 |
const combinedAst = { |
| 26 |
type: "File", |
| 27 |
program: { |
| 28 |
type: "Program", |
| 29 |
sourceType: "module", |
| 30 |
body: [].concat( |
| 31 |
parsed["a.js"].program.body, |
| 32 |
parsed["b.js"].program.body, |
| 33 |
), |
| 34 |
}, |
| 35 |
}; |
| 36 |
|
| 37 |
const generated = generate(combinedAst, { sourceMaps: true }, sources); |
| 38 |
|
| 39 |
expect(generated.map).toMatchInlineSnapshot(` |
| 40 |
Object { |
| 41 |
"file": undefined, |
| 42 |
"mappings": "AAAA,SAASA,EAAE,CAAEC,GAAG,EAAE;EAAEC,OAAO,CAACC,GAAG,CAACF,GAAG,CAAC;AAAE;ACAtCD,EAAE,CAAC,OAAO,CAAC", |
| 43 |
"names": Array [ |
| 44 |
"hi", |
| 45 |
"msg", |
| 46 |
"console", |
| 47 |
"log", |
| 48 |
], |
| 49 |
"sourceRoot": undefined, |
| 50 |
"sources": Array [ |
| 51 |
"a.js", |
| 52 |
"b.js", |
| 53 |
], |
| 54 |
"sourcesContent": Array [ |
| 55 |
"function hi (msg) { console.log(msg); } |
| 56 |
", |
| 57 |
"hi('hello'); |
| 58 |
", |
| 59 |
], |
| 60 |
"version": 3, |
| 61 |
} |
| 62 |
`); |
| 63 |
|
| 64 |
expect(generated.rawMappings).toMatchInlineSnapshot(` |
| 65 |
Array [ |
| 66 |
Object { |
| 67 |
"generated": Object { |
| 68 |
"column": 0, |
| 69 |
"line": 1, |
| 70 |
}, |
| 71 |
"name": undefined, |
| 72 |
"original": Object { |
| 73 |
"column": 0, |
| 74 |
"line": 1, |
| 75 |
}, |
| 76 |
"source": "a.js", |
| 77 |
}, |
| 78 |
Object { |
| 79 |
"generated": Object { |
| 80 |
"column": 9, |
| 81 |
"line": 1, |
| 82 |
}, |
| 83 |
"name": "hi", |
| 84 |
"original": Object { |
| 85 |
"column": 9, |
| 86 |
"line": 1, |
| 87 |
}, |
| 88 |
"source": "a.js", |
| 89 |
}, |
| 90 |
Object { |
| 91 |
"generated": Object { |
| 92 |
"column": 11, |
| 93 |
"line": 1, |
| 94 |
}, |
| 95 |
"name": undefined, |
| 96 |
"original": Object { |
| 97 |
"column": 11, |
| 98 |
"line": 1, |
| 99 |
}, |
| 100 |
"source": "a.js", |
| 101 |
}, |
| 102 |
Object { |
| 103 |
"generated": Object { |
| 104 |
"column": 12, |
| 105 |
"line": 1, |
| 106 |
}, |
| 107 |
"name": "msg", |
| 108 |
"original": Object { |
| 109 |
"column": 13, |
| 110 |
"line": 1, |
| 111 |
}, |
| 112 |
"source": "a.js", |
| 113 |
}, |
| 114 |
Object { |
| 115 |
"generated": Object { |
| 116 |
"column": 15, |
| 117 |
"line": 1, |
| 118 |
}, |
| 119 |
"name": undefined, |
| 120 |
"original": Object { |
| 121 |
"column": 16, |
| 122 |
"line": 1, |
| 123 |
}, |
| 124 |
"source": "a.js", |
| 125 |
}, |
| 126 |
Object { |
| 127 |
"generated": Object { |
| 128 |
"column": 17, |
| 129 |
"line": 1, |
| 130 |
}, |
| 131 |
"name": undefined, |
| 132 |
"original": Object { |
| 133 |
"column": 18, |
| 134 |
"line": 1, |
| 135 |
}, |
| 136 |
"source": "a.js", |
| 137 |
}, |
| 138 |
Object { |
| 139 |
"generated": Object { |
| 140 |
"column": 2, |
| 141 |
"line": 2, |
| 142 |
}, |
| 143 |
"name": "console", |
| 144 |
"original": Object { |
| 145 |
"column": 20, |
| 146 |
"line": 1, |
| 147 |
}, |
| 148 |
"source": "a.js", |
| 149 |
}, |
| 150 |
Object { |
| 151 |
"generated": Object { |
| 152 |
"column": 9, |
| 153 |
"line": 2, |
| 154 |
}, |
| 155 |
"name": undefined, |
| 156 |
"original": Object { |
| 157 |
"column": 27, |
| 158 |
"line": 1, |
| 159 |
}, |
| 160 |
"source": "a.js", |
| 161 |
}, |
| 162 |
Object { |
| 163 |
"generated": Object { |
| 164 |
"column": 10, |
| 165 |
"line": 2, |
| 166 |
}, |
| 167 |
"name": "log", |
| 168 |
"original": Object { |
| 169 |
"column": 28, |
| 170 |
"line": 1, |
| 171 |
}, |
| 172 |
"source": "a.js", |
| 173 |
}, |
| 174 |
Object { |
| 175 |
"generated": Object { |
| 176 |
"column": 13, |
| 177 |
"line": 2, |
| 178 |
}, |
| 179 |
"name": undefined, |
| 180 |
"original": Object { |
| 181 |
"column": 31, |
| 182 |
"line": 1, |
| 183 |
}, |
| 184 |
"source": "a.js", |
| 185 |
}, |
| 186 |
Object { |
| 187 |
"generated": Object { |
| 188 |
"column": 14, |
| 189 |
"line": 2, |
| 190 |
}, |
| 191 |
"name": "msg", |
| 192 |
"original": Object { |
| 193 |
"column": 32, |
| 194 |
"line": 1, |
| 195 |
}, |
| 196 |
"source": "a.js", |
| 197 |
}, |
| 198 |
Object { |
| 199 |
"generated": Object { |
| 200 |
"column": 17, |
| 201 |
"line": 2, |
| 202 |
}, |
| 203 |
"name": undefined, |
| 204 |
"original": Object { |
| 205 |
"column": 35, |
| 206 |
"line": 1, |
| 207 |
}, |
| 208 |
"source": "a.js", |
| 209 |
}, |
| 210 |
Object { |
| 211 |
"generated": Object { |
| 212 |
"column": 18, |
| 213 |
"line": 2, |
| 214 |
}, |
| 215 |
"name": undefined, |
| 216 |
"original": Object { |
| 217 |
"column": 36, |
| 218 |
"line": 1, |
| 219 |
}, |
| 220 |
"source": "a.js", |
| 221 |
}, |
| 222 |
Object { |
| 223 |
"generated": Object { |
| 224 |
"column": 0, |
| 225 |
"line": 3, |
| 226 |
}, |
| 227 |
"name": undefined, |
| 228 |
"original": Object { |
| 229 |
"column": 38, |
| 230 |
"line": 1, |
| 231 |
}, |
| 232 |
"source": "a.js", |
| 233 |
}, |
| 234 |
Object { |
| 235 |
"generated": Object { |
| 236 |
"column": 0, |
| 237 |
"line": 4, |
| 238 |
}, |
| 239 |
"name": "hi", |
| 240 |
"original": Object { |
| 241 |
"column": 0, |
| 242 |
"line": 1, |
| 243 |
}, |
| 244 |
"source": "b.js", |
| 245 |
}, |
| 246 |
Object { |
| 247 |
"generated": Object { |
| 248 |
"column": 2, |
| 249 |
"line": 4, |
| 250 |
}, |
| 251 |
"name": undefined, |
| 252 |
"original": Object { |
| 253 |
"column": 2, |
| 254 |
"line": 1, |
| 255 |
}, |
| 256 |
"source": "b.js", |
| 257 |
}, |
| 258 |
Object { |
| 259 |
"generated": Object { |
| 260 |
"column": 3, |
| 261 |
"line": 4, |
| 262 |
}, |
| 263 |
"name": undefined, |
| 264 |
"original": Object { |
| 265 |
"column": 3, |
| 266 |
"line": 1, |
| 267 |
}, |
| 268 |
"source": "b.js", |
| 269 |
}, |
| 270 |
Object { |
| 271 |
"generated": Object { |
| 272 |
"column": 10, |
| 273 |
"line": 4, |
| 274 |
}, |
| 275 |
"name": undefined, |
| 276 |
"original": Object { |
| 277 |
"column": 10, |
| 278 |
"line": 1, |
| 279 |
}, |
| 280 |
"source": "b.js", |
| 281 |
}, |
| 282 |
Object { |
| 283 |
"generated": Object { |
| 284 |
"column": 11, |
| 285 |
"line": 4, |
| 286 |
}, |
| 287 |
"name": undefined, |
| 288 |
"original": Object { |
| 289 |
"column": 11, |
| 290 |
"line": 1, |
| 291 |
}, |
| 292 |
"source": "b.js", |
| 293 |
}, |
| 294 |
] |
| 295 |
`); |
| 296 |
|
| 297 |
expect(generated.code).toBe( |
| 298 |
"function hi(msg) {\n console.log(msg);\n}\nhi('hello');", |
| 299 |
); |
| 300 |
}); |
| 301 |
|
| 302 |
it("identifierName", function () { |
| 303 |
const code = "function foo() { bar; }\n"; |
| 304 |
|
| 305 |
const ast = parse(code, { filename: "inline" }).program; |
| 306 |
const fn = ast.body[0]; |
| 307 |
|
| 308 |
const id = fn.id; |
| 309 |
id.name += "2"; |
| 310 |
id.loc.identifierName = "foo"; |
| 311 |
|
| 312 |
const id2 = fn.body.body[0].expression; |
| 313 |
id2.name += "2"; |
| 314 |
id2.loc.identifierName = "bar"; |
| 315 |
|
| 316 |
const generated = generate( |
| 317 |
ast, |
| 318 |
{ |
| 319 |
filename: "inline", |
| 320 |
sourceFileName: "inline", |
| 321 |
sourceMaps: true, |
| 322 |
}, |
| 323 |
code, |
| 324 |
); |
| 325 |
|
| 326 |
expect(generated.map).toEqual( |
| 327 |
{ |
| 328 |
version: 3, |
| 329 |
sources: ["inline"], |
| 330 |
names: ["foo", "bar"], |
| 331 |
mappings: "AAAA,SAASA,IAAG,GAAG;EAAEC,IAAG;AAAE", |
| 332 |
sourcesContent: ["function foo() { bar; }\n"], |
| 333 |
}, |
| 334 |
"sourcemap was incorrectly generated", |
| 335 |
); |
| 336 |
|
| 337 |
expect(generated.rawMappings).toEqual( |
| 338 |
[ |
| 339 |
{ |
| 340 |
name: undefined, |
| 341 |
generated: { line: 1, column: 0 }, |
| 342 |
source: "inline", |
| 343 |
original: { line: 1, column: 0 }, |
| 344 |
}, |
| 345 |
{ |
| 346 |
name: "foo", |
| 347 |
generated: { line: 1, column: 9 }, |
| 348 |
source: "inline", |
| 349 |
original: { line: 1, column: 9 }, |
| 350 |
}, |
| 351 |
{ |
| 352 |
name: undefined, |
| 353 |
generated: { line: 1, column: 13 }, |
| 354 |
source: "inline", |
| 355 |
original: { line: 1, column: 12 }, |
| 356 |
}, |
| 357 |
{ |
| 358 |
name: undefined, |
| 359 |
generated: { line: 1, column: 16 }, |
| 360 |
source: "inline", |
| 361 |
original: { line: 1, column: 15 }, |
| 362 |
}, |
| 363 |
{ |
| 364 |
name: "bar", |
| 365 |
generated: { line: 2, column: 2 }, |
| 366 |
source: "inline", |
| 367 |
original: { line: 1, column: 17 }, |
| 368 |
}, |
| 369 |
{ |
| 370 |
name: undefined, |
| 371 |
generated: { line: 2, column: 6 }, |
| 372 |
source: "inline", |
| 373 |
original: { line: 1, column: 20 }, |
| 374 |
}, |
| 375 |
{ |
| 376 |
name: undefined, |
| 377 |
generated: { line: 3, column: 0 }, |
| 378 |
source: "inline", |
| 379 |
original: { line: 1, column: 22 }, |
| 380 |
}, |
| 381 |
], |
| 382 |
"raw mappings were incorrectly generated", |
| 383 |
); |
| 384 |
|
| 385 |
expect(generated.code).toBe("function foo2() {\n bar2;\n}"); |
| 386 |
}); |
| 387 |
|
| 388 |
it("newline in template literal", () => { |
| 389 |
const code = "`before\n\nafter`;"; |
| 390 |
const ast = parse(code, { filename: "inline" }).program; |
| 391 |
const generated = generate( |
| 392 |
ast, |
| 393 |
{ |
| 394 |
filename: "inline", |
| 395 |
sourceFileName: "inline", |
| 396 |
sourceMaps: true, |
| 397 |
}, |
| 398 |
code, |
| 399 |
); |
| 400 |
|
| 401 |
const consumer = new TraceMap(generated.map); |
| 402 |
const loc = originalPositionFor(consumer, { line: 2, column: 1 }); |
| 403 |
expect(loc).toMatchObject({ |
| 404 |
column: 0, |
| 405 |
line: 2, |
| 406 |
}); |
| 407 |
}); |
| 408 |
|
| 409 |
it("newline in string literal", () => { |
| 410 |
const code = "'before\\\n\\\nafter';"; |
| 411 |
const ast = parse(code, { filename: "inline" }).program; |
| 412 |
const generated = generate( |
| 413 |
ast, |
| 414 |
{ |
| 415 |
filename: "inline", |
| 416 |
sourceFileName: "inline", |
| 417 |
sourceMaps: true, |
| 418 |
}, |
| 419 |
code, |
| 420 |
); |
| 421 |
|
| 422 |
const consumer = new TraceMap(generated.map); |
| 423 |
const loc = originalPositionFor(consumer, { line: 2, column: 1 }); |
| 424 |
expect(loc).toMatchObject({ |
| 425 |
column: 0, |
| 426 |
line: 2, |
| 427 |
}); |
| 428 |
}); |
| 429 |
|
| 430 |
it("lazy source map generation", function () { |
| 431 |
const code = "function hi (msg) { console.log(msg); }\n"; |
| 432 |
|
| 433 |
const ast = parse(code, { filename: "a.js" }).program; |
| 434 |
const generated = generate(ast, { |
| 435 |
sourceFileName: "a.js", |
| 436 |
sourceMaps: true, |
| 437 |
}); |
| 438 |
|
| 439 |
expect(Array.isArray(generated.rawMappings)).toBe(true); |
| 440 |
|
| 441 |
expect( |
| 442 |
Object.getOwnPropertyDescriptor(generated, "map"), |
| 443 |
).not.toHaveProperty("value"); |
| 444 |
|
| 445 |
expect(generated).toHaveProperty("map"); |
| 446 |
expect(typeof generated.map).toBe("object"); |
| 447 |
}); |
| 448 |
|
| 449 |
it("wraps around infer inside an array type", () => { |
| 450 |
const type = t.tsArrayType( |
| 451 |
t.tsInferType( |
| 452 |
t.tsTypeParameter( |
| 453 |
null, |
| 454 |
null, |
| 455 |
!process.env.BABEL_8_BREAKING ? "T" : t.identifier("T"), |
| 456 |
), |
| 457 |
), |
| 458 |
); |
| 459 |
|
| 460 |
const output = generate(type).code; |
| 461 |
expect(output).toBe("(infer T)[]"); |
| 462 |
}); |
| 463 |
|
| 464 |
it("should not deduplicate comments with same start index", () => { |
| 465 |
const code1 = "/*#__PURE__*/ a();"; |
| 466 |
const code2 = "/*#__PURE__*/ b();"; |
| 467 |
|
| 468 |
const ast1 = parse(code1).program; |
| 469 |
const ast2 = parse(code2).program; |
| 470 |
|
| 471 |
const ast = t.program([...ast1.body, ...ast2.body]); |
| 472 |
|
| 473 |
expect(generate(ast).code).toBe("/*#__PURE__*/a();\n/*#__PURE__*/b();"); |
| 474 |
}); |
| 475 |
|
| 476 |
it("comments with null or undefined loc", () => { |
| 477 |
const code = "/*#__PURE__*/ /*#__PURE__*/"; |
| 478 |
|
| 479 |
const ast = parse(code); |
| 480 |
|
| 481 |
ast.comments[0].loc = null; |
| 482 |
ast.comments[1].loc = undefined; |
| 483 |
|
| 484 |
expect(generate(ast).code).toBe("/*#__PURE__*/\n/*#__PURE__*/"); |
| 485 |
}); |
| 486 |
|
| 487 |
it("comments without loc", () => { |
| 488 |
const ast = parse( |
| 489 |
` |
| 490 |
import { |
| 491 |
Attribute, |
| 492 |
AttributeSDKType |
| 493 |
} |
| 494 |
from "../../base/v1beta1/attribute"; |
| 495 |
import { |
| 496 |
Rpc |
| 497 |
} |
| 498 |
from "../../../helpers"; |
| 499 |
import * as _m0 from "protobufjs/minimal"; |
| 500 |
import { |
| 501 |
MsgSignProviderAttributes, |
| 502 |
MsgSignProviderAttributesSDKType, |
| 503 |
MsgSignProviderAttributesResponse, |
| 504 |
MsgSignProviderAttributesResponseSDKType, |
| 505 |
MsgDeleteProviderAttributes, |
| 506 |
MsgDeleteProviderAttributesSDKType, |
| 507 |
MsgDeleteProviderAttributesResponse, |
| 508 |
MsgDeleteProviderAttributesResponseSDKType |
| 509 |
} |
| 510 |
from "./audit"; |
| 511 |
/** Msg defines the provider Msg service */ |
| 512 |
export interface Msg { |
| 513 |
/** SignProviderAttributes defines a method that signs provider attributes */ |
| 514 |
signProviderAttributes(request: MsgSignProviderAttributes): Promise < MsgSignProviderAttributesResponse > ; |
| 515 |
/** DeleteProviderAttributes defines a method that deletes provider attributes */ |
| 516 |
deleteProviderAttributes(request: MsgDeleteProviderAttributes): Promise < MsgDeleteProviderAttributesResponse > ; |
| 517 |
} |
| 518 |
export class MsgClientImpl implements Msg { |
| 519 |
private readonly rpc: Rpc; |
| 520 |
constructor(rpc: Rpc) { |
| 521 |
this.rpc = rpc; |
| 522 |
} |
| 523 |
/* SignProviderAttributes defines a method that signs provider attributes */ |
| 524 |
signProviderAttributes = async(request: MsgSignProviderAttributes): Promise < MsgSignProviderAttributesResponse > => { |
| 525 |
const data = MsgSignProviderAttributes.encode(request).finish(); |
| 526 |
const promise = this.rpc.request("akash.audit.v1beta1.Msg", "SignProviderAttributes", data); |
| 527 |
return promise.then(data => MsgSignProviderAttributesResponse.decode(new _m0.Reader(data))); |
| 528 |
}; |
| 529 |
/* DeleteProviderAttributes defines a method that deletes provider attributes */ |
| 530 |
deleteProviderAttributes = async(request: MsgDeleteProviderAttributes): Promise < MsgDeleteProviderAttributesResponse > => { |
| 531 |
const data = MsgDeleteProviderAttributes.encode(request).finish(); |
| 532 |
const promise = this.rpc.request("akash.audit.v1beta1.Msg", "DeleteProviderAttributes", data); |
| 533 |
return promise.then(data => MsgDeleteProviderAttributesResponse.decode(new _m0.Reader(data))); |
| 534 |
}; |
| 535 |
} |
| 536 |
`, |
| 537 |
{ sourceType: "module", plugins: ["typescript"] }, |
| 538 |
); |
| 539 |
|
| 540 |
for (const comment of ast.comments) { |
| 541 |
comment.loc = undefined; |
| 542 |
} |
| 543 |
|
| 544 |
expect(generate(ast).code).toMatchInlineSnapshot(` |
| 545 |
"import { Attribute, AttributeSDKType } from \\"../../base/v1beta1/attribute\\"; |
| 546 |
import { Rpc } from \\"../../../helpers\\"; |
| 547 |
import * as _m0 from \\"protobufjs/minimal\\"; |
| 548 |
import { MsgSignProviderAttributes, MsgSignProviderAttributesSDKType, MsgSignProviderAttributesResponse, MsgSignProviderAttributesResponseSDKType, MsgDeleteProviderAttributes, MsgDeleteProviderAttributesSDKType, MsgDeleteProviderAttributesResponse, MsgDeleteProviderAttributesResponseSDKType } from \\"./audit\\"; |
| 549 |
/** Msg defines the provider Msg service */ |
| 550 |
export interface Msg { |
| 551 |
/** SignProviderAttributes defines a method that signs provider attributes */ |
| 552 |
signProviderAttributes(request: MsgSignProviderAttributes): Promise<MsgSignProviderAttributesResponse>; |
| 553 |
/** DeleteProviderAttributes defines a method that deletes provider attributes */ |
| 554 |
deleteProviderAttributes(request: MsgDeleteProviderAttributes): Promise<MsgDeleteProviderAttributesResponse>; |
| 555 |
} |
| 556 |
export class MsgClientImpl implements Msg { |
| 557 |
private readonly rpc: Rpc; |
| 558 |
constructor(rpc: Rpc) { |
| 559 |
this.rpc = rpc; |
| 560 |
} |
| 561 |
/* SignProviderAttributes defines a method that signs provider attributes */ |
| 562 |
signProviderAttributes = async (request: MsgSignProviderAttributes): Promise<MsgSignProviderAttributesResponse> => { |
| 563 |
const data = MsgSignProviderAttributes.encode(request).finish(); |
| 564 |
const promise = this.rpc.request(\\"akash.audit.v1beta1.Msg\\", \\"SignProviderAttributes\\", data); |
| 565 |
return promise.then(data => MsgSignProviderAttributesResponse.decode(new _m0.Reader(data))); |
| 566 |
}; |
| 567 |
/* DeleteProviderAttributes defines a method that deletes provider attributes */ |
| 568 |
deleteProviderAttributes = async (request: MsgDeleteProviderAttributes): Promise<MsgDeleteProviderAttributesResponse> => { |
| 569 |
const data = MsgDeleteProviderAttributes.encode(request).finish(); |
| 570 |
const promise = this.rpc.request(\\"akash.audit.v1beta1.Msg\\", \\"DeleteProviderAttributes\\", data); |
| 571 |
return promise.then(data => MsgDeleteProviderAttributesResponse.decode(new _m0.Reader(data))); |
| 572 |
}; |
| 573 |
}" |
| 574 |
`); |
| 575 |
}); |
| 576 |
|
| 577 |
it("comments without loc2", () => { |
| 578 |
const ast = parse( |
| 579 |
` |
| 580 |
(function (_templateFactory) { |
| 581 |
"use strict"; |
| 582 |
|
| 583 |
const template = (0, _templateFactory.createTemplateFactory)( |
| 584 |
/*{{somevalue}}*/ |
| 585 |
{ |
| 586 |
"id": null, |
| 587 |
"block": "[[[1,[34,0]]],[],false,[\\"somevalue\\"]]", |
| 588 |
"moduleName": "(unknown template module)", |
| 589 |
"isStrictMode": false |
| 590 |
}); |
| 591 |
}); |
| 592 |
|
| 593 |
const template = (0, _templateFactory.createTemplateFactory)( |
| 594 |
/* |
| 595 |
{{somevalue}} |
| 596 |
*/ |
| 597 |
{ |
| 598 |
"id": null, |
| 599 |
"block": "[[[1,[34,0]]],[],false,[\\"somevalue\\"]]", |
| 600 |
"moduleName": "(unknown template module)", |
| 601 |
"isStrictMode": false |
| 602 |
}); |
| 603 |
`, |
| 604 |
{ sourceType: "module" }, |
| 605 |
); |
| 606 |
|
| 607 |
for (const comment of ast.comments) { |
| 608 |
comment.loc = undefined; |
| 609 |
} |
| 610 |
|
| 611 |
expect(generate(ast).code).toMatchInlineSnapshot(` |
| 612 |
"(function (_templateFactory) { |
| 613 |
\\"use strict\\"; |
| 614 |
|
| 615 |
const template = (0, _templateFactory.createTemplateFactory)( |
| 616 |
/*{{somevalue}}*/ |
| 617 |
{ |
| 618 |
\\"id\\": null, |
| 619 |
\\"block\\": \\"[[[1,[34,0]]],[],false,[\\\\\\"somevalue\\\\\\"]]\\", |
| 620 |
\\"moduleName\\": \\"(unknown template module)\\", |
| 621 |
\\"isStrictMode\\": false |
| 622 |
}); |
| 623 |
}); |
| 624 |
const template = (0, _templateFactory.createTemplateFactory)( |
| 625 |
/* |
| 626 |
{{somevalue}} |
| 627 |
*/ |
| 628 |
{ |
| 629 |
\\"id\\": null, |
| 630 |
\\"block\\": \\"[[[1,[34,0]]],[],false,[\\\\\\"somevalue\\\\\\"]]\\", |
| 631 |
\\"moduleName\\": \\"(unknown template module)\\", |
| 632 |
\\"isStrictMode\\": false |
| 633 |
});" |
| 634 |
`); |
| 635 |
}); |
| 636 |
|
| 637 |
it("comments without loc3", () => { |
| 638 |
const ast = parse( |
| 639 |
` |
| 640 |
/** This describes how the endpoint is implemented when the lease is deployed */ |
| 641 |
export enum Endpoint_Kind { |
| 642 |
/** SHARED_HTTP - Describes an endpoint that becomes a Kubernetes Ingress */ |
| 643 |
SHARED_HTTP = 0, |
| 644 |
/** RANDOM_PORT - Describes an endpoint that becomes a Kubernetes NodePort */ |
| 645 |
RANDOM_PORT = 1, |
| 646 |
UNRECOGNIZED = -1, |
| 647 |
} |
| 648 |
`, |
| 649 |
{ sourceType: "module", plugins: ["typescript"] }, |
| 650 |
); |
| 651 |
|
| 652 |
for (const comment of ast.comments) { |
| 653 |
comment.loc = undefined; |
| 654 |
} |
| 655 |
|
| 656 |
expect(generate(ast).code).toMatchInlineSnapshot(` |
| 657 |
"/** This describes how the endpoint is implemented when the lease is deployed */ |
| 658 |
export enum Endpoint_Kind { |
| 659 |
/** SHARED_HTTP - Describes an endpoint that becomes a Kubernetes Ingress */ |
| 660 |
SHARED_HTTP = 0, |
| 661 |
/** RANDOM_PORT - Describes an endpoint that becomes a Kubernetes NodePort */ |
| 662 |
RANDOM_PORT = 1, |
| 663 |
UNRECOGNIZED = -1, |
| 664 |
}" |
| 665 |
`); |
| 666 |
}); |
| 667 |
|
| 668 |
it("comments without node.loc", () => { |
| 669 |
const ast = parse( |
| 670 |
` |
| 671 |
(function (_templateFactory) { |
| 672 |
"use strict"; |
| 673 |
|
| 674 |
const template = (0, _templateFactory.createTemplateFactory)( |
| 675 |
/*{{somevalue}}*/ |
| 676 |
{ |
| 677 |
"id": null, |
| 678 |
"block": "[[[1,[34,0]]],[],false,[\\"somevalue\\"]]", |
| 679 |
"moduleName": "(unknown template module)", |
| 680 |
"isStrictMode": false |
| 681 |
}); |
| 682 |
}); |
| 683 |
|
| 684 |
const template = (0, _templateFactory.createTemplateFactory)( |
| 685 |
/* |
| 686 |
{{somevalue}} |
| 687 |
*/ |
| 688 |
{ |
| 689 |
"id": null, |
| 690 |
"block": "[[[1,[34,0]]],[],false,[\\"somevalue\\"]]", |
| 691 |
"moduleName": "(unknown template module)", |
| 692 |
"isStrictMode": false |
| 693 |
}); |
| 694 |
`, |
| 695 |
{ sourceType: "module" }, |
| 696 |
); |
| 697 |
|
| 698 |
const ast2 = t.cloneNode(ast, true, true); |
| 699 |
|
| 700 |
for (let i = 0; i < ast.comments.length; i++) { |
| 701 |
ast2.comments[i].loc = ast.comments[i].loc; |
| 702 |
} |
| 703 |
|
| 704 |
expect(generate(ast2).code).toMatchInlineSnapshot(` |
| 705 |
"(function (_templateFactory) { |
| 706 |
\\"use strict\\"; |
| 707 |
|
| 708 |
const template = (0, _templateFactory.createTemplateFactory)( |
| 709 |
/*{{somevalue}}*/ |
| 710 |
{ |
| 711 |
\\"id\\": null, |
| 712 |
\\"block\\": \\"[[[1,[34,0]]],[],false,[\\\\\\"somevalue\\\\\\"]]\\", |
| 713 |
\\"moduleName\\": \\"(unknown template module)\\", |
| 714 |
\\"isStrictMode\\": false |
| 715 |
}); |
| 716 |
}); |
| 717 |
const template = (0, _templateFactory.createTemplateFactory)( |
| 718 |
/* |
| 719 |
{{somevalue}} |
| 720 |
*/ |
| 721 |
{ |
| 722 |
\\"id\\": null, |
| 723 |
\\"block\\": \\"[[[1,[34,0]]],[],false,[\\\\\\"somevalue\\\\\\"]]\\", |
| 724 |
\\"moduleName\\": \\"(unknown template module)\\", |
| 725 |
\\"isStrictMode\\": false |
| 726 |
});" |
| 727 |
`); |
| 728 |
}); |
| 729 |
}); |
| 730 |
|
| 731 |
describe("programmatic generation", function () { |
| 732 |
it("should add parenthesis when NullishCoalescing is used along with ||", function () { |
| 733 |
|
| 734 |
const nullishCoalesc = t.logicalExpression( |
| 735 |
"??", |
| 736 |
t.logicalExpression("||", t.identifier("a"), t.identifier("b")), |
| 737 |
t.identifier("c"), |
| 738 |
); |
| 739 |
const output = generate(nullishCoalesc).code; |
| 740 |
expect(output).toBe(`(a || b) ?? c`); |
| 741 |
}); |
| 742 |
|
| 743 |
it("should add parenthesis when NullishCoalesing is used with &&", function () { |
| 744 |
const nullishCoalesc = t.logicalExpression( |
| 745 |
"??", |
| 746 |
t.identifier("a"), |
| 747 |
t.logicalExpression( |
| 748 |
"&&", |
| 749 |
t.identifier("b"), |
| 750 |
t.logicalExpression("&&", t.identifier("c"), t.identifier("d")), |
| 751 |
), |
| 752 |
); |
| 753 |
const output = generate(nullishCoalesc).code; |
| 754 |
expect(output).toBe(`a ?? (b && c && d)`); |
| 755 |
}); |
| 756 |
|
| 757 |
it("numeric member expression", function () { |
| 758 |
|
| 759 |
const mem = t.memberExpression( |
| 760 |
t.numericLiteral(60702), |
| 761 |
t.identifier("foo"), |
| 762 |
); |
| 763 |
new Function(generate(mem).code); |
| 764 |
}); |
| 765 |
|
| 766 |
it("nested if statements needs block", function () { |
| 767 |
const ifStatement = t.ifStatement( |
| 768 |
t.stringLiteral("top cond"), |
| 769 |
t.whileStatement( |
| 770 |
t.stringLiteral("while cond"), |
| 771 |
t.ifStatement( |
| 772 |
t.stringLiteral("nested"), |
| 773 |
t.expressionStatement(t.numericLiteral(1)), |
| 774 |
), |
| 775 |
), |
| 776 |
t.expressionStatement(t.stringLiteral("alt")), |
| 777 |
); |
| 778 |
|
| 779 |
const ast = parse(generate(ifStatement).code); |
| 780 |
expect(ast.program.body[0].consequent.type).toBe("BlockStatement"); |
| 781 |
}); |
| 782 |
|
| 783 |
it("prints directives in block with empty body", function () { |
| 784 |
const blockStatement = t.blockStatement( |
| 785 |
[], |
| 786 |
[t.directive(t.directiveLiteral("use strict"))], |
| 787 |
); |
| 788 |
|
| 789 |
const output = generate(blockStatement).code; |
| 790 |
expect(output).toBe(`{ |
| 791 |
"use strict"; |
| 792 |
}`); |
| 793 |
}); |
| 794 |
|
| 795 |
it("flow object indentation", function () { |
| 796 |
const objectStatement = t.objectTypeAnnotation([ |
| 797 |
t.objectTypeProperty(t.identifier("bar"), t.stringTypeAnnotation()), |
| 798 |
]); |
| 799 |
|
| 800 |
const output = generate(objectStatement).code; |
| 801 |
expect(output).toBe(`{ |
| 802 |
bar: string |
| 803 |
}`); |
| 804 |
}); |
| 805 |
|
| 806 |
it("flow object exact", function () { |
| 807 |
const objectStatement = t.objectTypeAnnotation( |
| 808 |
[t.objectTypeProperty(t.identifier("bar"), t.stringTypeAnnotation())], |
| 809 |
undefined, |
| 810 |
undefined, |
| 811 |
undefined, |
| 812 |
true, |
| 813 |
); |
| 814 |
|
| 815 |
const output = generate(objectStatement).code; |
| 816 |
expect(output).toBe(`{| |
| 817 |
bar: string |
| 818 |
|}`); |
| 819 |
}); |
| 820 |
|
| 821 |
it("flow object indentation with empty leading ObjectTypeProperty", function () { |
| 822 |
const objectStatement = t.objectTypeAnnotation( |
| 823 |
[], |
| 824 |
[ |
| 825 |
t.objectTypeIndexer( |
| 826 |
t.identifier("key"), |
| 827 |
t.anyTypeAnnotation(), |
| 828 |
t.numberTypeAnnotation(), |
| 829 |
), |
| 830 |
], |
| 831 |
); |
| 832 |
|
| 833 |
const output = generate(objectStatement).code; |
| 834 |
|
| 835 |
expect(output).toBe(`{ |
| 836 |
[key: any]: number |
| 837 |
}`); |
| 838 |
}); |
| 839 |
|
| 840 |
it("flow interface with nullish extends", () => { |
| 841 |
const interfaceDeclaration = t.interfaceDeclaration( |
| 842 |
t.identifier("A"), |
| 843 |
undefined, |
| 844 |
undefined, |
| 845 |
t.objectTypeAnnotation([]), |
| 846 |
); |
| 847 |
const output = generate(interfaceDeclaration).code; |
| 848 |
expect(output).toBe("interface A {}"); |
| 849 |
}); |
| 850 |
|
| 851 |
it("flow function type annotation with no parent", () => { |
| 852 |
const functionTypeAnnotation = t.functionTypeAnnotation( |
| 853 |
null, |
| 854 |
[], |
| 855 |
null, |
| 856 |
t.voidTypeAnnotation(), |
| 857 |
); |
| 858 |
const output = generate(functionTypeAnnotation).code; |
| 859 |
expect(output).toBe("() => void"); |
| 860 |
}); |
| 861 |
|
| 862 |
describe("directives", function () { |
| 863 |
it("preserves escapes", function () { |
| 864 |
const directive = t.directive( |
| 865 |
t.directiveLiteral(String.raw`us\x65 strict`), |
| 866 |
); |
| 867 |
const output = generate(directive).code; |
| 868 |
|
| 869 |
expect(output).toBe(String.raw`"us\x65 strict";`); |
| 870 |
}); |
| 871 |
|
| 872 |
it("preserves escapes in minified output", function () { |
| 873 |
|
| 874 |
|
| 875 |
const directive = t.directive(t.directiveLiteral(String.raw`foo\n\t\r`)); |
| 876 |
const output = generate(directive, { minified: true }).code; |
| 877 |
|
| 878 |
expect(output).toBe(String.raw`"foo\n\t\r";`); |
| 879 |
}); |
| 880 |
|
| 881 |
it("unescaped single quote", function () { |
| 882 |
const directive = t.directive(t.directiveLiteral(String.raw`'\'\"`)); |
| 883 |
const output = generate(directive).code; |
| 884 |
|
| 885 |
expect(output).toBe(String.raw`"'\'\"";`); |
| 886 |
}); |
| 887 |
|
| 888 |
it("unescaped double quote", function () { |
| 889 |
const directive = t.directive(t.directiveLiteral(String.raw`"\'\"`)); |
| 890 |
const output = generate(directive).code; |
| 891 |
|
| 892 |
expect(output).toBe(String.raw`'"\'\"';`); |
| 893 |
}); |
| 894 |
|
| 895 |
it("unescaped single and double quotes together throw", function () { |
| 896 |
const directive = t.directive(t.directiveLiteral(String.raw`'"`)); |
| 897 |
|
| 898 |
expect(() => { |
| 899 |
generate(directive); |
| 900 |
}).toThrow(); |
| 901 |
}); |
| 902 |
|
| 903 |
it("preserves single quotes if not minified", function () { |
| 904 |
const directive = parse("'use strict';").program.directives[0]; |
| 905 |
const output = generate(directive).code; |
| 906 |
|
| 907 |
expect(output).toBe("'use strict';"); |
| 908 |
}); |
| 909 |
|
| 910 |
it("converts single quotes to double quotes if minified", function () { |
| 911 |
const directive = parse("'use strict';").program.directives[0]; |
| 912 |
const output = generate(directive, { minified: true }).code; |
| 913 |
|
| 914 |
expect(output).toBe('"use strict";'); |
| 915 |
}); |
| 916 |
}); |
| 917 |
|
| 918 |
describe("typescript generate parentheses if necessary", function () { |
| 919 |
it("wraps around union for array", () => { |
| 920 |
const typeStatement = t.tsArrayType( |
| 921 |
t.tsUnionType([ |
| 922 |
t.tsIntersectionType([t.tsNumberKeyword(), t.tsBooleanKeyword()]), |
| 923 |
t.tsNullKeyword(), |
| 924 |
]), |
| 925 |
); |
| 926 |
const output = generate(typeStatement).code; |
| 927 |
expect(output).toBe("((number & boolean) | null)[]"); |
| 928 |
}); |
| 929 |
it("wraps around intersection for array", () => { |
| 930 |
const typeStatement = t.tsArrayType( |
| 931 |
t.tsIntersectionType([t.tsNumberKeyword(), t.tsBooleanKeyword()]), |
| 932 |
); |
| 933 |
const output = generate(typeStatement).code; |
| 934 |
expect(output).toBe("(number & boolean)[]"); |
| 935 |
}); |
| 936 |
it("wraps around rest", () => { |
| 937 |
const typeStatement = t.tsRestType( |
| 938 |
t.tsIntersectionType([t.tsNumberKeyword(), t.tsBooleanKeyword()]), |
| 939 |
); |
| 940 |
const output = generate(typeStatement).code; |
| 941 |
expect(output).toBe("...(number & boolean)"); |
| 942 |
}); |
| 943 |
it("wraps around optional type", () => { |
| 944 |
const typeStatement = t.tsOptionalType( |
| 945 |
t.tsIntersectionType([t.tsNumberKeyword(), t.tsBooleanKeyword()]), |
| 946 |
); |
| 947 |
const output = generate(typeStatement).code; |
| 948 |
expect(output).toBe("(number & boolean)?"); |
| 949 |
}); |
| 950 |
}); |
| 951 |
|
| 952 |
describe("object expressions", () => { |
| 953 |
it("not wrapped in parentheses when standalone", () => { |
| 954 |
const objectExpression = t.objectExpression([]); |
| 955 |
const output = generate(objectExpression).code; |
| 956 |
expect(output).toBe("{}"); |
| 957 |
}); |
| 958 |
|
| 959 |
it("wrapped in parentheses in expression statement", () => { |
| 960 |
const expressionStatement = t.expressionStatement(t.objectExpression([])); |
| 961 |
const output = generate(expressionStatement).code; |
| 962 |
expect(output).toBe("({});"); |
| 963 |
}); |
| 964 |
|
| 965 |
it("wrapped in parentheses in arrow function", () => { |
| 966 |
const arrowFunctionExpression = t.arrowFunctionExpression( |
| 967 |
[], |
| 968 |
t.objectExpression([]), |
| 969 |
); |
| 970 |
const output = generate(arrowFunctionExpression).code; |
| 971 |
expect(output).toBe("() => ({})"); |
| 972 |
}); |
| 973 |
|
| 974 |
it("not wrapped in parentheses in conditional", () => { |
| 975 |
const conditionalExpression = t.conditionalExpression( |
| 976 |
t.objectExpression([]), |
| 977 |
t.booleanLiteral(true), |
| 978 |
t.booleanLiteral(false), |
| 979 |
); |
| 980 |
const output = generate(conditionalExpression).code; |
| 981 |
expect(output).toBe("{} ? true : false"); |
| 982 |
}); |
| 983 |
|
| 984 |
it("wrapped in parentheses in conditional in expression statement", () => { |
| 985 |
const expressionStatement = t.expressionStatement( |
| 986 |
t.conditionalExpression( |
| 987 |
t.objectExpression([]), |
| 988 |
t.booleanLiteral(true), |
| 989 |
t.booleanLiteral(false), |
| 990 |
), |
| 991 |
); |
| 992 |
const output = generate(expressionStatement).code; |
| 993 |
expect(output).toBe("({}) ? true : false;"); |
| 994 |
}); |
| 995 |
|
| 996 |
it("wrapped in parentheses in conditional in arrow function", () => { |
| 997 |
const arrowFunctionExpression = t.arrowFunctionExpression( |
| 998 |
[], |
| 999 |
t.conditionalExpression( |
| 1000 |
t.objectExpression([]), |
| 1001 |
t.booleanLiteral(true), |
| 1002 |
t.booleanLiteral(false), |
| 1003 |
), |
| 1004 |
); |
| 1005 |
const output = generate(arrowFunctionExpression).code; |
| 1006 |
expect(output).toBe("() => ({}) ? true : false"); |
| 1007 |
}); |
| 1008 |
|
| 1009 |
it("not wrapped in parentheses in binary expression", () => { |
| 1010 |
const binaryExpression = t.binaryExpression( |
| 1011 |
"+", |
| 1012 |
t.objectExpression([]), |
| 1013 |
t.numericLiteral(1), |
| 1014 |
); |
| 1015 |
const output = generate(binaryExpression).code; |
| 1016 |
expect(output).toBe("{} + 1"); |
| 1017 |
}); |
| 1018 |
|
| 1019 |
it("wrapped in parentheses in binary expression in expression statement", () => { |
| 1020 |
const expressionStatement = t.expressionStatement( |
| 1021 |
t.binaryExpression("+", t.objectExpression([]), t.numericLiteral(1)), |
| 1022 |
); |
| 1023 |
const output = generate(expressionStatement).code; |
| 1024 |
expect(output).toBe("({}) + 1;"); |
| 1025 |
}); |
| 1026 |
|
| 1027 |
it("wrapped in parentheses in binary expression in arrow function", () => { |
| 1028 |
const arrowFunctionExpression = t.arrowFunctionExpression( |
| 1029 |
[], |
| 1030 |
t.binaryExpression("+", t.objectExpression([]), t.numericLiteral(1)), |
| 1031 |
); |
| 1032 |
const output = generate(arrowFunctionExpression).code; |
| 1033 |
expect(output).toBe("() => ({}) + 1"); |
| 1034 |
}); |
| 1035 |
|
| 1036 |
it("not wrapped in parentheses in sequence expression", () => { |
| 1037 |
const sequenceExpression = t.sequenceExpression([ |
| 1038 |
t.objectExpression([]), |
| 1039 |
t.numericLiteral(1), |
| 1040 |
]); |
| 1041 |
const output = generate(sequenceExpression).code; |
| 1042 |
expect(output).toBe("{}, 1"); |
| 1043 |
}); |
| 1044 |
|
| 1045 |
it("wrapped in parentheses in sequence expression in expression statement", () => { |
| 1046 |
const expressionStatement = t.expressionStatement( |
| 1047 |
t.sequenceExpression([t.objectExpression([]), t.numericLiteral(1)]), |
| 1048 |
); |
| 1049 |
const output = generate(expressionStatement).code; |
| 1050 |
expect(output).toBe("({}), 1;"); |
| 1051 |
}); |
| 1052 |
|
| 1053 |
it("wrapped in parentheses in sequence expression in arrow function", () => { |
| 1054 |
const arrowFunctionExpression = t.arrowFunctionExpression( |
| 1055 |
[], |
| 1056 |
t.sequenceExpression([t.objectExpression([]), t.numericLiteral(1)]), |
| 1057 |
); |
| 1058 |
const output = generate(arrowFunctionExpression).code; |
| 1059 |
expect(output).toBe("() => (({}), 1)"); |
| 1060 |
}); |
| 1061 |
}); |
| 1062 |
|
| 1063 |
describe("function expressions", () => { |
| 1064 |
it("not wrapped in parentheses when standalone", () => { |
| 1065 |
const functionExpression = t.functionExpression( |
| 1066 |
null, |
| 1067 |
[], |
| 1068 |
t.blockStatement([]), |
| 1069 |
); |
| 1070 |
const output = generate(functionExpression).code; |
| 1071 |
expect(output).toBe("function () {}"); |
| 1072 |
}); |
| 1073 |
|
| 1074 |
it("wrapped in parentheses in expression statement", () => { |
| 1075 |
const expressionStatement = t.expressionStatement( |
| 1076 |
t.functionExpression(null, [], t.blockStatement([])), |
| 1077 |
); |
| 1078 |
const output = generate(expressionStatement).code; |
| 1079 |
expect(output).toBe("(function () {});"); |
| 1080 |
}); |
| 1081 |
|
| 1082 |
it("wrapped in parentheses in export default declaration", () => { |
| 1083 |
const exportDefaultDeclaration = t.exportDefaultDeclaration( |
| 1084 |
t.functionExpression(null, [], t.blockStatement([])), |
| 1085 |
); |
| 1086 |
const output = generate(exportDefaultDeclaration).code; |
| 1087 |
expect(output).toBe("export default (function () {});"); |
| 1088 |
}); |
| 1089 |
}); |
| 1090 |
|
| 1091 |
describe("class expressions", () => { |
| 1092 |
it("not wrapped in parentheses when standalone", () => { |
| 1093 |
const classExpression = t.classExpression(null, null, t.classBody([])); |
| 1094 |
const output = generate(classExpression).code; |
| 1095 |
expect(output).toBe("class {}"); |
| 1096 |
}); |
| 1097 |
|
| 1098 |
it("wrapped in parentheses in expression statement", () => { |
| 1099 |
const expressionStatement = t.expressionStatement( |
| 1100 |
t.classExpression(null, null, t.classBody([])), |
| 1101 |
); |
| 1102 |
const output = generate(expressionStatement).code; |
| 1103 |
expect(output).toBe("(class {});"); |
| 1104 |
}); |
| 1105 |
|
| 1106 |
it("wrapped in parentheses in export default declaration", () => { |
| 1107 |
const exportDefaultDeclaration = t.exportDefaultDeclaration( |
| 1108 |
t.classExpression(null, null, t.classBody([])), |
| 1109 |
); |
| 1110 |
const output = generate(exportDefaultDeclaration).code; |
| 1111 |
expect(output).toBe("export default (class {});"); |
| 1112 |
}); |
| 1113 |
}); |
| 1114 |
|
| 1115 |
describe("jsescOption.minimal", () => { |
| 1116 |
const string = t.stringLiteral("\u8868\u683C_\u526F\u672C"); |
| 1117 |
|
| 1118 |
it("true", () => { |
| 1119 |
const output = generate(string, { jsescOption: { minimal: true } }).code; |
| 1120 |
expect(output).toBe(`"表格_副本"`); |
| 1121 |
}); |
| 1122 |
|
| 1123 |
it("false", () => { |
| 1124 |
const output = generate(string, { jsescOption: { minimal: false } }).code; |
| 1125 |
expect(output).toBe(`"\\u8868\\u683C_\\u526F\\u672C"`); |
| 1126 |
}); |
| 1127 |
|
| 1128 |
if (process.env.BABEL_8_BREAKING) { |
| 1129 |
it("default", () => { |
| 1130 |
const output = generate(string).code; |
| 1131 |
|
| 1132 |
expect(output).toBe(`"表格_副本"`); |
| 1133 |
}); |
| 1134 |
} else { |
| 1135 |
it("default in Babel 7", () => { |
| 1136 |
const output = generate(string).code; |
| 1137 |
|
| 1138 |
expect(output).toBe(`"\\u8868\\u683C_\\u526F\\u672C"`); |
| 1139 |
}); |
| 1140 |
} |
| 1141 |
}); |
| 1142 |
|
| 1143 |
describe("typescript interface declaration", () => { |
| 1144 |
it("empty extends array", () => { |
| 1145 |
const tsInterfaceDeclaration = t.tsInterfaceDeclaration( |
| 1146 |
t.identifier("A"), |
| 1147 |
undefined, |
| 1148 |
[], |
| 1149 |
t.tsInterfaceBody([]), |
| 1150 |
); |
| 1151 |
const output = generate(tsInterfaceDeclaration).code; |
| 1152 |
expect(output).toBe("interface A {}"); |
| 1153 |
}); |
| 1154 |
}); |
| 1155 |
|
| 1156 |
describe("identifier let", () => { |
| 1157 |
it("detects open bracket from non-optional OptionalMemberExpression", () => { |
| 1158 |
const ast = parse(`for (let?.[x];;);`, { |
| 1159 |
sourceType: "script", |
| 1160 |
strictMode: "false", |
| 1161 |
}); |
| 1162 |
ast.program.body[0].init.optional = false; |
| 1163 |
const output = generate(ast).code; |
| 1164 |
expect(output).toBe("for ((let)[x];;);"); |
| 1165 |
}); |
| 1166 |
}); |
| 1167 |
|
| 1168 |
describe("should print inner comments even if there are no suitable inner locations", () => { |
| 1169 |
it("atomic node", () => { |
| 1170 |
const id = t.identifier("foo"); |
| 1171 |
id.innerComments = [{ type: "CommentBlock", value: "foo" }]; |
| 1172 |
expect(generate(id).code).toMatchInlineSnapshot(`"foo /*foo*/"`); |
| 1173 |
}); |
| 1174 |
|
| 1175 |
it("node without inner locations", () => { |
| 1176 |
const expr = t.binaryExpression( |
| 1177 |
"+", |
| 1178 |
t.numericLiteral(1), |
| 1179 |
t.numericLiteral(2), |
| 1180 |
); |
| 1181 |
expr.innerComments = [{ type: "CommentBlock", value: "foo" }]; |
| 1182 |
expect(generate(expr).code).toMatchInlineSnapshot(`"1 + 2 /*foo*/"`); |
| 1183 |
}); |
| 1184 |
|
| 1185 |
it("comment skipped in arrow function because of newlines", () => { |
| 1186 |
const arrow = t.arrowFunctionExpression( |
| 1187 |
[t.identifier("x"), t.identifier("y")], |
| 1188 |
t.identifier("z"), |
| 1189 |
); |
| 1190 |
arrow.innerComments = [ |
| 1191 |
{ type: "CommentBlock", value: "foo" }, |
| 1192 |
{ type: "CommentBlock", value: "new\nline" }, |
| 1193 |
]; |
| 1194 |
expect(generate(arrow).code).toMatchInlineSnapshot(` |
| 1195 |
"(x, y) /*foo*/ => z |
| 1196 |
/*new |
| 1197 |
line*/" |
| 1198 |
`); |
| 1199 |
}); |
| 1200 |
|
| 1201 |
it("comment in arrow function with return type", () => { |
| 1202 |
const arrow = t.arrowFunctionExpression( |
| 1203 |
[t.identifier("x"), t.identifier("y")], |
| 1204 |
t.identifier("z"), |
| 1205 |
); |
| 1206 |
arrow.returnType = t.tsTypeAnnotation(t.tsAnyKeyword()); |
| 1207 |
arrow.returnType.trailingComments = [ |
| 1208 |
{ type: "CommentBlock", value: "foo" }, |
| 1209 |
|
| 1210 |
|
| 1211 |
{ type: "CommentBlock", value: "new\nline" }, |
| 1212 |
]; |
| 1213 |
expect(generate(arrow).code).toMatchInlineSnapshot( |
| 1214 |
`"(x, y): any /*foo*/ => z"`, |
| 1215 |
); |
| 1216 |
}); |
| 1217 |
}); |
| 1218 |
}); |
| 1219 |
|
| 1220 |
describe("CodeGenerator", function () { |
| 1221 |
it("generate", function () { |
| 1222 |
const codeGen = new CodeGenerator(t.numericLiteral(123)); |
| 1223 |
const code = codeGen.generate().code; |
| 1224 |
expect(parse(code).program.body[0].expression.value).toBe(123); |
| 1225 |
}); |
| 1226 |
}); |
| 1227 |
|
| 1228 |
const suites = (fixtures.default || fixtures)( |
| 1229 |
path.join(path.dirname(fileURLToPath(import.meta.url)), "fixtures"), |
| 1230 |
); |
| 1231 |
|
| 1232 |
suites.forEach(function (testSuite) { |
| 1233 |
describe("generation/" + testSuite.title, function () { |
| 1234 |
testSuite.tests.forEach(function (task) { |
| 1235 |
const testFn = task.disabled ? it.skip : it; |
| 1236 |
|
| 1237 |
testFn( |
| 1238 |
task.title, |
| 1239 |
|
| 1240 |
function () { |
| 1241 |
const expected = task.expect; |
| 1242 |
const actual = task.actual; |
| 1243 |
const actualCode = actual.code; |
| 1244 |
|
| 1245 |
if (actualCode) { |
| 1246 |
const parserOpts = { |
| 1247 |
filename: actual.loc, |
| 1248 |
plugins: task.options.plugins || [], |
| 1249 |
strictMode: task.options.strictMode === false ? false : true, |
| 1250 |
sourceType: "module", |
| 1251 |
sourceMaps: !!task.sourceMap, |
| 1252 |
...task.options.parserOpts, |
| 1253 |
}; |
| 1254 |
const actualAst = parse(actualCode, parserOpts); |
| 1255 |
const options = { |
| 1256 |
sourceFileName: path.relative( |
| 1257 |
path.dirname(fileURLToPath(import.meta.url)), |
| 1258 |
actual.loc, |
| 1259 |
), |
| 1260 |
...task.options, |
| 1261 |
sourceMaps: task.sourceMap ? true : task.options.sourceMaps, |
| 1262 |
}; |
| 1263 |
|
| 1264 |
const run = () => { |
| 1265 |
return generate(actualAst, options, actualCode); |
| 1266 |
}; |
| 1267 |
|
| 1268 |
const throwMsg = options.throws; |
| 1269 |
if (throwMsg) { |
| 1270 |
expect(() => run()).toThrow( |
| 1271 |
throwMsg === true ? undefined : throwMsg, |
| 1272 |
); |
| 1273 |
} else { |
| 1274 |
const result = run(); |
| 1275 |
|
| 1276 |
if (options.sourceMaps) { |
| 1277 |
try { |
| 1278 |
expect(result.map).toEqual(task.sourceMap); |
| 1279 |
} catch (e) { |
| 1280 |
if (!process.env.OVERWRITE && task.sourceMap) throw e; |
| 1281 |
console.log(`Updated test file: ${task.sourceMapFile.loc}`); |
| 1282 |
fs.writeFileSync( |
| 1283 |
task.sourceMapFile.loc, |
| 1284 |
JSON.stringify(result.map, null, 2), |
| 1285 |
); |
| 1286 |
} |
| 1287 |
} |
| 1288 |
|
| 1289 |
if ( |
| 1290 |
!expected.code && |
| 1291 |
result.code && |
| 1292 |
fs.statSync(path.dirname(expected.loc)).isDirectory() && |
| 1293 |
!process.env.CI |
| 1294 |
) { |
| 1295 |
console.log(`New test file created: ${expected.loc}`); |
| 1296 |
fs.writeFileSync(expected.loc, result.code); |
| 1297 |
} else { |
| 1298 |
try { |
| 1299 |
expect(result.code).toBe(expected.code); |
| 1300 |
if (!options.expectedReParseError) { |
| 1301 |
expect(() => { |
| 1302 |
parse(result.code, parserOpts); |
| 1303 |
}).not.toThrow(); |
| 1304 |
} |
| 1305 |
} catch (e) { |
| 1306 |
if (!process.env.OVERWRITE) throw e; |
| 1307 |
console.log(`Updated test file: ${expected.loc}`); |
| 1308 |
fs.writeFileSync(expected.loc, result.code); |
| 1309 |
} |
| 1310 |
} |
| 1311 |
} |
| 1312 |
} |
| 1313 |
}, |
| 1314 |
); |
| 1315 |
}); |
| 1316 |
}); |
| 1317 |
}); |