@skathi-native/cli 0.1.0-beta.3 → 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.
Files changed (68) hide show
  1. package/aot-inputs.ts +21 -2
  2. package/build.ts +241 -60
  3. package/check/bundle.ts +265 -0
  4. package/check/codes.ts +19 -0
  5. package/check/command.ts +5 -3
  6. package/check/graph.ts +3 -1
  7. package/check/index.ts +54 -2
  8. package/check/program.ts +26 -6
  9. package/cli/aot-build.ts +10 -7
  10. package/cli/build-app.ts +37 -8
  11. package/cli/config.ts +2 -2
  12. package/cli/create.ts +31 -8
  13. package/cli/dev.ts +109 -19
  14. package/cli/effects.ts +162 -0
  15. package/cli/elf.ts +3 -2
  16. package/cli/help.ts +56 -147
  17. package/cli/host-fetch.ts +33 -10
  18. package/cli/host.ts +43 -33
  19. package/cli/sk.ts +437 -234
  20. package/cli/skpk.ts +2 -0
  21. package/cli/stamp.ts +2 -2
  22. package/cli/term.ts +169 -38
  23. package/cli/workload-cache.ts +258 -0
  24. package/devtools/commands.ts +25 -0
  25. package/effects/compile.ts +1081 -0
  26. package/effects/redslangc.json +23 -0
  27. package/effects/redslangc.ts +220 -0
  28. package/effects/sk.slang +882 -0
  29. package/package.json +10 -4
  30. package/prod.ts +40 -7
  31. package/svelte.ts +76 -0
  32. package/templates/icon.png +0 -0
  33. package/templates/minimal/src/App.vue +2 -2
  34. package/templates/showcase/assets/textures/golden.png +0 -0
  35. package/templates/showcase/assets/textures/hammered.png +0 -0
  36. package/templates/showcase/assets/textures/noir.png +0 -0
  37. package/templates/showcase/assets/textures/reeded.png +0 -0
  38. package/templates/showcase/assets/textures/teal-orange.png +0 -0
  39. package/templates/showcase/shaders/dissolve.slang +40 -0
  40. package/templates/showcase/shaders/fireflies.slang +58 -0
  41. package/templates/showcase/shaders/glass.slang +31 -0
  42. package/templates/showcase/shaders/grade.slang +34 -0
  43. package/templates/showcase/shaders/halftone.slang +42 -0
  44. package/templates/showcase/shaders/lens.slang +51 -0
  45. package/templates/showcase/shaders/nebula.slang +57 -0
  46. package/templates/showcase/shaders/orbit.slang +52 -0
  47. package/templates/showcase/shaders/pond.slang +76 -0
  48. package/templates/showcase/shaders/ripples.slang +71 -0
  49. package/templates/showcase/shaders/shatter.slang +116 -0
  50. package/templates/showcase/shaders/sheen.slang +23 -0
  51. package/templates/showcase/shaders/snow.slang +57 -0
  52. package/templates/showcase/shaders/spotlight.slang +52 -0
  53. package/templates/showcase/src/App.vue +11 -16
  54. package/templates/showcase/src/components/FullWindow.vue +77 -0
  55. package/templates/showcase/src/components/Page.vue +2 -0
  56. package/templates/showcase/src/fullwindow.ts +36 -0
  57. package/templates/showcase/src/icons.ts +2 -0
  58. package/templates/showcase/src/pages/GraphicsPage.vue +122 -0
  59. package/templates/showcase/src/pages/MotionPage.vue +71 -1
  60. package/templates/showcase/src/pages/ShadersPage.vue +443 -0
  61. package/templates/showcase/src/pages/StylesPage.vue +9 -1
  62. package/templates/showcase/src/router.ts +32 -1
  63. package/templates/showcase/src/styles.css +2 -2
  64. package/templates/svelte/.vscode/settings.json +3 -0
  65. package/templates/svelte/assets/hello.txt +1 -0
  66. package/templates/svelte/src/App.svelte +142 -0
  67. package/templates/svelte/src/main.ts +4 -0
  68. package/vitecompat.ts +2 -0
