( js: CombinatorCompiler, node: BaseNode )
| 598 | } |
| 599 | |
| 600 | const writeDescribedCheck = ( |
| 601 | js: CombinatorCompiler, |
| 602 | node: BaseNode |
| 603 | ): string => { |
| 604 | const site = checkOf(node)! |
| 605 | const condition = node.jsCond!(js, "data")! |
| 606 | const constant = constantChecks[condition] |
| 607 | if (constant) { |
| 608 | const check = runtime[constant] as runtime.Check |
| 609 | if (site.code === check.code && site.description === check.description) |
| 610 | return `runtime.${constant}` |
| 611 | } |
| 612 | const kind = ruleFactories.find(kind => node.is(kind)) |
| 613 | const { rule, exclusive } = node.inner as { rule?: unknown; exclusive?: true } |
| 614 | if (kind && typeof rule === "number") { |
| 615 | if ( |
| 616 | ruleCheckOf(kind, rule) === runtime.integerCheck && |
| 617 | site.description === "an integer" |
| 618 | ) |
| 619 | return "runtime.integerCheck" |
| 620 | const factory = |
| 621 | exclusive ? |
| 622 | kind === "min" ? |
| 623 | "moreThan" |
| 624 | : "lessThan" |
| 625 | : kind |
| 626 | return writeCall( |
| 627 | factory, |
| 628 | `${rule}`, |
| 629 | unlessDerived( |
| 630 | (runtime[factory] as (rule: number) => runtime.Check)(rule).description, |
| 631 | site |
| 632 | ) |
| 633 | ) |
| 634 | } |
| 635 | if (node.is("pattern")) { |
| 636 | const instance = js.ref(node.instance) |
| 637 | if (condition === `${instance}.test(data)`) { |
| 638 | return writeCall( |
| 639 | "pattern", |
| 640 | instance, |
| 641 | unlessDerived(runtime.pattern(node.instance).description, site) |
| 642 | ) |
| 643 | } |
| 644 | } |
| 645 | return writeCall( |
| 646 | "check", |
| 647 | JSON.stringify(site.code), |
| 648 | JSON.stringify(site.description), |
| 649 | constant ? `runtime.${constant}.test` : `data => ${condition}` |
| 650 | ) |
| 651 | } |
| 652 | |
| 653 | const writeLiteral = (js: CombinatorCompiler, value: unknown): string => |
| 654 | value === undefined ? "void 0" : js.serialize(value) |
no test coverage detected