@blundergoat/gruff-ts 0.5.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +82 -0
- package/CONTRIBUTING.md +32 -25
- package/README.md +49 -50
- package/SECURITY.md +18 -31
- package/UPGRADING.md +101 -0
- package/bin/gruff-ts +3 -0
- package/docs/README.md +11 -10
- package/docs/agent-hook.md +32 -35
- package/docs/ci-integration.md +38 -24
- package/docs/configuration.md +160 -89
- package/docs/dashboard.md +12 -13
- package/docs/output-formats.md +100 -25
- package/docs/philosophy.md +2 -2
- package/docs/reports-and-ci.md +14 -172
- package/docs/rules.md +126 -98
- package/package.json +10 -8
- package/src/analyser.ts +817 -323
- package/src/baseline-file.ts +867 -0
- package/src/baseline-identity.ts +463 -0
- package/src/baseline-options.ts +139 -34
- package/src/baseline.ts +62 -68
- package/src/blocks.ts +512 -336
- package/src/changed-regions.ts +380 -79
- package/src/check-ignore.ts +28 -13
- package/src/class-rules.ts +445 -436
- package/src/cli-program.ts +915 -172
- package/src/cli.ts +9 -3
- package/src/comment-rules.ts +236 -418
- package/src/comment-scanner.ts +339 -47
- package/src/complexity-metrics.ts +111 -35
- package/src/config-load-error.ts +17 -8
- package/src/config-parse.ts +291 -98
- package/src/config-preservation.ts +81 -26
- package/src/config.ts +751 -223
- package/src/constants.ts +7 -6
- package/src/context-doc-rules.ts +182 -54
- package/src/dashboard.ts +88 -23
- package/src/dead-code-rules.ts +153 -74
- package/src/discovery.ts +493 -140
- package/src/doc-rules.ts +602 -126
- package/src/duplicate-test-group-rules.ts +359 -0
- package/src/findings-helpers.ts +134 -109
- package/src/findings.ts +47 -27
- package/src/fixture-purpose-rules.ts +194 -50
- package/src/github-actions-event-guards.ts +331 -0
- package/src/github-actions-rules.ts +328 -121
- package/src/hook-contract.ts +681 -255
- package/src/init-config.ts +163 -118
- package/src/inline-suppressions.ts +340 -0
- package/src/limit-band.ts +56 -0
- package/src/line-rules.ts +331 -468
- package/src/migrate-config.ts +670 -0
- package/src/naming-pushers.ts +32 -127
- package/src/parsed-script.ts +868 -314
- package/src/pillar-summary.ts +56 -40
- package/src/profiles.ts +86 -74
- package/src/project-config-rules.ts +324 -140
- package/src/project-rules.ts +237 -115
- package/src/public-exports.ts +102 -38
- package/src/report-html.ts +217 -85
- package/src/report-renderers.ts +821 -203
- package/src/rule-list.ts +166 -46
- package/src/rules.ts +100 -104
- package/src/safety-rules.ts +191 -132
- package/src/scoring.ts +249 -55
- package/src/security-flow-rules.ts +791 -115
- package/src/selectors.ts +169 -0
- package/src/sensitive-data-rules.ts +460 -387
- package/src/sensitive-exclusions.ts +435 -0
- package/src/source-text.ts +378 -104
- package/src/static-analysis-redundant-rules.ts +298 -41
- package/src/test-block-rules.ts +343 -253
- package/src/text-scans.ts +62 -11
- package/src/types.ts +220 -71
- package/tsconfig.json +2 -1
- package/src/process-exec-metadata.ts +0 -163
- package/src/test-fixtures.ts +0 -671
|
@@ -0,0 +1,331 @@
|
|
|
1
|
+
// Bounded event-guard evaluator for GitHub Actions `if:` conditions on jobs and steps.
|
|
2
|
+
//
|
|
3
|
+
// Proves when a `secrets.*` reference cannot execute under a `pull_request_target` trigger so guarded steps stay quiet.
|
|
4
|
+
// Intentionally scoped to workflow event guards rather than general YAML or expression evaluation.
|
|
5
|
+
type GuardTruth = boolean | undefined;
|
|
6
|
+
|
|
7
|
+
// Own mapping key or sequence entry, with an inclusive source range and direct children.
|
|
8
|
+
interface GuardNode {
|
|
9
|
+
key: string;
|
|
10
|
+
result: string;
|
|
11
|
+
indent: number;
|
|
12
|
+
start: number;
|
|
13
|
+
end: number;
|
|
14
|
+
isItem: boolean;
|
|
15
|
+
children: GuardNode[];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type GuardOperand =
|
|
19
|
+
| { kind: "event" }
|
|
20
|
+
| { kind: "literal"; result: string }
|
|
21
|
+
| { kind: "truth"; result: GuardTruth };
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Computes source line numbers in workflow YAML covered by job or step guards proved false for every event.
|
|
25
|
+
* Empty events, missing jobs, or structure rejected by the ownership scan yield an empty set, leaving warnings in place.
|
|
26
|
+
* Only a proved guard contributes a range, and a proved job also covers steps whose own guards are unproven.
|
|
27
|
+
*
|
|
28
|
+
* @param source - Workflow YAML file text to scan for job and step guards.
|
|
29
|
+
* @param events - Trigger event names to test against each guard condition.
|
|
30
|
+
* @returns Source line numbers inside proved unreachable job or step ranges.
|
|
31
|
+
*/
|
|
32
|
+
export function unreachableWorkflowSecretLines(source: string, events: readonly string[]): Set<number> {
|
|
33
|
+
const root = guardOwnership(source);
|
|
34
|
+
const unreachable = new Set<number>();
|
|
35
|
+
const jobs = root?.children.find((node) => node.key === "jobs" && !node.isItem && node.result === "");
|
|
36
|
+
if (!jobs || events.length === 0) return unreachable;
|
|
37
|
+
for (const job of jobs.children) {
|
|
38
|
+
if (job.isItem || job.result !== "") continue;
|
|
39
|
+
if (guardRejectsEvents(job, events)) markGuardLines(job, unreachable);
|
|
40
|
+
markUnreachableSteps(job, events, unreachable);
|
|
41
|
+
}
|
|
42
|
+
return unreachable;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Marks each qualifying step's own source-line range from `job` in `unreachable`.
|
|
47
|
+
* A step qualifies when its guard rejects every event, or when it has an `if` entry with empty events; missing steps add nothing.
|
|
48
|
+
*
|
|
49
|
+
* @param job - Job ownership node whose `steps` child sequence is inspected.
|
|
50
|
+
* @param events - Trigger event names tested against each step guard.
|
|
51
|
+
* @param unreachable - Collected unreachable lines updated in place with qualifying step source ranges.
|
|
52
|
+
* @returns Nothing. Adds each qualifying step's inclusive source-line range to `unreachable`.
|
|
53
|
+
*/
|
|
54
|
+
function markUnreachableSteps(job: GuardNode, events: readonly string[], unreachable: Set<number>): void {
|
|
55
|
+
const steps = job.children.find((node) => node.key === "steps" && node.result === "");
|
|
56
|
+
for (const step of steps?.children ?? []) {
|
|
57
|
+
if (step.isItem && guardRejectsEvents(step, events)) markGuardLines(step, unreachable);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Checks whether `node`'s direct-child `if` condition is provably false for every event.
|
|
63
|
+
* A missing condition or any unknown result fails the check; an existing condition with no events passes.
|
|
64
|
+
* Nested inputs and shell text cannot guard `node`.
|
|
65
|
+
*
|
|
66
|
+
* @param node - Job or step ownership node whose direct `if` child is evaluated.
|
|
67
|
+
* @param events - Trigger event names required to evaluate to false.
|
|
68
|
+
* @returns True only when a direct `if` condition exists and evaluates to false for every event.
|
|
69
|
+
*/
|
|
70
|
+
function guardRejectsEvents(node: GuardNode, events: readonly string[]): boolean {
|
|
71
|
+
const condition = node.children.find((child) => child.key === "if" && !child.isItem)?.result;
|
|
72
|
+
return condition !== undefined && events.every((event) => eventGuardTruth(condition, event) === false);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Records `node`'s inclusive source-line range in `lines`, including references before its guard.
|
|
77
|
+
*
|
|
78
|
+
* @param node - Ownership node supplying the inclusive start and end line numbers.
|
|
79
|
+
* @param lines - Target collection updated in place with each covered line number.
|
|
80
|
+
* @returns Nothing. Adds every line from `node.start` through `node.end` to `lines`.
|
|
81
|
+
*/
|
|
82
|
+
function markGuardLines(node: GuardNode, lines: Set<number>): void {
|
|
83
|
+
for (let line = node.start; line <= node.end; line++) lines.add(line);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Strips trailing whitespace and any unquoted YAML comment from `raw`, preserving hashes inside quoted spans.
|
|
88
|
+
* Blank or comment-only input yields an empty result; an unterminated quoted span abandons guard proof.
|
|
89
|
+
*
|
|
90
|
+
* @param raw - Single raw line of workflow YAML text.
|
|
91
|
+
* @returns Trimmed text without unquoted comments, or undefined when a quoted span is unterminated.
|
|
92
|
+
*/
|
|
93
|
+
function guardYamlText(raw: string): string | undefined {
|
|
94
|
+
for (let index = 0; index < raw.length; index++) {
|
|
95
|
+
const char = raw[index];
|
|
96
|
+
if (char === "'" || char === '"') {
|
|
97
|
+
const quoted = raw.slice(index).match(/^(?:'(?:[^']|'')*'|"(?:[^"\\]|\\.)*")/)?.[0];
|
|
98
|
+
if (!quoted) return undefined;
|
|
99
|
+
index += quoted.length - 1;
|
|
100
|
+
continue;
|
|
101
|
+
}
|
|
102
|
+
if (char === "#" && (index === 0 || /\s/.test(raw[index - 1] ?? ""))) return raw.slice(0, index).trimEnd();
|
|
103
|
+
}
|
|
104
|
+
return raw.trimEnd();
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Builds an ownership tree from workflow YAML `source` so late job and step guards also cover earlier references.
|
|
109
|
+
* Aborts on tabs before content, malformed quotes, or ambiguous structure so callers retain warnings.
|
|
110
|
+
*
|
|
111
|
+
* @param source - Complete workflow YAML document text.
|
|
112
|
+
* @returns Root ownership node, or undefined when the scan detects unsupported or ambiguous structure.
|
|
113
|
+
*/
|
|
114
|
+
function guardOwnership(source: string): GuardNode | undefined {
|
|
115
|
+
const lines = source.split(/\r?\n/);
|
|
116
|
+
const root: GuardNode = { key: "", result: "", indent: -1, start: 1, end: lines.length, isItem: false, children: [] };
|
|
117
|
+
const stack = [root];
|
|
118
|
+
let scalarIndent = -1;
|
|
119
|
+
for (const [index, raw] of lines.entries()) {
|
|
120
|
+
if (!raw.trim() || raw.trimStart().startsWith("#")) continue;
|
|
121
|
+
const indent = raw.match(/^ */)?.[0].length ?? 0;
|
|
122
|
+
if (scalarIndent >= 0 && indent > scalarIndent) continue;
|
|
123
|
+
scalarIndent = -1;
|
|
124
|
+
if (/^ *\t/.test(raw)) return undefined;
|
|
125
|
+
const text = guardYamlText(raw)?.trim();
|
|
126
|
+
if (text === undefined) return undefined;
|
|
127
|
+
const prefix = text.match(/^-(?: +|$)/)?.[0];
|
|
128
|
+
const parent = guardParent(stack, indent, index, prefix, lines.length);
|
|
129
|
+
if (!parent) return undefined;
|
|
130
|
+
const node = guardEntry(parent, text, prefix, indent, index, lines.length);
|
|
131
|
+
if (!node) return undefined;
|
|
132
|
+
if (node !== parent) stack.push(node);
|
|
133
|
+
if (/^[|>](?:[+-]?\d*|\d*[+-]?)$/.test(node.result)) scalarIndent = node.indent;
|
|
134
|
+
}
|
|
135
|
+
return root;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Pops entries from `stack` at or deeper than `indent`, closing their end lines from `index`, and resolves the active parent.
|
|
140
|
+
* Without `prefix`, returns the mapping parent; a dash adds a new sequence item to its parent's children and pushes it onto `stack`.
|
|
141
|
+
* Rejects a missing or scalar-valued parent or mixed child kinds to avoid guessing line ownership.
|
|
142
|
+
*
|
|
143
|
+
* @param stack - Open ownership ancestor stack mutated by closing deeper nodes and pushing new sequence items.
|
|
144
|
+
* @param indent - Leading space count of the current YAML line.
|
|
145
|
+
* @param index - Zero-based line index of the current YAML line.
|
|
146
|
+
* @param prefix - Optional sequence dash marker preceding the entry on the line.
|
|
147
|
+
* @param total - Total line count of the YAML document used as the initial end line.
|
|
148
|
+
* @returns Active parent or newly pushed sequence item, or undefined on invalid parent or mixed child kinds.
|
|
149
|
+
*/
|
|
150
|
+
function guardParent(stack: GuardNode[], indent: number, index: number, prefix: string | undefined, total: number): GuardNode | undefined {
|
|
151
|
+
while (stack.length > 1 && (stack.at(-1)?.indent ?? -1) >= indent) {
|
|
152
|
+
const closed = stack.pop();
|
|
153
|
+
if (closed) closed.end = index;
|
|
154
|
+
}
|
|
155
|
+
const parent = stack.at(-1);
|
|
156
|
+
if (!parent || parent.result !== "") return undefined;
|
|
157
|
+
if (prefix === undefined) return parent.children.some((child) => child.isItem) ? undefined : parent;
|
|
158
|
+
if (parent.children.some((child) => !child.isItem)) return undefined;
|
|
159
|
+
const stepItem: GuardNode = { key: "", result: "", indent, start: index + 1, end: total, isItem: true, children: [] };
|
|
160
|
+
parent.children.push(stepItem);
|
|
161
|
+
stack.push(stepItem);
|
|
162
|
+
return stepItem;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Parses `text` under `parent`, adding a mapping child node or storing a non-key sequence value in `parent.result`.
|
|
167
|
+
* Rejects anchor or alias patterns even in quotes, merge or duplicate keys, or unsupported entries to avoid false proof.
|
|
168
|
+
*
|
|
169
|
+
* @param parent - Enclosing mapping or sequence-item node receiving the parsed entry.
|
|
170
|
+
* @param text - Trimmed line text with unquoted comments removed.
|
|
171
|
+
* @param prefix - Optional sequence dash marker stripped before matching a mapping key.
|
|
172
|
+
* @param indent - Leading space count of the line before any sequence prefix.
|
|
173
|
+
* @param index - Zero-based line index of the current YAML line.
|
|
174
|
+
* @param total - Total line count of the YAML document used as the child's initial end line.
|
|
175
|
+
* @returns Newly created child node, `parent` for a plain sequence scalar, or undefined on unsupported syntax.
|
|
176
|
+
*/
|
|
177
|
+
function guardEntry(parent: GuardNode, text: string, prefix: string | undefined, indent: number, index: number, total: number): GuardNode | undefined {
|
|
178
|
+
if (/(?:^|\s)[&*][A-Za-z0-9_-]+/.test(text)) return undefined;
|
|
179
|
+
const entry = text.slice(prefix?.length ?? 0).match(/^(?:([A-Za-z0-9_.-]+)|'([^']+)'|"([^"\\]+)"|(<<)):\s*(.*)$/);
|
|
180
|
+
if (!entry) {
|
|
181
|
+
if (prefix === undefined) return undefined;
|
|
182
|
+
parent.result = text.slice(prefix.length);
|
|
183
|
+
return parent;
|
|
184
|
+
}
|
|
185
|
+
const key = entry[1] ?? entry[2] ?? entry[3] ?? entry[4] ?? "";
|
|
186
|
+
const result = entry[5] ?? "";
|
|
187
|
+
if (key === "<<") return undefined;
|
|
188
|
+
if (parent.children.some((child) => child.key === key)) return undefined;
|
|
189
|
+
const node: GuardNode = { key, result, indent: indent + (prefix?.length ?? 0), start: index + 1, end: total, isItem: false, children: [] };
|
|
190
|
+
parent.children.push(node);
|
|
191
|
+
return node;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Decodes supported YAML quoting and an enclosing `${{ ... }}` wrapper from `yamlValue`, then tokenizes the expression.
|
|
196
|
+
* An absent token result cannot justify suppressing a warning, so malformed double-quoted values need no separate JSON parse log.
|
|
197
|
+
*
|
|
198
|
+
* @param yamlValue - Raw YAML scalar text from a job or step `if` field.
|
|
199
|
+
* @returns Guard tokens, an empty list for empty input, or undefined on malformed quoting, wrappers, or unsupported syntax.
|
|
200
|
+
*/
|
|
201
|
+
function guardTokens(yamlValue: string): string[] | undefined {
|
|
202
|
+
let expression = yamlValue.trim();
|
|
203
|
+
if (expression.startsWith('"')) {
|
|
204
|
+
try {
|
|
205
|
+
const decoded: unknown = JSON.parse(expression);
|
|
206
|
+
if (typeof decoded !== "string") return undefined;
|
|
207
|
+
expression = decoded;
|
|
208
|
+
} catch { return undefined; }
|
|
209
|
+
} else if (expression.startsWith("'")) {
|
|
210
|
+
if (!/^'(?:[^']|'')*'$/.test(expression)) return undefined;
|
|
211
|
+
expression = expression.slice(1, -1).replaceAll("''", "'");
|
|
212
|
+
}
|
|
213
|
+
expression = expression.trim();
|
|
214
|
+
if (expression.startsWith("$" + "{{")) {
|
|
215
|
+
if (!expression.endsWith("}}")) return undefined;
|
|
216
|
+
expression = expression.slice(3, -2).trim();
|
|
217
|
+
}
|
|
218
|
+
return tokenizeGuard(expression);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Splits `expression` into supported guard tokens while omitting whitespace to bound the work of recursive parsing.
|
|
223
|
+
*
|
|
224
|
+
* @param expression - Unwrapped expression body to scan sequentially.
|
|
225
|
+
* @returns Guard tokens, an empty list for whitespace-only input, or undefined on unsupported syntax or over 128 tokens.
|
|
226
|
+
*/
|
|
227
|
+
function tokenizeGuard(expression: string): string[] | undefined {
|
|
228
|
+
const tokens: string[] = [];
|
|
229
|
+
while (expression) {
|
|
230
|
+
const match = expression.match(/^(?:\s+|'(?:[^']|'')*'|[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*|==|!=|&&|\|\||[!()])/);
|
|
231
|
+
if (!match) return undefined;
|
|
232
|
+
if (match[0].trim()) tokens.push(match[0]);
|
|
233
|
+
expression = expression.slice(match[0].length);
|
|
234
|
+
if (tokens.length > 128) return undefined;
|
|
235
|
+
}
|
|
236
|
+
return tokens;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Evaluates the workflow `if` value in `yamlValue` for the GitHub event named by `event`.
|
|
241
|
+
* Complete parsing is required because false AND a malformed expression must retain the warning.
|
|
242
|
+
*
|
|
243
|
+
* @param yamlValue - Raw YAML `if` expression value to tokenize and parse.
|
|
244
|
+
* @param event - GitHub Actions event name compared against `github.event_name`.
|
|
245
|
+
* @returns True or false when proved for `event`, or undefined for empty input, invalid syntax, or an undecided guard.
|
|
246
|
+
*/
|
|
247
|
+
function eventGuardTruth(yamlValue: string, event: string): GuardTruth {
|
|
248
|
+
const tokens = guardTokens(yamlValue) ?? [];
|
|
249
|
+
if (tokens.length === 0) return undefined;
|
|
250
|
+
let position = 0;
|
|
251
|
+
let isValid = true;
|
|
252
|
+
/**
|
|
253
|
+
* Extracts the proved boolean truth carried by `operand`, discarding non-boolean operands.
|
|
254
|
+
*
|
|
255
|
+
* @param operand - Parsed guard operand (event marker, literal, or truth value).
|
|
256
|
+
* @returns Proved boolean result from `operand`, or undefined for unknown truth, an event marker, or a literal.
|
|
257
|
+
*/
|
|
258
|
+
const truth = (operand: GuardOperand): GuardTruth => operand.kind === "truth" ? operand.result : undefined;
|
|
259
|
+
/**
|
|
260
|
+
* Consumes the next operand from `tokens`, advancing `position` and marking `isValid` false on invalid syntax.
|
|
261
|
+
* Negation binds before equality, and parentheses parse a complete nested expression.
|
|
262
|
+
*
|
|
263
|
+
* @returns Event marker, decoded literal, or three-valued truth operand where undefined truth means no proof.
|
|
264
|
+
*/
|
|
265
|
+
function unary(): GuardOperand {
|
|
266
|
+
const token = tokens[position++];
|
|
267
|
+
if (token === "!") {
|
|
268
|
+
const result = truth(unary());
|
|
269
|
+
return { kind: "truth", result: result === undefined ? undefined : !result };
|
|
270
|
+
}
|
|
271
|
+
if (token === "(") {
|
|
272
|
+
const result = disjunction();
|
|
273
|
+
if (tokens[position++] !== ")") isValid = false;
|
|
274
|
+
return { kind: "truth", result };
|
|
275
|
+
}
|
|
276
|
+
if (token?.startsWith("'")) return { kind: "literal", result: token.slice(1, -1).replaceAll("''", "'") };
|
|
277
|
+
if (token === "github.event_name") return { kind: "event" };
|
|
278
|
+
if (!token || !/^[A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)*$/.test(token)) isValid = false;
|
|
279
|
+
return { kind: "truth", result: undefined };
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Consumes one comparison from `tokens`, advancing `position` and propagating syntax failures to `isValid`.
|
|
283
|
+
* A lone operand keeps its known truth; unsupported pairs leave the comparison undecided.
|
|
284
|
+
*
|
|
285
|
+
* @returns Case-insensitive equality or inequality result for an event and literal, lone operand truth, or undefined.
|
|
286
|
+
*/
|
|
287
|
+
function comparison(): GuardTruth {
|
|
288
|
+
const left = unary();
|
|
289
|
+
const operator = tokens[position];
|
|
290
|
+
if (operator !== "==" && operator !== "!=") return truth(left);
|
|
291
|
+
position++;
|
|
292
|
+
const right = unary();
|
|
293
|
+
const literal = left.kind === "event" && right.kind === "literal" ? right
|
|
294
|
+
: right.kind === "event" && left.kind === "literal" ? left : undefined;
|
|
295
|
+
if (!literal) return undefined;
|
|
296
|
+
const equal = event.toLowerCase() === literal.result.toLowerCase();
|
|
297
|
+
return operator === "==" ? equal : !equal;
|
|
298
|
+
}
|
|
299
|
+
/**
|
|
300
|
+
* Consumes an AND expression from `tokens`, advancing `position` and propagating invalid operands to `isValid`.
|
|
301
|
+
* Every term is parsed to detect malformed syntax even when an earlier term is already false.
|
|
302
|
+
*
|
|
303
|
+
* @returns False if any term is false, true if all terms are true, or undefined otherwise.
|
|
304
|
+
*/
|
|
305
|
+
function conjunction(): GuardTruth {
|
|
306
|
+
let result = comparison();
|
|
307
|
+
while (tokens[position] === "&&") {
|
|
308
|
+
position++;
|
|
309
|
+
const right = comparison();
|
|
310
|
+
result = result === false || right === false ? false : result === true && right === true ? true : undefined;
|
|
311
|
+
}
|
|
312
|
+
return result;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Consumes an OR expression from `tokens`, advancing `position` and propagating invalid operands to `isValid`.
|
|
316
|
+
* Every term is parsed to detect malformed syntax even when an earlier term is already true.
|
|
317
|
+
*
|
|
318
|
+
* @returns True if any term is true, false if all terms are false, or undefined otherwise.
|
|
319
|
+
*/
|
|
320
|
+
function disjunction(): GuardTruth {
|
|
321
|
+
let result = conjunction();
|
|
322
|
+
while (tokens[position] === "||") {
|
|
323
|
+
position++;
|
|
324
|
+
const right = conjunction();
|
|
325
|
+
result = result === true || right === true ? true : result === false && right === false ? false : undefined;
|
|
326
|
+
}
|
|
327
|
+
return result;
|
|
328
|
+
}
|
|
329
|
+
const result = disjunction();
|
|
330
|
+
return isValid && position === tokens.length ? result : undefined;
|
|
331
|
+
}
|