@@ -0,0 +1,265 @@
1
+ // What of a source file runs in the production bundle. The bundler keeps code behind a guard it could have folded
2
+ // (`if (DEV)` with esm-env's `DEV` resolved to `false` in another module), so the bundle itself is read: a finding is
3
+ // dropped when the code it maps to sits under a guard that is constant there, or in a function all of whose
4
+ // references in the bundle do. The bundle holds compiled components too, so no caller is out of sight.
5
+
6
+ import { resolve } from "node:path";
7
+ import type ts from "typescript";
8
+ import type { Range } from "./schema";
9
+ import type { ModuleGraph } from "./graph";
10
+ import type { TypeScript } from "./program";
11
+
12
+ type Bundle = NonNullable<ModuleGraph["bundle"]>;
13
+ /** [original line, original column, generated line, generated column], zero-based. */
14
+ type Segment = [number, number, number, number];
15
+
16
+ const B64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
17
+ const MAX_DEPTH = 40;
18
+
19
+ function decode(bundle: Bundle, key: (path: string) => string): Map<string, Segment[]> {
20
+ const bySource = new Map<number, Segment[]>();
21
+ let source = 0;
22
+ let line = 0;
23
+ let column = 0;
24
+ const groups = bundle.mappings.split(";");
25
+ for (let genLine = 0; genLine < groups.length; genLine++) {
26
+ let genColumn = 0;
27
+ for (const segment of groups[genLine]!.split(",")) {
28
+ if (!segment) continue;
29
+ const fields: number[] = [];
30
+ for (let i = 0, shift = 0, value = 0; i < segment.length; i++) {
31
+ const digit = B64.indexOf(segment[i]!);
32
+ value += (digit & 31) << shift;
33
+ if (digit & 32) {
34
+ shift += 5;
35
+ continue;
36
+ }
37
+ fields.push(value & 1 ? -(value >>> 1) : value >>> 1);
38
+ shift = 0;
39
+ value = 0;
40
+ }
41
+ genColumn += fields[0] ?? 0;
42
+ if (fields.length < 4) continue;
43
+ source += fields[1]!;
44
+ line += fields[2]!;
45
+ column += fields[3]!;
46
+ const list = bySource.get(source) ?? [];
47
+ list.push([line, column, genLine, genColumn]);
48
+ bySource.set(source, list);
49
+ }
50
+ }
51
+ const out = new Map<string, Segment[]>();
52
+ for (const [index, list] of bySource) {
53
+ const path = bundle.sources[index];
54
+ if (!path) continue;
55
+ out.set(
56
+ key(path),
57
+ list.sort((a, b) => a[0] - b[0] || a[1] - b[1]),
58
+ );
59
+ }
60
+ return out;
61
+ }
62
+
63
+ export class BundleLiveness {
64
+ private segments: Map<string, Segment[]> | null = null;
65
+ private file: ts.SourceFile | null = null;
66
+ private checker: ts.TypeChecker | null = null;
67
+ private byName: Map<string, ts.Identifier[]> | null = null;
68
+ private readonly functions = new Map<ts.Node, boolean>();
69
+
70
+ constructor(
71
+ private readonly t: TypeScript,
72
+ private readonly bundle: Bundle,
73
+ ) {}
74
+
75
+ private key(path: string): string {
76
+ return this.t.sys.useCaseSensitiveFileNames ? resolve(path) : resolve(path).toLowerCase();
77
+ }
78
+
79
+ /** False when the code at `range` of `path` is in the bundle but cannot run there. */
80
+ live(path: string, range: Range): boolean {
81
+ this.segments ??= decode(this.bundle, (p) => this.key(p));
82
+ const segments = this.segments.get(this.key(path));
83
+ if (!segments) return true;
84
+ const at = this.generated(segments, range);
85
+ if (!at) return true;
86
+ this.parse();
87
+ const offset = this.file!.getPositionOfLineAndCharacter(at[0], at[1]);
88
+ return !this.dead(this.nodeAt(offset), 0);
89
+ }
90
+
91
+ private generated(segments: Segment[], range: Range): [number, number] | null {
92
+ const before = (s: Segment, line: number, character: number) => s[0] < line || (s[0] === line && s[1] < character);
93
+ let lo = 0;
94
+ let hi = segments.length;
95
+ while (lo < hi) {
96
+ const mid = (lo + hi) >> 1;
97
+ if (before(segments[mid]!, range.start.line, range.start.character)) lo = mid + 1;
98
+ else hi = mid;
99
+ }
100
+ const inside = segments[lo];
101
+ if (inside && before(inside, range.end.line, range.end.character + 1)) return [inside[2], inside[3]];
102
+ // A position the bundler mapped only at the start of its line.
103
+ const previous = segments[lo - 1];
104
+ return previous && previous[0] === range.start.line ? [previous[2], previous[3]] : null;
105
+ }
106
+
107
+ private parse(): void {
108
+ if (this.file) return;
109
+ const t = this.t;
110
+ const name = "/sk-check-bundle.js";
111
+ const file = t.createSourceFile(name, this.bundle.code, t.ScriptTarget.Latest, true, t.ScriptKind.JS);
112
+ const options: ts.CompilerOptions = { allowJs: true, noLib: true, noResolve: true, types: [] };
113
+ const host = t.createCompilerHost(options);
114
+ host.getSourceFile = (path) => (path === name ? file : undefined);
115
+ host.fileExists = (path) => path === name;
116
+ host.readFile = (path) => (path === name ? this.bundle.code : undefined);
117
+ this.checker = t.createProgram({ rootNames: [name], options, host }).getTypeChecker();
118
+ this.file = file;
119
+ }
120
+
121
+ private nodeAt(offset: number): ts.Node {
122
+ let node: ts.Node = this.file!;
123
+ for (let found = true; found;) {
124
+ found = false;
125
+ this.t.forEachChild(node, (child) => {
126
+ if (found || offset < child.getStart(this.file!) || offset >= child.end) return;
127
+ node = child;
128
+ found = true;
129
+ });
130
+ }
131
+ return node;
132
+ }
133
+
134
+ private dead(node: ts.Node, depth: number): boolean {
135
+ const t = this.t;
136
+ for (let cur = node; cur.parent; cur = cur.parent) {
137
+ const p = cur.parent;
138
+ if (t.isIfStatement(p) && cur !== p.expression) {
139
+ const test = this.truth(p.expression, depth);
140
+ if (test === (cur === p.elseStatement)) return true;
141
+ } else if (t.isConditionalExpression(p) && cur !== p.condition) {
142
+ const test = this.truth(p.condition, depth);
143
+ if (test === (cur === p.whenFalse)) return true;
144
+ } else if (t.isBinaryExpression(p) && cur === p.right) {
145
+ const op = p.operatorToken.kind;
146
+ const left = this.truth(p.left, depth);
147
+ if (op === t.SyntaxKind.AmpersandAmpersandToken && left === false) return true;
148
+ if (op === t.SyntaxKind.BarBarToken && left === true) return true;
149
+ }
150
+ if (t.isFunctionDeclaration(cur) || t.isFunctionExpression(cur) || t.isArrowFunction(cur)) {
151
+ const verdict = this.uncalled(cur, depth);
152
+ if (verdict !== undefined) return verdict;
153
+ }
154
+ }
155
+ return false;
156
+ }
157
+
158
+ /** For a named function: whether every reference to it is dead; undefined when it has no name to follow. */
159
+ private uncalled(fn: ts.FunctionLikeDeclaration, depth: number): boolean | undefined {
160
+ const t = this.t;
161
+ const holder = t.isFunctionDeclaration(fn)
162
+ ? fn.name
163
+ : t.isVariableDeclaration(fn.parent) && fn.parent.initializer === fn && t.isIdentifier(fn.parent.name)
164
+ ? fn.parent.name
165
+ : undefined;
166
+ if (!holder) return undefined;
167
+ const known = this.functions.get(fn);
168
+ if (known !== undefined) return known;
169
+ if (depth > MAX_DEPTH) return false;
170
+ // While its callers are judged, a recursive call counts as live.
171
+ this.functions.set(fn, false);
172
+ const refs = this.references(holder);
173
+ const verdict = refs.length > 0 && refs.every((ref) => this.dead(ref, depth + 1));
174
+ this.functions.set(fn, verdict);
175
+ return verdict;
176
+ }
177
+
178
+ private references(name: ts.Identifier): ts.Identifier[] {
179
+ const t = this.t;
180
+ const checker = this.checker!;
181
+ if (!this.byName) {
182
+ this.byName = new Map();
183
+ const visit = (node: ts.Node): void => {
184
+ if (t.isIdentifier(node)) {
185
+ const list = this.byName!.get(node.text) ?? [];
186
+ list.push(node);
187
+ this.byName!.set(node.text, list);
188
+ }
189
+ t.forEachChild(node, visit);
190
+ };
191
+ visit(this.file!);
192
+ }
193
+ const symbolOf = (id: ts.Identifier) =>
194
+ t.isShorthandPropertyAssignment(id.parent)
195
+ ? checker.getShorthandAssignmentValueSymbol(id.parent)
196
+ : checker.getSymbolAtLocation(id);
197
+ const symbol = symbolOf(name);
198
+ if (!symbol) return [];
199
+ return (this.byName.get(name.text) ?? []).filter((id) => id !== name && symbolOf(id) === symbol);
200
+ }
201
+
202
+ /** The truthiness of `expr` wherever it runs in the bundle, or undefined when it is not constant. */
203
+ private truth(expr: ts.Expression, depth: number): boolean | undefined {
204
+ const value = this.constant(expr, depth);
205
+ return value === NOT_CONSTANT ? undefined : !!value;
206
+ }
207
+
208
+ private constant(expr: ts.Expression, depth: number): unknown {
209
+ const t = this.t;
210
+ if (depth > MAX_DEPTH) return NOT_CONSTANT;
211
+ if (t.isParenthesizedExpression(expr)) return this.constant(expr.expression, depth);
212
+ if (expr.kind === t.SyntaxKind.TrueKeyword) return true;
213
+ if (expr.kind === t.SyntaxKind.FalseKeyword) return false;
214
+ if (expr.kind === t.SyntaxKind.NullKeyword) return null;
215
+ if (t.isNumericLiteral(expr)) return Number(expr.text);
216
+ if (t.isStringLiteral(expr) || t.isNoSubstitutionTemplateLiteral(expr)) return expr.text;
217
+ if (t.isVoidExpression(expr)) return undefined;
218
+ if (t.isPrefixUnaryExpression(expr) && expr.operator === t.SyntaxKind.ExclamationToken) {
219
+ const value = this.constant(expr.operand, depth + 1);
220
+ return value === NOT_CONSTANT ? NOT_CONSTANT : !value;
221
+ }
222
+ if (t.isBinaryExpression(expr)) {
223
+ const op = expr.operatorToken.kind;
224
+ const left = this.constant(expr.left, depth + 1);
225
+ if (left === NOT_CONSTANT) return NOT_CONSTANT;
226
+ if (op === t.SyntaxKind.AmpersandAmpersandToken) return left ? this.constant(expr.right, depth + 1) : left;
227
+ if (op === t.SyntaxKind.BarBarToken) return left ? left : this.constant(expr.right, depth + 1);
228
+ const right = this.constant(expr.right, depth + 1);
229
+ if (right === NOT_CONSTANT) return NOT_CONSTANT;
230
+ if (op === t.SyntaxKind.EqualsEqualsEqualsToken) return left === right;
231
+ if (op === t.SyntaxKind.ExclamationEqualsEqualsToken) return left !== right;
232
+ if (op === t.SyntaxKind.EqualsEqualsToken) return left == right;
233
+ if (op === t.SyntaxKind.ExclamationEqualsToken) return left != right;
234
+ return NOT_CONSTANT;
235
+ }
236
+ if (t.isIdentifier(expr)) return this.binding(expr, depth);
237
+ return NOT_CONSTANT;
238
+ }
239
+
240
+ /** A variable initialised to a constant and never written again. */
241
+ private binding(id: ts.Identifier, depth: number): unknown {
242
+ const t = this.t;
243
+ const symbol = this.checker!.getSymbolAtLocation(id);
244
+ const declarations = symbol?.declarations ?? [];
245
+ const decl = declarations[0];
246
+ if (declarations.length !== 1 || !decl || !t.isVariableDeclaration(decl) || !decl.initializer) {
247
+ return NOT_CONSTANT;
248
+ }
249
+ if (!t.isIdentifier(decl.name)) return NOT_CONSTANT;
250
+ for (const ref of this.references(decl.name)) {
251
+ const p = ref.parent;
252
+ const written =
253
+ (t.isBinaryExpression(p) &&
254
+ p.left === ref &&
255
+ p.operatorToken.kind >= t.SyntaxKind.FirstAssignment &&
256
+ p.operatorToken.kind <= t.SyntaxKind.LastAssignment) ||
257
+ ((t.isPrefixUnaryExpression(p) || t.isPostfixUnaryExpression(p)) &&
258
+ (p.operator === t.SyntaxKind.PlusPlusToken || p.operator === t.SyntaxKind.MinusMinusToken));
259
+ if (written) return NOT_CONSTANT;
260
+ }
261
+ return this.constant(decl.initializer, depth + 1);
262
+ }
263
+ }
264
+
265
+ const NOT_CONSTANT = Symbol("not constant");
package/check/codes.ts CHANGED
@@ -57,6 +57,25 @@ const CODES: CodeInfo[] = [
57
57
  fix: "Fix the build error (sk build shows the same one), then run sk check again.",
58
58
  fatal: true,
59
59
  },
