@turingcode/cli 0.2.12 → 0.3.4
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 +2 -0
- package/dist/cli/create-cli.d.ts +10 -0
- package/dist/cli/create-cli.js +12 -2
- package/dist/cli/index.d.ts +9 -0
- package/dist/cli/index.js +5 -1
- package/dist/core/bound-error.d.ts +10 -0
- package/dist/core/date.d.ts +12 -0
- package/dist/core/exact/expression.d.ts +49 -0
- package/dist/core/exact/fraction.d.ts +85 -0
- package/dist/core/files.d.ts +44 -0
- package/dist/core/files.js +46 -1
- package/dist/core/json-shape.d.ts +45 -0
- package/dist/core/json-shape.js +0 -43
- package/dist/core/mcp.d.ts +92 -0
- package/dist/core/mcp.js +57 -30
- package/dist/core/query.d.ts +69 -0
- package/dist/core/query.js +11 -0
- package/dist/core/report.d.ts +11 -0
- package/dist/core/tables/csv.d.ts +18 -0
- package/dist/core/tables/markdown.d.ts +22 -0
- package/dist/core/tables/types.d.ts +25 -0
- package/dist/features/contract/check.d.ts +30 -0
- package/dist/features/contract/check.js +2 -3
- package/dist/features/contract/command.d.ts +12 -0
- package/dist/features/contract/command.js +64 -64
- package/dist/features/contract/describe.d.ts +22 -0
- package/dist/features/contract/diagnostics.d.ts +108 -0
- package/dist/features/contract/load.d.ts +58 -0
- package/dist/features/contract/load.js +24 -17
- package/dist/features/contract/model.d.ts +67 -0
- package/dist/features/contract/rules.d.ts +137 -0
- package/dist/features/contract/rules.js +11 -12
- package/dist/features/debug/clear_breakpoint.d.ts +2 -0
- package/dist/features/debug/clear_breakpoint.js +24 -0
- package/dist/features/debug/command.d.ts +5 -0
- package/dist/features/debug/command.js +66 -0
- package/dist/features/debug/continue.d.ts +2 -0
- package/dist/features/debug/continue.js +22 -0
- package/dist/features/debug/debug_start.d.ts +2 -0
- package/dist/features/debug/debug_start.js +22 -0
- package/dist/features/debug/debug_stop.d.ts +2 -0
- package/dist/features/debug/debug_stop.js +11 -0
- package/dist/features/debug/evaluate.d.ts +2 -0
- package/dist/features/debug/evaluate.js +48 -0
- package/dist/features/debug/get_stack.d.ts +2 -0
- package/dist/features/debug/get_stack.js +14 -0
- package/dist/features/debug/get_variables.d.ts +2 -0
- package/dist/features/debug/get_variables.js +26 -0
- package/dist/features/debug/halt.d.ts +2 -0
- package/dist/features/debug/halt.js +16 -0
- package/dist/features/debug/set_breakpoint.d.ts +2 -0
- package/dist/features/debug/set_breakpoint.js +30 -0
- package/dist/features/debug/step.d.ts +2 -0
- package/dist/features/debug/step.js +23 -0
- package/dist/features/install-mods/command.d.ts +6 -0
- package/dist/features/install-mods/command.js +18 -0
- package/dist/features/install-mods/install.d.ts +23 -0
- package/dist/features/install-mods/install.js +48 -0
- package/dist/features/install-skills/command.d.ts +7 -0
- package/dist/features/install-skills/command.js +22 -0
- package/dist/features/install-skills/install.d.ts +13 -0
- package/dist/features/install-skills/install.js +29 -0
- package/dist/features/probe/command.d.ts +5 -0
- package/dist/features/probe/command.js +44 -0
- package/dist/features/probe/kill_all_debuggers.d.ts +2 -0
- package/dist/features/probe/kill_all_debuggers.js +50 -0
- package/dist/features/probe/list_probes.d.ts +2 -0
- package/dist/features/probe/list_probes.js +8 -0
- package/dist/features/probe/program_project.d.ts +2 -0
- package/dist/features/probe/program_project.js +29 -0
- package/dist/features/probe/read_fault_status.d.ts +2 -0
- package/dist/features/probe/read_fault_status.js +19 -0
- package/dist/features/probe/read_memory.d.ts +2 -0
- package/dist/features/probe/read_memory.js +36 -0
- package/dist/features/probe/read_peripheral.d.ts +2 -0
- package/dist/features/probe/read_peripheral.js +58 -0
- package/dist/features/probe/reset_probe_usb.d.ts +2 -0
- package/dist/features/probe/reset_probe_usb.js +17 -0
- package/dist/features/probe/reset_target.d.ts +2 -0
- package/dist/features/probe/reset_target.js +16 -0
- package/dist/features/probe/target_info.d.ts +2 -0
- package/dist/features/probe/target_info.js +34 -0
- package/dist/features/project/analysis_static.d.ts +2 -0
- package/dist/features/project/analysis_static.js +19 -0
- package/dist/features/project/analysis_syntax_check.d.ts +2 -0
- package/dist/features/project/analysis_syntax_check.js +21 -0
- package/dist/features/project/command.d.ts +5 -0
- package/dist/features/project/command.js +64 -0
- package/dist/features/project/project_build.d.ts +2 -0
- package/dist/features/project/project_build.js +22 -0
- package/dist/features/project/project_download_template.d.ts +2 -0
- package/dist/features/project/project_download_template.js +34 -0
- package/dist/features/project/project_regenerate.d.ts +2 -0
- package/dist/features/project/project_regenerate.js +20 -0
- package/dist/features/project/project_sync_from_server.d.ts +2 -0
- package/dist/features/project/project_sync_from_server.js +13 -0
- package/dist/features/project/project_sync_to_server.d.ts +2 -0
- package/dist/features/project/project_sync_to_server.js +51 -0
- package/dist/features/project/stm32_cubetools_download_drivers.d.ts +2 -0
- package/dist/features/project/stm32_cubetools_download_drivers.js +19 -0
- package/dist/features/status/command.d.ts +2 -0
- package/dist/features/status/command.js +6 -0
- package/dist/features/status/get_status.d.ts +2 -0
- package/dist/features/status/get_status.js +44 -0
- package/dist/features/table/check.d.ts +38 -0
- package/dist/features/table/check.js +16 -23
- package/dist/features/table/command.d.ts +3 -0
- package/dist/features/table/command.js +4 -5
- package/dist/features/table/read.d.ts +28 -0
- package/dist/features/table/read.js +23 -3
- package/dist/features/tool/command.d.ts +35 -0
- package/dist/lib.d.ts +11 -0
- package/dist/lib.js +9 -0
- package/dist/stm32/actions/DebugAction.d.ts +63 -0
- package/dist/stm32/actions/DebugAction.js +105 -0
- package/dist/stm32/actions/DebugActionInfo.d.ts +7 -0
- package/dist/stm32/actions/DebugActionInfo.js +9 -0
- package/dist/stm32/actions/DebugActionListProbes.d.ts +9 -0
- package/dist/stm32/actions/DebugActionListProbes.js +10 -0
- package/dist/stm32/actions/DebugActionProgram.d.ts +28 -0
- package/dist/stm32/actions/DebugActionProgram.js +79 -0
- package/dist/stm32/actions/DebugActionRead.d.ts +38 -0
- package/dist/stm32/actions/DebugActionRead.js +59 -0
- package/dist/stm32/actions/DebugActionReset.d.ts +11 -0
- package/dist/stm32/actions/DebugActionReset.js +9 -0
- package/dist/stm32/actions/DebugActionServer.d.ts +7 -0
- package/dist/stm32/actions/DebugActionServer.js +9 -0
- package/dist/stm32/analysis.d.ts +19 -0
- package/dist/stm32/analysis.js +21 -0
- package/dist/stm32/build.d.ts +34 -0
- package/dist/stm32/build.js +79 -0
- package/dist/stm32/context.d.ts +23 -0
- package/dist/stm32/context.js +42 -0
- package/dist/stm32/daemon.d.ts +63 -0
- package/dist/stm32/daemon.js +202 -0
- package/dist/stm32/regen.d.ts +68 -0
- package/dist/stm32/regen.js +225 -0
- package/dist/stm32/session.d.ts +134 -0
- package/dist/stm32/session.js +374 -0
- package/dist/stm32/sync.d.ts +24 -0
- package/dist/stm32/sync.js +72 -0
- package/dist/stm32/tool.d.ts +43 -0
- package/dist/stm32/tool.js +20 -0
- package/dist/stm32/toolCommand.d.ts +50 -0
- package/dist/stm32/toolCommand.js +102 -0
- package/dist/stm32/utils/discover_tools.d.ts +24 -0
- package/dist/stm32/utils/discover_tools.js +121 -0
- package/dist/stm32/utils/downloadzip_utils.d.ts +57 -0
- package/dist/stm32/utils/downloadzip_utils.js +180 -0
- package/dist/stm32/utils/faultUtils.d.ts +14 -0
- package/dist/stm32/utils/faultUtils.js +69 -0
- package/dist/stm32/utils/findElf.d.ts +9 -0
- package/dist/stm32/utils/findElf.js +19 -0
- package/dist/stm32/utils/hash_utils.d.ts +19 -0
- package/dist/stm32/utils/hash_utils.js +28 -0
- package/dist/stm32/utils/listSyncFiles.d.ts +9 -0
- package/dist/stm32/utils/listSyncFiles.js +98 -0
- package/dist/stm32/utils/mcu_utils.d.ts +59 -0
- package/dist/stm32/utils/mcu_utils.js +99 -0
- package/dist/stm32/utils/project_utils.d.ts +54 -0
- package/dist/stm32/utils/project_utils.js +102 -0
- package/dist/stm32/utils/svdUtils.d.ts +31 -0
- package/dist/stm32/utils/svdUtils.js +30 -0
- package/dist/stm32/utils/truncate.d.ts +7 -0
- package/dist/stm32/utils/truncate.js +9 -0
- package/dist/stm32/utils/usb_utils.d.ts +5 -0
- package/dist/stm32/utils/usb_utils.js +23 -0
- package/dist/stm32/utils/user_code.d.ts +14 -0
- package/dist/stm32/utils/user_code.js +43 -0
- package/dist/stm32/utils/utils_mutex.d.ts +15 -0
- package/dist/stm32/utils/utils_mutex.js +93 -0
- package/dist/stm32/validate.d.ts +16 -0
- package/dist/stm32/validate.js +46 -0
- package/mods/claude/.claude-plugin/marketplace.json +8 -0
- package/mods/claude/turingcode-skills/.claude-plugin/plugin.json +5 -0
- package/mods/claude/turingcode-skills/skills/turingcode-cli/SKILL.md +50 -0
- package/mods/claude/turingcode-skills/skills/turingcode-cli/references/board-and-debug.md +22 -0
- package/mods/claude/turingcode-skills/skills/turingcode-cli/references/build-and-check.md +19 -0
- package/mods/claude/turingcode-skills/skills/turingcode-cli/references/new-project.md +9 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/SKILL.md +43 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/concurrency.md +42 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/init-and-timing.md +35 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/input-and-bounds.md +34 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/registers-and-units.md +35 -0
- package/package.json +11 -3
- package/dist/features/clock/command.js +0 -63
package/dist/core/mcp.js
CHANGED
|
@@ -6,6 +6,7 @@ export const TOKEN_HELP = [
|
|
|
6
6
|
"`export TURINGCODE_TOKEN=<token>` to ~/.bashrc or ~/.zshrc and open a new shell",
|
|
7
7
|
"(Windows: `setx TURINGCODE_TOKEN <token>`, then a new terminal). For an AI agent, set it in the",
|
|
8
8
|
"agent's environment settings (e.g. \"env\" in Claude Code's settings.json). Keep it out of repositories.",
|
|
9
|
+
"Or put `TURINGCODE_TOKEN=<token>` in a git-ignored .env in the working directory; a set variable wins.",
|
|
9
10
|
`TURINGCODE_URL overrides the gateway (default ${DEFAULT_URL}).`,
|
|
10
11
|
].join("\n");
|
|
11
12
|
/**
|
|
@@ -18,27 +19,38 @@ export const TOKEN_HELP = [
|
|
|
18
19
|
* @param {Record<string, unknown>} params - Method params.
|
|
19
20
|
* @param {string} token - Bearer token for the gateway.
|
|
20
21
|
* @param {string} [url=DEFAULT_URL] - Gateway endpoint.
|
|
22
|
+
* @param {number} [timeoutMs=125000] - Time before the request fails; one HTTP 429 reply is retried after 2 s.
|
|
21
23
|
* @returns {Promise<any>} The reply's `result`.
|
|
22
|
-
* @throws {Error} When the gateway cannot be reached, on HTTP errors, or on a JSON-RPC error reply.
|
|
24
|
+
* @throws {Error} When the gateway cannot be reached or times out, on HTTP errors, or on a JSON-RPC error reply.
|
|
23
25
|
*/
|
|
24
|
-
async function postGateway(method, params, token, url = DEFAULT_URL) {
|
|
26
|
+
async function postGateway(method, params, token, url = DEFAULT_URL, timeoutMs = 125_000) {
|
|
27
|
+
const post = () => fetch(url, {
|
|
28
|
+
method: "POST",
|
|
29
|
+
headers: {
|
|
30
|
+
"Authorization": `Bearer ${token}`,
|
|
31
|
+
"Content-Type": "application/json",
|
|
32
|
+
"Accept": "application/json, text/event-stream",
|
|
33
|
+
},
|
|
34
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
|
|
35
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
36
|
+
});
|
|
25
37
|
let response;
|
|
38
|
+
let body;
|
|
26
39
|
try {
|
|
27
|
-
response = await
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
},
|
|
34
|
-
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params }),
|
|
35
|
-
});
|
|
40
|
+
response = await post();
|
|
41
|
+
if (response.status === 429) {
|
|
42
|
+
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
43
|
+
response = await post();
|
|
44
|
+
}
|
|
45
|
+
body = await response.text();
|
|
36
46
|
}
|
|
37
47
|
catch (e) {
|
|
48
|
+
if (e.name === "TimeoutError") {
|
|
49
|
+
throw new Error(`the gateway did not answer within ${timeoutMs / 1000} s (builds are incremental, so run it again)`);
|
|
50
|
+
}
|
|
38
51
|
const cause = e.cause;
|
|
39
52
|
throw new Error(`cannot reach the gateway at ${url}: ${cause?.code ?? cause?.message ?? e.message}`);
|
|
40
53
|
}
|
|
41
|
-
const body = await response.text();
|
|
42
54
|
if (!response.ok) {
|
|
43
55
|
throw new Error(`gateway HTTP ${response.status}: ${body.trim() || response.statusText}`);
|
|
44
56
|
}
|
|
@@ -51,11 +63,40 @@ async function postGateway(method, params, token, url = DEFAULT_URL) {
|
|
|
51
63
|
* @param {Record<string, unknown>} args - Tool arguments.
|
|
52
64
|
* @param {string} token - Bearer token for the gateway.
|
|
53
65
|
* @param {string} [url=DEFAULT_URL] - Gateway endpoint.
|
|
66
|
+
* @param {number} [timeoutMs=125000] - Time before the request fails.
|
|
54
67
|
* @returns {Promise<ToolResult>} The tool's text content and error flag.
|
|
55
|
-
* @throws {Error} On HTTP errors or a JSON-RPC error reply.
|
|
68
|
+
* @throws {Error} On HTTP errors, a timeout or a JSON-RPC error reply.
|
|
69
|
+
*/
|
|
70
|
+
export async function callTool(name, args, token, url = DEFAULT_URL, timeoutMs) {
|
|
71
|
+
return toToolResult(await postGateway("tools/call", { name, arguments: args }, token, url, timeoutMs));
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Calls one tool on the gateway and returns its text, throwing when the tool reports an error.
|
|
75
|
+
*
|
|
76
|
+
* @param {string} name - Tool name.
|
|
77
|
+
* @param {object} args - Tool arguments.
|
|
78
|
+
* @param {string} token - Bearer token for the gateway.
|
|
79
|
+
* @param {string} [url=DEFAULT_URL] - Gateway endpoint.
|
|
80
|
+
* @param {number} [timeoutMs] - Time before the request fails.
|
|
81
|
+
* @returns {Promise<string>} The tool's text content.
|
|
82
|
+
* @throws {Error} With the tool's text when it reports an error, or on a failed request.
|
|
56
83
|
*/
|
|
57
|
-
export async function
|
|
58
|
-
|
|
84
|
+
export async function callToolText(name, args, token, url = DEFAULT_URL, timeoutMs) {
|
|
85
|
+
const result = await callTool(name, args, token, url, timeoutMs);
|
|
86
|
+
if (result.isError) {
|
|
87
|
+
throw new Error(result.text);
|
|
88
|
+
}
|
|
89
|
+
return result.text;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* A tool reply without the server's trailing footer lines (`Credits: ...`), for replies that are parsed
|
|
93
|
+
* (a link, JSON) rather than shown.
|
|
94
|
+
*
|
|
95
|
+
* @param {string} text - Tool reply.
|
|
96
|
+
* @returns {string} The reply body, trimmed.
|
|
97
|
+
*/
|
|
98
|
+
export function replyBody(text) {
|
|
99
|
+
return text.trim().replace(/(\r?\n\s*Credits:[^\n]*)+$/, "").trim();
|
|
59
100
|
}
|
|
60
101
|
/**
|
|
61
102
|
* Lists the tools the gateway token can reach (the gateway applies its allow/deny policy).
|
|
@@ -97,27 +138,13 @@ export function parseRpcReply(body) {
|
|
|
97
138
|
* @param {any} result - The JSON-RPC result of `tools/call`.
|
|
98
139
|
* @returns {ToolResult} The joined text content and error flag.
|
|
99
140
|
*/
|
|
100
|
-
function toToolResult(result) {
|
|
141
|
+
export function toToolResult(result) {
|
|
101
142
|
const content = result?.content ?? [];
|
|
102
143
|
return {
|
|
103
144
|
text: content.filter(c => c.type === "text").map(c => c.text).join("\n"),
|
|
104
145
|
isError: Boolean(result?.isError),
|
|
105
146
|
};
|
|
106
147
|
}
|
|
107
|
-
/**
|
|
108
|
-
* Turns a gateway `tools/call` reply (plain JSON or an SSE stream) into a ToolResult.
|
|
109
|
-
*
|
|
110
|
-
* @param {string} body - Raw response body.
|
|
111
|
-
* @returns {ToolResult} The joined text content and error flag.
|
|
112
|
-
* @throws {Error} When the reply holds no JSON-RPC response or carries an error.
|
|
113
|
-
*
|
|
114
|
-
* @example
|
|
115
|
-
* parseToolReply('data: {"jsonrpc":"2.0","id":1,"result":{"content":[{"type":"text","text":"ok"}]}}');
|
|
116
|
-
* // { text: "ok", isError: false }
|
|
117
|
-
*/
|
|
118
|
-
export function parseToolReply(body) {
|
|
119
|
-
return toToolResult(parseRpcReply(body));
|
|
120
|
-
}
|
|
121
148
|
/**
|
|
122
149
|
* Reads the gateway token and endpoint from TURINGCODE_TOKEN and TURINGCODE_URL.
|
|
123
150
|
*
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
/** A MongoDB-style query. */
|
|
2
|
+
export type Query = Record<string, unknown>;
|
|
3
|
+
/** A cell as a query sees it: text, `true`/`false` for booleans, items for lists, null when missing. */
|
|
4
|
+
export type Value = string | boolean | string[] | null;
|
|
5
|
+
/** A row as a query sees it: column name -> value. */
|
|
6
|
+
export type Doc = Record<string, Value>;
|
|
7
|
+
/** Operators that need the caller's data (other tables, the document text, exact arithmetic over the row). */
|
|
8
|
+
export interface QueryHooks {
|
|
9
|
+
/** `$ref`: whether the value exists in the referenced column. */
|
|
10
|
+
ref(ref: string, value: string): boolean;
|
|
11
|
+
/** `$mentioned`: whether the value is mentioned in the targets. */
|
|
12
|
+
mentioned(targets: string[], value: string): boolean;
|
|
13
|
+
/** `$calc`: whether the comparison holds for the row (true when it cannot be evaluated). */
|
|
14
|
+
calc(param: unknown, doc: Doc): boolean;
|
|
15
|
+
}
|
|
16
|
+
/** A query rewritten for sift, with what it uses and what is wrong with it. */
|
|
17
|
+
export interface PreparedQuery {
|
|
18
|
+
query: Query;
|
|
19
|
+
/** Column names the query reads. */
|
|
20
|
+
fields: string[];
|
|
21
|
+
/** Every `$ref` value. */
|
|
22
|
+
refs: string[];
|
|
23
|
+
/** Every `$mentioned` target. */
|
|
24
|
+
mentions: string[];
|
|
25
|
+
/** Every `$calc` parameter. */
|
|
26
|
+
calcs: unknown[];
|
|
27
|
+
/** Readable problems; the query must not be run when there are any. */
|
|
28
|
+
problems: string[];
|
|
29
|
+
}
|
|
30
|
+
/** Operators on one column's value. */
|
|
31
|
+
export declare const FIELD_OPERATORS: string[];
|
|
32
|
+
/** Operators on a whole row (besides column names). */
|
|
33
|
+
export declare const ROW_OPERATORS: string[];
|
|
34
|
+
/**
|
|
35
|
+
* Checks a row query and rewrites it for sift: a column's `$or`/`$and`/`$nor`
|
|
36
|
+
* is lifted to the row (sift allows them only there), and `$every`/`$length`
|
|
37
|
+
* sub-queries are prepared too.
|
|
38
|
+
*
|
|
39
|
+
* @param {unknown} query - The query from the contract.
|
|
40
|
+
* @returns {PreparedQuery} The rewritten query, what it uses and its problems.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* prepareQuery({ Kind: { $in: ["a", "b"] }, Qty: { $gte: 1 } }).fields; // ["Kind", "Qty"]
|
|
44
|
+
*/
|
|
45
|
+
export declare function prepareQuery(query: unknown): PreparedQuery;
|
|
46
|
+
/**
|
|
47
|
+
* Compiles a prepared query into a row tester. `$unique` remembers the values
|
|
48
|
+
* it has seen, so run one tester over the rows in order.
|
|
49
|
+
*
|
|
50
|
+
* @param {Query} query - `prepareQuery(...).query` with no problems.
|
|
51
|
+
* @param {QueryHooks} hooks - `$ref`, `$mentioned` and `$calc`.
|
|
52
|
+
* @returns {(doc: Doc) => boolean} Whether a row matches.
|
|
53
|
+
* @throws {Error} When sift rejects the query, e.g. an invalid `$regex`.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* compileQuery(prepareQuery({ Qty: { $gte: 1 } }).query, hooks)({ Qty: "2" }); // true
|
|
57
|
+
*/
|
|
58
|
+
export declare function compileQuery(query: Query, hooks: QueryHooks): (doc: Doc) => boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Prepares and compiles a query in one step (for queries `contract check` already validated).
|
|
61
|
+
*
|
|
62
|
+
* @param {unknown} query - The query from the contract.
|
|
63
|
+
* @param {QueryHooks} hooks - `$ref`, `$mentioned` and `$calc`.
|
|
64
|
+
* @returns {(doc: Doc) => boolean} Whether a row matches.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* queryTester({ Qty: { $gte: 1 } }, hooks)({ Qty: "2" }); // true
|
|
68
|
+
*/
|
|
69
|
+
export declare const queryTester: (query: unknown, hooks: QueryHooks) => ((doc: Doc) => boolean);
|
package/dist/core/query.js
CHANGED
|
@@ -233,3 +233,14 @@ export function compileQuery(query, hooks) {
|
|
|
233
233
|
};
|
|
234
234
|
return createQueryTester(query, { operations });
|
|
235
235
|
}
|
|
236
|
+
/**
|
|
237
|
+
* Prepares and compiles a query in one step (for queries `contract check` already validated).
|
|
238
|
+
*
|
|
239
|
+
* @param {unknown} query - The query from the contract.
|
|
240
|
+
* @param {QueryHooks} hooks - `$ref`, `$mentioned` and `$calc`.
|
|
241
|
+
* @returns {(doc: Doc) => boolean} Whether a row matches.
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* queryTester({ Qty: { $gte: 1 } }, hooks)({ Qty: "2" }); // true
|
|
245
|
+
*/
|
|
246
|
+
export const queryTester = (query, hooks) => compileQuery(prepareQuery(query).query, hooks);
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Prints a tool result, as compact text or (with json) as one-line JSON,
|
|
3
|
+
* and sets the process exit code from its status.
|
|
4
|
+
*
|
|
5
|
+
* @param {{status: string}} result - The tool result.
|
|
6
|
+
* @param {string} text - Its compact text form.
|
|
7
|
+
* @param {boolean} [json] - Print the full JSON object instead.
|
|
8
|
+
*/
|
|
9
|
+
export declare function report(result: {
|
|
10
|
+
status: string;
|
|
11
|
+
}, text: string, json?: boolean): void;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { TablesDoc } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Parses CSV text (RFC 4180: quoted fields, doubled quotes, CRLF).
|
|
4
|
+
*
|
|
5
|
+
* @param {string} text - The CSV text.
|
|
6
|
+
* @returns {string[][]} Records; fields are not trimmed.
|
|
7
|
+
* @throws {Error} On an unterminated quoted field.
|
|
8
|
+
*/
|
|
9
|
+
export declare function parseCsv(text: string): string[][];
|
|
10
|
+
/**
|
|
11
|
+
* Reads a folder of CSV tables: holding one `<Table>.csv` per table.
|
|
12
|
+
*
|
|
13
|
+
* @param {string} dir - The folder.
|
|
14
|
+
* @param {number} maxRows - `bounds.max_table_rows`.
|
|
15
|
+
* @returns {TablesDoc} The tables.
|
|
16
|
+
* @throws {BoundError} When a table has more than maxRows rows.
|
|
17
|
+
*/
|
|
18
|
+
export declare function parseCsvFolder(dir: string, maxRows: number): TablesDoc;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { TablesDoc } from "./types.js";
|
|
2
|
+
/**
|
|
3
|
+
* Splits one Markdown table line into trimmed cells; `\|` is a literal pipe.
|
|
4
|
+
*
|
|
5
|
+
* @param {string} line - A line starting with `|`.
|
|
6
|
+
* @returns {string[]} The cells.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* splitRow("| a | b \\| c |"); // ["a", "b | c"]
|
|
10
|
+
*/
|
|
11
|
+
export declare function splitRow(line: string): string[];
|
|
12
|
+
/**
|
|
13
|
+
* Reads the tables of a Markdown document. A table is the first pipe
|
|
14
|
+
* table under a `## <Name>` heading; pipe tables inside fenced code blocks
|
|
15
|
+
* are ignored.
|
|
16
|
+
*
|
|
17
|
+
* @param {string} text - The Markdown text.
|
|
18
|
+
* @param {number} maxRows - `bounds.max_table_rows`.
|
|
19
|
+
* @returns {TablesDoc} The tables.
|
|
20
|
+
* @throws {BoundError} When a table has more than maxRows rows.
|
|
21
|
+
*/
|
|
22
|
+
export declare function parseMarkdown(text: string, maxRows: number): TablesDoc;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/** One table read from a Markdown or CSV document. */
|
|
2
|
+
export interface ParsedTable {
|
|
3
|
+
name: string;
|
|
4
|
+
header: string[];
|
|
5
|
+
rows: string[][];
|
|
6
|
+
/** 1-based source line of each row, for messages. */
|
|
7
|
+
rowLines: number[];
|
|
8
|
+
}
|
|
9
|
+
/** A document of tables, plus the problems found while reading it. */
|
|
10
|
+
export interface TablesDoc {
|
|
11
|
+
tables: Map<string, ParsedTable>;
|
|
12
|
+
/** Table names that appear more than once. */
|
|
13
|
+
duplicates: string[];
|
|
14
|
+
/** `[table, message]` for tables whose layout could not be read. */
|
|
15
|
+
malformed: [string, string][];
|
|
16
|
+
/** Full text, for citation checks. */
|
|
17
|
+
text: string;
|
|
18
|
+
/**
|
|
19
|
+
* The text without the named tables (their header, separator and rows).
|
|
20
|
+
*
|
|
21
|
+
* @param {string[]} sections - Table names to leave out.
|
|
22
|
+
* @returns {string} The remaining text.
|
|
23
|
+
*/
|
|
24
|
+
textOutside(sections: string[]): string;
|
|
25
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { Diagnostic } from "./diagnostics.js";
|
|
2
|
+
export interface ContractCheckResult {
|
|
3
|
+
status: "VALID" | "INVALID_CONTRACT" | "MISSING_DATA";
|
|
4
|
+
contract: string;
|
|
5
|
+
/** Labels of the contract and every dependency that was checked. */
|
|
6
|
+
checked: string[];
|
|
7
|
+
diagnostics: Diagnostic[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* contract check: validates a contract and the contracts it depends on.
|
|
11
|
+
*
|
|
12
|
+
* @param {string} path - Contract file.
|
|
13
|
+
* @returns {ContractCheckResult} Status and diagnostics.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* contractCheck("orders.contract.json").status; // "VALID"
|
|
17
|
+
*/
|
|
18
|
+
export declare function contractCheck(path: string): ContractCheckResult;
|
|
19
|
+
/**
|
|
20
|
+
* Compact text for a contract check (or describe) result.
|
|
21
|
+
*
|
|
22
|
+
* @param {{status: string, contract: string, diagnostics: Diagnostic[], checked?: string[]}} r - The result.
|
|
23
|
+
* @returns {string} Status line, then the diagnostics.
|
|
24
|
+
*/
|
|
25
|
+
export declare function formatContractCheck(r: {
|
|
26
|
+
status: string;
|
|
27
|
+
contract: string;
|
|
28
|
+
diagnostics: Diagnostic[];
|
|
29
|
+
checked?: string[];
|
|
30
|
+
}): string;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { formatDiagnostics, sortDiagnostics } from "./diagnostics.js";
|
|
2
|
-
import { loadContract } from "./load.js";
|
|
2
|
+
import { failedLoadStatus, loadContract } from "./load.js";
|
|
3
3
|
/** Labels of a loaded contract and all its dependencies. */
|
|
4
4
|
const labels = (c) => [c.label, ...[...c.deps.values()].flatMap(labels)];
|
|
5
5
|
/**
|
|
@@ -14,8 +14,7 @@ const labels = (c) => [c.label, ...[...c.deps.values()].flatMap(labels)];
|
|
|
14
14
|
export function contractCheck(path) {
|
|
15
15
|
const diags = [];
|
|
16
16
|
const loaded = loadContract(path, diags);
|
|
17
|
-
const status =
|
|
18
|
-
: loaded.contract ? "VALID" : "INVALID_CONTRACT";
|
|
17
|
+
const status = loaded.contract ? "VALID" : failedLoadStatus(diags);
|
|
19
18
|
return { status, contract: loaded.label, checked: [...new Set(labels(loaded))], diagnostics: sortDiagnostics(diags) };
|
|
20
19
|
}
|
|
21
20
|
/**
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CommandModule } from "yargs";
|
|
2
|
+
export declare const CONTRACT: {
|
|
3
|
+
readonly type: "string";
|
|
4
|
+
readonly demandOption: true;
|
|
5
|
+
readonly describe: "Contract JSON (required)";
|
|
6
|
+
};
|
|
7
|
+
export declare const JSON_FLAG: {
|
|
8
|
+
readonly type: "boolean";
|
|
9
|
+
readonly describe: "Full result as one-line JSON";
|
|
10
|
+
};
|
|
11
|
+
declare const contract: CommandModule;
|
|
12
|
+
export default contract;
|
|
@@ -4,70 +4,70 @@ import { describeContract } from "./describe.js";
|
|
|
4
4
|
export const CONTRACT = { type: "string", demandOption: true, describe: "Contract JSON (required)" };
|
|
5
5
|
export const JSON_FLAG = { type: "boolean", describe: "Full result as one-line JSON" };
|
|
6
6
|
/** The contract format in brief, for `contract --help`. Only tables, and each table's and column's name, are required. */
|
|
7
|
-
const FORMAT = `Contract JSON. Only "tables" and each table's and column's "name" are required; every other
|
|
8
|
-
field is optional. "(default)" marks the value used when a field is left out; other values are examples.
|
|
9
|
-
{
|
|
10
|
-
"name": "shop.orders", "version": "1.0.0", "description": "...", // shown in output
|
|
11
|
-
"format": "markdown", // (default), or "format": "csv": one <table>.csv per table. The tables
|
|
12
|
-
// given with --tables / --dep must be in their own contract's format
|
|
13
|
-
"missing_values": [""], // (default) cell texts that count as empty (null in queries), e.g. ["", "-"].
|
|
14
|
-
// Keep a marker like "-" out of it when "-" is a real value: "-" allowed
|
|
15
|
-
// but an empty cell forbidden is "required": true plus
|
|
16
|
-
// "match": {"$or": [{"$eq": "-"}, {"$every": {"$ref": "Tests.ID"}}]}
|
|
17
|
-
"allow_extra_tables": false, // (default) true: tables not declared here are allowed
|
|
18
|
-
"depends_on": { "customers": "customers.contract.json" }, // path absolute or relative to this contract;
|
|
19
|
-
// its tables passed with --dep customers=<file>, referenced as "customers:Table.Column"
|
|
20
|
-
"tables": [{
|
|
21
|
-
"name": "Orders",
|
|
22
|
-
"required": true, // (default) the table must be in the document
|
|
23
|
-
"primary_key": "Order_ID", // or ["A", "B"]; always filled and unique, no "required" needed
|
|
24
|
-
"ordered": false, // (default) true: columns must be in the declared order
|
|
25
|
-
"allow_extra_columns": false, // (default) true: columns not declared here are allowed
|
|
26
|
-
"columns": [{
|
|
27
|
-
"name": "Total",
|
|
28
|
-
"type": "number", // string (default) | integer | number | boolean | date (YYYY-MM-DD) | list
|
|
29
|
-
"required": false, // (default) true: every cell must be filled (an empty cell fails
|
|
30
|
-
// MISSING_VALUE); the column itself must always be in the header
|
|
31
|
-
"separator": ",", // (default) list item separator
|
|
32
|
-
"match": { "$gte": 0, "$lte": 100 } // condition every filled cell meets (MongoDB query);
|
|
33
|
-
// empty cells are skipped: use "required" to forbid them
|
|
34
|
-
}],
|
|
35
|
-
"rules": [{ // exactly one of require, count
|
|
36
|
-
"id": "...", "description": "...", // shown in findings; description replaces the "expected" text
|
|
37
|
-
"severity": "error", // (default) or "warning": reported as WARN, does not fail the check
|
|
38
|
-
"on": "Table", // or "alias:Table"; default: the table the rule is in
|
|
39
|
-
"when": { "Status": "shipped" }, // rows the rule applies to; default: all rows
|
|
40
|
-
"require": { "Tracking": { "$ne": null } }, // row query each of them matches
|
|
41
|
-
"count": { "$gte": 1 } // or: condition on how many there are
|
|
42
|
-
}]
|
|
43
|
-
}]
|
|
44
|
-
}
|
|
45
|
-
Queries are MongoDB syntax: cells are text, an empty cell is null, a list is an array of its items
|
|
46
|
-
(split on "separator", trimmed; an operator matches if any item does, so {"$eq": "x"} means "has item x"
|
|
47
|
-
and {"$ne": "x"} means "no item is x"). Operators:
|
|
48
|
-
$eq $ne $in $nin $not $regex $options $or $and $nor (also inside a column's condition)
|
|
49
|
-
$gt $gte $lt $lte exact number comparison, or date comparison with a "YYYY-MM-DD" bound
|
|
50
|
-
$ref "Table.Column" value exists there (or "alias:Table.Column" of a dependency)
|
|
51
|
-
$every {...} every list item meets the condition
|
|
52
|
-
$length {...} length (chars, or list items) meets the condition
|
|
53
|
-
$unique true no two rows share the value; in a row query ["A", "B"]: the combination
|
|
54
|
-
$mentioned "Table.Column" | [...] | "*" value appears there as a word; "*": anywhere in the
|
|
55
|
-
document outside the value's own table (prose and other tables both count)
|
|
56
|
-
$calc "Qty * Price == Total" | {"expr": ..., "tolerance": 0.01} row query only; exact arithmetic
|
|
57
|
-
Codes: each failing operator reports its own: $eq $in → BAD_VALUE, $regex → PATTERN, $gt.. → OUT_OF_RANGE,
|
|
58
|
-
$length → BAD_LENGTH, $ref → UNKNOWN_REF, $unique → DUPLICATE_KEY, $mentioned → NOT_MENTIONED,
|
|
59
|
-
{"$ne": null} in a rule → REQUIRED_IF, a wrong type → BAD_TYPE, a rule's other queries → MATCH.
|
|
60
|
-
A value failing every branch of an $or reports the branches' code when they agree, else NONE_OF
|
|
61
|
-
(each branch's code in the message, and in --json "alternatives").
|
|
62
|
-
Examples:
|
|
63
|
-
"match": { "$regex": "^O-[0-9]+$" }
|
|
64
|
-
"match": { "$in": ["open", "paid", "shipped"] }
|
|
65
|
-
"match": { "$length": { "$gte": 1 }, "$every": { "$ref": "Lines.Line_ID" } }
|
|
66
|
-
"match": { "$or": [{ "$eq": "always" }, { "$ref": "Scenarios.ID" }] }
|
|
67
|
-
"match": { "$ref": "customers:Customers.ID" } // a column of the dependency "customers"
|
|
68
|
-
{ "when": { "Kind": { "$in": ["resistor", "capacitor"] } }, "require": { "Value": { "$ne": null } } }
|
|
69
|
-
{ "require": { "$calc": { "expr": "Qty * Price == Total", "tolerance": "0.005" } } }
|
|
70
|
-
{ "on": "bom:Parts", "require": { "Ref": { "$ref": "Stock.Ref" } } }
|
|
7
|
+
const FORMAT = `Contract JSON. Only "tables" and each table's and column's "name" are required; every other
|
|
8
|
+
field is optional. "(default)" marks the value used when a field is left out; other values are examples.
|
|
9
|
+
{
|
|
10
|
+
"name": "shop.orders", "version": "1.0.0", "description": "...", // shown in output
|
|
11
|
+
"format": "markdown", // (default), or "format": "csv": one <table>.csv per table. The tables
|
|
12
|
+
// given with --tables / --dep must be in their own contract's format
|
|
13
|
+
"missing_values": [""], // (default) cell texts that count as empty (null in queries), e.g. ["", "-"].
|
|
14
|
+
// Keep a marker like "-" out of it when "-" is a real value: "-" allowed
|
|
15
|
+
// but an empty cell forbidden is "required": true plus
|
|
16
|
+
// "match": {"$or": [{"$eq": "-"}, {"$every": {"$ref": "Tests.ID"}}]}
|
|
17
|
+
"allow_extra_tables": false, // (default) true: tables not declared here are allowed
|
|
18
|
+
"depends_on": { "customers": "customers.contract.json" }, // path absolute or relative to this contract;
|
|
19
|
+
// its tables passed with --dep customers=<file>, referenced as "customers:Table.Column"
|
|
20
|
+
"tables": [{
|
|
21
|
+
"name": "Orders",
|
|
22
|
+
"required": true, // (default) the table must be in the document
|
|
23
|
+
"primary_key": "Order_ID", // or ["A", "B"]; always filled and unique, no "required" needed
|
|
24
|
+
"ordered": false, // (default) true: columns must be in the declared order
|
|
25
|
+
"allow_extra_columns": false, // (default) true: columns not declared here are allowed
|
|
26
|
+
"columns": [{
|
|
27
|
+
"name": "Total",
|
|
28
|
+
"type": "number", // string (default) | integer | number | boolean | date (YYYY-MM-DD) | list
|
|
29
|
+
"required": false, // (default) true: every cell must be filled (an empty cell fails
|
|
30
|
+
// MISSING_VALUE); the column itself must always be in the header
|
|
31
|
+
"separator": ",", // (default) list item separator
|
|
32
|
+
"match": { "$gte": 0, "$lte": 100 } // condition every filled cell meets (MongoDB query);
|
|
33
|
+
// empty cells are skipped: use "required" to forbid them
|
|
34
|
+
}],
|
|
35
|
+
"rules": [{ // exactly one of require, count
|
|
36
|
+
"id": "...", "description": "...", // shown in findings; description replaces the "expected" text
|
|
37
|
+
"severity": "error", // (default) or "warning": reported as WARN, does not fail the check
|
|
38
|
+
"on": "Table", // or "alias:Table"; default: the table the rule is in
|
|
39
|
+
"when": { "Status": "shipped" }, // rows the rule applies to; default: all rows
|
|
40
|
+
"require": { "Tracking": { "$ne": null } }, // row query each of them matches
|
|
41
|
+
"count": { "$gte": 1 } // or: condition on how many there are
|
|
42
|
+
}]
|
|
43
|
+
}]
|
|
44
|
+
}
|
|
45
|
+
Queries are MongoDB syntax: cells are text, an empty cell is null, a list is an array of its items
|
|
46
|
+
(split on "separator", trimmed; an operator matches if any item does, so {"$eq": "x"} means "has item x"
|
|
47
|
+
and {"$ne": "x"} means "no item is x"). Operators:
|
|
48
|
+
$eq $ne $in $nin $not $regex $options $or $and $nor (also inside a column's condition)
|
|
49
|
+
$gt $gte $lt $lte exact number comparison, or date comparison with a "YYYY-MM-DD" bound
|
|
50
|
+
$ref "Table.Column" value exists there (or "alias:Table.Column" of a dependency)
|
|
51
|
+
$every {...} every list item meets the condition
|
|
52
|
+
$length {...} length (chars, or list items) meets the condition
|
|
53
|
+
$unique true no two rows share the value; in a row query ["A", "B"]: the combination
|
|
54
|
+
$mentioned "Table.Column" | [...] | "*" value appears there as a word; "*": anywhere in the
|
|
55
|
+
document outside the value's own table (prose and other tables both count)
|
|
56
|
+
$calc "Qty * Price == Total" | {"expr": ..., "tolerance": 0.01} row query only; exact arithmetic
|
|
57
|
+
Codes: each failing operator reports its own: $eq $in → BAD_VALUE, $regex → PATTERN, $gt.. → OUT_OF_RANGE,
|
|
58
|
+
$length → BAD_LENGTH, $ref → UNKNOWN_REF, $unique → DUPLICATE_KEY, $mentioned → NOT_MENTIONED,
|
|
59
|
+
{"$ne": null} in a rule → REQUIRED_IF, a wrong type → BAD_TYPE, a rule's other queries → MATCH.
|
|
60
|
+
A value failing every branch of an $or reports the branches' code when they agree, else NONE_OF
|
|
61
|
+
(each branch's code in the message, and in --json "alternatives").
|
|
62
|
+
Examples:
|
|
63
|
+
"match": { "$regex": "^O-[0-9]+$" }
|
|
64
|
+
"match": { "$in": ["open", "paid", "shipped"] }
|
|
65
|
+
"match": { "$length": { "$gte": 1 }, "$every": { "$ref": "Lines.Line_ID" } }
|
|
66
|
+
"match": { "$or": [{ "$eq": "always" }, { "$ref": "Scenarios.ID" }] }
|
|
67
|
+
"match": { "$ref": "customers:Customers.ID" } // a column of the dependency "customers"
|
|
68
|
+
{ "when": { "Kind": { "$in": ["resistor", "capacitor"] } }, "require": { "Value": { "$ne": null } } }
|
|
69
|
+
{ "require": { "$calc": { "expr": "Qty * Price == Total", "tolerance": "0.005" } } }
|
|
70
|
+
{ "on": "bom:Parts", "require": { "Ref": { "$ref": "Stock.Ref" } } }
|
|
71
71
|
{ "severity": "warning", "require": { "ID": { "$mentioned": "*" } } }`;
|
|
72
72
|
const check = {
|
|
73
73
|
command: "check",
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Diagnostic } from "./diagnostics.js";
|
|
2
|
+
import { Contract } from "./model.js";
|
|
3
|
+
export interface DescribeResult {
|
|
4
|
+
status: "OK" | "INVALID_CONTRACT" | "MISSING_DATA";
|
|
5
|
+
contract: string;
|
|
6
|
+
markdown?: string;
|
|
7
|
+
diagnostics: Diagnostic[];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Renders a valid contract as a Markdown guide to the tables it expects.
|
|
11
|
+
*
|
|
12
|
+
* @param {Contract} c - The contract.
|
|
13
|
+
* @returns {string} The guide.
|
|
14
|
+
*/
|
|
15
|
+
export declare function renderGuide(c: Contract): string;
|
|
16
|
+
/**
|
|
17
|
+
* contract describe: validates the contract, then renders its guide.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} path - Contract file.
|
|
20
|
+
* @returns {DescribeResult} OK with the Markdown, or the contract's problems.
|
|
21
|
+
*/
|
|
22
|
+
export declare function describeContract(path: string): DescribeResult;
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/** One finding of `contract check` or `table check`. */
|
|
2
|
+
export interface Diagnostic {
|
|
3
|
+
severity: "FAIL" | "WARN";
|
|
4
|
+
code: Code;
|
|
5
|
+
/** Table name, or "-" for document- or contract-level findings. */
|
|
6
|
+
table: string;
|
|
7
|
+
/** Row label (primary key or `#n`), or "-". */
|
|
8
|
+
row: string;
|
|
9
|
+
/** Column name or contract field path, or "-". */
|
|
10
|
+
column: string;
|
|
11
|
+
message: string;
|
|
12
|
+
found: string;
|
|
13
|
+
expected: string;
|
|
14
|
+
fix: string;
|
|
15
|
+
/** ID of the contract rule that failed, if a rule did. */
|
|
16
|
+
rule?: string;
|
|
17
|
+
/** For NONE_OF: why each `$or` branch failed, in order. */
|
|
18
|
+
alternatives?: Alternative[];
|
|
19
|
+
}
|
|
20
|
+
/** Why one branch of a failing `$or` failed. */
|
|
21
|
+
export interface Alternative {
|
|
22
|
+
code: Code;
|
|
23
|
+
message: string;
|
|
24
|
+
}
|
|
25
|
+
/** Every code, with its fix hint. */
|
|
26
|
+
declare const HINTS: {
|
|
27
|
+
readonly "CONTRACT.BAD_JSON": "Fix the JSON syntax of the contract file.";
|
|
28
|
+
readonly "CONTRACT.BAD_FIELD": "Give the field the type or value named in 'expected'; remove unknown fields.";
|
|
29
|
+
readonly "CONTRACT.BAD_REGEX": "Write a valid JavaScript regular expression.";
|
|
30
|
+
readonly "CONTRACT.BAD_EXPRESSION": "Write an arithmetic expression over this table's number columns.";
|
|
31
|
+
readonly "CONTRACT.UNKNOWN_TABLE": "Name a table the contract declares.";
|
|
32
|
+
readonly "CONTRACT.UNKNOWN_COLUMN": "Name a column the table declares.";
|
|
33
|
+
readonly "CONTRACT.UNKNOWN_DEPENDENCY": "Declare the alias in depends_on.";
|
|
34
|
+
readonly "CONTRACT.DUPLICATE": "Give each table, column and rule a unique name.";
|
|
35
|
+
readonly "CONTRACT.CONFLICT": "Make the settings agree (e.g. minimum <= maximum, constraint fits the type).";
|
|
36
|
+
readonly "CONTRACT.CYCLE": "Remove the circular depends_on.";
|
|
37
|
+
readonly "CONTRACT.MISSING_FILE": "Check the path; the file must exist and be readable.";
|
|
38
|
+
readonly MISSING_TABLE: "Add the table under a '## <name>' heading (Markdown) or as <name>.csv.";
|
|
39
|
+
readonly EXTRA_TABLE: "Remove the table, or declare it in the contract.";
|
|
40
|
+
readonly DUPLICATE_TABLE: "Write each table once.";
|
|
41
|
+
readonly BAD_TABLE: "Write the table as a header row, a '|---|' separator row, then data rows.";
|
|
42
|
+
readonly MISSING_COLUMN: "Add the column to the header.";
|
|
43
|
+
readonly EXTRA_COLUMN: "Remove the column, or declare it in the contract.";
|
|
44
|
+
readonly DUPLICATE_COLUMN: "Name each column once.";
|
|
45
|
+
readonly COLUMN_ORDER: "Put the columns in the declared order.";
|
|
46
|
+
readonly ROW_SHAPE: "Give the row one cell per column; in Markdown write '\\|' for a '|' inside a cell.";
|
|
47
|
+
readonly MISSING_VALUE: "Fill the cell.";
|
|
48
|
+
readonly BAD_TYPE: "Write the value in the column's type.";
|
|
49
|
+
readonly BAD_VALUE: "Use one of the allowed values, spelled exactly.";
|
|
50
|
+
readonly PATTERN: "Write the value in the required format.";
|
|
51
|
+
readonly OUT_OF_RANGE: "Use a value within the allowed range.";
|
|
52
|
+
readonly BAD_LENGTH: "Use a value of the allowed length.";
|
|
53
|
+
readonly DUPLICATE_KEY: "Make the value unique in its table.";
|
|
54
|
+
readonly UNKNOWN_REF: "Use only values that exist in the referenced column.";
|
|
55
|
+
readonly REQUIRED_IF: "Fill the cell; the rule requires it for this row.";
|
|
56
|
+
readonly EMPTY_IF: "Leave the cell empty; the rule forbids a value for this row.";
|
|
57
|
+
readonly COMPARE: "Correct the values so the comparison holds.";
|
|
58
|
+
readonly UNIQUE_TOGETHER: "Make the combination of values unique.";
|
|
59
|
+
readonly ROW_COUNT: "Add or remove rows so the count fits.";
|
|
60
|
+
readonly NOT_MENTIONED: "Mention the value where the rule says.";
|
|
61
|
+
readonly MATCH: "Correct the row so it matches the rule's query (shown in 'expected').";
|
|
62
|
+
readonly NONE_OF: "Make the value meet one of the alternatives in 'expected'.";
|
|
63
|
+
readonly MISSING_FILE: "Check the path; the file must exist and be readable.";
|
|
64
|
+
readonly MISSING_DEPENDENCY: "Pass the dependency's tables with --dep <alias>=<file>.";
|
|
65
|
+
readonly TOO_LARGE: "Split the table; it exceeds the row limit.";
|
|
66
|
+
readonly INVALID_INPUT: "Fix the command arguments named in the message.";
|
|
67
|
+
};
|
|
68
|
+
export type Code = keyof typeof HINTS;
|
|
69
|
+
/** Where a finding points. */
|
|
70
|
+
export interface Where {
|
|
71
|
+
table?: string;
|
|
72
|
+
row?: string;
|
|
73
|
+
column?: string;
|
|
74
|
+
rule?: string;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Builds a diagnostic with its fix hint.
|
|
78
|
+
*
|
|
79
|
+
* @param {Code} code - The code.
|
|
80
|
+
* @param {string} message - What is wrong.
|
|
81
|
+
* @param {Where} at - Table, row, column and rule.
|
|
82
|
+
* @param {string} found - Observed value.
|
|
83
|
+
* @param {string} expected - Required value.
|
|
84
|
+
* @param {"FAIL"|"WARN"} [severity="FAIL"] - Severity.
|
|
85
|
+
* @param {Alternative[]} [alternatives] - For NONE_OF: why each `$or` branch failed.
|
|
86
|
+
* @returns {Diagnostic} The diagnostic.
|
|
87
|
+
*/
|
|
88
|
+
export declare function diag(code: Code, message: string, at: Where, found: string, expected: string, severity?: "FAIL" | "WARN", alternatives?: Alternative[]): Diagnostic;
|
|
89
|
+
/**
|
|
90
|
+
* Sorts diagnostics deterministically: FAIL before WARN, then by table, row, column, code.
|
|
91
|
+
*
|
|
92
|
+
* @param {Diagnostic[]} list - Diagnostics.
|
|
93
|
+
* @returns {Diagnostic[]} A sorted copy.
|
|
94
|
+
*/
|
|
95
|
+
export declare function sortDiagnostics(list: Diagnostic[]): Diagnostic[];
|
|
96
|
+
/**
|
|
97
|
+
* Renders diagnostics as compact text: one line per finding, findings that
|
|
98
|
+
* differ only by row grouped onto one line, then each fix hint once per code.
|
|
99
|
+
*
|
|
100
|
+
* @param {Diagnostic[]} diags - Sorted diagnostics.
|
|
101
|
+
* @returns {string[]} The lines.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* formatDiagnostics(result.diagnostics);
|
|
105
|
+
* // ["FAIL BAD_VALUE Orders Status ×2: O-1=Open, O-4=Shipped; expected open, closed", "fix BAD_VALUE: ..."]
|
|
106
|
+
*/
|
|
107
|
+
export declare function formatDiagnostics(diags: Diagnostic[]): string[];
|
|
108
|
+
export {};
|