webpipe-js 0.1.0 → 0.1.2
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/dist/index.cjs +721 -0
- package/dist/{parser.d.ts → index.d.cts} +26 -23
- package/dist/index.d.ts +132 -0
- package/{parser.ts → dist/index.mjs} +297 -349
- package/package.json +16 -6
- package/comprehensive_test.wp +0 -1139
- package/dist/parser.js +0 -594
- package/dist/tests/parser.test.d.ts +0 -1
- package/dist/tests/parser.test.js +0 -106
- package/tests/parser.test.ts +0 -119
- package/tsconfig.json +0 -18
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,721 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
getPipelineRanges: () => getPipelineRanges,
|
|
34
|
+
getVariableRanges: () => getVariableRanges,
|
|
35
|
+
parseProgram: () => parseProgram,
|
|
36
|
+
parseProgramWithDiagnostics: () => parseProgramWithDiagnostics,
|
|
37
|
+
prettyPrint: () => prettyPrint
|
|
38
|
+
});
|
|
39
|
+
module.exports = __toCommonJS(index_exports);
|
|
40
|
+
|
|
41
|
+
// src/parser.ts
|
|
42
|
+
var import_meta = {};
|
|
43
|
+
var Parser = class {
|
|
44
|
+
constructor(text) {
|
|
45
|
+
this.pos = 0;
|
|
46
|
+
this.diagnostics = [];
|
|
47
|
+
this.pipelineRanges = /* @__PURE__ */ new Map();
|
|
48
|
+
this.variableRanges = /* @__PURE__ */ new Map();
|
|
49
|
+
this.text = text;
|
|
50
|
+
this.len = text.length;
|
|
51
|
+
}
|
|
52
|
+
getDiagnostics() {
|
|
53
|
+
return this.diagnostics.slice();
|
|
54
|
+
}
|
|
55
|
+
getPipelineRanges() {
|
|
56
|
+
return new Map(this.pipelineRanges);
|
|
57
|
+
}
|
|
58
|
+
getVariableRanges() {
|
|
59
|
+
return new Map(this.variableRanges);
|
|
60
|
+
}
|
|
61
|
+
report(message, start, end, severity) {
|
|
62
|
+
this.diagnostics.push({ message, start, end, severity });
|
|
63
|
+
}
|
|
64
|
+
findLineStart(pos) {
|
|
65
|
+
let i = Math.max(0, Math.min(pos, this.len));
|
|
66
|
+
while (i > 0 && this.text[i - 1] !== "\n") i--;
|
|
67
|
+
return i;
|
|
68
|
+
}
|
|
69
|
+
findLineEnd(pos) {
|
|
70
|
+
let i = Math.max(0, Math.min(pos, this.len));
|
|
71
|
+
while (i < this.text.length && this.text[i] !== "\n") i++;
|
|
72
|
+
return i;
|
|
73
|
+
}
|
|
74
|
+
parseProgram() {
|
|
75
|
+
this.skipSpaces();
|
|
76
|
+
const configs = [];
|
|
77
|
+
const pipelines = [];
|
|
78
|
+
const variables = [];
|
|
79
|
+
const routes = [];
|
|
80
|
+
const describes = [];
|
|
81
|
+
while (!this.eof()) {
|
|
82
|
+
this.skipSpaces();
|
|
83
|
+
if (this.eof()) break;
|
|
84
|
+
const start = this.pos;
|
|
85
|
+
const cfg = this.tryParse(() => this.parseConfig());
|
|
86
|
+
if (cfg) {
|
|
87
|
+
configs.push(cfg);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
const namedPipe = this.tryParse(() => this.parseNamedPipeline());
|
|
91
|
+
if (namedPipe) {
|
|
92
|
+
pipelines.push(namedPipe);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const variable = this.tryParse(() => this.parseVariable());
|
|
96
|
+
if (variable) {
|
|
97
|
+
variables.push(variable);
|
|
98
|
+
continue;
|
|
99
|
+
}
|
|
100
|
+
const route = this.tryParse(() => this.parseRoute());
|
|
101
|
+
if (route) {
|
|
102
|
+
routes.push(route);
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
const describe = this.tryParse(() => this.parseDescribe());
|
|
106
|
+
if (describe) {
|
|
107
|
+
describes.push(describe);
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
if (this.pos === start) {
|
|
111
|
+
const lineStart = this.findLineStart(this.pos);
|
|
112
|
+
const lineEnd = this.findLineEnd(this.pos);
|
|
113
|
+
this.report("Unrecognized or unsupported syntax", lineStart, lineEnd, "warning");
|
|
114
|
+
this.skipToEol();
|
|
115
|
+
if (this.cur() === "\n") this.pos++;
|
|
116
|
+
this.consumeWhile((c) => c === "\n");
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
const backtickCount = (this.text.match(/`/g) || []).length;
|
|
120
|
+
if (backtickCount % 2 === 1) {
|
|
121
|
+
const idx = this.text.lastIndexOf("`");
|
|
122
|
+
const start = Math.max(0, idx);
|
|
123
|
+
this.report("Unclosed backtick-delimited string", start, start + 1, "warning");
|
|
124
|
+
}
|
|
125
|
+
return { configs, pipelines, variables, routes, describes };
|
|
126
|
+
}
|
|
127
|
+
eof() {
|
|
128
|
+
return this.pos >= this.len;
|
|
129
|
+
}
|
|
130
|
+
peek() {
|
|
131
|
+
return this.text[this.pos] ?? "\0";
|
|
132
|
+
}
|
|
133
|
+
cur() {
|
|
134
|
+
return this.text[this.pos] ?? "\0";
|
|
135
|
+
}
|
|
136
|
+
ahead(n) {
|
|
137
|
+
return this.text[this.pos + n] ?? "\0";
|
|
138
|
+
}
|
|
139
|
+
tryParse(fn) {
|
|
140
|
+
const save = this.pos;
|
|
141
|
+
try {
|
|
142
|
+
const value = fn();
|
|
143
|
+
return value;
|
|
144
|
+
} catch (_e) {
|
|
145
|
+
this.pos = save;
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
skipSpaces() {
|
|
150
|
+
while (true) {
|
|
151
|
+
this.consumeWhile((ch) => ch === " " || ch === " " || ch === "\r" || ch === "\n");
|
|
152
|
+
if (this.text.startsWith("#", this.pos)) {
|
|
153
|
+
this.skipToEol();
|
|
154
|
+
if (this.cur() === "\n") this.pos++;
|
|
155
|
+
continue;
|
|
156
|
+
}
|
|
157
|
+
if (this.text.startsWith("//", this.pos)) {
|
|
158
|
+
this.skipToEol();
|
|
159
|
+
if (this.cur() === "\n") this.pos++;
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
skipInlineSpaces() {
|
|
166
|
+
this.consumeWhile((ch) => ch === " " || ch === " " || ch === "\r");
|
|
167
|
+
}
|
|
168
|
+
consumeWhile(pred) {
|
|
169
|
+
const start = this.pos;
|
|
170
|
+
while (!this.eof() && pred(this.text[this.pos])) this.pos++;
|
|
171
|
+
return this.text.slice(start, this.pos);
|
|
172
|
+
}
|
|
173
|
+
match(str) {
|
|
174
|
+
if (this.text.startsWith(str, this.pos)) {
|
|
175
|
+
this.pos += str.length;
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
180
|
+
expect(str) {
|
|
181
|
+
if (!this.match(str)) throw new ParseFailure(`expected '${str}'`, this.pos);
|
|
182
|
+
}
|
|
183
|
+
skipToEol() {
|
|
184
|
+
while (!this.eof() && this.cur() !== "\n") this.pos++;
|
|
185
|
+
}
|
|
186
|
+
isIdentStart(ch) {
|
|
187
|
+
return /[A-Za-z_]/.test(ch);
|
|
188
|
+
}
|
|
189
|
+
isIdentCont(ch) {
|
|
190
|
+
return /[A-Za-z0-9_\-]/.test(ch);
|
|
191
|
+
}
|
|
192
|
+
parseIdentifier() {
|
|
193
|
+
if (!this.isIdentStart(this.cur())) throw new ParseFailure("identifier", this.pos);
|
|
194
|
+
const start = this.pos;
|
|
195
|
+
this.pos++;
|
|
196
|
+
while (!this.eof() && this.isIdentCont(this.cur())) this.pos++;
|
|
197
|
+
return this.text.slice(start, this.pos);
|
|
198
|
+
}
|
|
199
|
+
parseNumber() {
|
|
200
|
+
const start = this.pos;
|
|
201
|
+
const digits = this.consumeWhile((c) => /[0-9]/.test(c));
|
|
202
|
+
if (digits.length === 0) throw new ParseFailure("number", this.pos);
|
|
203
|
+
return parseInt(this.text.slice(start, this.pos), 10);
|
|
204
|
+
}
|
|
205
|
+
parseQuotedString() {
|
|
206
|
+
this.expect('"');
|
|
207
|
+
const start = this.pos;
|
|
208
|
+
while (!this.eof()) {
|
|
209
|
+
const ch = this.cur();
|
|
210
|
+
if (ch === '"') break;
|
|
211
|
+
this.pos++;
|
|
212
|
+
}
|
|
213
|
+
const content = this.text.slice(start, this.pos);
|
|
214
|
+
this.expect('"');
|
|
215
|
+
return content;
|
|
216
|
+
}
|
|
217
|
+
parseBacktickString() {
|
|
218
|
+
this.expect("`");
|
|
219
|
+
const start = this.pos;
|
|
220
|
+
while (!this.eof()) {
|
|
221
|
+
const ch = this.cur();
|
|
222
|
+
if (ch === "`") break;
|
|
223
|
+
this.pos++;
|
|
224
|
+
}
|
|
225
|
+
const content = this.text.slice(start, this.pos);
|
|
226
|
+
this.expect("`");
|
|
227
|
+
return content;
|
|
228
|
+
}
|
|
229
|
+
parseMethod() {
|
|
230
|
+
const methods = ["GET", "POST", "PUT", "PATCH", "DELETE"];
|
|
231
|
+
for (const m of methods) {
|
|
232
|
+
if (this.text.startsWith(m, this.pos)) {
|
|
233
|
+
this.pos += m.length;
|
|
234
|
+
return m;
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
throw new ParseFailure("method", this.pos);
|
|
238
|
+
}
|
|
239
|
+
parseStepConfig() {
|
|
240
|
+
const bt = this.tryParse(() => this.parseBacktickString());
|
|
241
|
+
if (bt !== null) return bt;
|
|
242
|
+
const dq = this.tryParse(() => this.parseQuotedString());
|
|
243
|
+
if (dq !== null) return dq;
|
|
244
|
+
const id = this.tryParse(() => this.parseIdentifier());
|
|
245
|
+
if (id !== null) return id;
|
|
246
|
+
throw new ParseFailure("step-config", this.pos);
|
|
247
|
+
}
|
|
248
|
+
parseConfigValue() {
|
|
249
|
+
const envWithDefault = this.tryParse(() => {
|
|
250
|
+
this.expect("$");
|
|
251
|
+
const variable = this.parseIdentifier();
|
|
252
|
+
this.skipInlineSpaces();
|
|
253
|
+
this.expect("||");
|
|
254
|
+
this.skipInlineSpaces();
|
|
255
|
+
const def = this.parseQuotedString();
|
|
256
|
+
return { kind: "EnvVar", var: variable, default: def };
|
|
257
|
+
});
|
|
258
|
+
if (envWithDefault) return envWithDefault;
|
|
259
|
+
const envNoDefault = this.tryParse(() => {
|
|
260
|
+
this.expect("$");
|
|
261
|
+
const variable = this.parseIdentifier();
|
|
262
|
+
return { kind: "EnvVar", var: variable };
|
|
263
|
+
});
|
|
264
|
+
if (envNoDefault) return envNoDefault;
|
|
265
|
+
const str = this.tryParse(() => this.parseQuotedString());
|
|
266
|
+
if (str !== null) return { kind: "String", value: str };
|
|
267
|
+
const bool = this.tryParse(() => {
|
|
268
|
+
if (this.match("true")) return true;
|
|
269
|
+
if (this.match("false")) return false;
|
|
270
|
+
throw new ParseFailure("bool", this.pos);
|
|
271
|
+
});
|
|
272
|
+
if (bool !== null) return { kind: "Boolean", value: bool };
|
|
273
|
+
const num = this.tryParse(() => this.parseNumber());
|
|
274
|
+
if (num !== null) return { kind: "Number", value: num };
|
|
275
|
+
throw new ParseFailure("config-value", this.pos);
|
|
276
|
+
}
|
|
277
|
+
parseConfigProperty() {
|
|
278
|
+
this.skipSpaces();
|
|
279
|
+
const key = this.parseIdentifier();
|
|
280
|
+
this.skipInlineSpaces();
|
|
281
|
+
this.expect(":");
|
|
282
|
+
this.skipInlineSpaces();
|
|
283
|
+
const value = this.parseConfigValue();
|
|
284
|
+
return { key, value };
|
|
285
|
+
}
|
|
286
|
+
parseConfig() {
|
|
287
|
+
this.expect("config");
|
|
288
|
+
this.skipInlineSpaces();
|
|
289
|
+
const name = this.parseIdentifier();
|
|
290
|
+
this.skipInlineSpaces();
|
|
291
|
+
this.expect("{");
|
|
292
|
+
this.skipSpaces();
|
|
293
|
+
const properties = [];
|
|
294
|
+
while (true) {
|
|
295
|
+
const prop = this.tryParse(() => this.parseConfigProperty());
|
|
296
|
+
if (!prop) break;
|
|
297
|
+
properties.push(prop);
|
|
298
|
+
this.skipSpaces();
|
|
299
|
+
}
|
|
300
|
+
this.skipSpaces();
|
|
301
|
+
this.expect("}");
|
|
302
|
+
this.skipSpaces();
|
|
303
|
+
return { name, properties };
|
|
304
|
+
}
|
|
305
|
+
parsePipelineStep() {
|
|
306
|
+
const result = this.tryParse(() => this.parseResultStep());
|
|
307
|
+
if (result) return result;
|
|
308
|
+
return this.parseRegularStep();
|
|
309
|
+
}
|
|
310
|
+
parseRegularStep() {
|
|
311
|
+
this.skipSpaces();
|
|
312
|
+
this.expect("|>");
|
|
313
|
+
this.skipInlineSpaces();
|
|
314
|
+
const name = this.parseIdentifier();
|
|
315
|
+
this.expect(":");
|
|
316
|
+
this.skipInlineSpaces();
|
|
317
|
+
const config = this.parseStepConfig();
|
|
318
|
+
this.skipSpaces();
|
|
319
|
+
return { kind: "Regular", name, config };
|
|
320
|
+
}
|
|
321
|
+
parseResultStep() {
|
|
322
|
+
this.skipSpaces();
|
|
323
|
+
this.expect("|>");
|
|
324
|
+
this.skipInlineSpaces();
|
|
325
|
+
this.expect("result");
|
|
326
|
+
this.skipSpaces();
|
|
327
|
+
const branches = [];
|
|
328
|
+
while (true) {
|
|
329
|
+
const br = this.tryParse(() => this.parseResultBranch());
|
|
330
|
+
if (!br) break;
|
|
331
|
+
branches.push(br);
|
|
332
|
+
}
|
|
333
|
+
return { kind: "Result", branches };
|
|
334
|
+
}
|
|
335
|
+
parseResultBranch() {
|
|
336
|
+
this.skipSpaces();
|
|
337
|
+
const branchIdent = this.parseIdentifier();
|
|
338
|
+
let branchType;
|
|
339
|
+
if (branchIdent === "ok") branchType = { kind: "Ok" };
|
|
340
|
+
else if (branchIdent === "default") branchType = { kind: "Default" };
|
|
341
|
+
else branchType = { kind: "Custom", name: branchIdent };
|
|
342
|
+
this.expect("(");
|
|
343
|
+
const statusCode = this.parseNumber();
|
|
344
|
+
if (statusCode < 100 || statusCode > 599) {
|
|
345
|
+
this.report(
|
|
346
|
+
`Invalid HTTP status code: ${statusCode}`,
|
|
347
|
+
this.pos - String(statusCode).length,
|
|
348
|
+
this.pos,
|
|
349
|
+
"error"
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
this.expect(")");
|
|
353
|
+
this.expect(":");
|
|
354
|
+
this.skipSpaces();
|
|
355
|
+
const pipeline = this.parsePipeline();
|
|
356
|
+
return { branchType, statusCode, pipeline };
|
|
357
|
+
}
|
|
358
|
+
parsePipeline() {
|
|
359
|
+
const steps = [];
|
|
360
|
+
while (true) {
|
|
361
|
+
const save = this.pos;
|
|
362
|
+
this.skipSpaces();
|
|
363
|
+
if (!this.text.startsWith("|>", this.pos)) {
|
|
364
|
+
this.pos = save;
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
const step = this.parsePipelineStep();
|
|
368
|
+
steps.push(step);
|
|
369
|
+
}
|
|
370
|
+
return { steps };
|
|
371
|
+
}
|
|
372
|
+
parseNamedPipeline() {
|
|
373
|
+
const start = this.pos;
|
|
374
|
+
this.expect("pipeline");
|
|
375
|
+
this.skipInlineSpaces();
|
|
376
|
+
const name = this.parseIdentifier();
|
|
377
|
+
this.skipInlineSpaces();
|
|
378
|
+
this.expect("=");
|
|
379
|
+
this.skipInlineSpaces();
|
|
380
|
+
const beforePipeline = this.pos;
|
|
381
|
+
const pipeline = this.parsePipeline();
|
|
382
|
+
const end = this.pos;
|
|
383
|
+
this.pipelineRanges.set(name, { start, end });
|
|
384
|
+
this.skipSpaces();
|
|
385
|
+
return { name, pipeline };
|
|
386
|
+
}
|
|
387
|
+
parsePipelineRef() {
|
|
388
|
+
const inline = this.tryParse(() => this.parsePipeline());
|
|
389
|
+
if (inline && inline.steps.length > 0) return { kind: "Inline", pipeline: inline };
|
|
390
|
+
const named = this.tryParse(() => {
|
|
391
|
+
this.skipSpaces();
|
|
392
|
+
this.expect("|>");
|
|
393
|
+
this.skipInlineSpaces();
|
|
394
|
+
this.expect("pipeline:");
|
|
395
|
+
this.skipInlineSpaces();
|
|
396
|
+
const name = this.parseIdentifier();
|
|
397
|
+
return { kind: "Named", name };
|
|
398
|
+
});
|
|
399
|
+
if (named) return named;
|
|
400
|
+
throw new Error("pipeline-ref");
|
|
401
|
+
}
|
|
402
|
+
parseVariable() {
|
|
403
|
+
const start = this.pos;
|
|
404
|
+
const varType = this.parseIdentifier();
|
|
405
|
+
this.skipInlineSpaces();
|
|
406
|
+
const name = this.parseIdentifier();
|
|
407
|
+
this.skipInlineSpaces();
|
|
408
|
+
this.expect("=");
|
|
409
|
+
this.skipInlineSpaces();
|
|
410
|
+
const value = this.parseBacktickString();
|
|
411
|
+
const end = this.pos;
|
|
412
|
+
this.variableRanges.set(`${varType}::${name}`, { start, end });
|
|
413
|
+
this.skipSpaces();
|
|
414
|
+
return { varType, name, value };
|
|
415
|
+
}
|
|
416
|
+
parseRoute() {
|
|
417
|
+
const method = this.parseMethod();
|
|
418
|
+
this.skipInlineSpaces();
|
|
419
|
+
const path = this.consumeWhile((c) => c !== " " && c !== "\n");
|
|
420
|
+
this.skipSpaces();
|
|
421
|
+
const pipeline = this.parsePipelineRef();
|
|
422
|
+
this.skipSpaces();
|
|
423
|
+
return { method, path, pipeline };
|
|
424
|
+
}
|
|
425
|
+
parseWhen() {
|
|
426
|
+
const calling = this.tryParse(() => {
|
|
427
|
+
this.expect("calling");
|
|
428
|
+
this.skipInlineSpaces();
|
|
429
|
+
const method = this.parseMethod();
|
|
430
|
+
this.skipInlineSpaces();
|
|
431
|
+
const path = this.consumeWhile((c) => c !== "\n");
|
|
432
|
+
return { kind: "CallingRoute", method, path };
|
|
433
|
+
});
|
|
434
|
+
if (calling) return calling;
|
|
435
|
+
const executingPipeline = this.tryParse(() => {
|
|
436
|
+
this.expect("executing");
|
|
437
|
+
this.skipInlineSpaces();
|
|
438
|
+
this.expect("pipeline");
|
|
439
|
+
this.skipInlineSpaces();
|
|
440
|
+
const name = this.parseIdentifier();
|
|
441
|
+
return { kind: "ExecutingPipeline", name };
|
|
442
|
+
});
|
|
443
|
+
if (executingPipeline) return executingPipeline;
|
|
444
|
+
const executingVariable = this.tryParse(() => {
|
|
445
|
+
this.expect("executing");
|
|
446
|
+
this.skipInlineSpaces();
|
|
447
|
+
this.expect("variable");
|
|
448
|
+
this.skipInlineSpaces();
|
|
449
|
+
const varType = this.parseIdentifier();
|
|
450
|
+
this.skipInlineSpaces();
|
|
451
|
+
const name = this.parseIdentifier();
|
|
452
|
+
return { kind: "ExecutingVariable", varType, name };
|
|
453
|
+
});
|
|
454
|
+
if (executingVariable) return executingVariable;
|
|
455
|
+
throw new ParseFailure("when", this.pos);
|
|
456
|
+
}
|
|
457
|
+
parseCondition() {
|
|
458
|
+
this.skipSpaces();
|
|
459
|
+
const ct = (() => {
|
|
460
|
+
if (this.match("then")) return "Then";
|
|
461
|
+
if (this.match("and")) return "And";
|
|
462
|
+
throw new Error("condition-type");
|
|
463
|
+
})();
|
|
464
|
+
this.skipInlineSpaces();
|
|
465
|
+
const field = this.consumeWhile((c) => c !== " " && c !== "\n" && c !== "`");
|
|
466
|
+
this.skipInlineSpaces();
|
|
467
|
+
const jqExpr = this.tryParse(() => this.parseBacktickString());
|
|
468
|
+
this.skipInlineSpaces();
|
|
469
|
+
const comparison = this.consumeWhile((c) => c !== " " && c !== "\n");
|
|
470
|
+
this.skipInlineSpaces();
|
|
471
|
+
const value = (() => {
|
|
472
|
+
const v1 = this.tryParse(() => this.parseBacktickString());
|
|
473
|
+
if (v1 !== null) return v1;
|
|
474
|
+
const v2 = this.tryParse(() => this.parseQuotedString());
|
|
475
|
+
if (v2 !== null) return v2;
|
|
476
|
+
return this.consumeWhile((c) => c !== "\n");
|
|
477
|
+
})();
|
|
478
|
+
return { conditionType: ct, field, jqExpr: jqExpr ?? void 0, comparison, value };
|
|
479
|
+
}
|
|
480
|
+
parseMockHead(prefixWord) {
|
|
481
|
+
this.skipSpaces();
|
|
482
|
+
this.expect(prefixWord);
|
|
483
|
+
this.skipInlineSpaces();
|
|
484
|
+
this.expect("mock");
|
|
485
|
+
this.skipInlineSpaces();
|
|
486
|
+
const target = this.consumeWhile((c) => c !== " " && c !== "\n");
|
|
487
|
+
this.skipInlineSpaces();
|
|
488
|
+
this.expect("returning");
|
|
489
|
+
this.skipInlineSpaces();
|
|
490
|
+
const returnValue = this.parseBacktickString();
|
|
491
|
+
this.skipSpaces();
|
|
492
|
+
return { target, returnValue };
|
|
493
|
+
}
|
|
494
|
+
parseMock() {
|
|
495
|
+
return this.parseMockHead("with");
|
|
496
|
+
}
|
|
497
|
+
parseAndMock() {
|
|
498
|
+
return this.parseMockHead("and");
|
|
499
|
+
}
|
|
500
|
+
parseIt() {
|
|
501
|
+
this.skipSpaces();
|
|
502
|
+
this.expect("it");
|
|
503
|
+
this.skipInlineSpaces();
|
|
504
|
+
this.expect('"');
|
|
505
|
+
const name = this.consumeWhile((c) => c !== '"');
|
|
506
|
+
this.expect('"');
|
|
507
|
+
this.skipSpaces();
|
|
508
|
+
const mocks = [];
|
|
509
|
+
while (true) {
|
|
510
|
+
const m = this.tryParse(() => this.parseMock());
|
|
511
|
+
if (!m) break;
|
|
512
|
+
mocks.push(m);
|
|
513
|
+
}
|
|
514
|
+
this.expect("when");
|
|
515
|
+
this.skipInlineSpaces();
|
|
516
|
+
const when = this.parseWhen();
|
|
517
|
+
this.skipSpaces();
|
|
518
|
+
const input = this.tryParse(() => {
|
|
519
|
+
this.expect("with");
|
|
520
|
+
this.skipInlineSpaces();
|
|
521
|
+
this.expect("input");
|
|
522
|
+
this.skipInlineSpaces();
|
|
523
|
+
const v = this.parseBacktickString();
|
|
524
|
+
this.skipSpaces();
|
|
525
|
+
return v;
|
|
526
|
+
}) ?? void 0;
|
|
527
|
+
const extraMocks = [];
|
|
528
|
+
while (true) {
|
|
529
|
+
const m = this.tryParse(() => this.parseAndMock());
|
|
530
|
+
if (!m) break;
|
|
531
|
+
extraMocks.push(m);
|
|
532
|
+
this.skipSpaces();
|
|
533
|
+
}
|
|
534
|
+
const conditions = [];
|
|
535
|
+
while (true) {
|
|
536
|
+
const c = this.tryParse(() => this.parseCondition());
|
|
537
|
+
if (!c) break;
|
|
538
|
+
conditions.push(c);
|
|
539
|
+
}
|
|
540
|
+
return { name, mocks: [...mocks, ...extraMocks], when, input, conditions };
|
|
541
|
+
}
|
|
542
|
+
parseDescribe() {
|
|
543
|
+
this.skipSpaces();
|
|
544
|
+
this.expect("describe");
|
|
545
|
+
this.skipInlineSpaces();
|
|
546
|
+
this.expect('"');
|
|
547
|
+
const name = this.consumeWhile((c) => c !== '"');
|
|
548
|
+
this.expect('"');
|
|
549
|
+
this.skipSpaces();
|
|
550
|
+
const mocks = [];
|
|
551
|
+
while (true) {
|
|
552
|
+
const m = this.tryParse(() => this.parseMock());
|
|
553
|
+
if (!m) break;
|
|
554
|
+
mocks.push(m);
|
|
555
|
+
this.skipSpaces();
|
|
556
|
+
}
|
|
557
|
+
const tests = [];
|
|
558
|
+
while (true) {
|
|
559
|
+
const it = this.tryParse(() => this.parseIt());
|
|
560
|
+
if (!it) break;
|
|
561
|
+
tests.push(it);
|
|
562
|
+
}
|
|
563
|
+
return { name, mocks, tests };
|
|
564
|
+
}
|
|
565
|
+
};
|
|
566
|
+
function parseProgram(text) {
|
|
567
|
+
const parser = new Parser(text);
|
|
568
|
+
return parser.parseProgram();
|
|
569
|
+
}
|
|
570
|
+
function parseProgramWithDiagnostics(text) {
|
|
571
|
+
const parser = new Parser(text);
|
|
572
|
+
const program = parser.parseProgram();
|
|
573
|
+
return { program, diagnostics: parser.getDiagnostics() };
|
|
574
|
+
}
|
|
575
|
+
function getPipelineRanges(text) {
|
|
576
|
+
const parser = new Parser(text);
|
|
577
|
+
parser.parseProgram();
|
|
578
|
+
return parser.getPipelineRanges();
|
|
579
|
+
}
|
|
580
|
+
function getVariableRanges(text) {
|
|
581
|
+
const parser = new Parser(text);
|
|
582
|
+
parser.parseProgram();
|
|
583
|
+
return parser.getVariableRanges();
|
|
584
|
+
}
|
|
585
|
+
var ParseFailure = class extends Error {
|
|
586
|
+
constructor(message, at) {
|
|
587
|
+
super(message);
|
|
588
|
+
this.at = at;
|
|
589
|
+
}
|
|
590
|
+
};
|
|
591
|
+
function prettyPrint(program) {
|
|
592
|
+
const lines = [];
|
|
593
|
+
program.configs.forEach((config) => {
|
|
594
|
+
lines.push(`config ${config.name} {`);
|
|
595
|
+
config.properties.forEach((prop) => {
|
|
596
|
+
const value = formatConfigValue(prop.value);
|
|
597
|
+
lines.push(` ${prop.key}: ${value}`);
|
|
598
|
+
});
|
|
599
|
+
lines.push("}");
|
|
600
|
+
lines.push("");
|
|
601
|
+
});
|
|
602
|
+
program.variables.forEach((variable) => {
|
|
603
|
+
lines.push(`${variable.varType} ${variable.name} = \`${variable.value}\``);
|
|
604
|
+
});
|
|
605
|
+
if (program.variables.length > 0) lines.push("");
|
|
606
|
+
program.pipelines.forEach((pipeline) => {
|
|
607
|
+
lines.push(`pipeline ${pipeline.name} =`);
|
|
608
|
+
pipeline.pipeline.steps.forEach((step) => {
|
|
609
|
+
lines.push(formatPipelineStep(step));
|
|
610
|
+
});
|
|
611
|
+
lines.push("");
|
|
612
|
+
});
|
|
613
|
+
program.routes.forEach((route) => {
|
|
614
|
+
lines.push(`${route.method} ${route.path}`);
|
|
615
|
+
const pipelineLines = formatPipelineRef(route.pipeline);
|
|
616
|
+
pipelineLines.forEach((line) => lines.push(line));
|
|
617
|
+
lines.push("");
|
|
618
|
+
});
|
|
619
|
+
program.describes.forEach((describe) => {
|
|
620
|
+
lines.push(`describe "${describe.name}"`);
|
|
621
|
+
describe.mocks.forEach((mock) => {
|
|
622
|
+
lines.push(` with mock ${mock.target} returning \`${mock.returnValue}\``);
|
|
623
|
+
});
|
|
624
|
+
lines.push("");
|
|
625
|
+
describe.tests.forEach((test) => {
|
|
626
|
+
lines.push(` it "${test.name}"`);
|
|
627
|
+
test.mocks.forEach((mock) => {
|
|
628
|
+
lines.push(` with mock ${mock.target} returning \`${mock.returnValue}\``);
|
|
629
|
+
});
|
|
630
|
+
lines.push(` when ${formatWhen(test.when)}`);
|
|
631
|
+
if (test.input) {
|
|
632
|
+
lines.push(` with input \`${test.input}\``);
|
|
633
|
+
}
|
|
634
|
+
test.conditions.forEach((condition) => {
|
|
635
|
+
const condType = condition.conditionType.toLowerCase();
|
|
636
|
+
const jqPart = condition.jqExpr ? ` \`${condition.jqExpr}\`` : "";
|
|
637
|
+
lines.push(` ${condType} ${condition.field}${jqPart} ${condition.comparison} ${condition.value}`);
|
|
638
|
+
});
|
|
639
|
+
lines.push("");
|
|
640
|
+
});
|
|
641
|
+
});
|
|
642
|
+
return lines.join("\n").trim();
|
|
643
|
+
}
|
|
644
|
+
function formatConfigValue(value) {
|
|
645
|
+
switch (value.kind) {
|
|
646
|
+
case "String":
|
|
647
|
+
return `"${value.value}"`;
|
|
648
|
+
case "EnvVar":
|
|
649
|
+
return value.default ? `$${value.var} || "${value.default}"` : `$${value.var}`;
|
|
650
|
+
case "Boolean":
|
|
651
|
+
return value.value.toString();
|
|
652
|
+
case "Number":
|
|
653
|
+
return value.value.toString();
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
function formatPipelineStep(step, indent = " ") {
|
|
657
|
+
if (step.kind === "Regular") {
|
|
658
|
+
return `${indent}|> ${step.name}: ${formatStepConfig(step.config)}`;
|
|
659
|
+
} else {
|
|
660
|
+
const lines = [`${indent}|> result`];
|
|
661
|
+
step.branches.forEach((branch) => {
|
|
662
|
+
const branchName = branch.branchType.kind === "Ok" ? "ok" : branch.branchType.kind === "Default" ? "default" : branch.branchType.name;
|
|
663
|
+
lines.push(`${indent} ${branchName}(${branch.statusCode}):`);
|
|
664
|
+
branch.pipeline.steps.forEach((branchStep) => {
|
|
665
|
+
lines.push(formatPipelineStep(branchStep, indent + " "));
|
|
666
|
+
});
|
|
667
|
+
});
|
|
668
|
+
return lines.join("\n");
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
function formatStepConfig(config) {
|
|
672
|
+
if (config.includes("`")) {
|
|
673
|
+
return `\`${config}\``;
|
|
674
|
+
} else if (config.includes(" ") || config.includes("\n")) {
|
|
675
|
+
return `"${config}"`;
|
|
676
|
+
} else {
|
|
677
|
+
return config;
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
function formatPipelineRef(ref) {
|
|
681
|
+
if (ref.kind === "Named") {
|
|
682
|
+
return [` |> pipeline: ${ref.name}`];
|
|
683
|
+
} else {
|
|
684
|
+
const lines = [];
|
|
685
|
+
ref.pipeline.steps.forEach((step) => {
|
|
686
|
+
lines.push(formatPipelineStep(step));
|
|
687
|
+
});
|
|
688
|
+
return lines;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
function formatWhen(when) {
|
|
692
|
+
switch (when.kind) {
|
|
693
|
+
case "CallingRoute":
|
|
694
|
+
return `calling ${when.method} ${when.path}`;
|
|
695
|
+
case "ExecutingPipeline":
|
|
696
|
+
return `executing pipeline ${when.name}`;
|
|
697
|
+
case "ExecutingVariable":
|
|
698
|
+
return `executing variable ${when.varType} ${when.name}`;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
if (import_meta.url === `file://${process.argv[1]}`) {
|
|
702
|
+
(async () => {
|
|
703
|
+
const fs = await import("fs/promises");
|
|
704
|
+
const path = process.argv[2];
|
|
705
|
+
if (!path) {
|
|
706
|
+
console.error("Usage: node dist/index.mjs <file.wp>");
|
|
707
|
+
process.exit(1);
|
|
708
|
+
}
|
|
709
|
+
const src = await fs.readFile(path, "utf8");
|
|
710
|
+
const program = parseProgram(src);
|
|
711
|
+
console.log(JSON.stringify(program, null, 2));
|
|
712
|
+
})();
|
|
713
|
+
}
|
|
714
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
715
|
+
0 && (module.exports = {
|
|
716
|
+
getPipelineRanges,
|
|
717
|
+
getVariableRanges,
|
|
718
|
+
parseProgram,
|
|
719
|
+
parseProgramWithDiagnostics,
|
|
720
|
+
prettyPrint
|
|
721
|
+
});
|