60
+ {
61
+ code: "SK0005",
62
+ severity: "error",
63
+ title: "an effect shader does not compile",
64
+ explanation:
65
+ "The app's shaders/*.slang are its own `--shading` effects, compiled into the app for every OS by sk build. One of them does not compile, or breaks a rule of the effect schema (an attribute, a parameter type, a name), so sk build refuses the app.",
66
+ example:
67
+ '[SkEffect("ripple")]\nstruct Ripple : IEffect\n{\n int rings; // parameters are float, float2, float3 or float4\n float4 shade(EffectInput input) { return float4(0, 0, 0, 0); }\n};',
68
+ fix: "Fix the shader at the reported line; the rules are in the comments of the `sk` prelude (`import sk;`).",
69
+ },
70
+ {
71
+ code: "SK0006",
72
+ severity: "warning",
73
+ title: "the effect shaders could not be checked",
74
+ explanation:
75
+ "The app has shaders/*.slang, but the shader compiler (redslangc) could not be found or downloaded, so they were not compiled. sk build needs it too.",
76
+ example: "SK_HOST_OFFLINE=1 with no cached redslangc",
77
+ fix: "Allow the download once, or set SK_REDSLANGC to a redslangc binary.",
78
+ },
60
79
  {
61
80
  code: "SK1001",
62
81
  severity: "error",
package/check/command.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { colorEnabled, colorFlag, paint, quietProgress, spinner, type Progress } from "../cli/term";
1
+ import { colorEnabled, colorFlag, interactive, paint, quietProgress, spinner, type Progress } from "../cli/term";
2
2
  import { highlighter, type Highlight } from "./highlight";
3
3
  import { exitCode, explain, formatPretty, listCodes, loadTypeScript, runCheck } from "./index";
4
4
 
@@ -25,7 +25,9 @@ export const defaultIo = (flags: Pick<CheckFlags, "color" | "no-color"> = {}): I
25
25
  err: (text) => process.stderr.write(`${text}\n`),
26
26
  color,
27
27
  progress: () =>
28
- process.stderr.isTTY ? spinner(process.stderr, colorEnabled(process.stderr, colorFlag(flags))) : quietProgress,
28
+ interactive(process.stderr)
29
+ ? spinner(process.stderr, colorEnabled(process.stderr, colorFlag(flags)))
30
+ : quietProgress,
29
31
  };
