| 107 | } |
| 108 | |
| 109 | compile(js: NodeCompiler): void { |
| 110 | const condition = `${js.ref(this.predicate)}(data, ctx${this.says ? `, ${JSON.stringify(this.says)}` : ""})` |
| 111 | if (js.trav === "Allows") { |
| 112 | js.return( |
| 113 | returnsReports.has(this.predicate) ? |
| 114 | `${js.ref(this.predicate)}(data, ${js.ref(silentCtx)})` |
| 115 | : condition |
| 116 | ) |
| 117 | return |
| 118 | } |
| 119 | if (js.addsNoIssues?.(this.predicate)) { |
| 120 | // a build may answer `allows` with this, and a narrow returns anything |
| 121 | js.return(`!!${condition} || !${js.error(this)}`) |
| 122 | return |
| 123 | } |
| 124 | |
| 125 | js.initErrCount() |
| 126 | const label = this.meta.label |
| 127 | // a labeled narrow names its value in what it raises, as in the error |
| 128 | // returning false adds: an unlabeled one's call is as it was. The label |
| 129 | // it runs under is a callback's that applied it, set again once it returns |
| 130 | if (label !== undefined) { |
| 131 | js.const("lbl", "ctx.lbl") |
| 132 | .line(`ctx.lbl = ${JSON.stringify(label)}`) |
| 133 | .const("valid", condition) |
| 134 | .line("ctx.lbl = lbl") |
| 135 | } |
| 136 | js.if( |
| 137 | // only add the default error if the predicate didn't add one itself |
| 138 | `!${label === undefined ? condition : "valid"} && ctx.errN === errN`, |
| 139 | () => js.return(`!${js.error(this)}`) |
| 140 | ) |
| 141 | js.return("!ctx.failFast || ctx.errN === errN") |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | export const Predicate = { |