writes its Allows: each child's check, a basis's folded into the first key read
(js: NodeCompiler)
| 328 | |
| 329 | /** writes its Allows: each child's check, a basis's folded into the first key read */ |
| 330 | private jsAllows(js: NodeCompiler): void { |
| 331 | const read = |
| 332 | this.inner.domain?.domain === "object" && !this.preStruct.length ? |
| 333 | this.structure?.allowsProps[0] |
| 334 | : undefined |
| 335 | // a key is read before data is checked to be an object, in a switch |
| 336 | // rejecting what none can be read from |
| 337 | if (read && !js.shortest) { |
| 338 | js.block("switch (data)", () => |
| 339 | js |
| 340 | .line("case null: case undefined: break") |
| 341 | .line("default:") |
| 342 | .line(`data${js.prop(read.key)}`) |
| 343 | .if( |
| 344 | this.children |
| 345 | .map(child => |
| 346 | child === this.basis ? |
| 347 | objectCondition |
| 348 | : `(${js.condition(child)})` |
| 349 | ) |
| 350 | .join(" && "), |
| 351 | () => js.return(true) |
| 352 | ) |
| 353 | ).return(js.rejection(this)) |
| 354 | return |
| 355 | } |
| 356 | const sequence = this.inlinedSequence |
| 357 | const checks = () => { |
| 358 | for (const child of this.children) { |
| 359 | // the last child, returning true once each element is allowed |
| 360 | if (child === this.structure && sequence) { |
| 361 | sequence.compile(js) |
| 362 | return js |
| 363 | } |
| 364 | if (child !== this.basis || !read) |
| 365 | js.check(child, { breaks: !js.shortest }) |
| 366 | else { |
| 367 | js.rejectNullish(this) |
| 368 | .line(`data${js.prop(read.key)}`) |
| 369 | .if(`!${objectCondition}`, () => js.return(false)) |
| 370 | } |
| 371 | } |
| 372 | return js.return(true) |
| 373 | } |
| 374 | const rejection = js.rejection(this) |
| 375 | // a loop keeps a minifier from joining the checks into one expression, |
| 376 | // which a build for size allows |
| 377 | if (js.shortest) checks() |
| 378 | else if ( |
| 379 | !sequence && |
| 380 | rejection === "false" && |
| 381 | this.children.every(child => child.jsCond !== undefined) |
| 382 | ) { |
| 383 | js.return( |
| 384 | this.children.map(child => `(${js.condition(child)})`).join(" && ") || |
| 385 | "true" |
| 386 | ) |
| 387 | } else js.block("for (;;)", checks).return(rejection) |