30
32
  };
31
33
 
@@ -73,7 +75,7 @@ export async function checkCommand(
73
75
  }
74
76
  return exitCode(report, flags["deny-warnings"] === true);
75
77
  } catch (e) {
76
- progress.end();
78
+ progress.fail();
77
79
  fail(e instanceof Error ? e.message : String(e));
78
80
  return 2;
79
81
  }
package/check/graph.ts CHANGED
@@ -12,8 +12,10 @@ export interface ModuleGraph {
12
12
  aliases: Record<string, string>;
13
13
  /** Ambient declarations the runtime contributes. */
14
14
  declarations: string[];
15
- /** Absolute paths of the .js/.ts/.vue files with code in the bundle. */
15
+ /** Absolute paths of the .js/.ts/.vue/.svelte files with code in the bundle. */
16
16
  modules: string[];
17
+ /** The production bundle and its sourcemap; a source is null when its positions are not the file's own. */
18
+ bundle?: { code: string; sources: (string | null)[]; mappings: string };
17
19
  errors: GraphError[];
18
20
  }
19
21
 
package/check/index.ts CHANGED
@@ -1,12 +1,15 @@
1
1
  import { readFileSync } from "node:fs";
2
2
  import { createRequire } from "node:module";
3
- import { join, relative, resolve, sep } from "node:path";
3
+ import { isAbsolute, join, relative, resolve, sep } from "node:path";
4
4
  import { loadAppConfig } from "../cli/config";
