@dforge-core/metadata 0.0.22 → 0.0.23
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 +134 -0
- package/README.md +45 -0
- package/dist/actions-DvYTrCbP.d.ts +60 -0
- package/dist/dsl/index.d.ts +223 -0
- package/dist/dsl/index.js +1108 -0
- package/dist/dsl/index.js.map +1 -0
- package/dist/index.d.ts +3 -60
- package/package.json +10 -4
- package/schemas/jobs.schema.json +2 -2
- package/src/dsl/builtins.ts +210 -0
- package/src/dsl/check.ts +450 -0
- package/src/dsl/index.ts +36 -0
- package/src/dsl/lexer.ts +364 -0
- package/src/dsl/parse.ts +639 -0
- package/src/dsl/types.ts +90 -0
package/src/dsl/check.ts
ADDED
|
@@ -0,0 +1,450 @@
|
|
|
1
|
+
// Static checks for an action DSL body (`logic/actions/*.dsl`).
|
|
2
|
+
//
|
|
3
|
+
// Every rule here mirrors something the platform only tells you at pack or
|
|
4
|
+
// install time — a slow, tenant-bound round trip. The point is to move that
|
|
5
|
+
// feedback to where the script is written, and to the validator that runs
|
|
6
|
+
// before a pack, from one implementation rather than two.
|
|
7
|
+
//
|
|
8
|
+
// The rules that need to know about the module take it through `DslContext`,
|
|
9
|
+
// which is small on purpose: a module code, the action, and the columns of the
|
|
10
|
+
// entity behind the record context. Anything a host can't resolve it simply
|
|
11
|
+
// omits, and the rules that depend on it stand down. See ./types.
|
|
12
|
+
|
|
13
|
+
import { BUILTIN_BY_NAME } from "./builtins";
|
|
14
|
+
import { CONTROL_KEYWORDS, type Token } from "./lexer";
|
|
15
|
+
import { type DslDocument, type Span, blockLabelAt, parseDsl } from "./parse";
|
|
16
|
+
import type { ColumnLookup, DslContext, DslIssue, DslSeverity } from "./types";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Check a DSL body. Returns [] for a clean script.
|
|
20
|
+
*
|
|
21
|
+
* Issues come back in source-feature order (field reads, params, execution
|
|
22
|
+
* mode, block spelling, built-ins, top-level returns, inline assignments)
|
|
23
|
+
* rather than sorted by position — a host that wants them in file order can
|
|
24
|
+
* sort on `start`.
|
|
25
|
+
*/
|
|
26
|
+
export function checkDsl(text: string, ctx: DslContext = {}): DslIssue[] {
|
|
27
|
+
const parsed = parseDsl(text);
|
|
28
|
+
const out: DslIssue[] = [];
|
|
29
|
+
const positionAt = lineIndexer(text);
|
|
30
|
+
|
|
31
|
+
const add = (
|
|
32
|
+
span: Span,
|
|
33
|
+
message: string,
|
|
34
|
+
severity: DslSeverity,
|
|
35
|
+
rule: string,
|
|
36
|
+
) => {
|
|
37
|
+
const { line, column } = positionAt(span.start);
|
|
38
|
+
out.push({
|
|
39
|
+
rule,
|
|
40
|
+
severity,
|
|
41
|
+
message,
|
|
42
|
+
start: span.start,
|
|
43
|
+
end: span.end,
|
|
44
|
+
line,
|
|
45
|
+
column,
|
|
46
|
+
});
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const action = ctx.action;
|
|
50
|
+
const entity = ctx.currentEntity ?? null;
|
|
51
|
+
const isBatch = (action?.executionMode ?? "single") === "batch";
|
|
52
|
+
const hasColumn = columnMatcher(entity?.columns);
|
|
53
|
+
|
|
54
|
+
// ── record fields ────────────────────────────────────────────────
|
|
55
|
+
for (const ref of parsed.fieldRefs) {
|
|
56
|
+
// Neither block is rewritten by TransformLine, so brackets in them are
|
|
57
|
+
// data rather than field reads: `schema:` is embedded as a JS object
|
|
58
|
+
// literal, and `params:` is read by RxParamDecl — whose trailing
|
|
59
|
+
// key=value pairs take a bracketed option list, `options=[cash]`.
|
|
60
|
+
if (ref.block === "schema" || ref.block === "params") continue;
|
|
61
|
+
|
|
62
|
+
// Single-hop is the DSL's own rewrite (`RxRefNav`), so the rule belongs
|
|
63
|
+
// to the blocks that go through it. canExecute: reaches the formula
|
|
64
|
+
// engine verbatim, and a formula walks a chain of any depth —
|
|
65
|
+
// `[vehicle].[depot].[city_name]` resolves there and is tested.
|
|
66
|
+
const isDslBlock = ref.block === "execute" || ref.block === "onBeforeStart";
|
|
67
|
+
// The compiler rewrites the first hop and then fails on a surviving
|
|
68
|
+
// `.[` — so it is the bracket, not the hop count, that marks a second
|
|
69
|
+
// reference. `[vehicle].plate.length` compiles to
|
|
70
|
+
// `__ref_vehicle.get('plate').length`: one hop, then JavaScript.
|
|
71
|
+
const secondRef = ref.navigation.findIndex((h, idx) => idx > 0 && h.bracketed);
|
|
72
|
+
if (isDslBlock && secondRef > 0) {
|
|
73
|
+
const first = ref.navigation[secondRef]!;
|
|
74
|
+
const last = ref.navigation[ref.navigation.length - 1]!;
|
|
75
|
+
add(
|
|
76
|
+
{ start: first.span.start, end: last.span.end },
|
|
77
|
+
`Ref navigation is single-hop in the DSL. Use getRecord() or select() to reach past "${ref.navigation[0]!.name}".`,
|
|
78
|
+
"error",
|
|
79
|
+
"dsl/multi-hop-nav",
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// Batch execute: applies no `[field]` rewrite at all, so `[true]` stays
|
|
84
|
+
// the literal it is — and a keyword is never a column name anyway.
|
|
85
|
+
if (isBatch && ref.block === "execute" && LITERAL_NAMES.has(ref.name))
|
|
86
|
+
continue;
|
|
87
|
+
|
|
88
|
+
if (!entity) continue;
|
|
89
|
+
if (entity.columns.size === 0) continue;
|
|
90
|
+
if (hasColumn(ref.name)) continue;
|
|
91
|
+
// `var ids = [orderId]` is an array literal, not a field read. Only the
|
|
92
|
+
// name tells the two apart, so a name bound in this script stands down —
|
|
93
|
+
// the same fail-open the entity checks above use.
|
|
94
|
+
//
|
|
95
|
+
// Only the bare bracket is ambiguous. `r[stauts]` and
|
|
96
|
+
// `records[0][stauts]` name a column outright — the compiler rewrites
|
|
97
|
+
// each to `.get('stauts')` with the literal name — so a same-named
|
|
98
|
+
// local says nothing about them.
|
|
99
|
+
if (ref.isCurrentRecord && parsed.locals.has(ref.name)) continue;
|
|
100
|
+
|
|
101
|
+
add(
|
|
102
|
+
ref.span,
|
|
103
|
+
`"${ref.name}" is not a column on ${entity.qualified}. [field] names are checked at pack time — this is a compile error, not an empty read.`,
|
|
104
|
+
"error",
|
|
105
|
+
"dsl/unknown-column",
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// `old[field]` names columns of the same entity — RxOldAccess feeds them
|
|
110
|
+
// into the very same check — so a typo there is a compile error too.
|
|
111
|
+
if (entity && entity.columns.size > 0) {
|
|
112
|
+
for (const ref of parsed.globalRefs) {
|
|
113
|
+
if (ref.global !== "old") continue;
|
|
114
|
+
if (ref.block !== "execute" && ref.block !== "onBeforeStart") continue;
|
|
115
|
+
if (hasColumn(ref.property)) continue;
|
|
116
|
+
add(
|
|
117
|
+
ref.span,
|
|
118
|
+
`"${ref.property}" is not a column on ${entity.qualified}. old[field] reads the pre-change value of a column, and the name is checked at pack time.`,
|
|
119
|
+
"error",
|
|
120
|
+
"dsl/unknown-column",
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// ── params ───────────────────────────────────────────────────────
|
|
126
|
+
// `ValidateParamUsages` compares with OrdinalIgnoreCase, so `params[Qty]`
|
|
127
|
+
// against a declared `qty` compiles — flagging it would be a false error.
|
|
128
|
+
const declared = new Set(parsed.params.map((p) => p.name.toLowerCase()));
|
|
129
|
+
const hasParamsBlock = parsed.blocks.some((b) => b.kind === "params");
|
|
130
|
+
for (const ref of parsed.globalRefs) {
|
|
131
|
+
if (ref.global !== "params") continue;
|
|
132
|
+
if (!hasParamsBlock || declared.has(ref.property.toLowerCase())) continue;
|
|
133
|
+
add(
|
|
134
|
+
ref.span,
|
|
135
|
+
`"${ref.property}" is not declared in the params: block.`,
|
|
136
|
+
"error",
|
|
137
|
+
"dsl/unknown-param",
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// ── execution mode ───────────────────────────────────────────────
|
|
142
|
+
if (action) {
|
|
143
|
+
const mode = action.executionMode ?? "single";
|
|
144
|
+
// `recordsRefs` covers both `records[x]` and the bare `for x in records`,
|
|
145
|
+
// and comes from the lexer, so the word inside a string or a comment
|
|
146
|
+
// does not count — nor does a script that binds its own `records`,
|
|
147
|
+
// which shadows the batch global and says nothing about the mode.
|
|
148
|
+
const recordsSpan = parsed.locals.has("records")
|
|
149
|
+
? undefined
|
|
150
|
+
: parsed.recordsRefs[0];
|
|
151
|
+
// execute: alone. `onBeforeStart:` is compiled by CompilePerRecordBlock
|
|
152
|
+
// whatever the mode — "always per-record (no batch form)" — so a bare
|
|
153
|
+
// field is exactly how it is meant to be written there.
|
|
154
|
+
//
|
|
155
|
+
// Batch mode never applies the `[field]` rewrite at all, which is why
|
|
156
|
+
// `var ids = [id]` survives as the array literal it looks like. Same
|
|
157
|
+
// disambiguation as the column rule: a name bound in this script is a
|
|
158
|
+
// local, not a column.
|
|
159
|
+
const bareFields = parsed.fieldRefs.filter(
|
|
160
|
+
(r) =>
|
|
161
|
+
r.block === "execute" &&
|
|
162
|
+
r.isCurrentRecord &&
|
|
163
|
+
!parsed.locals.has(r.name) &&
|
|
164
|
+
!LITERAL_NAMES.has(r.name),
|
|
165
|
+
);
|
|
166
|
+
|
|
167
|
+
if (mode === "batch") {
|
|
168
|
+
// Every one of them: an editor underlines what it is told to, and
|
|
169
|
+
// reporting only the first hides the rest behind a re-check.
|
|
170
|
+
for (const field of bareFields) {
|
|
171
|
+
add(
|
|
172
|
+
field.outerSpan,
|
|
173
|
+
`Action "${action.code}" runs in batch mode, which has no current record. Iterate instead: for x in records { x[${field.name}] }.`,
|
|
174
|
+
"error",
|
|
175
|
+
"dsl/batch-bare-field",
|
|
176
|
+
);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (mode !== "batch" && recordsSpan) {
|
|
180
|
+
add(
|
|
181
|
+
recordsSpan,
|
|
182
|
+
`"records" only exists in batch mode; action "${action.code}" runs in "${mode}" mode.`,
|
|
183
|
+
"warning",
|
|
184
|
+
"dsl/records-outside-batch",
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// ── block-specific spelling ──────────────────────────────────────
|
|
190
|
+
// canExecute is parsed by the formula engine, execute by the JavaScript
|
|
191
|
+
// compiler. They share more vocabulary than the docs suggest — NULL and
|
|
192
|
+
// lowercase booleans work in both, and shipped modules rely on that — so
|
|
193
|
+
// only the operators that genuinely have no formula spelling are flagged.
|
|
194
|
+
for (const token of parsed.tokens) {
|
|
195
|
+
if (blockOf(parsed, token.start) !== "canExecute") continue;
|
|
196
|
+
if (token.text !== "&&" && token.text !== "||") continue;
|
|
197
|
+
add(
|
|
198
|
+
{ start: token.start, end: token.end },
|
|
199
|
+
`canExecute is a formula, not JavaScript — write ${token.text === "&&" ? "AND" : "OR"}.`,
|
|
200
|
+
"warning",
|
|
201
|
+
"dsl/js-in-formula",
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// ── built-in misuse ──────────────────────────────────────────────
|
|
206
|
+
for (const call of parsed.calls) {
|
|
207
|
+
if (call.name === "userId") {
|
|
208
|
+
add(
|
|
209
|
+
call.nameSpan,
|
|
210
|
+
"userId is a bare identifier — userId() is a compile error.",
|
|
211
|
+
"error",
|
|
212
|
+
"dsl/user-id-call",
|
|
213
|
+
);
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const builtin = BUILTIN_BY_NAME.get(call.name);
|
|
218
|
+
if (!builtin || builtin.entityArg === undefined) continue;
|
|
219
|
+
const arg = call.firstStringArg;
|
|
220
|
+
if (!arg) continue;
|
|
221
|
+
|
|
222
|
+
// A hyphenated module code can't be schema-qualified, so the rule is
|
|
223
|
+
// skipped there rather than reported as unfixable.
|
|
224
|
+
if (arg.value.includes(".") || ctx.moduleCode?.includes("-")) continue;
|
|
225
|
+
add(
|
|
226
|
+
arg.span,
|
|
227
|
+
`Qualify the entity code as "${ctx.moduleCode ? `${ctx.moduleCode}.` : "<module>."}${arg.value}" — entity_cd is only unique per module, so a bare code can resolve to another module's entity.`,
|
|
228
|
+
"info",
|
|
229
|
+
"dsl/unqualified-entity",
|
|
230
|
+
);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// ── indented headers, and returns outside a function ─────────────
|
|
234
|
+
// An indented `params:` inside an object literal is an ordinary key, which
|
|
235
|
+
// is the whole reason the compiler anchors headers at column 0 rather than
|
|
236
|
+
// trusting the line — so that rule reads brace depth.
|
|
237
|
+
//
|
|
238
|
+
// `return` reads function scope instead. Both blocks compile to a bare
|
|
239
|
+
// script, and Esprima's ParseScript rejects a return outside a function
|
|
240
|
+
// wherever it sits: `if (x) { return }` and `if (x) return` fail exactly as
|
|
241
|
+
// a return on its own line does.
|
|
242
|
+
let depth = 0;
|
|
243
|
+
const functionDepths: number[] = [];
|
|
244
|
+
// Index of the `(` that the most recent `)` closed. Tracked with a stack
|
|
245
|
+
// because the nearest open paren is not the matching one: in `if (now()) {`
|
|
246
|
+
// it is `now`'s, and in `function f(x = (1)) {` it is the default value's.
|
|
247
|
+
const parenStack: number[] = [];
|
|
248
|
+
let matchingParenOpen = -1;
|
|
249
|
+
for (let i = 0; i < parsed.tokens.length; i++) {
|
|
250
|
+
const token = parsed.tokens[i]!;
|
|
251
|
+
|
|
252
|
+
if (token.text === "(") parenStack.push(i);
|
|
253
|
+
else if (token.text === ")") matchingParenOpen = parenStack.pop() ?? -1;
|
|
254
|
+
|
|
255
|
+
if (token.text === "{") {
|
|
256
|
+
depth++;
|
|
257
|
+
if (opensFunctionBody(parsed.tokens, i, matchingParenOpen))
|
|
258
|
+
functionDepths.push(depth);
|
|
259
|
+
} else if (token.text === "}") {
|
|
260
|
+
if (functionDepths[functionDepths.length - 1] === depth)
|
|
261
|
+
functionDepths.pop();
|
|
262
|
+
depth = Math.max(0, depth - 1);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (depth === 0 && token.character > 0) {
|
|
266
|
+
const kind = blockLabelAt(parsed.tokens, i);
|
|
267
|
+
// `execute: number` inside params: declares a param called execute.
|
|
268
|
+
// RxParamDecl takes any `\w+`, so a block name is a legal param
|
|
269
|
+
// name — and a declaration the parser already read is not a header.
|
|
270
|
+
const isParamDecl = parsed.params.some(
|
|
271
|
+
(p) => p.nameSpan.start === token.start,
|
|
272
|
+
);
|
|
273
|
+
if (kind && !isParamDecl) {
|
|
274
|
+
add(
|
|
275
|
+
{ start: token.start, end: parsed.tokens[i + 1]!.end },
|
|
276
|
+
`Block headers start at column 0. Indented, "${kind}:" is body text — the compiler never opens the block, and it runs as if empty.`,
|
|
277
|
+
"error",
|
|
278
|
+
"dsl/indented-block-header",
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (token.kind !== "ident" || token.text !== "return") continue;
|
|
284
|
+
// `return` is a reserved word, so it is a statement unless it is being
|
|
285
|
+
// used as a name: `result.return` reads a property, `{ return: 1 }`
|
|
286
|
+
// declares a key and `{ return() { … } }` declares a method. All three
|
|
287
|
+
// are legal JavaScript.
|
|
288
|
+
if (parsed.tokens[i - 1]?.text === ".") continue;
|
|
289
|
+
if (parsed.tokens[i + 1]?.text === ":") continue;
|
|
290
|
+
if (isMethodName(parsed.tokens, i)) continue;
|
|
291
|
+
if (functionDepths.length > 0) continue;
|
|
292
|
+
const block = blockOf(parsed, token.start);
|
|
293
|
+
if (block !== "execute" && block !== "onBeforeStart") continue;
|
|
294
|
+
add(
|
|
295
|
+
{ start: token.start, end: token.end },
|
|
296
|
+
`A return outside a function is a syntax error — ${block}: compiles to a bare script. Use exit(message, level) to stop early and keep the changes made so far.`,
|
|
297
|
+
"error",
|
|
298
|
+
"dsl/top-level-return",
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
out.push(...inlineAssignmentIssues(parsed, positionAt));
|
|
303
|
+
return out;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Column matching is case-insensitive at the other end — `EntityColumnLookup`
|
|
308
|
+
* hands the compiler an OrdinalIgnoreCase set — but a host passes us whatever
|
|
309
|
+
* `Set` or `Map` it already holds, and a mixed-case column name is legal.
|
|
310
|
+
*
|
|
311
|
+
* So fold both sides: the names once per check when the lookup can enumerate
|
|
312
|
+
* them (`Set` and `Map` both can), and the reference on the way in. A lookup
|
|
313
|
+
* that can do neither falls back to trying the reference in lower case, which
|
|
314
|
+
* is all that is left to try.
|
|
315
|
+
*/
|
|
316
|
+
function columnMatcher(columns?: ColumnLookup): (name: string) => boolean {
|
|
317
|
+
if (!columns) return () => true;
|
|
318
|
+
if (typeof columns.keys !== "function")
|
|
319
|
+
return (name) => columns.has(name) || columns.has(name.toLowerCase());
|
|
320
|
+
|
|
321
|
+
const folded = new Set<string>();
|
|
322
|
+
for (const key of columns.keys()) folded.add(key.toLowerCase());
|
|
323
|
+
return (name) => folded.has(name.toLowerCase());
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Whether the name at `i` heads a method shorthand — `return() { … }`, whose
|
|
328
|
+
* parameter list is followed by a body. `return (x + 1)` is a statement with a
|
|
329
|
+
* parenthesized expression and has no brace after the closing paren, so the
|
|
330
|
+
* two are told apart by what follows the parens rather than by what opens them.
|
|
331
|
+
*/
|
|
332
|
+
function isMethodName(tokens: Token[], i: number): boolean {
|
|
333
|
+
if (tokens[i + 1]?.text !== "(") return false;
|
|
334
|
+
let depth = 0;
|
|
335
|
+
for (let j = i + 1; j < tokens.length; j++) {
|
|
336
|
+
const text = tokens[j]!.text;
|
|
337
|
+
if (text === "(") depth++;
|
|
338
|
+
else if (text === ")") {
|
|
339
|
+
depth--;
|
|
340
|
+
if (depth === 0) return tokens[j + 1]?.text === "{";
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Whether the `{` at `i` opens a function body — where a `return` is legal.
|
|
348
|
+
*
|
|
349
|
+
* `=> {` is one. So is `) {` when the name in front of the matching `(` is not
|
|
350
|
+
* a control keyword: that covers `function (x) {`, `function f(x) {` and the
|
|
351
|
+
* `f() {` method shorthand, while leaving `if (x) {` and `for (…) {` alone.
|
|
352
|
+
*/
|
|
353
|
+
function opensFunctionBody(
|
|
354
|
+
tokens: Token[],
|
|
355
|
+
i: number,
|
|
356
|
+
matchingParenOpen: number,
|
|
357
|
+
): boolean {
|
|
358
|
+
const prev = tokens[i - 1];
|
|
359
|
+
if (prev?.text === "=>") return true;
|
|
360
|
+
if (prev?.text !== ")" || matchingParenOpen < 0) return false;
|
|
361
|
+
const head = tokens[matchingParenOpen - 1];
|
|
362
|
+
return head?.kind === "ident" && !CONTROL_KEYWORDS.has(head.text);
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Keyword literals, which `[…]` can only be holding as array elements. Both
|
|
367
|
+
* spellings: the DSL takes SQL-style `NULL`/`TRUE`/`FALSE` as well as the
|
|
368
|
+
* JavaScript ones, and rewrites them to the latter.
|
|
369
|
+
*/
|
|
370
|
+
const LITERAL_NAMES = new Set([
|
|
371
|
+
"true",
|
|
372
|
+
"false",
|
|
373
|
+
"null",
|
|
374
|
+
"TRUE",
|
|
375
|
+
"FALSE",
|
|
376
|
+
"NULL",
|
|
377
|
+
]);
|
|
378
|
+
|
|
379
|
+
function blockOf(parsed: DslDocument, offset: number) {
|
|
380
|
+
return (
|
|
381
|
+
parsed.blocks.find((b) => offset >= b.body.start && offset < b.body.end)
|
|
382
|
+
?.kind ?? null
|
|
383
|
+
);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* `if (…) { [field] = x }` on a single line mis-compiles — the assignment is
|
|
388
|
+
* rewritten in place and the setter call is lost. One assignment per line.
|
|
389
|
+
*/
|
|
390
|
+
function inlineAssignmentIssues(
|
|
391
|
+
parsed: DslDocument,
|
|
392
|
+
positionAt: (offset: number) => { line: number; column: number },
|
|
393
|
+
): DslIssue[] {
|
|
394
|
+
const out: DslIssue[] = [];
|
|
395
|
+
for (const ref of parsed.fieldRefs) {
|
|
396
|
+
// `ref.endIndex` is where the parser stopped: a `.prop` hop is 2 tokens
|
|
397
|
+
// but a `.[prop]` hop is 4, so the count cannot be derived here.
|
|
398
|
+
const assign = parsed.tokens[ref.endIndex + 1];
|
|
399
|
+
if (!assign || assign.kind !== "punct" || assign.text !== "=") continue;
|
|
400
|
+
|
|
401
|
+
// Walk back over this line only, looking for a brace that opened here.
|
|
402
|
+
const line = parsed.tokens[ref.startIndex]!.line;
|
|
403
|
+
let openedHere = false;
|
|
404
|
+
for (let i = ref.startIndex - 1; i >= 0; i--) {
|
|
405
|
+
const t = parsed.tokens[i]!;
|
|
406
|
+
if (t.line !== line) break;
|
|
407
|
+
if (t.text === "{") {
|
|
408
|
+
openedHere = true;
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
if (!openedHere) continue;
|
|
413
|
+
|
|
414
|
+
const pos = positionAt(ref.outerSpan.start);
|
|
415
|
+
out.push({
|
|
416
|
+
rule: "dsl/inline-assignment",
|
|
417
|
+
severity: "warning",
|
|
418
|
+
message:
|
|
419
|
+
"An assignment to a record field on the same line as the brace that opens its block mis-compiles. Put the assignment on its own line.",
|
|
420
|
+
start: ref.outerSpan.start,
|
|
421
|
+
end: ref.outerSpan.end,
|
|
422
|
+
line: pos.line,
|
|
423
|
+
column: pos.column,
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
return out;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/**
|
|
430
|
+
* Offset → 1-indexed line/column, over a line-start table built once per check.
|
|
431
|
+
* Scanning the prefix per issue is O(n) each, and a body with many issues is
|
|
432
|
+
* exactly the one where that shows.
|
|
433
|
+
*/
|
|
434
|
+
function lineIndexer(text: string): (offset: number) => { line: number; column: number } {
|
|
435
|
+
const starts = [0];
|
|
436
|
+
for (let i = 0; i < text.length; i++) {
|
|
437
|
+
if (text[i] === "\n") starts.push(i + 1);
|
|
438
|
+
}
|
|
439
|
+
return (offset: number) => {
|
|
440
|
+
// Binary search for the last line start at or before `offset`.
|
|
441
|
+
let lo = 0;
|
|
442
|
+
let hi = starts.length - 1;
|
|
443
|
+
while (lo < hi) {
|
|
444
|
+
const mid = (lo + hi + 1) >> 1;
|
|
445
|
+
if (starts[mid]! <= offset) lo = mid;
|
|
446
|
+
else hi = mid - 1;
|
|
447
|
+
}
|
|
448
|
+
return { line: lo + 1, column: offset - starts[lo]! + 1 };
|
|
449
|
+
};
|
|
450
|
+
}
|
package/src/dsl/index.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// @dforge-core/metadata/dsl — the action-DSL analyzer.
|
|
2
|
+
//
|
|
3
|
+
// A separate entry point, not part of the root barrel: this is a lexer, a
|
|
4
|
+
// parser and a rule engine, while the root export is registries and types that
|
|
5
|
+
// the web app and the VS Code webview pull into a browser bundle. Importing
|
|
6
|
+
// `@dforge-core/metadata` must not drag a tokenizer in behind it.
|
|
7
|
+
//
|
|
8
|
+
// Consumers: the language server (diagnostics, completion, hover,
|
|
9
|
+
// go-to-definition) and the module validator that runs before a pack.
|
|
10
|
+
|
|
11
|
+
export { tokenize, stringValue } from "./lexer";
|
|
12
|
+
export type { Token, TokenKind } from "./lexer";
|
|
13
|
+
|
|
14
|
+
export { parseDsl, BLOCK_KINDS } from "./parse";
|
|
15
|
+
export type {
|
|
16
|
+
BlockKind,
|
|
17
|
+
CallRef,
|
|
18
|
+
DslBlock,
|
|
19
|
+
DslDocument,
|
|
20
|
+
DslParam,
|
|
21
|
+
FieldRef,
|
|
22
|
+
GlobalRef,
|
|
23
|
+
Span,
|
|
24
|
+
} from "./parse";
|
|
25
|
+
|
|
26
|
+
export { BUILTINS, BUILTIN_BY_NAME, BUILTIN_VALUES } from "./builtins";
|
|
27
|
+
export type { Builtin } from "./builtins";
|
|
28
|
+
|
|
29
|
+
export { checkDsl } from "./check";
|
|
30
|
+
export type {
|
|
31
|
+
ColumnLookup,
|
|
32
|
+
DslContext,
|
|
33
|
+
DslIssue,
|
|
34
|
+
DslSeverity,
|
|
35
|
+
EntityShape,
|
|
36
|
+
} from "./types";
|