@jigs.design/compiler 0.1.1 → 0.2.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/README.md +4 -4
- package/dist/ast.d.ts +4 -4
- package/dist/block-hoist.d.ts +2 -0
- package/dist/check-code.d.ts +4 -2
- package/dist/check-code.js +10 -10
- package/dist/codegen.d.ts +2 -0
- package/dist/codegen.js +28 -28
- package/dist/compile.d.ts +17 -11
- package/dist/compile.js +131 -111
- package/dist/hoist.js +2 -2
- package/dist/index.d.ts +5 -5
- package/dist/index.js +4 -4
- package/dist/offsets.d.ts +9 -0
- package/dist/offsets.js +14 -1
- package/dist/parse.d.ts +3 -3
- package/dist/parse.js +5 -5
- package/dist/patch.d.ts +1 -1
- package/dist/patch.js +1 -1
- package/dist/script.js +5 -5
- package/dist/signature.js +2 -2
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# @jigs.design/compiler
|
|
2
2
|
|
|
3
|
-
Today's
|
|
3
|
+
Today's Jigs compiler: the parser, the checker against a catalog, the code generator, recipe libraries, and the SEARCH/REPLACE edit helpers. `@jigs.design/server` and `@jigs.design/prompt` use it. You import it for tooling, not to run a session.
|
|
4
4
|
|
|
5
|
-
It parses embedded code with [acorn](https://github.com/acornjs/acorn) in strict mode, the mode programs run in, and checks names and the JavaScript subset. It has no built-in catalog. Every caller passes one, and its prop kinds are boolean, enum, handler, number, rows and string.
|
|
5
|
+
Its diagnostics are the `Diagnostic` type from `@jigs.design/protocol`, the shape the server sends; it imports that package's types and nothing else from it. It parses embedded code with [acorn](https://github.com/acornjs/acorn) in strict mode, the mode programs run in, and checks names and the JavaScript subset. It has no built-in catalog. Every caller passes one, and its prop kinds are boolean, enum, handler, number, rows and string.
|
|
6
6
|
|
|
7
7
|
The [language RFC](../../docs/language-v2/rfc.md) plans its next changes. It evolves in place, behind the same server API.
|
|
8
8
|
|
|
@@ -10,8 +10,8 @@ The [language RFC](../../docs/language-v2/rfc.md) plans its next changes. It evo
|
|
|
10
10
|
|
|
11
11
|
| Export | What it is |
|
|
12
12
|
| ---------------------------------------------------------- | ---------------------------------------------------------------- |
|
|
13
|
-
| `
|
|
14
|
-
| `
|
|
13
|
+
| `parseJigs` | Source to a document tree. Tolerates a prefix cut off mid-stream |
|
|
14
|
+
| `compileJigs` | Checks a document against a catalog and generates its program |
|
|
15
15
|
| `compileLibrary` | Compiles recipes into a library a document can use |
|
|
16
16
|
| `recipeSignature` | A recipe's props and slot, as the prompt lists them |
|
|
17
17
|
| `isEditReply`, `parseEdits`, `applyEdits`, `SEARCH_MARKER` | Recognise and apply a reply made of SEARCH/REPLACE edits |
|
package/dist/ast.d.ts
CHANGED
|
@@ -28,7 +28,7 @@ export interface PropNode {
|
|
|
28
28
|
}
|
|
29
29
|
/** `start` and `end` cover the opening tag, or the Markdown that made the element. */
|
|
30
30
|
export interface ElementNode extends Range {
|
|
31
|
-
children: Array<
|
|
31
|
+
children: Array<JigsNode>;
|
|
32
32
|
/** Just past its closing tag, or its own end when self-closing; absent when the parser closed it. */
|
|
33
33
|
closeEnd?: number;
|
|
34
34
|
line: number;
|
|
@@ -59,7 +59,7 @@ export interface Statement extends Range {
|
|
|
59
59
|
interface StatementNode extends Statement {
|
|
60
60
|
type: "statement";
|
|
61
61
|
}
|
|
62
|
-
export type
|
|
62
|
+
export type JigsNode = ElementNode | ExprNode | StatementNode | TextNode;
|
|
63
63
|
/** Something the parser fixed or dropped so a partial document still renders. */
|
|
64
64
|
export interface Repair {
|
|
65
65
|
end?: number;
|
|
@@ -67,8 +67,8 @@ export interface Repair {
|
|
|
67
67
|
message: string;
|
|
68
68
|
start?: number;
|
|
69
69
|
}
|
|
70
|
-
export interface
|
|
71
|
-
children: Array<
|
|
70
|
+
export interface JigsDocument {
|
|
71
|
+
children: Array<JigsNode>;
|
|
72
72
|
/** Text that could not be read even though the input is complete there. */
|
|
73
73
|
errors: Array<Repair>;
|
|
74
74
|
repairs: Array<Repair>;
|
package/dist/check-code.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import
|
|
1
|
+
import "./ast.js";
|
|
2
|
+
import { Finding } from "./compile.js";
|
|
3
|
+
import "./block-hoist.js";
|
|
2
4
|
import { ProcedureCall } from "./script.js";
|
|
3
5
|
//#region src/check-code.d.ts
|
|
4
6
|
/** A procedure call in the document, by line, for the server to check against its host. */
|
|
@@ -10,7 +12,7 @@ export interface ProcedureUse extends Omit<ProcedureCall, "end" | "offset"> {
|
|
|
10
12
|
start?: number;
|
|
11
13
|
}
|
|
12
14
|
/** Where a diagnostic points: a line, and a range in the source when the code has one. */
|
|
13
|
-
type Place = Pick<
|
|
15
|
+
type Place = Pick<Finding, "end" | "line" | "start">;
|
|
14
16
|
/** A step of `DataRead.path` into each element of an array, as an `<each>` item takes. */
|
|
15
17
|
export declare const ELEMENT = "[]";
|
|
16
18
|
/**
|
package/dist/check-code.js
CHANGED
|
@@ -8,7 +8,7 @@ const ELEMENT = "[]";
|
|
|
8
8
|
/**
|
|
9
9
|
* Names that do not throw ReferenceError in the sandbox: the language's
|
|
10
10
|
* built-ins, the worker's own globals that `LOCKED_GLOBALS` in
|
|
11
|
-
* `@jigs.design/runtime/frame` leaves in place, and
|
|
11
|
+
* `@jigs.design/runtime/frame` leaves in place, and Jigs's bindings. Kept by
|
|
12
12
|
* hand, since the compiler runs on the server; `checks.test.ts` fails if a
|
|
13
13
|
* name here is one the worker locks, or exists in no JavaScript runtime.
|
|
14
14
|
* `Function`, `Proxy`, `Reflect`, `WeakRef`, `FinalizationRegistry` and
|
|
@@ -45,7 +45,7 @@ const KNOWN_GLOBALS = /* @__PURE__ */ new Set([
|
|
|
45
45
|
"NaN",
|
|
46
46
|
"Number",
|
|
47
47
|
"Object",
|
|
48
|
-
"
|
|
48
|
+
"Jigs",
|
|
49
49
|
"Promise",
|
|
50
50
|
"RangeError",
|
|
51
51
|
"ReferenceError",
|
|
@@ -66,9 +66,9 @@ const KNOWN_GLOBALS = /* @__PURE__ */ new Set([
|
|
|
66
66
|
"Uint8ClampedArray",
|
|
67
67
|
"WeakMap",
|
|
68
68
|
"WeakSet",
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
69
|
+
"__jigs",
|
|
70
|
+
"__jigsConstants",
|
|
71
|
+
"__jigsSafe",
|
|
72
72
|
"arguments",
|
|
73
73
|
"atob",
|
|
74
74
|
"btoa",
|
|
@@ -277,18 +277,18 @@ function readBody(statements, define, outer) {
|
|
|
277
277
|
* Checks every piece of code in the page and its defines, adding to
|
|
278
278
|
* `diagnostics`, and returns the procedure calls it saw, in source order.
|
|
279
279
|
*/
|
|
280
|
-
function checkCode(tree, defines, pageStatements,
|
|
280
|
+
function checkCode(tree, defines, pageStatements, findings, hoists, dropped = /* @__PURE__ */ new Set()) {
|
|
281
281
|
const procedures = [];
|
|
282
282
|
const reads = [];
|
|
283
283
|
const report = (place, code, message, fix) => {
|
|
284
|
-
const
|
|
284
|
+
const finding = {
|
|
285
285
|
code,
|
|
286
286
|
...place,
|
|
287
287
|
message,
|
|
288
|
-
severity: "
|
|
288
|
+
severity: "error"
|
|
289
289
|
};
|
|
290
|
-
if (fix !== void 0)
|
|
291
|
-
|
|
290
|
+
if (fix !== void 0) finding.fix = fix;
|
|
291
|
+
findings.push(finding);
|
|
292
292
|
};
|
|
293
293
|
const checkSnippet = (snippet, body, scope) => {
|
|
294
294
|
const facts = factsOf(snippet);
|
package/dist/codegen.d.ts
CHANGED
package/dist/codegen.js
CHANGED
|
@@ -2,7 +2,7 @@ import { analyze, isValidBinding } from "./script.js";
|
|
|
2
2
|
//#region src/codegen.ts
|
|
3
3
|
/**
|
|
4
4
|
* Adds `{ key }` to single-argument `useState(...)` and `useMutation(...)`
|
|
5
|
-
* calls, bare or with `
|
|
5
|
+
* calls, bare or with `Jigs.`, named after the declaration, so state and a
|
|
6
6
|
* mutation's status survive recompiles and reordering. The calls come from the
|
|
7
7
|
* parser, so text that only looks like one, in a string or a comment, is left
|
|
8
8
|
* alone.
|
|
@@ -32,12 +32,12 @@ function injectStateKeys(code, baseKey) {
|
|
|
32
32
|
}
|
|
33
33
|
/** Names a spec may not declare: the runtime's own bindings. */
|
|
34
34
|
const RESERVED = /* @__PURE__ */ new Set([
|
|
35
|
-
"
|
|
35
|
+
"Jigs",
|
|
36
36
|
"useMutation",
|
|
37
37
|
"useQuery",
|
|
38
38
|
"useState"
|
|
39
39
|
]);
|
|
40
|
-
const isReserved = (name) => RESERVED.has(name) || name.startsWith("
|
|
40
|
+
const isReserved = (name) => RESERVED.has(name) || name.startsWith("__jigs");
|
|
41
41
|
/** Whether `name` can be bound as a loop variable without hiding the runtime's own names. */
|
|
42
42
|
function isBindingName(name) {
|
|
43
43
|
return !isReserved(name) && isValidBinding(name);
|
|
@@ -47,30 +47,30 @@ function statementProblem(code) {
|
|
|
47
47
|
const analysis = analyze(code, "statement");
|
|
48
48
|
if (!analysis.ok) return analysis.message.includes("'return' outside of function") ? "`return` can't be used in a {@body} line: the page always renders. To hide markup, wrap it in <if test={…}>. The line was removed" : `{@body} line is not valid JavaScript (${analysis.message}); it was removed`;
|
|
49
49
|
const reserved = analysis.declared.find(isReserved);
|
|
50
|
-
return reserved === void 0 ? null : `{@body} line declares "${reserved}", a name
|
|
50
|
+
return reserved === void 0 ? null : `{@body} line declares "${reserved}", a name Jigs uses itself; rename it. The line was removed`;
|
|
51
51
|
}
|
|
52
52
|
/** Why `code` can't be spliced in as an expression, or null when it can. */
|
|
53
53
|
function expressionProblem(code) {
|
|
54
54
|
const analysis = analyze(code, "expression");
|
|
55
55
|
return analysis.ok ? null : analysis.message;
|
|
56
56
|
}
|
|
57
|
-
const fragment = (children) => `
|
|
58
|
-
"
|
|
57
|
+
const fragment = (children) => `__jigs.jsx(${[
|
|
58
|
+
"__jigs.Fragment",
|
|
59
59
|
"null",
|
|
60
60
|
...children
|
|
61
61
|
].join(", ")})`;
|
|
62
62
|
const documentLines = { line: String };
|
|
63
|
-
const libraryLines = { line: () => "
|
|
63
|
+
const libraryLines = { line: () => "__jigsAt" };
|
|
64
64
|
function generateProgram(statements, tree, defines = []) {
|
|
65
|
-
const
|
|
65
|
+
const findings = [];
|
|
66
66
|
const constants = {};
|
|
67
67
|
let nextConstant = 0;
|
|
68
68
|
const invalid = (code, line, message) => {
|
|
69
|
-
|
|
69
|
+
findings.push({
|
|
70
70
|
code,
|
|
71
71
|
line,
|
|
72
72
|
message,
|
|
73
|
-
severity: "
|
|
73
|
+
severity: "error"
|
|
74
74
|
});
|
|
75
75
|
};
|
|
76
76
|
/** Statement lines, run in order, plus a `let` for every name they declare. */
|
|
@@ -88,7 +88,7 @@ function generateProgram(statements, tree, defines = []) {
|
|
|
88
88
|
for (const name of names) declared.add(name);
|
|
89
89
|
const code = injectStateKeys(statement.code, names[0] ?? `statement:${statement.line}`);
|
|
90
90
|
const run = names.length === 0 ? `(() => {\n${code}\n})();` : `({ ${names.join(", ")} } = (() => {\n${code}\nreturn { ${names.join(", ")} };\n})());`;
|
|
91
|
-
body.push(`try {\n${run}\n} catch (error) {
|
|
91
|
+
body.push(`try {\n${run}\n} catch (error) { Jigs.reportError(${lines.line(statement.line)}, error); }`);
|
|
92
92
|
}
|
|
93
93
|
return declared.size > 0 ? [`let ${[...declared].join(", ")};`, ...body] : body;
|
|
94
94
|
};
|
|
@@ -123,7 +123,7 @@ function generateProgram(statements, tree, defines = []) {
|
|
|
123
123
|
const params = [node.item, ...node.index === null ? [] : [node.index]];
|
|
124
124
|
const iteration = [...genStatements(node.statements, lines), `return ${fragment(genChildren(node.children, lines))};`].join("\n");
|
|
125
125
|
const at = lines.line(node.line);
|
|
126
|
-
return `
|
|
126
|
+
return `__jigsSafe(() => __jigs.each((${node.items}\n), (${params.join(", ")}) => {\n${iteration}\n}, ${at}), null, ${at})`;
|
|
127
127
|
}
|
|
128
128
|
case "if": {
|
|
129
129
|
const problem = expressionProblem(node.test);
|
|
@@ -133,12 +133,12 @@ function generateProgram(statements, tree, defines = []) {
|
|
|
133
133
|
}
|
|
134
134
|
const shown = node.statements.length === 0 ? fragment(genChildren(node.children, lines)) : `(() => {\n${[...genStatements(node.statements, lines), `return ${fragment(genChildren(node.children, lines))};`].join("\n")}\n})()`;
|
|
135
135
|
const at = lines.line(node.line);
|
|
136
|
-
return `
|
|
136
|
+
return `__jigsSafe(() => (${node.test}\n) ? ${shown} : null, null, ${at})`;
|
|
137
137
|
}
|
|
138
138
|
case "text": {
|
|
139
139
|
const key = String(nextConstant++);
|
|
140
140
|
constants[key] = node.value;
|
|
141
|
-
return `
|
|
141
|
+
return `__jigsConstants[${JSON.stringify(key)}]`;
|
|
142
142
|
}
|
|
143
143
|
case "expr": {
|
|
144
144
|
const problem = expressionProblem(node.code);
|
|
@@ -146,19 +146,19 @@ function generateProgram(statements, tree, defines = []) {
|
|
|
146
146
|
invalid("invalid_expression", node.line, `{${node.code}} is not a valid expression (${problem}); it was removed`);
|
|
147
147
|
return null;
|
|
148
148
|
}
|
|
149
|
-
return `
|
|
149
|
+
return `__jigsSafe(() => (${node.code}\n), null, ${lines.line(node.line)})`;
|
|
150
150
|
}
|
|
151
151
|
case "element": {
|
|
152
152
|
const propEntries = genPropEntries(node.props, node.line, node.component);
|
|
153
153
|
const children = genChildren(node.children, lines);
|
|
154
|
-
const element = `
|
|
154
|
+
const element = `__jigs.jsx(${[
|
|
155
155
|
JSON.stringify(node.component),
|
|
156
156
|
`{ ${propEntries.props.join(", ")} }`,
|
|
157
157
|
...children
|
|
158
158
|
].join(", ")})`;
|
|
159
|
-
return propEntries.dynamic ? `
|
|
159
|
+
return propEntries.dynamic ? `__jigsSafe(() => ${element}, null, ${lines.line(node.line)})` : element;
|
|
160
160
|
}
|
|
161
|
-
case "slot": return "
|
|
161
|
+
case "slot": return "__jigsChildren";
|
|
162
162
|
case "call": {
|
|
163
163
|
const at = lines.line(node.line);
|
|
164
164
|
const entries = node.key === null ? node.props : {
|
|
@@ -169,12 +169,12 @@ function generateProgram(statements, tree, defines = []) {
|
|
|
169
169
|
const childExprs = genChildren(node.children, lines);
|
|
170
170
|
const children = childExprs.length === 0 ? "null" : childExprs.length === 1 ? childExprs[0] ?? "null" : fragment(childExprs);
|
|
171
171
|
const library = node.origin === "library" ? ", true" : "";
|
|
172
|
-
return `
|
|
172
|
+
return `__jigsSafe(() => __jigs.call(__jigsDef_${node.name}, { ${props.join(", ")} }, ${children}, ${at}, ${node.ordinal}, ${JSON.stringify(node.name)}${library}), null, ${at})`;
|
|
173
173
|
}
|
|
174
174
|
}
|
|
175
175
|
};
|
|
176
176
|
const genBinding = (prop, lines, line) => {
|
|
177
|
-
const read = `
|
|
177
|
+
const read = `__jigsProps.${prop.name}`;
|
|
178
178
|
if (prop.required || prop.defaultValue === null) return `const ${prop.name} = ${read};`;
|
|
179
179
|
if (prop.defaultValue.kind === "literal") return `const ${prop.name} = ${read} !== undefined ? ${read} : ${JSON.stringify(prop.defaultValue.value)};`;
|
|
180
180
|
const problem = expressionProblem(prop.defaultValue.code);
|
|
@@ -182,7 +182,7 @@ function generateProgram(statements, tree, defines = []) {
|
|
|
182
182
|
invalid("invalid_expression", line, `<define ${prop.name}={${prop.defaultValue.code}}> is not a valid expression (${problem}); the default was removed`);
|
|
183
183
|
return `const ${prop.name} = ${read};`;
|
|
184
184
|
}
|
|
185
|
-
return `const ${prop.name} = ${read} !== undefined ? ${read} :
|
|
185
|
+
return `const ${prop.name} = ${read} !== undefined ? ${read} : __jigsSafe(() => (${prop.defaultValue.code}\n), undefined, ${lines.line(line)});`;
|
|
186
186
|
};
|
|
187
187
|
const genDefine = (def) => {
|
|
188
188
|
const lines = def.origin === "library" ? libraryLines : documentLines;
|
|
@@ -190,22 +190,22 @@ function generateProgram(statements, tree, defines = []) {
|
|
|
190
190
|
const view = fragment(genChildren(def.body, lines));
|
|
191
191
|
const bodyStatements = genStatements(def.statements, lines);
|
|
192
192
|
const body = bodyStatements.length === 0 ? `return ${view};` : `return (() => {\n${bodyStatements.join("\n")}\nreturn ${view};\n})();`;
|
|
193
|
-
return `function
|
|
193
|
+
return `function __jigsDef_${def.name}(__jigsProps, __jigsChildren, __jigsAt) {\n${[...bindings, body].join("\n")}\n}`;
|
|
194
194
|
};
|
|
195
195
|
const functions = defines.map((def) => genDefine(def));
|
|
196
196
|
const children = genChildren(tree, documentLines);
|
|
197
197
|
return {
|
|
198
|
-
|
|
198
|
+
findings,
|
|
199
199
|
output: {
|
|
200
200
|
code: [
|
|
201
201
|
"\"use strict\";",
|
|
202
|
-
"const
|
|
203
|
-
"const useState =
|
|
202
|
+
"const __jigsConstants = Jigs.useConstants();",
|
|
203
|
+
"const useState = Jigs.useState, useQuery = Jigs.useQuery, useMutation = Jigs.useMutation;",
|
|
204
204
|
...functions,
|
|
205
|
-
"
|
|
205
|
+
"Jigs.render(__jigs.jsx(() => {",
|
|
206
206
|
...genStatements(statements, documentLines),
|
|
207
|
-
`return
|
|
208
|
-
"
|
|
207
|
+
`return __jigs.jsx(${[
|
|
208
|
+
"__jigs.Fragment",
|
|
209
209
|
"null",
|
|
210
210
|
...children
|
|
211
211
|
].join(",\n")});`,
|
package/dist/compile.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { ExprNode, PropValue, Statement, TextNode } from "./ast.js";
|
|
|
3
3
|
import { CompileCatalog } from "./spec.js";
|
|
4
4
|
import { ProgramOutput } from "./codegen.js";
|
|
5
5
|
import { DataRead, ProcedureUse } from "./check-code.js";
|
|
6
|
+
import { Diagnostic, Severity } from "@jigs.design/protocol";
|
|
6
7
|
//#region src/compile.d.ts
|
|
7
8
|
type DiagnosticCode = "coerced" | "handle_field" | "hook_placement" | "invalid_control" | "invalid_define" | "invalid_expression" | "invalid_literal" | "invalid_statement" | "missing_declarations" | "missing_prop" | "mutate_in_render" | "outer_reference" | "query_options" | "repaired" | "shadows_library" | "unclosed" | "undefined_compare" | "undefined_reference" | "unexpected_children" | "unknown_component" | "unknown_prop" | "unstable_input" | "unsupported_syntax" | "use_before_declare";
|
|
8
9
|
/** Replace `[start, end)` of the source with `text`. */
|
|
@@ -14,27 +15,32 @@ interface FixEdit {
|
|
|
14
15
|
/**
|
|
15
16
|
* A rewrite that makes a diagnostic's problem go away. The compiler attaches
|
|
16
17
|
* one only when there is a single way to write it, and applies it itself:
|
|
17
|
-
* see `
|
|
18
|
+
* see `compileJigs`.
|
|
18
19
|
*/
|
|
19
20
|
export interface Fix {
|
|
20
21
|
edits: Array<FixEdit>;
|
|
21
22
|
/** What the fix does, such as "Replace with `q.status === "pending"`". */
|
|
22
23
|
title: string;
|
|
23
24
|
}
|
|
24
|
-
|
|
25
|
+
/**
|
|
26
|
+
* A diagnostic while the compiler works: flat offsets into the source being
|
|
27
|
+
* compiled, and a fix as offset edits, so `applyFixes` can apply it. The
|
|
28
|
+
* result reports each as a `Diagnostic`.
|
|
29
|
+
*/
|
|
30
|
+
export interface Finding {
|
|
25
31
|
code: DiagnosticCode;
|
|
26
|
-
/** The 1-based column of `start` on its line. */
|
|
27
|
-
column?: number;
|
|
28
32
|
/** Just past what the diagnostic is about, as an offset into the source. */
|
|
29
33
|
end?: number;
|
|
30
34
|
fix?: Fix;
|
|
31
35
|
/** The line of `start` when there is one. */
|
|
32
36
|
line: number;
|
|
33
37
|
message: string;
|
|
34
|
-
severity:
|
|
38
|
+
severity: Severity;
|
|
35
39
|
/** The first character of what the diagnostic is about, as an offset into the source. */
|
|
36
40
|
start?: number;
|
|
37
41
|
}
|
|
42
|
+
/** What `compileJigs` reports: the shape the server sends and the eval results store. */
|
|
43
|
+
type Diagnostic$1 = Diagnostic<DiagnosticCode>;
|
|
38
44
|
interface RenderElement {
|
|
39
45
|
children: Array<RenderNode>;
|
|
40
46
|
component: string;
|
|
@@ -123,7 +129,7 @@ export interface Program {
|
|
|
123
129
|
statements: Array<Statement>;
|
|
124
130
|
tree: Array<RenderNode>;
|
|
125
131
|
}
|
|
126
|
-
/** A reusable component written in
|
|
132
|
+
/** A reusable component written in Jigs: source with exactly one `<define>`. */
|
|
127
133
|
export interface Recipe {
|
|
128
134
|
name: string;
|
|
129
135
|
source: string;
|
|
@@ -138,7 +144,7 @@ export interface CompileOptions {
|
|
|
138
144
|
}
|
|
139
145
|
export interface CompileResult {
|
|
140
146
|
defines: ReadonlyArray<CompiledDefine>;
|
|
141
|
-
diagnostics: Array<Diagnostic>;
|
|
147
|
+
diagnostics: Array<Diagnostic$1>;
|
|
142
148
|
/** The recipes this document was compiled against. */
|
|
143
149
|
library: ReadonlyMap<string, CompiledDefine>;
|
|
144
150
|
output: ProgramOutput;
|
|
@@ -165,13 +171,13 @@ export interface CompileResult {
|
|
|
165
171
|
*/
|
|
166
172
|
export declare function compileLibrary(recipes: ReadonlyArray<Recipe>, catalog: CompileCatalog): Library;
|
|
167
173
|
/**
|
|
168
|
-
* Compiles a document.
|
|
174
|
+
* Compiles a document. An error that carries a fix is applied rather than
|
|
169
175
|
* sent to the model: the fixed source is compiled again, and when it has
|
|
170
176
|
* exactly the problems left that the fixes did not cover, its program is
|
|
171
|
-
* used and those
|
|
177
|
+
* used and those errors become `info`, as a quoted number on a number prop
|
|
172
178
|
* already is. Diagnostics, procedures and their spans stay those of `source`,
|
|
173
179
|
* which is what the model wrote and what the session keeps.
|
|
174
180
|
*/
|
|
175
|
-
export declare function
|
|
181
|
+
export declare function compileJigs(source: string, options: CompileOptions): CompileResult;
|
|
176
182
|
//#endregion
|
|
177
|
-
export {};
|
|
183
|
+
export { Diagnostic$1 as Diagnostic };
|
package/dist/compile.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { analyze } from "./script.js";
|
|
2
2
|
import { generateProgram, isBindingName } from "./codegen.js";
|
|
3
|
-
import {
|
|
3
|
+
import { lineAt, rangeIn, sourceRange, spanIn } from "./offsets.js";
|
|
4
4
|
import { closest, nearest } from "./suggest.js";
|
|
5
5
|
import { checkCode } from "./check-code.js";
|
|
6
|
-
import {
|
|
6
|
+
import { parseJigs } from "./parse.js";
|
|
7
7
|
import { isValidValue } from "./spec.js";
|
|
8
8
|
import { blockHoists } from "./block-hoist.js";
|
|
9
9
|
//#region src/compile.ts
|
|
@@ -34,7 +34,7 @@ const HOOK_FORM = {
|
|
|
34
34
|
useState: "const [open, setOpen] = useState(false)"
|
|
35
35
|
};
|
|
36
36
|
/**
|
|
37
|
-
* A hook used as a value, written `
|
|
37
|
+
* A hook used as a value, written `Jigs.useX`, called inside a handler
|
|
38
38
|
* or anywhere but as a top-level declaration's initializer, or declared
|
|
39
39
|
* inside `<each>` or `<if>`. Null when the code parses and uses hooks only
|
|
40
40
|
* as it may, or does not parse (the parse error is reported elsewhere).
|
|
@@ -50,7 +50,7 @@ function hookMisuse(code, kind, block) {
|
|
|
50
50
|
if (value !== void 0) return message(`\`${value.hook}\` is not a value: call it in a {@body} line at the top of the document or of a <define>, \`${HOOK_FORM[value.hook]}\``);
|
|
51
51
|
const [call] = facts.hookCalls;
|
|
52
52
|
if (call === void 0) return null;
|
|
53
|
-
const written = call.prefixed ? `
|
|
53
|
+
const written = call.prefixed ? `Jigs.${call.hook}` : call.hook;
|
|
54
54
|
if (block === "each" || block === "if") return message(`${written} ${HOOK_MESSAGE[call.hook][block]}`);
|
|
55
55
|
if (call.prefixed) return message(`\`${written}\` is not supported: write \`${call.hook}\` without the prefix, \`${HOOK_FORM[call.hook]}\``);
|
|
56
56
|
if (call.place === "handler") return message(call.hook === "useMutation" ? `\`useMutation(...)\` inside a handler is not supported: declare \`${HOOK_FORM.useMutation}\` in a {@body} line at the top of the document or of a <define>, and call \`archive.mutate(input)\` from the handler` : `\`${call.hook}(...)\` inside a handler is not supported: declare it in a {@body} line at the top of the document or of a <define>, \`${HOOK_FORM[call.hook]}\`, and read it from the handler`);
|
|
@@ -109,37 +109,37 @@ function treeHasSlot(nodes) {
|
|
|
109
109
|
}
|
|
110
110
|
return false;
|
|
111
111
|
}
|
|
112
|
-
function readDefine(node,
|
|
112
|
+
function readDefine(node, findings, libraryNames, components) {
|
|
113
113
|
const nameProp = node.props.find((prop) => prop.name === "name");
|
|
114
114
|
const name = nameProp !== void 0 && nameProp.shorthand !== true && nameProp.value.kind === "literal" && typeof nameProp.value.value === "string" ? nameProp.value.value : null;
|
|
115
115
|
if (name === null) {
|
|
116
|
-
|
|
116
|
+
findings.push({
|
|
117
117
|
code: "invalid_define",
|
|
118
118
|
...placeOf(node),
|
|
119
119
|
message: `<define> needs name="componentName"; it was removed`,
|
|
120
|
-
severity: "
|
|
120
|
+
severity: "error"
|
|
121
121
|
});
|
|
122
122
|
return null;
|
|
123
123
|
}
|
|
124
124
|
if (componentSpec(components, name) !== void 0 || RESERVED_TAGS.has(name)) {
|
|
125
|
-
|
|
125
|
+
findings.push({
|
|
126
126
|
code: "invalid_define",
|
|
127
127
|
...placeOf(node),
|
|
128
128
|
message: `<define name="${name}"> clashes with <${name}>; it was removed`,
|
|
129
|
-
severity: "
|
|
129
|
+
severity: "error"
|
|
130
130
|
});
|
|
131
131
|
return null;
|
|
132
132
|
}
|
|
133
133
|
if (!isBindingName(name)) {
|
|
134
|
-
|
|
134
|
+
findings.push({
|
|
135
135
|
code: "invalid_define",
|
|
136
136
|
...placeOf(node),
|
|
137
137
|
message: `<define name="${name}"> is not a usable component name; it was removed`,
|
|
138
|
-
severity: "
|
|
138
|
+
severity: "error"
|
|
139
139
|
});
|
|
140
140
|
return null;
|
|
141
141
|
}
|
|
142
|
-
if (libraryNames.has(name))
|
|
142
|
+
if (libraryNames.has(name)) findings.push({
|
|
143
143
|
code: "shadows_library",
|
|
144
144
|
...placeOf(node),
|
|
145
145
|
message: `<define name="${name}"> overrides the library component`,
|
|
@@ -150,38 +150,38 @@ function readDefine(node, diagnostics, libraryNames, components) {
|
|
|
150
150
|
for (const prop of node.props) {
|
|
151
151
|
if (prop === nameProp) continue;
|
|
152
152
|
if (prop.name === "key") {
|
|
153
|
-
|
|
153
|
+
findings.push({
|
|
154
154
|
code: "invalid_define",
|
|
155
155
|
...placeOf(node, prop),
|
|
156
156
|
message: `<define name="${name}"> prop "key" is reserved for the instance; it was removed`,
|
|
157
|
-
severity: "
|
|
157
|
+
severity: "error"
|
|
158
158
|
});
|
|
159
159
|
continue;
|
|
160
160
|
}
|
|
161
161
|
if (prop.name === "name") {
|
|
162
|
-
|
|
162
|
+
findings.push({
|
|
163
163
|
code: "invalid_define",
|
|
164
164
|
...placeOf(node, prop),
|
|
165
165
|
message: `<define name="${name}"> cannot take a prop called "name", which names the define itself; call it "title" or "label". It was removed`,
|
|
166
|
-
severity: "
|
|
166
|
+
severity: "error"
|
|
167
167
|
});
|
|
168
168
|
continue;
|
|
169
169
|
}
|
|
170
170
|
if (prop.name === "children" || !isBindingName(prop.name)) {
|
|
171
|
-
|
|
171
|
+
findings.push({
|
|
172
172
|
code: "invalid_define",
|
|
173
173
|
...placeOf(node, prop),
|
|
174
174
|
message: `<define name="${name}"> prop "${prop.name}" is not a usable name; it was removed`,
|
|
175
|
-
severity: "
|
|
175
|
+
severity: "error"
|
|
176
176
|
});
|
|
177
177
|
continue;
|
|
178
178
|
}
|
|
179
179
|
if (seen.has(prop.name)) {
|
|
180
|
-
|
|
180
|
+
findings.push({
|
|
181
181
|
code: "invalid_define",
|
|
182
182
|
...placeOf(node, prop),
|
|
183
183
|
message: `<define name="${name}"> prop "${prop.name}" is duplicated; the later one was removed`,
|
|
184
|
-
severity: "
|
|
184
|
+
severity: "error"
|
|
185
185
|
});
|
|
186
186
|
continue;
|
|
187
187
|
}
|
|
@@ -225,7 +225,7 @@ const TAG_ALIASES = /* @__PURE__ */ new Map([
|
|
|
225
225
|
["ul", "list"],
|
|
226
226
|
["view", "box"]
|
|
227
227
|
]);
|
|
228
|
-
/** Tags a model writes for control flow that
|
|
228
|
+
/** Tags a model writes for control flow that Jigs spells differently. */
|
|
229
229
|
const CONTROL_HINTS = /* @__PURE__ */ new Map([
|
|
230
230
|
["elif", "There is no <elif>: add another <if> with its own test"],
|
|
231
231
|
["else", "There is no <else>: add a second <if> with the opposite test"],
|
|
@@ -243,7 +243,7 @@ function unknownComponentMessage(name, defines, components) {
|
|
|
243
243
|
const hint = lookupHint(name, known) ?? (alias !== void 0 && known.includes(alias) ? alias : void 0) ?? nearest(name, known, 1)[0];
|
|
244
244
|
return hint === void 0 ? `<${name}> is not in the catalog; rendering its children only` : `<${name}> is not in the catalog; did you mean <${hint}>? Rendering its children only`;
|
|
245
245
|
}
|
|
246
|
-
/** Props other UI libraries use, and the name
|
|
246
|
+
/** Props other UI libraries use, and the name Jigs uses for the same thing. */
|
|
247
247
|
const PROP_ALIASES = /* @__PURE__ */ new Map([
|
|
248
248
|
["checked", "value"],
|
|
249
249
|
["onChangeText", "onChange"],
|
|
@@ -355,13 +355,13 @@ function placeOfCode(code) {
|
|
|
355
355
|
};
|
|
356
356
|
}
|
|
357
357
|
const codeAt = (value) => value?.kind === "expr" ? value.at : void 0;
|
|
358
|
-
function validateControl(node, name, children, statements,
|
|
359
|
-
const
|
|
360
|
-
|
|
358
|
+
function validateControl(node, name, children, statements, findings) {
|
|
359
|
+
const reject = (code, message, prop) => {
|
|
360
|
+
findings.push({
|
|
361
361
|
code,
|
|
362
362
|
...placeOf(node, prop),
|
|
363
363
|
message,
|
|
364
|
-
severity: "
|
|
364
|
+
severity: "error"
|
|
365
365
|
});
|
|
366
366
|
};
|
|
367
367
|
const props = new Map(node.props.map((prop) => [prop.name, prop.value]));
|
|
@@ -369,21 +369,21 @@ function validateControl(node, name, children, statements, diagnostics) {
|
|
|
369
369
|
if (controlProps[name].some((allowed) => allowed === prop.name)) continue;
|
|
370
370
|
const fix = name === "each" && prop.name === "key" ? dropAttribute(node, index) : null;
|
|
371
371
|
if (fix === null) {
|
|
372
|
-
|
|
372
|
+
reject("unknown_prop", `<${name}> has no prop "${prop.name}"`, prop);
|
|
373
373
|
continue;
|
|
374
374
|
}
|
|
375
|
-
|
|
375
|
+
findings.push({
|
|
376
376
|
code: "unknown_prop",
|
|
377
377
|
...placeOf(node, prop),
|
|
378
378
|
fix,
|
|
379
379
|
message: "<each> has no prop \"key\": items are keyed by their own `id` or `key`",
|
|
380
|
-
severity: "
|
|
380
|
+
severity: "error"
|
|
381
381
|
});
|
|
382
382
|
}
|
|
383
383
|
if (name === "if") {
|
|
384
384
|
const test = asCode(props.get("test"));
|
|
385
385
|
if (test === null) {
|
|
386
|
-
|
|
386
|
+
reject("invalid_control", "<if> needs test={condition}; it was removed");
|
|
387
387
|
return [];
|
|
388
388
|
}
|
|
389
389
|
const testAt = codeAt(props.get("test"));
|
|
@@ -399,14 +399,14 @@ function validateControl(node, name, children, statements, diagnostics) {
|
|
|
399
399
|
const items = asCode(props.get("of"));
|
|
400
400
|
const itemsAt = codeAt(props.get("of"));
|
|
401
401
|
if (items === null) {
|
|
402
|
-
|
|
402
|
+
reject("invalid_control", "<each> needs of={list}; it was removed");
|
|
403
403
|
return [];
|
|
404
404
|
}
|
|
405
405
|
const binding = (prop, fallback) => {
|
|
406
406
|
const value = props.get(prop);
|
|
407
407
|
if (value === void 0) return fallback;
|
|
408
408
|
if (value.kind === "literal" && isBindingName(String(value.value))) return String(value.value);
|
|
409
|
-
|
|
409
|
+
reject("invalid_control", `<each ${prop}> must be a plain variable name like "item"; using ${fallback === null ? "none" : `"${fallback}"`}`);
|
|
410
410
|
return fallback;
|
|
411
411
|
};
|
|
412
412
|
return [{
|
|
@@ -422,25 +422,25 @@ function validateControl(node, name, children, statements, diagnostics) {
|
|
|
422
422
|
}
|
|
423
423
|
function validateSlot(node, state) {
|
|
424
424
|
if (!state.inDefine) {
|
|
425
|
-
state.
|
|
425
|
+
state.findings.push({
|
|
426
426
|
code: "invalid_define",
|
|
427
427
|
...placeOf(node),
|
|
428
428
|
message: "<slot> is only allowed inside <define>; it was removed",
|
|
429
|
-
severity: "
|
|
429
|
+
severity: "error"
|
|
430
430
|
});
|
|
431
431
|
return [];
|
|
432
432
|
}
|
|
433
|
-
for (const prop of node.props) state.
|
|
433
|
+
for (const prop of node.props) state.findings.push({
|
|
434
434
|
code: "unknown_prop",
|
|
435
435
|
...placeOf(node, prop),
|
|
436
436
|
message: `<slot> has no prop "${prop.name}"`,
|
|
437
|
-
severity: "
|
|
437
|
+
severity: "error"
|
|
438
438
|
});
|
|
439
|
-
if (node.children.length > 0) state.
|
|
439
|
+
if (node.children.length > 0) state.findings.push({
|
|
440
440
|
code: "unexpected_children",
|
|
441
441
|
...placeOf(node),
|
|
442
442
|
message: "<slot> takes no children; they were dropped",
|
|
443
|
-
severity: "
|
|
443
|
+
severity: "error"
|
|
444
444
|
});
|
|
445
445
|
return [{
|
|
446
446
|
line: node.line,
|
|
@@ -448,15 +448,15 @@ function validateSlot(node, state) {
|
|
|
448
448
|
}];
|
|
449
449
|
}
|
|
450
450
|
/** Keeps the first `<slot/>` that survived validation and drops the rest. */
|
|
451
|
-
function trimExtraSlots(nodes, defineName,
|
|
451
|
+
function trimExtraSlots(nodes, defineName, findings, seen) {
|
|
452
452
|
return nodes.flatMap((node) => {
|
|
453
453
|
if (node.type === "slot") {
|
|
454
454
|
if (seen.count > 0) {
|
|
455
|
-
|
|
455
|
+
findings.push({
|
|
456
456
|
code: "invalid_define",
|
|
457
457
|
...placeOf(node),
|
|
458
458
|
message: `<${defineName}> already has a <slot>; this one was removed`,
|
|
459
|
-
severity: "
|
|
459
|
+
severity: "error"
|
|
460
460
|
});
|
|
461
461
|
return [];
|
|
462
462
|
}
|
|
@@ -465,7 +465,7 @@ function trimExtraSlots(nodes, defineName, diagnostics, seen) {
|
|
|
465
465
|
}
|
|
466
466
|
if (node.type === "call" || node.type === "each" || node.type === "element" || node.type === "if") return [{
|
|
467
467
|
...node,
|
|
468
|
-
children: trimExtraSlots(node.children, defineName,
|
|
468
|
+
children: trimExtraSlots(node.children, defineName, findings, seen)
|
|
469
469
|
}];
|
|
470
470
|
return [node];
|
|
471
471
|
});
|
|
@@ -480,11 +480,11 @@ function validateCall(node, def, state, scope) {
|
|
|
480
480
|
for (const prop of node.props) {
|
|
481
481
|
if (prop.name === "key") {
|
|
482
482
|
if (prop.value.kind === "literal" && typeof prop.value.value !== "string" && typeof prop.value.value !== "number") {
|
|
483
|
-
state.
|
|
483
|
+
state.findings.push({
|
|
484
484
|
code: "invalid_literal",
|
|
485
485
|
...placeOf(node, prop),
|
|
486
486
|
message: `<${def.name} key={${JSON.stringify(prop.value.value)}}> must be a string or number`,
|
|
487
|
-
severity: "
|
|
487
|
+
severity: "error"
|
|
488
488
|
});
|
|
489
489
|
continue;
|
|
490
490
|
}
|
|
@@ -494,17 +494,17 @@ function validateCall(node, def, state, scope) {
|
|
|
494
494
|
const spec = known.get(prop.name);
|
|
495
495
|
if (spec === void 0) {
|
|
496
496
|
const near = nearest(prop.name, known.keys(), 1)[0];
|
|
497
|
-
state.
|
|
497
|
+
state.findings.push({
|
|
498
498
|
code: "unknown_prop",
|
|
499
499
|
...placeOf(node, prop),
|
|
500
500
|
message: near === void 0 ? `<${def.name}> has no prop "${prop.name}"` : `<${def.name}> has no prop "${prop.name}"; did you mean ${near}?`,
|
|
501
|
-
severity: "
|
|
501
|
+
severity: "error"
|
|
502
502
|
});
|
|
503
503
|
continue;
|
|
504
504
|
}
|
|
505
505
|
if (spec.kind === "number" && prop.value.kind === "literal" && typeof prop.value.value === "string" && NUMERIC.test(prop.value.value)) {
|
|
506
506
|
const value = Number(prop.value.value);
|
|
507
|
-
state.
|
|
507
|
+
state.findings.push({
|
|
508
508
|
code: "coerced",
|
|
509
509
|
...placeOf(node, prop),
|
|
510
510
|
message: `<${def.name} ${writtenProp(prop)}> is text; it was read as ${prop.name}={${String(value)}}. Write numbers in braces`,
|
|
@@ -517,27 +517,27 @@ function validateCall(node, def, state, scope) {
|
|
|
517
517
|
continue;
|
|
518
518
|
}
|
|
519
519
|
if (prop.value.kind === "literal" && spec.kind !== null && typeof prop.value.value !== spec.kind) {
|
|
520
|
-
state.
|
|
520
|
+
state.findings.push({
|
|
521
521
|
code: "invalid_literal",
|
|
522
522
|
...placeOf(node, prop),
|
|
523
523
|
message: `<${def.name} ${writtenProp(prop)}> does not match a ${spec.kind} value`,
|
|
524
|
-
severity: "
|
|
524
|
+
severity: "error"
|
|
525
525
|
});
|
|
526
526
|
continue;
|
|
527
527
|
}
|
|
528
528
|
props[prop.name] = prop.value;
|
|
529
529
|
}
|
|
530
|
-
for (const spec of def.props) if (spec.required && !Object.hasOwn(props, spec.name)) state.
|
|
530
|
+
for (const spec of def.props) if (spec.required && !Object.hasOwn(props, spec.name)) state.findings.push({
|
|
531
531
|
code: "missing_prop",
|
|
532
532
|
...placeOf(node),
|
|
533
533
|
message: `<${def.name}> is missing required prop "${spec.name}"`,
|
|
534
|
-
severity: "
|
|
534
|
+
severity: "error"
|
|
535
535
|
});
|
|
536
|
-
if (!def.hasSlot && def.incomplete !== true && children.length > 0) state.
|
|
536
|
+
if (!def.hasSlot && def.incomplete !== true && children.length > 0) state.findings.push({
|
|
537
537
|
code: "unexpected_children",
|
|
538
538
|
...placeOf(node),
|
|
539
539
|
message: `<${def.name}> has no <slot>, so its children were dropped`,
|
|
540
|
-
severity: "
|
|
540
|
+
severity: "error"
|
|
541
541
|
});
|
|
542
542
|
return [{
|
|
543
543
|
children: def.hasSlot ? children : [],
|
|
@@ -562,14 +562,14 @@ function validateNodes(nodes, state, scope) {
|
|
|
562
562
|
if (misuse !== null) {
|
|
563
563
|
const inBlock = scope.block === "each" || scope.block === "if";
|
|
564
564
|
const hoist = inBlock ? state.hoists.byStart.get(node.start) : void 0;
|
|
565
|
-
const
|
|
565
|
+
const finding = {
|
|
566
566
|
code: inBlock ? "invalid_statement" : "hook_placement",
|
|
567
567
|
...placeOfCode(node),
|
|
568
568
|
message: `${misuse.message}${hoist?.reason === null || hoist?.reason === void 0 ? "" : `; it can't move out by itself, as it ${hoist.reason}`}. The statement was removed`,
|
|
569
|
-
severity: "
|
|
569
|
+
severity: "error"
|
|
570
570
|
};
|
|
571
|
-
if (hoist?.fix)
|
|
572
|
-
state.
|
|
571
|
+
if (hoist?.fix) finding.fix = hoist.fix;
|
|
572
|
+
state.findings.push(finding);
|
|
573
573
|
for (const name of misuse.declared) state.dropped.add(name);
|
|
574
574
|
return [];
|
|
575
575
|
}
|
|
@@ -585,11 +585,11 @@ function validateNodes(nodes, state, scope) {
|
|
|
585
585
|
if (node.type === "expr") {
|
|
586
586
|
const misuse = hookMisuse(node.code, "expression", scope.block);
|
|
587
587
|
if (misuse !== null) {
|
|
588
|
-
state.
|
|
588
|
+
state.findings.push({
|
|
589
589
|
code: "hook_placement",
|
|
590
590
|
...placeOfCode(node),
|
|
591
591
|
message: `${misuse.message}. The expression was removed`,
|
|
592
|
-
severity: "
|
|
592
|
+
severity: "error"
|
|
593
593
|
});
|
|
594
594
|
return [];
|
|
595
595
|
}
|
|
@@ -597,11 +597,11 @@ function validateNodes(nodes, state, scope) {
|
|
|
597
597
|
}
|
|
598
598
|
if (node.type !== "element") return [node];
|
|
599
599
|
if (node.name === "define") {
|
|
600
|
-
state.
|
|
600
|
+
state.findings.push({
|
|
601
601
|
code: "invalid_define",
|
|
602
602
|
...placeOf(node),
|
|
603
603
|
message: "<define> is only allowed at the top level; it was removed",
|
|
604
|
-
severity: "
|
|
604
|
+
severity: "error"
|
|
605
605
|
});
|
|
606
606
|
return [];
|
|
607
607
|
}
|
|
@@ -612,18 +612,18 @@ function validateNodes(nodes, state, scope) {
|
|
|
612
612
|
block: node.name,
|
|
613
613
|
statements
|
|
614
614
|
});
|
|
615
|
-
return validateControl(node, node.name, children, statements, state.
|
|
615
|
+
return validateControl(node, node.name, children, statements, state.findings);
|
|
616
616
|
}
|
|
617
617
|
const defined = state.defines.get(node.name);
|
|
618
618
|
if (defined !== void 0) return validateCall(node, defined, state, scope);
|
|
619
619
|
const children = validateNodes(node.children, state, scope);
|
|
620
620
|
const spec = componentSpec(state.catalog, node.name);
|
|
621
621
|
if (spec === void 0) {
|
|
622
|
-
state.
|
|
622
|
+
state.findings.push({
|
|
623
623
|
code: "unknown_component",
|
|
624
624
|
...placeOf(node),
|
|
625
625
|
message: unknownComponentMessage(node.name, state.defines, state.catalog),
|
|
626
|
-
severity: "
|
|
626
|
+
severity: "error"
|
|
627
627
|
});
|
|
628
628
|
return children;
|
|
629
629
|
}
|
|
@@ -634,29 +634,29 @@ function validateNodes(nodes, state, scope) {
|
|
|
634
634
|
const propDefinition = propSpec(spec, prop.name);
|
|
635
635
|
if (propDefinition === void 0) {
|
|
636
636
|
const wrap = flexWrap(node, prop, state.catalog);
|
|
637
|
-
const
|
|
637
|
+
const finding = {
|
|
638
638
|
code: "unknown_prop",
|
|
639
639
|
...placeOf(node, prop),
|
|
640
640
|
message: wrap === null ? unknownPropMessage(component, prop.name, spec.props) : `<${component}> has no prop "flex"; wrap it in <box ${wrap.written}>`,
|
|
641
|
-
severity: "
|
|
641
|
+
severity: "error"
|
|
642
642
|
};
|
|
643
|
-
if (wrap !== null)
|
|
644
|
-
state.
|
|
643
|
+
if (wrap !== null) finding.fix = wrap.fix;
|
|
644
|
+
state.findings.push(finding);
|
|
645
645
|
continue;
|
|
646
646
|
}
|
|
647
647
|
const misuse = prop.value.kind === "expr" ? hookMisuse(prop.value.code, "expression", scope.block) : null;
|
|
648
648
|
if (misuse !== null) {
|
|
649
|
-
state.
|
|
649
|
+
state.findings.push({
|
|
650
650
|
code: "hook_placement",
|
|
651
651
|
...placeOf(node, prop),
|
|
652
652
|
message: `<${component} ${prop.name}={…}>: ${misuse.message}. The prop was removed`,
|
|
653
|
-
severity: "
|
|
653
|
+
severity: "error"
|
|
654
654
|
});
|
|
655
655
|
continue;
|
|
656
656
|
}
|
|
657
657
|
const coerced = coerceNumber(propDefinition, prop.value);
|
|
658
658
|
if (coerced !== null && coerced.kind === "literal") {
|
|
659
|
-
state.
|
|
659
|
+
state.findings.push({
|
|
660
660
|
code: "coerced",
|
|
661
661
|
...placeOf(node, prop),
|
|
662
662
|
message: `<${component} ${writtenProp(prop)}> is text; it was read as ${prop.name}={${String(coerced.value)}}. Write numbers in braces`,
|
|
@@ -666,22 +666,22 @@ function validateNodes(nodes, state, scope) {
|
|
|
666
666
|
continue;
|
|
667
667
|
}
|
|
668
668
|
if (prop.value.kind === "literal" && !isValidValue(propDefinition, prop.value.value)) {
|
|
669
|
-
state.
|
|
669
|
+
state.findings.push({
|
|
670
670
|
code: "invalid_literal",
|
|
671
671
|
...placeOf(node, prop),
|
|
672
672
|
message: `<${component} ${writtenProp(prop)}> does not match ${describe(propDefinition, prop.value.value)}`,
|
|
673
|
-
severity: "
|
|
673
|
+
severity: "error"
|
|
674
674
|
});
|
|
675
675
|
continue;
|
|
676
676
|
}
|
|
677
677
|
props[prop.name] = prop.value;
|
|
678
678
|
}
|
|
679
679
|
const allowsChildren = spec.children !== "none";
|
|
680
|
-
if (!allowsChildren && children.length > 0) state.
|
|
680
|
+
if (!allowsChildren && children.length > 0) state.findings.push({
|
|
681
681
|
code: "unexpected_children",
|
|
682
682
|
...placeOf(node),
|
|
683
683
|
message: `<${component}> takes no children; they were dropped`,
|
|
684
|
-
severity: "
|
|
684
|
+
severity: "error"
|
|
685
685
|
});
|
|
686
686
|
return [{
|
|
687
687
|
children: allowsChildren ? children : [],
|
|
@@ -755,8 +755,8 @@ function countUses(tree, docDefines) {
|
|
|
755
755
|
return [...counts.values()].toSorted((a, b) => a.name.localeCompare(b.name));
|
|
756
756
|
}
|
|
757
757
|
function compileDocument(source, library, components) {
|
|
758
|
-
const document =
|
|
759
|
-
const
|
|
758
|
+
const document = parseJigs(source);
|
|
759
|
+
const findings = [...document.repairs.map((repair) => ({
|
|
760
760
|
code: "repaired",
|
|
761
761
|
...placeOf(repair),
|
|
762
762
|
message: repair.message,
|
|
@@ -765,7 +765,7 @@ function compileDocument(source, library, components) {
|
|
|
765
765
|
code: "unclosed",
|
|
766
766
|
...placeOf(error),
|
|
767
767
|
message: error.message,
|
|
768
|
-
severity: "
|
|
768
|
+
severity: "error"
|
|
769
769
|
}))];
|
|
770
770
|
const libraryNames = new Set(library.keys());
|
|
771
771
|
const docDefines = [];
|
|
@@ -774,14 +774,14 @@ function compileDocument(source, library, components) {
|
|
|
774
774
|
const docNames = /* @__PURE__ */ new Set();
|
|
775
775
|
for (const node of document.children) {
|
|
776
776
|
if (node.type === "element" && node.name === "define") {
|
|
777
|
-
const def = readDefine(node,
|
|
777
|
+
const def = readDefine(node, findings, libraryNames, components);
|
|
778
778
|
if (def === null) continue;
|
|
779
779
|
if (docNames.has(def.name)) {
|
|
780
|
-
|
|
780
|
+
findings.push({
|
|
781
781
|
code: "invalid_define",
|
|
782
782
|
...placeOf(node),
|
|
783
783
|
message: `<define name="${def.name}"> is already defined; this one was removed`,
|
|
784
|
-
severity: "
|
|
784
|
+
severity: "error"
|
|
785
785
|
});
|
|
786
786
|
continue;
|
|
787
787
|
}
|
|
@@ -801,8 +801,8 @@ function compileDocument(source, library, components) {
|
|
|
801
801
|
catalog: components,
|
|
802
802
|
defineName: null,
|
|
803
803
|
defines: available,
|
|
804
|
-
diagnostics,
|
|
805
804
|
dropped,
|
|
805
|
+
findings,
|
|
806
806
|
hoists,
|
|
807
807
|
inDefine: false,
|
|
808
808
|
ordinals: /* @__PURE__ */ new Map()
|
|
@@ -813,15 +813,15 @@ function compileDocument(source, library, components) {
|
|
|
813
813
|
catalog: components,
|
|
814
814
|
defineName: def.name,
|
|
815
815
|
defines: available,
|
|
816
|
-
diagnostics,
|
|
817
816
|
dropped,
|
|
817
|
+
findings,
|
|
818
818
|
hoists,
|
|
819
819
|
inDefine: true,
|
|
820
820
|
ordinals: /* @__PURE__ */ new Map()
|
|
821
821
|
}, {
|
|
822
822
|
block: "define",
|
|
823
823
|
statements
|
|
824
|
-
}), def.name,
|
|
824
|
+
}), def.name, findings, { count: 0 });
|
|
825
825
|
def.statements = statements;
|
|
826
826
|
}
|
|
827
827
|
const statements = [];
|
|
@@ -831,11 +831,11 @@ function compileDocument(source, library, components) {
|
|
|
831
831
|
statements.push(statement);
|
|
832
832
|
continue;
|
|
833
833
|
}
|
|
834
|
-
|
|
834
|
+
findings.push({
|
|
835
835
|
code: "hook_placement",
|
|
836
836
|
...placeOfCode(statement),
|
|
837
837
|
message: `${misuse.message}. The statement was removed`,
|
|
838
|
-
severity: "
|
|
838
|
+
severity: "error"
|
|
839
839
|
});
|
|
840
840
|
for (const name of misuse.declared) dropped.add(name);
|
|
841
841
|
}
|
|
@@ -843,17 +843,14 @@ function compileDocument(source, library, components) {
|
|
|
843
843
|
block: null,
|
|
844
844
|
statements
|
|
845
845
|
});
|
|
846
|
-
const uses = checkCode(tree, docDefines, statements,
|
|
846
|
+
const uses = checkCode(tree, docDefines, statements, findings, hoists, dropped);
|
|
847
847
|
const generated = generateProgram(statements, tree, emittedDefines(tree, docDefines, available));
|
|
848
|
-
|
|
849
|
-
for (const
|
|
850
|
-
|
|
851
|
-
diagnostic.column = columnAt(source, diagnostic.start);
|
|
852
|
-
}
|
|
853
|
-
diagnostics.sort((a, b) => a.line - b.line);
|
|
848
|
+
findings.push(...generated.findings);
|
|
849
|
+
for (const finding of findings) if (finding.start !== void 0) finding.line = lineAt(source, finding.start);
|
|
850
|
+
findings.sort((a, b) => a.line - b.line);
|
|
854
851
|
return {
|
|
855
852
|
defines: docDefines,
|
|
856
|
-
|
|
853
|
+
findings,
|
|
857
854
|
library,
|
|
858
855
|
output: generated.output,
|
|
859
856
|
procedures: uses.procedures,
|
|
@@ -871,7 +868,7 @@ function buildLibrary(list, components) {
|
|
|
871
868
|
for (const recipe of list) {
|
|
872
869
|
if (built.has(recipe.name)) throw new Error(`Recipe name "${recipe.name}" is duplicated`);
|
|
873
870
|
const result = compileDocument(recipe.source, built, components);
|
|
874
|
-
const problem = result.
|
|
871
|
+
const problem = result.findings[0];
|
|
875
872
|
if (problem !== void 0) throw new Error(`Recipe "${recipe.name}" failed to compile: line ${problem.line}: ${problem.message}`);
|
|
876
873
|
const def = result.defines[0];
|
|
877
874
|
if (result.defines.length !== 1 || def === void 0) throw new Error(`Recipe "${recipe.name}" must contain exactly one <define>`);
|
|
@@ -917,34 +914,57 @@ function applyFixes(source, fixes) {
|
|
|
917
914
|
text
|
|
918
915
|
};
|
|
919
916
|
}
|
|
920
|
-
const problems = (
|
|
917
|
+
const problems = (findings) => findings.filter((finding) => finding.severity === "error").length;
|
|
918
|
+
/** `finding` as a `Diagnostic`, its spans counted in `source`. */
|
|
919
|
+
function toDiagnostic(source, finding) {
|
|
920
|
+
const diagnostic = {
|
|
921
|
+
code: finding.code,
|
|
922
|
+
message: finding.message,
|
|
923
|
+
severity: finding.severity,
|
|
924
|
+
span: spanIn(source, finding)
|
|
925
|
+
};
|
|
926
|
+
if (finding.fix !== void 0) diagnostic.fix = {
|
|
927
|
+
edits: finding.fix.edits.map((edit) => ({
|
|
928
|
+
span: rangeIn(source, edit.start, edit.end),
|
|
929
|
+
text: edit.text
|
|
930
|
+
})),
|
|
931
|
+
title: finding.fix.title
|
|
932
|
+
};
|
|
933
|
+
return diagnostic;
|
|
934
|
+
}
|
|
935
|
+
/** `document`'s result, reporting `findings` as diagnostics of `source`. */
|
|
936
|
+
function withDiagnostics({ findings: _, ...result }, source, findings) {
|
|
937
|
+
return {
|
|
938
|
+
...result,
|
|
939
|
+
diagnostics: findings.map((finding) => toDiagnostic(source, finding))
|
|
940
|
+
};
|
|
941
|
+
}
|
|
921
942
|
/**
|
|
922
|
-
* Compiles a document.
|
|
943
|
+
* Compiles a document. An error that carries a fix is applied rather than
|
|
923
944
|
* sent to the model: the fixed source is compiled again, and when it has
|
|
924
945
|
* exactly the problems left that the fixes did not cover, its program is
|
|
925
|
-
* used and those
|
|
946
|
+
* used and those errors become `info`, as a quoted number on a number prop
|
|
926
947
|
* already is. Diagnostics, procedures and their spans stay those of `source`,
|
|
927
948
|
* which is what the model wrote and what the session keeps.
|
|
928
949
|
*/
|
|
929
|
-
function
|
|
950
|
+
function compileJigs(source, options) {
|
|
930
951
|
const library = options.library ?? NO_LIBRARY;
|
|
931
952
|
const result = compileDocument(source, library, options.catalog);
|
|
932
|
-
const fixes = new Set(result.
|
|
933
|
-
if (fixes.size === 0) return result;
|
|
953
|
+
const fixes = new Set(result.findings.flatMap((finding) => finding.severity === "error" && finding.fix !== void 0 ? [finding.fix] : []));
|
|
954
|
+
if (fixes.size === 0) return withDiagnostics(result, source, result.findings);
|
|
934
955
|
const patched = applyFixes(source, [...fixes]);
|
|
935
956
|
const fixed = compileDocument(patched.text, library, options.catalog);
|
|
936
|
-
const covered = result.
|
|
937
|
-
if (problems(fixed.
|
|
938
|
-
return {
|
|
957
|
+
const covered = result.findings.filter((finding) => finding.fix !== void 0 && patched.applied.has(finding.fix)).length;
|
|
958
|
+
if (problems(fixed.findings) !== problems(result.findings) - covered) return withDiagnostics(result, source, result.findings);
|
|
959
|
+
return withDiagnostics({
|
|
939
960
|
...fixed,
|
|
940
|
-
diagnostics: result.diagnostics.map((diagnostic) => diagnostic.fix !== void 0 && patched.applied.has(diagnostic.fix) ? {
|
|
941
|
-
...diagnostic,
|
|
942
|
-
message: `${diagnostic.message}; the compiler made that change`,
|
|
943
|
-
severity: "info"
|
|
944
|
-
} : diagnostic),
|
|
945
961
|
procedures: result.procedures,
|
|
946
962
|
reads: result.reads
|
|
947
|
-
}
|
|
963
|
+
}, source, result.findings.map((finding) => finding.fix !== void 0 && patched.applied.has(finding.fix) ? {
|
|
964
|
+
...finding,
|
|
965
|
+
message: `${finding.message}; the compiler made that change`,
|
|
966
|
+
severity: "info"
|
|
967
|
+
} : finding));
|
|
948
968
|
}
|
|
949
969
|
//#endregion
|
|
950
|
-
export {
|
|
970
|
+
export { compileJigs, compileLibrary };
|
package/dist/hoist.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { lineAt } from "./offsets.js";
|
|
2
2
|
import { statementOrder } from "./check-code.js";
|
|
3
|
-
import {
|
|
3
|
+
import { parseJigs } from "./parse.js";
|
|
4
4
|
//#region src/hoist.ts
|
|
5
5
|
const blank = (line) => (line ?? "").trim() === "";
|
|
6
6
|
/** Tags whose `{@body}` lines are their own, not the page's. */
|
|
@@ -31,7 +31,7 @@ function hoistDeclarations(source) {
|
|
|
31
31
|
const lines = source.split("\n");
|
|
32
32
|
/** Each statement's first and last line, 0-based. */
|
|
33
33
|
const spans = [];
|
|
34
|
-
const document =
|
|
34
|
+
const document = parseJigs(source);
|
|
35
35
|
const statements = [...document.statements, ...nested(document.children)].toSorted((a, b) => a.start - b.start);
|
|
36
36
|
for (const statement of statements) {
|
|
37
37
|
const last = lineAt(source, statement.end - 1) - 1;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { columnAt, lineAt } from "./offsets.js";
|
|
2
|
-
import {
|
|
1
|
+
import { columnAt, lineAt, rangeIn, spanIn } from "./offsets.js";
|
|
2
|
+
import { JigsNode, Statement } from "./ast.js";
|
|
3
3
|
import { CompileCatalog, ComponentSpec, PropSpec } from "./spec.js";
|
|
4
4
|
import { ProgramOutput } from "./codegen.js";
|
|
5
|
-
import { CompileOptions, CompileResult, CompiledDefine, Diagnostic, Library, Program, Recipe, RenderNode,
|
|
5
|
+
import { CompileOptions, CompileResult, CompiledDefine, Diagnostic, Library, Program, Recipe, RenderNode, compileJigs, compileLibrary } from "./compile.js";
|
|
6
6
|
import { DataRead, ELEMENT, ProcedureUse } from "./check-code.js";
|
|
7
7
|
import { Hoisted, hoistDeclarations } from "./hoist.js";
|
|
8
|
-
import {
|
|
8
|
+
import { parseJigs } from "./parse.js";
|
|
9
9
|
import { nearest } from "./suggest.js";
|
|
10
10
|
import { recipeSignature } from "./signature.js";
|
|
11
11
|
import { Edit, EditFailure, EditFailureReason, SEARCH_MARKER, applyEdits, editBreakage, isEditReply, parseEdits } from "./patch.js";
|
|
12
|
-
export { type CompileCatalog, type CompileOptions, type CompileResult, type CompiledDefine, type ComponentSpec, type DataRead, type Diagnostic, ELEMENT, type Edit, type EditFailure, type EditFailureReason, type Hoisted, type
|
|
12
|
+
export { type CompileCatalog, type CompileOptions, type CompileResult, type CompiledDefine, type ComponentSpec, type DataRead, type Diagnostic, ELEMENT, type Edit, type EditFailure, type EditFailureReason, type Hoisted, type JigsNode, type Library, type ProcedureUse, type Program, type ProgramOutput, type PropSpec, type Recipe, type RenderNode, SEARCH_MARKER, type Statement, applyEdits, columnAt, compileJigs, compileLibrary, editBreakage, hoistDeclarations, isEditReply, lineAt, nearest, parseEdits, parseJigs, rangeIn, recipeSignature, spanIn };
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { columnAt, lineAt } from "./offsets.js";
|
|
1
|
+
import { columnAt, lineAt, rangeIn, spanIn } from "./offsets.js";
|
|
2
2
|
import { nearest } from "./suggest.js";
|
|
3
3
|
import { ELEMENT } from "./check-code.js";
|
|
4
|
-
import {
|
|
4
|
+
import { parseJigs } from "./parse.js";
|
|
5
5
|
import { hoistDeclarations } from "./hoist.js";
|
|
6
|
-
import {
|
|
6
|
+
import { compileJigs, compileLibrary } from "./compile.js";
|
|
7
7
|
import { recipeSignature } from "./signature.js";
|
|
8
8
|
import { SEARCH_MARKER, applyEdits, editBreakage, isEditReply, parseEdits } from "./patch.js";
|
|
9
|
-
export { ELEMENT, SEARCH_MARKER, applyEdits, columnAt,
|
|
9
|
+
export { ELEMENT, SEARCH_MARKER, applyEdits, columnAt, compileJigs, compileLibrary, editBreakage, hoistDeclarations, isEditReply, lineAt, nearest, parseEdits, parseJigs, rangeIn, recipeSignature, spanIn };
|
package/dist/offsets.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Span } from "@jigs.design/protocol";
|
|
1
2
|
//#region src/offsets.d.ts
|
|
2
3
|
/**
|
|
3
4
|
* Where text the parser cut out sits in the source. Each entry is
|
|
@@ -15,4 +16,12 @@ export interface Range {
|
|
|
15
16
|
export declare function columnAt(source: string, offset: number): number;
|
|
16
17
|
/** The 1-based line of `offset` in `source`. */
|
|
17
18
|
export declare function lineAt(source: string, offset: number): number;
|
|
19
|
+
/** The span of `[start, end)` in `source`: its line, column and range. */
|
|
20
|
+
export declare function rangeIn(source: string, start: number, end: number): Span;
|
|
21
|
+
/** The span of `place` in `source`: its line, and its range and column when it has one. */
|
|
22
|
+
export declare function spanIn(source: string, place: {
|
|
23
|
+
end?: number;
|
|
24
|
+
line: number;
|
|
25
|
+
start?: number;
|
|
26
|
+
}): Span;
|
|
18
27
|
//#endregion
|
package/dist/offsets.js
CHANGED
|
@@ -36,5 +36,18 @@ function lineAt(source, offset) {
|
|
|
36
36
|
for (let newline = source.indexOf("\n"); newline !== -1 && newline < offset; newline = source.indexOf("\n", newline + 1)) line++;
|
|
37
37
|
return line;
|
|
38
38
|
}
|
|
39
|
+
/** The span of `[start, end)` in `source`: its line, column and range. */
|
|
40
|
+
function rangeIn(source, start, end) {
|
|
41
|
+
return {
|
|
42
|
+
column: columnAt(source, start),
|
|
43
|
+
end,
|
|
44
|
+
line: lineAt(source, start),
|
|
45
|
+
start
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
/** The span of `place` in `source`: its line, and its range and column when it has one. */
|
|
49
|
+
function spanIn(source, place) {
|
|
50
|
+
return place.start === void 0 || place.end === void 0 ? { line: place.line } : rangeIn(source, place.start, place.end);
|
|
51
|
+
}
|
|
39
52
|
//#endregion
|
|
40
|
-
export { columnAt, extendMap, lineAt, sliceMap, sourceOffset, sourceRange };
|
|
53
|
+
export { columnAt, extendMap, lineAt, rangeIn, sliceMap, sourceOffset, sourceRange, spanIn };
|
package/dist/parse.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { JigsDocument } from "./ast.js";
|
|
2
2
|
//#region src/parse.d.ts
|
|
3
3
|
/**
|
|
4
|
-
* Parses
|
|
4
|
+
* Parses Jigs: Markdown blocks, catalog tags and `{@body ...}` statements.
|
|
5
5
|
*
|
|
6
6
|
* Never throws. Partial input (a response that is still being written) is
|
|
7
7
|
* repaired: open elements are closed and a trailing incomplete tag, expression
|
|
@@ -12,5 +12,5 @@ import { PickleDocument } from "./ast.js";
|
|
|
12
12
|
* blank line, unless one of its attributes has a `{` still open. Since that text can't be
|
|
13
13
|
* finished by more streaming, it is reported in `errors`.
|
|
14
14
|
*/
|
|
15
|
-
export declare function
|
|
15
|
+
export declare function parseJigs(source: string): JigsDocument;
|
|
16
16
|
//#endregion
|
package/dist/parse.js
CHANGED
|
@@ -201,7 +201,7 @@ function scanCloseTag(source, start, map) {
|
|
|
201
201
|
};
|
|
202
202
|
}
|
|
203
203
|
/**
|
|
204
|
-
* Splits a run of
|
|
204
|
+
* Splits a run of Jigs into tags, `{expressions}` and text. While more input
|
|
205
205
|
* may follow, an unclosed tag or `{` ends the run as `incomplete`. Once
|
|
206
206
|
* `final`, nothing more can arrive: an unclosed `{` is kept as text and an
|
|
207
207
|
* unclosed tag is dropped, each with a problem. `map` gives the source
|
|
@@ -436,7 +436,7 @@ function createTreeBuilder(repairs, errors) {
|
|
|
436
436
|
};
|
|
437
437
|
/**
|
|
438
438
|
* A Markdown block (heading, paragraph, list item) whose content is inline
|
|
439
|
-
*
|
|
439
|
+
* Jigs. `map` gives the source offsets of `content`.
|
|
440
440
|
*/
|
|
441
441
|
const inlineBlock = (name, props, content, line, final, map) => {
|
|
442
442
|
const range = sourceRange(map, 0, content.length);
|
|
@@ -488,7 +488,7 @@ const headingSizes = [
|
|
|
488
488
|
"md"
|
|
489
489
|
];
|
|
490
490
|
/**
|
|
491
|
-
* Parses
|
|
491
|
+
* Parses Jigs: Markdown blocks, catalog tags and `{@body ...}` statements.
|
|
492
492
|
*
|
|
493
493
|
* Never throws. Partial input (a response that is still being written) is
|
|
494
494
|
* repaired: open elements are closed and a trailing incomplete tag, expression
|
|
@@ -499,7 +499,7 @@ const headingSizes = [
|
|
|
499
499
|
* blank line, unless one of its attributes has a `{` still open. Since that text can't be
|
|
500
500
|
* finished by more streaming, it is reported in `errors`.
|
|
501
501
|
*/
|
|
502
|
-
function
|
|
502
|
+
function parseJigs(source) {
|
|
503
503
|
const lines = source.split(/\r?\n/);
|
|
504
504
|
const lineStarts = [0];
|
|
505
505
|
for (let newline = source.indexOf("\n"); newline !== -1; newline = source.indexOf("\n", newline + 1)) lineStarts.push(newline + 1);
|
|
@@ -687,4 +687,4 @@ function parsePickle(source) {
|
|
|
687
687
|
};
|
|
688
688
|
}
|
|
689
689
|
//#endregion
|
|
690
|
-
export {
|
|
690
|
+
export { parseJigs };
|
package/dist/patch.d.ts
CHANGED
|
@@ -6,7 +6,7 @@ export interface Edit {
|
|
|
6
6
|
export declare const SEARCH_MARKER = "<<<<<<< SEARCH";
|
|
7
7
|
/**
|
|
8
8
|
* Whether a reply is a set of edits rather than a full document. Null while
|
|
9
|
-
* too little has arrived to tell (
|
|
9
|
+
* too little has arrived to tell (Jigs can also start with `<`).
|
|
10
10
|
*/
|
|
11
11
|
export declare function isEditReply(text: string): boolean | null;
|
|
12
12
|
/** Complete SEARCH/REPLACE blocks in `text`; a block still being written is skipped. */
|
package/dist/patch.js
CHANGED
|
@@ -5,7 +5,7 @@ const REPLACE_MARKER = ">>>>>>> REPLACE";
|
|
|
5
5
|
const FENCE = /^`{3}[\w-]*$/;
|
|
6
6
|
/**
|
|
7
7
|
* Whether a reply is a set of edits rather than a full document. Null while
|
|
8
|
-
* too little has arrived to tell (
|
|
8
|
+
* too little has arrived to tell (Jigs can also start with `<`).
|
|
9
9
|
*/
|
|
10
10
|
function isEditReply(text) {
|
|
11
11
|
const start = text.trimStart().replace(/^`{3}[\w-]*\n/, "");
|
package/dist/script.js
CHANGED
|
@@ -55,10 +55,10 @@ const LOOPS = {
|
|
|
55
55
|
ForStatement: "for",
|
|
56
56
|
WhileStatement: "while"
|
|
57
57
|
};
|
|
58
|
-
const unsupported = (syntax, advice) => `${syntax} isn't part of
|
|
58
|
+
const unsupported = (syntax, advice) => `${syntax} isn't part of Jigs's JavaScript: ${advice}`;
|
|
59
59
|
const MESSAGES = {
|
|
60
60
|
class: unsupported("`class`", "use plain objects and arrow functions"),
|
|
61
|
-
fetch: "There is no `fetch` in
|
|
61
|
+
fetch: "There is no `fetch` in Jigs: read data with `useQuery` and a query the prompt lists",
|
|
62
62
|
function: (name) => unsupported(`\`function ${name}()\``, `write \`const ${name} = (…) => …\` instead`),
|
|
63
63
|
getter: unsupported("A getter or setter", "compute the value in a {@body} line instead"),
|
|
64
64
|
import: unsupported("`import()`", "everything a spec can use is built in"),
|
|
@@ -132,11 +132,11 @@ function varNames(node, out) {
|
|
|
132
132
|
if (node.type === "VariableDeclaration" && node.kind === "var") for (const declarator of node.declarations) patternNames(declarator.id, out);
|
|
133
133
|
for (const child of childNodes(node)) if (!isFunction(child)) varNames(child, out);
|
|
134
134
|
}
|
|
135
|
-
/** The name a callee is written as, bare or as `
|
|
135
|
+
/** The name a callee is written as, bare or as `Jigs.name`. */
|
|
136
136
|
function calleeName(node) {
|
|
137
|
-
return node.type === "Identifier" ? node.name : node.type === "MemberExpression" && !node.computed && node.object.type === "Identifier" && node.object.name === "
|
|
137
|
+
return node.type === "Identifier" ? node.name : node.type === "MemberExpression" && !node.computed && node.object.type === "Identifier" && node.object.name === "Jigs" && node.property.type === "Identifier" ? node.property.name : null;
|
|
138
138
|
}
|
|
139
|
-
/** `useQuery`, bare or as `
|
|
139
|
+
/** `useQuery`, bare or as `Jigs.useQuery`. */
|
|
140
140
|
function hookCallee(node) {
|
|
141
141
|
const name = calleeName(node);
|
|
142
142
|
return name === "useMutation" || name === "useQuery" || name === "useState" ? name : null;
|
package/dist/signature.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { parseJigs } from "./parse.js";
|
|
2
2
|
//#region src/signature.ts
|
|
3
3
|
function formatAttr(prop) {
|
|
4
4
|
if (prop.shorthand === true) return prop.name;
|
|
@@ -15,7 +15,7 @@ function hasSlot(nodes) {
|
|
|
15
15
|
}
|
|
16
16
|
/** `<statCard label value delta={0} note="">…</statCard>` when the define has a slot. */
|
|
17
17
|
function recipeSignature(recipe) {
|
|
18
|
-
const define =
|
|
18
|
+
const define = parseJigs(recipe.source).children.find((node) => node.type === "element" && node.name === "define");
|
|
19
19
|
if (define === void 0) return `<${recipe.name}>`;
|
|
20
20
|
const attrs = define.props.filter((prop) => prop.name !== "name").map((prop) => formatAttr(prop));
|
|
21
21
|
const open = attrs.length === 0 ? `<${recipe.name}>` : `<${recipe.name} ${attrs.join(" ")}>`;
|
package/package.json
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jigs.design/compiler",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Today's
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Today's Jigs compiler: parser, checker against a catalog, code generator, recipes, and the edit helpers. The v2 rewrite replaces it.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
8
|
-
"url": "git+https://github.com/yoamomonstruos/
|
|
8
|
+
"url": "git+https://github.com/yoamomonstruos/jigs.git",
|
|
9
9
|
"directory": "packages/compiler"
|
|
10
10
|
},
|
|
11
11
|
"type": "module",
|
|
@@ -25,6 +25,7 @@
|
|
|
25
25
|
"access": "public"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"acorn": "8.19.0"
|
|
28
|
+
"acorn": "8.19.0",
|
|
29
|
+
"@jigs.design/protocol": "0.2.0"
|
|
29
30
|
}
|
|
30
31
|
}
|