5
+ import { appShadersDir, compileAppEffects, EffectsError, hasAppEffects } from "../effects/compile";
6
+ import { resolveCompiler } from "../effects/redslangc";
5
7
  import { quietProgress, type Progress } from "../cli/term";
6
8
  import { Builtins } from "./builtins";
7
9
  import { codeInfo, REGISTRY } from "./codes";
8
10
  import { parseCheckConfig, Suppressions } from "./config";
9
11
  import { rangeOf, ZERO_RANGE, type Finding } from "./finding";
12
+ import { BundleLiveness } from "./bundle";
10
13
  import { buildGraph, GraphFailure, type ModuleGraph } from "./graph";
11
14
  import { Packages } from "./packages";
12
15
  import { buildProgram, readUnits, type TypeScript } from "./program";
@@ -53,6 +56,8 @@ export interface AnalyzeInput {
53
56
  /** Findings made before the analysis (configuration problems). */
54
57
  findings?: Finding[];
55
58
  progress?: Progress | undefined;
59
+ /** The production bundle: a rule finding in code it cannot run is not reported. */
60
+ bundle?: ModuleGraph["bundle"];
56
61
  }
57
62
 
58
63
  export function loadTypeScript(appRoot: string): TypeScript {
@@ -91,6 +96,7 @@ export async function runCheck(options: CheckOptions): Promise<CheckReport> {
91
96
  const typescript = loadTypeScript(config.appRoot);
92
97
  const base = { typescript, root: config.appRoot, entry: config.entry, toolVersion: options.toolVersion, progress };
93
98
  if (findings.length) return analyze({ ...base, modules: [], findings });
99
+ findings.push(...(await effectFindings(config.appRoot, progress)));
94
100
 
95
101
  progress.step("module graph");
96
102
  let graph: ModuleGraph;
@@ -135,11 +141,52 @@ export async function runCheck(options: CheckOptions): Promise<CheckReport> {
135
141
  modules: graph.modules,
136
142
  aliases: graph.aliases,
137
143
  declarations: graph.declarations,
144
+ bundle: graph.bundle,
138
145
  allow: checkConfig.allow,
139
146
  deps: options.deps ?? true,
147
+ findings,
140
148
  });
141
149
  }
142
150
 
151
+ /** The app's shaders/*.slang, compiled as sk build would: one SK0005 per error, at the author's line. */
152
+ export async function effectFindings(appRoot: string, progress: Progress = quietProgress): Promise<Finding[]> {
153
+ if (!hasAppEffects(appRoot)) return [];
154
+ const dir = appShadersDir(appRoot);
155
+ progress.step("effect shaders");
156
+ let compiler: string;
157
+ try {
158
+ compiler = (
159
+ await resolveCompiler({
160
+ log: (line) => progress.info(line),
161
+ onBytes: (done, total) => progress.bytes(done, total),
162
+ })
163
+ ).path;
164
+ } catch (e) {
165
+ const message = `the effect shaders were not compiled: ${e instanceof Error ? e.message : String(e)}`;
166
+ return [{ code: "SK0006", message, file: dir, range: ZERO_RANGE, related: [] }];
167
+ }
168
+ try {
169
+ await compileAppEffects(appRoot, compiler);
170
+ return [];
171
+ } catch (e) {
172
+ if (!(e instanceof EffectsError)) throw e;
173
+ return e.problems
174
+ .filter((p) => p.severity === "error")
175
+ .map((p): Finding => {
176
+ const own = isAbsolute(p.file);
177
+ const line = own ? Math.max(0, p.line - 1) : 0;
178
+ const character = own ? Math.max(0, p.column - 1) : 0;
179
+ return {
180
+ code: "SK0005",
181
+ message: own ? p.message : `${p.file}${p.line ? `:${p.line}` : ""}: ${p.message}`,
182
+ file: own ? p.file : dir,
183
+ range: { start: { line, character }, end: { line, character } },
184
+ related: [],
185
+ };
186
+ });
187
+ }
188
+ }
189
+
143
190
  export function analyze(input: AnalyzeInput): CheckReport {
144
191
  const { typescript } = input;
145
192
  const progress = input.progress ?? quietProgress;
@@ -172,6 +219,7 @@ export function analyze(input: AnalyzeInput): CheckReport {
172
219
  if (built.configNote) progress.warn(built.configNote);
173
220
  const { program, checker } = built;
174
221
  const rules = new Rules(typescript, checker, new Builtins(typescript, program, checker));
222
+ const liveness = input.bundle ? new BundleLiveness(typescript, input.bundle) : null;
175
223
  progress.step("AOT rules", units.length);
176
224
  for (const unit of units) {
177
225
  progress.tick();
@@ -201,7 +249,11 @@ export function analyze(input: AnalyzeInput): CheckReport {
201
249
  delete unit.sourceFile;
202
250
  continue;
203
251
  }
204
- if (deps || !unit.owner) rules.run(unit, file, findings);
252
+ if (deps || !unit.owner) {
253
+ const found: Finding[] = [];
254
+ rules.run(unit, file, found);
255
+ findings.push(...found.filter((f) => !liveness || liveness.live(f.file, f.range)));
256
+ }
205
257
  }
206
258
  progress.step("typing quality");
207
259
  const typing = new Typing(
package/check/program.ts CHANGED
@@ -8,11 +8,11 @@ export type TypeScript = typeof ts;
8
8
 
9
9
  /** One file of the bundle as the analysis sees it. */
10
10
  export interface Unit {
11
- /** The file on disk (.vue for a single-file component). */
11
+ /** The file on disk (.vue or .svelte for a component). */
12
12
  path: string;
13
- /** Its name in the TypeScript program: `<path>.ts`/`.js` for a .vue file. */
13
+ /** Its name in the TypeScript program: `<path>.ts`/`.js` for a component. */
14
14
  programPath: string;
15
- /** The text positions refer to; for a .vue file everything but its scripts is blanked. */
15
+ /** The text positions refer to; for a component everything but its scripts is blanked. */
16
16
  text: string;
17
17
  /** TypeScript rather than JavaScript. */
18
18
  typed: boolean;
@@ -27,6 +27,7 @@ export interface Unreadable {
27
27
  }
28
28
 
29
29
  const TS_FILE = /\.[cm]?tsx?$/i;
30
+ const COMPONENT_FILE = /\.(vue|svelte)$/i;
30
31
 
31
32
  /** The scripts of an SFC at their own offsets, everything else replaced by spaces. */
32
33
  export function vueScripts(source: string, path: string): { text: string; typed: boolean } | null {
@@ -41,6 +42,25 @@ export function vueScripts(source: string, path: string): { text: string; typed:
41
42
  return { text: chars.join(""), typed: blocks.some((b) => /^tsx?$/.test(b!.lang ?? "")) };
42
43
  }
43
44
 
45
+ const blank = (text: string) => text.replace(/[^\r\n]/g, " ");
46
+
47
+ /** The `<script>` blocks of a Svelte component at their own offsets, everything else replaced by spaces. */
48
+ export function svelteScripts(source: string): { text: string; typed: boolean } | null {
49
+ // A `<script>` in a comment or in `<svelte:head>` is markup, not the component's script.
50
+ const scan = source.replace(/<!--[\s\S]*?-->|<svelte:head[\s>][\s\S]*?<\/svelte:head\s*>/gi, blank);
51
+ const chars = blank(source).split("");
52
+ let found = false;
53
+ let typed = false;
54
+ for (const m of scan.matchAll(/<script(\s[^>]*)?>([\s\S]*?)<\/script\s*>/gi)) {
55
+ const attrs = m[1] ?? "";
56
+ const start = m.index + "<script".length + attrs.length + 1;
57
+ for (let i = 0; i < m[2]!.length; i++) chars[start + i] = source[start + i]!;
58
+ found = true;
59
+ if (/\blang\s*=\s*["']?(ts|typescript)\b/i.test(attrs)) typed = true;
60
+ }
61
+ return found ? { text: chars.join(""), typed } : null;
62
+ }
63
+
44
64
  export function readUnits(
45
65
  modules: string[],
46
66
  packages: Packages,
@@ -57,10 +77,10 @@ export function readUnits(
57
77
  unreadable.push({ path, owner, message: (e as Error).message });
58
78
  continue;
59
79
  }
60
- if (/\.vue$/i.test(path)) {
80
+ if (COMPONENT_FILE.test(path)) {
61
81
  let scripts: ReturnType<typeof vueScripts>;
62
82
  try {
63
- scripts = vueScripts(source, path);
83
+ scripts = /\.svelte$/i.test(path) ? svelteScripts(source) : vueScripts(source, path);
64
84
  } catch (e) {
65
85
  unreadable.push({ path, owner, message: (e as Error).message });
66
86
  continue;
@@ -188,7 +208,7 @@ export function buildProgram(input: ProgramInput): BuiltProgram {
188
208
  }
189
209
  const found = typescript.resolveModuleName(specifier, importer, options, host).resolvedModule;
190
210
  if (found) return found;
191
- if (/\.vue$/i.test(specifier) && specifier.startsWith(".")) {
211
+ if (COMPONENT_FILE.test(specifier) && specifier.startsWith(".")) {
192
212
  for (const ext of ["ts", "js"] as const) {
193
213
  const path = resolve(dirname(importer), `${specifier}.${ext}`);
194
214
  if (own.has(key(path)))
package/cli/aot-build.ts CHANGED
@@ -15,7 +15,7 @@ import { TRANSFORM } from "../aot/transform";
15
15
  import { exitCode, formatPretty, runCheck, type CheckReport } from "../check/index";
16
16
  import { ConfigError } from "./config";
17
17
  import { CLI_VERSION, HostError, REPO_ROOT, type HostAot, type ResolvedHost } from "./host";
18
- import { cachedAotCompiler, fetchAotCompiler } from "./host-fetch";
18
+ import { cachedAotCompiler, fetchAotCompiler, type OnBytes } from "./host-fetch";
19
19
  import type { Platform } from "./platform";
20
20
  import { KIND, NAMES, type SkpkInput } from "./skpk";
21
21
  import type { Progress } from "./term";
@@ -55,13 +55,13 @@ export function hostAot(host: ResolvedHost): HostAot {
55
55
  if (!host.info.features.includes("native-aot") || !aot) {
56
56
  throw new HostError(
57
57
  `${host.exe} cannot load native images (it was built without the native AOT loader) — ` +
58
- `use a Skathi ${CLI_VERSION} release host (sk host fetch), or build one with the native-aot cargo feature`,
58
+ `use a Skathi ${CLI_VERSION} release workload (sk workload install), or build one with the native-aot cargo feature`,
59
59
  );
60
60
  }
61
61
  if (aot.transform !== TRANSFORM) {
62
62
  throw new HostError(
63
63
  `${host.exe} expects native images made with the ${aot.transform} transform and this sk makes ${TRANSFORM} — ` +
64
- "use the sk that matches the host",
64
+ "use the sk that matches the workload",
65
65
  );
66
66
  }
67
67
  return aot;
@@ -81,12 +81,13 @@ export async function resolveAotCompiler(
81
81
  aot: HostAot,
82
82
  named: string | undefined,
83
83
  log: (line: string) => void,
84
+ onBytes?: OnBytes,
84
85
  ): Promise<CompilerChoice> {
85
86
  const explicit = named ?? process.env.SK_AOT_COMPILER;
86
87
  if (explicit) {
87
88
  const info = await compilerInfo(explicit);
88
89
  const mismatch = compilerMismatch(info, aot);
89
- if (mismatch) throw new AotError(`${explicit} compiles for another host: ${mismatch}`);
90
+ if (mismatch) throw new AotError(`${explicit} compiles for another workload: ${mismatch}`);
90
91
  return { path: explicit, info, source: named ? "--aot-compiler" : "SK_AOT_COMPILER" };
91
92
  }
92
93
  const tried: string[] = [];
@@ -102,11 +103,11 @@ export async function resolveAotCompiler(
102
103
  }
103
104
  if (process.env.SK_HOST_OFFLINE === "1") {
104
105
  throw new AotError(
105
- `no sk-aot matches the host${tried.length ? `:\n ${tried.join("\n ")}` : ""}\n` +
106
+ `no sk-aot matches the workload${tried.length ? `:\n ${tried.join("\n ")}` : ""}\n` +
106
107
  "SK_HOST_OFFLINE=1 forbids downloading it; pass --aot-compiler <sk-aot.exe>",
107
108
  );
108
109
  }
109
- const fetched = await fetchAotCompiler({ log });
110
+ const fetched = await fetchAotCompiler({ log, onBytes });
110
111
  const mismatch = compilerMismatch(fetched.info, aot);
111
112
  if (mismatch) throw new AotError(`the Skathi ${CLI_VERSION} release's sk-aot: ${mismatch}`);
112
113
  return { path: fetched.path, info: await compilerInfo(fetched.path), source: "release" };
@@ -156,7 +157,9 @@ export async function buildNative(options: {
156
157
  }): Promise<NativeBuild> {
157
158
  const { progress, log, aot, work } = options;
158
159
  progress.step("AOT toolchain");
159
- const compiler = await resolveAotCompiler(aot, options.compilerPath, log);
160
+ const compiler = await resolveAotCompiler(aot, options.compilerPath, log, (done, total) =>
161
+ progress.bytes(done, total),
162
+ );
160
163
  progress.detail(`sk-aot (${compiler.source})`);
161
164
  progress.step("native image");
162
165
  const native = await compileNative({