@skathi-native/cli 0.0.0 → 0.1.0-beta.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/aot-inputs.ts +294 -0
- package/ascii.ts +397 -0
- package/build.ts +1236 -0
- package/check/builtins.json +1219 -0
- package/check/builtins.ts +112 -0
- package/check/codes.ts +178 -0
- package/check/command.ts +105 -0
- package/check/config.ts +76 -0
- package/check/finding.ts +30 -0
- package/check/graph.ts +69 -0
- package/check/highlight.ts +40 -0
- package/check/index.ts +308 -0
- package/check/packages.ts +54 -0
- package/check/program.ts +209 -0
- package/check/report.ts +219 -0
- package/check/rules.ts +307 -0
- package/check/schema.ts +69 -0
- package/check/typing.ts +703 -0
- package/cli/assets.ts +114 -0
- package/cli/build-app.ts +587 -0
- package/cli/config.ts +646 -0
- package/cli/create.ts +327 -0
- package/cli/dev.ts +92 -0
- package/cli/elf.ts +142 -0
- package/cli/help.ts +160 -0
- package/cli/host-fetch.ts +178 -0
- package/cli/host.ts +378 -0
- package/cli/icon.ts +303 -0
- package/cli/macos-bundle.ts +451 -0
- package/cli/pe.ts +429 -0
- package/cli/platform.ts +74 -0
- package/cli/sk.ts +290 -0
- package/cli/skpk.ts +268 -0
- package/cli/stamp.ts +254 -0
- package/cli/term.ts +176 -0
- package/cli/versioninfo.ts +123 -0
- package/css-prefixes.ts +118 -0
- package/devtools/apps.ts +68 -0
- package/devtools/commands.ts +860 -0
- package/devtools/ctl.ts +166 -0
- package/package.json +52 -3
- package/prod.ts +701 -0
- package/runtime-path.ts +24 -0
- package/tailwind.ts +76 -0
- package/templates/minimal/assets/hello.txt +1 -0
- package/templates/minimal/src/App.vue +142 -0
- package/templates/minimal/src/main.ts +4 -0
- package/templates/showcase/assets/hello.txt +1 -0
- package/templates/showcase/assets/media/aurora.png +0 -0
- package/templates/showcase/assets/media/clip.mp4 +0 -0
- package/templates/showcase/assets/media/sticker.tgs +0 -0
- package/templates/showcase/src/App.vue +128 -0
- package/templates/showcase/src/components/Card.vue +41 -0
- package/templates/showcase/src/components/Icon.vue +29 -0
- package/templates/showcase/src/components/Missing.vue +26 -0
- package/templates/showcase/src/components/Page.vue +41 -0
- package/templates/showcase/src/components/Titlebar.vue +128 -0
- package/templates/showcase/src/engine.ts +33 -0
- package/templates/showcase/src/icons.ts +22 -0
- package/templates/showcase/src/main.ts +7 -0
- package/templates/showcase/src/pages/AudioPage.vue +124 -0
- package/templates/showcase/src/pages/FilesPage.vue +91 -0
- package/templates/showcase/src/pages/FormsPage.vue +139 -0
- package/templates/showcase/src/pages/GraphicsPage.vue +156 -0
- package/templates/showcase/src/pages/HomePage.vue +141 -0
- package/templates/showcase/src/pages/LocalePage.vue +71 -0
- package/templates/showcase/src/pages/MediaPage.vue +85 -0
- package/templates/showcase/src/pages/MotionPage.vue +199 -0
- package/templates/showcase/src/pages/NetworkPage.vue +103 -0
- package/templates/showcase/src/pages/PermissionsPage.vue +80 -0
- package/templates/showcase/src/pages/StoragePage.vue +117 -0
- package/templates/showcase/src/pages/StylesPage.vue +199 -0
- package/templates/showcase/src/pages/SystemPage.vue +89 -0
- package/templates/showcase/src/pages/TrayPage.vue +66 -0
- package/templates/showcase/src/pages/WindowPage.vue +128 -0
- package/templates/showcase/src/product.ts +2 -0
- package/templates/showcase/src/router.ts +149 -0
- package/templates/showcase/src/stores/counter.ts +9 -0
- package/templates/showcase/src/stores/desktop.ts +115 -0
- package/templates/showcase/src/styles.css +140 -0
- package/vitecompat.ts +1151 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import type ts from "typescript";
|
|
4
|
+
import type { TypeScript } from "./program";
|
|
5
|
+
|
|
6
|
+
interface Table {
|
|
7
|
+
table: string;
|
|
8
|
+
engine: string;
|
|
9
|
+
locked: string[];
|
|
10
|
+
replaceable: string[];
|
|
11
|
+
lockedObjects: string[];
|
|
12
|
+
objects: string[];
|
|
13
|
+
}
|
|
14
|
+
const data = JSON.parse(readFileSync(join(import.meta.dir, "builtins.json"), "utf8")) as Table;
|
|
15
|
+
|
|
16
|
+
/** Engine built-ins whose replacement or deletion throws under AOT (`Array.prototype.push`, `Map`). */
|
|
17
|
+
export const LOCKED: ReadonlySet<string> = new Set(data.locked);
|
|
18
|
+
/** Engine built-ins that stay replaceable (`Date.now`, `Promise`). */
|
|
19
|
+
export const REPLACEABLE: ReadonlySet<string> = new Set(data.replaceable);
|
|
20
|
+
/** Objects that take no new members (`Object.prototype`). */
|
|
21
|
+
export const LOCKED_OBJECTS: ReadonlySet<string> = new Set(data.lockedObjects);
|
|
22
|
+
/** Prototypes, constructors and namespaces. */
|
|
23
|
+
export const OBJECTS: ReadonlySet<string> = new Set(data.objects);
|
|
24
|
+
export const TABLE_ID: string = data.table;
|
|
25
|
+
|
|
26
|
+
const GLOBAL_OBJECTS = new Set(["globalThis", "window", "self", "global"]);
|
|
27
|
+
const WELL_KNOWN_SYMBOL = /^Symbol\.[A-Za-z]+$/;
|
|
28
|
+
|
|
29
|
+
/** `holder.member` in the table's notation. */
|
|
30
|
+
export const memberPath = (holder: string, member: string) =>
|
|
31
|
+
holder === "globalThis" ? member : WELL_KNOWN_SYMBOL.test(member) ? `${holder}[${member}]` : `${holder}.${member}`;
|
|
32
|
+
|
|
33
|
+
/** Resolves expressions to the engine built-in they name, using the checker to see through shadowing. */
|
|
34
|
+
export class Builtins {
|
|
35
|
+
constructor(
|
|
36
|
+
private readonly typescript: TypeScript,
|
|
37
|
+
private readonly program: ts.Program,
|
|
38
|
+
private readonly checker: ts.TypeChecker,
|
|
39
|
+
) {}
|
|
40
|
+
|
|
41
|
+
private globalDeclaration(declaration: ts.Declaration): boolean {
|
|
42
|
+
const file = declaration.getSourceFile();
|
|
43
|
+
if (this.program.isSourceFileDefaultLibrary(file)) return true;
|
|
44
|
+
if (!file.isDeclarationFile) return false;
|
|
45
|
+
if (!this.typescript.isExternalModule(file)) return true;
|
|
46
|
+
for (let node: ts.Node | undefined = declaration; node; node = node.parent) {
|
|
47
|
+
if (this.typescript.isModuleDeclaration(node) && node.name.text === "global") return true;
|
|
48
|
+
}
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** The identifier refers to the global binding of its name, not to a local or an import. */
|
|
53
|
+
isGlobal(id: ts.Identifier): boolean {
|
|
54
|
+
let symbol: ts.Symbol | undefined;
|
|
55
|
+
try {
|
|
56
|
+
symbol = this.checker.getSymbolAtLocation(id);
|
|
57
|
+
} catch {
|
|
58
|
+
symbol = undefined;
|
|
59
|
+
}
|
|
60
|
+
const declarations = symbol?.declarations ?? [];
|
|
61
|
+
return declarations.every((d) => this.globalDeclaration(d));
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** A property name as written, if it is a constant: `.push`, `["push"]`, `[Symbol.iterator]`. */
|
|
65
|
+
memberName(node: ts.Expression | ts.PropertyName | ts.MemberName): string | null {
|
|
66
|
+
const t = this.typescript;
|
|
67
|
+
if (t.isIdentifier(node) || t.isPrivateIdentifier(node)) return t.isIdentifier(node) ? node.text : null;
|
|
68
|
+
if (t.isStringLiteral(node) || t.isNoSubstitutionTemplateLiteral(node) || t.isNumericLiteral(node))
|
|
69
|
+
return node.text;
|
|
70
|
+
if (t.isComputedPropertyName(node)) return this.memberName(node.expression);
|
|
71
|
+
if (t.isPropertyAccessExpression(node) && t.isIdentifier(node.expression) && node.expression.text === "Symbol") {
|
|
72
|
+
return this.isGlobal(node.expression) ? `Symbol.${node.name.text}` : null;
|
|
73
|
+
}
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** "globalThis", a global's name, or a dotted built-in path; null when the expression is not a built-in. */
|
|
78
|
+
path(node: ts.Expression, depth = 0): string | null {
|
|
79
|
+
const t = this.typescript;
|
|
80
|
+
if (depth > 8) return null;
|
|
81
|
+
if (t.isParenthesizedExpression(node) || t.isNonNullExpression(node) || t.isAsExpression(node))
|
|
82
|
+
return this.path(node.expression, depth + 1);
|
|
83
|
+
if (t.isIdentifier(node)) {
|
|
84
|
+
if (this.isGlobal(node)) return GLOBAL_OBJECTS.has(node.text) ? "globalThis" : node.text;
|
|
85
|
+
return this.alias(node, depth);
|
|
86
|
+
}
|
|
87
|
+
if (t.isPropertyAccessExpression(node) || t.isElementAccessExpression(node)) {
|
|
88
|
+
const name = this.memberName(t.isPropertyAccessExpression(node) ? node.name : node.argumentExpression);
|
|
89
|
+
if (name === null) return null;
|
|
90
|
+
const holder = this.path(node.expression, depth + 1);
|
|
91
|
+
if (holder === null) return null;
|
|
92
|
+
if (holder === "globalThis" && GLOBAL_OBJECTS.has(name)) return "globalThis";
|
|
93
|
+
return memberPath(holder, name);
|
|
94
|
+
}
|
|
95
|
+
return null;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** `const AP = Array.prototype`: the built-in a constant names. */
|
|
99
|
+
private alias(id: ts.Identifier, depth: number): string | null {
|
|
100
|
+
const t = this.typescript;
|
|
101
|
+
const declaration = this.checker.getSymbolAtLocation(id)?.valueDeclaration;
|
|
102
|
+
if (
|
|
103
|
+
!declaration ||
|
|
104
|
+
!t.isVariableDeclaration(declaration) ||
|
|
105
|
+
!declaration.initializer ||
|
|
106
|
+
!t.isIdentifier(declaration.name) ||
|
|
107
|
+
!(t.getCombinedNodeFlags(declaration) & t.NodeFlags.Const)
|
|
108
|
+
)
|
|
109
|
+
return null;
|
|
110
|
+
return this.path(declaration.initializer, depth + 1);
|
|
111
|
+
}
|
|
112
|
+
}
|
package/check/codes.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { Severity } from "./schema";
|
|
2
|
+
|
|
3
|
+
export interface CodeInfo {
|
|
4
|
+
code: string;
|
|
5
|
+
severity: Severity;
|
|
6
|
+
title: string;
|
|
7
|
+
explanation: string;
|
|
8
|
+
example: string;
|
|
9
|
+
fix: string;
|
|
10
|
+
/** The check could not run as asked: exit code 2. */
|
|
11
|
+
fatal?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const RANGES = [
|
|
15
|
+
{ from: 0, to: 999, what: "the check itself" },
|
|
16
|
+
{ from: 1000, to: 1999, what: "breaks under AOT" },
|
|
17
|
+
{ from: 2000, to: 2999, what: "works under AOT, but slower" },
|
|
18
|
+
{ from: 3000, to: 3999, what: "typing quality: compiles, but AOT gets no type information" },
|
|
19
|
+
] as const;
|
|
20
|
+
|
|
21
|
+
const CODES: CodeInfo[] = [
|
|
22
|
+
{
|
|
23
|
+
code: "SK0001",
|
|
24
|
+
severity: "error",
|
|
25
|
+
title: "a file of the bundle cannot be read",
|
|
26
|
+
explanation:
|
|
27
|
+
"The production build put this file into the bundle, but the check could not read it, so nothing in it was checked.",
|
|
28
|
+
example: "a module deleted or locked between the build and the check",
|
|
29
|
+
fix: "Make sure the file exists and is readable, then run sk check again.",
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
code: "SK0002",
|
|
33
|
+
severity: "warning",
|
|
34
|
+
title: "a file of the bundle does not parse",
|
|
35
|
+
explanation:
|
|
36
|
+
"The TypeScript parser reports a syntax error in a file the bundler accepted. The file is skipped, so its findings are missing from the report.",
|
|
37
|
+
example: "const x = ;",
|
|
38
|
+
fix: "Fix the syntax error; if the file is valid for the bundler, report it as a bug in sk check.",
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
code: "SK0003",
|
|
42
|
+
severity: "error",
|
|
43
|
+
title: "the check configuration is invalid",
|
|
44
|
+
explanation:
|
|
45
|
+
'sk.config.json "check" or the app\'s tsconfig cannot be used as written, for example an unknown code in "check.allow". The check stops with exit code 2 so a typo cannot silently allow everything.',
|
|
46
|
+
example: '"check": { "allow": { "SK9999": ["some-package"] } }',
|
|
47
|
+
fix: 'Use codes from `sk explain --list`; "allow" maps a code to package names or app-relative globs.',
|
|
48
|
+
fatal: true,
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
code: "SK0004",
|
|
52
|
+
severity: "error",
|
|
53
|
+
title: "the production build fails, so the bundle's files are unknown",
|
|
54
|
+
explanation:
|
|
55
|
+
"sk check checks exactly the files the production build puts into the bundle. When that build fails, for example on an import that does not resolve, there is no bundle to check.",
|
|
56
|
+
example: 'import { thing } from "a-package-that-is-not-installed";',
|
|
57
|
+
fix: "Fix the build error (sk build shows the same one), then run sk check again.",
|
|
58
|
+
fatal: true,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
code: "SK1001",
|
|
62
|
+
severity: "error",
|
|
63
|
+
title: "a built-in method or property is replaced or deleted",
|
|
64
|
+
explanation:
|
|
65
|
+
"In an AOT image the engine's built-in objects are read-only: replacing or deleting a member they already have throws a TypeError at run time (the build rejects it outright when the assignment is unconditional), even though the property descriptor still reports it as writable. Adding a new member works, except on Object.prototype, Promise.prototype and RegExp.prototype, which take no new members. Assignments guarded by a check for the member (polyfills such as `if (!Array.prototype.at) …`) and assignments that put back the value read from the same property are not reported.",
|
|
66
|
+
example: "Array.prototype.push = function (...items) {\n return items.length;\n};",
|
|
67
|
+
fix: "Leave the built-in alone: call a helper function, or wrap or subclass the object instead of patching it.",
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
code: "SK1002",
|
|
71
|
+
severity: "error",
|
|
72
|
+
title: "a built-in is redefined through Object.defineProperty, Object.assign or Reflect",
|
|
73
|
+
explanation:
|
|
74
|
+
"Redefining an existing member of a built-in prototype or constructor through Object.defineProperty, Object.defineProperties or Object.assign throws a TypeError in an AOT image, as a plain assignment does. Reflect.set and Reflect.defineProperty do not throw: they return false and leave the built-in unchanged, so code that relies on the patch silently misbehaves.",
|
|
75
|
+
example: 'Object.defineProperty(Array.prototype, "includes", { value: () => false });',
|
|
76
|
+
fix: "Do not redefine built-ins; use a helper function or your own subclass.",
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
code: "SK1003",
|
|
80
|
+
severity: "error",
|
|
81
|
+
title: "the prototype of a built-in is changed",
|
|
82
|
+
explanation:
|
|
83
|
+
"Changing what a built-in prototype or constructor inherits from (Object.setPrototypeOf, Reflect.setPrototypeOf or a `__proto__` assignment) throws a TypeError in an AOT image; Reflect.setPrototypeOf returns false instead.",
|
|
84
|
+
example: "Object.setPrototypeOf(Array.prototype, Object.create(null));",
|
|
85
|
+
fix: "Build your own class hierarchy (class MyList extends Array) instead of rewiring the built-in one.",
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
code: "SK1004",
|
|
89
|
+
severity: "error",
|
|
90
|
+
title: "a global built-in is replaced or deleted",
|
|
91
|
+
explanation:
|
|
92
|
+
"Replacing or deleting a global the language itself relies on (Array, Map, Object, Math, JSON, Symbol, …) throws a TypeError in an AOT image. Some globals stay replaceable (Promise, Date, Error, console and others the engine marks so); those are not reported.",
|
|
93
|
+
example: "globalThis.Map = class MyMap<K, V> extends Map<K, V> {};",
|
|
94
|
+
fix: "Import your implementation under its own name instead of installing it over the global.",
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
code: "SK2001",
|
|
98
|
+
severity: "warning",
|
|
99
|
+
title: "eval runs interpreted",
|
|
100
|
+
explanation:
|
|
101
|
+
"eval still works in an AOT image, but an AOT image has no JIT: the evaluated code runs in the interpreter, and it cannot be optimised with the rest of the program. Direct and indirect eval (`(0, eval)(…)`, `globalThis.eval`) are both reported.",
|
|
102
|
+
example: 'const n = Math.random();\nconst value = eval("1 + " + n);',
|
|
103
|
+
fix: "Replace eval with ordinary code (a lookup table, JSON.parse, a small parser).",
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
code: "SK2002",
|
|
107
|
+
severity: "warning",
|
|
108
|
+
title: "the Function constructor runs interpreted",
|
|
109
|
+
explanation:
|
|
110
|
+
"`new Function(…)` and `Function(…)` still work in an AOT image, but the generated function runs in the interpreter, without a JIT. Libraries that generate their hot code this way (validators, template engines, serialisers) lose most of their speed.",
|
|
111
|
+
example: 'const add = new Function("a", "b", "return a + b");',
|
|
112
|
+
fix: "Write the function in source, or switch the library to its non-generating mode (often called jitless or CSP-safe).",
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
code: "SK2003",
|
|
116
|
+
severity: "warning",
|
|
117
|
+
title: "a timer is given a string of code",
|
|
118
|
+
explanation:
|
|
119
|
+
"In a browser, setTimeout, setInterval and setImmediate evaluate a string like eval, which an AOT image could only interpret. Skathi's timers (like Bun's) take functions only: the string never runs, and the timer reports a TypeError when it fires (Bun throws at the call). Code that relies on it is broken on every Skathi host, with or without AOT.",
|
|
120
|
+
example: 'setTimeout("globalThis.ticked = true", 0);',
|
|
121
|
+
fix: "Pass a function: setTimeout(tick, 100).",
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
code: "SK2004",
|
|
125
|
+
severity: "warning",
|
|
126
|
+
title: "import() with a computed specifier",
|
|
127
|
+
explanation:
|
|
128
|
+
"The AOT build can only compile modules it can find at build time. With a computed specifier it compiles every file the expression might name and warns; at run time a path it did not compile fails to load.",
|
|
129
|
+
example: 'const name = "home";\nconst page = await import("./pages/" + name + ".ts");',
|
|
130
|
+
fix: 'Use string literals, for example a map of `() => import("./pages/home.ts")` loaders, or import.meta.glob.',
|
|
131
|
+
},
|
|
132
|
+
{
|
|
133
|
+
code: "SK3001",
|
|
134
|
+
severity: "warning",
|
|
135
|
+
title: "a dependency ships no type declarations",
|
|
136
|
+
explanation:
|
|
137
|
+
"The AOT compiler uses types to lay out objects and specialise code. This package has no `types`/`typings` entry, no .d.ts beside its modules and no @types package installed, so all of it is compiled untyped, through the generic guarded paths.",
|
|
138
|
+
example: "a JavaScript-only package such as an older jQuery plugin",
|
|
139
|
+
fix: "Install its @types package, prefer a typed alternative, or add a .d.ts for the parts you use.",
|
|
140
|
+
},
|
|
141
|
+
{
|
|
142
|
+
code: "SK3002",
|
|
143
|
+
severity: "warning",
|
|
144
|
+
title: "a dependency's declarations use `any` in what the app imports",
|
|
145
|
+
explanation:
|
|
146
|
+
"The declarations of what the app imports from this package type parameters, results or members as `any` (or as the catch-alls `Function`, `Object`, `{}` and `[key: string]: any`). Values that cross these declarations reach the AOT compiler without a type, so code using them takes the generic path. One diagnostic per package, from 15% of the declarations the app reaches (the typing summary lists every package); the worst are listed as related locations. `any` in type-level machinery (constraints, conditional checks, `keyof`, results of callbacks the app passes in, the plumbing of vue-tsc component declarations) is not counted.",
|
|
147
|
+
example: "export declare function parse(input: any): any;",
|
|
148
|
+
fix: "Upgrade to a better-typed version or alternative, or wrap the import in a typed module of your own.",
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
code: "SK3003",
|
|
152
|
+
severity: "warning",
|
|
153
|
+
title: "the app's code uses `any` or suppresses type checking",
|
|
154
|
+
explanation:
|
|
155
|
+
"Explicit `any` (annotations, `as any`, `<any>`), parameters that are implicitly `any`, and `// @ts-nocheck`, `@ts-ignore` or `@ts-expect-error` give the AOT compiler no type information for the code involved. One diagnostic per file; the places are listed as related locations.",
|
|
156
|
+
example: "export function total(items: any) { return items.reduce((n, i) => n + i.price, 0); }",
|
|
157
|
+
fix: "Give the values real types; `unknown` plus narrowing where the type is genuinely open.",
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
code: "SK3004",
|
|
161
|
+
severity: "warning",
|
|
162
|
+
title: "an ambient catch-all or untyped JavaScript makes imports `any`",
|
|
163
|
+
explanation:
|
|
164
|
+
'A shorthand ambient module (`declare module "x";`), a `declare module "*"`, a package whose "exports" lead TypeScript to its JavaScript instead of the declarations it ships, or an app JavaScript file without JSDoc types gives everything imported through it the type `any`.',
|
|
165
|
+
example: 'declare module "some-package";',
|
|
166
|
+
fix: "Remove the shorthand declaration (or give it a body with real types), and convert the JavaScript file to TypeScript or annotate it with JSDoc.",
|
|
167
|
+
},
|
|
168
|
+
];
|
|
169
|
+
|
|
170
|
+
export const REGISTRY: ReadonlyMap<string, CodeInfo> = new Map(CODES.map((c) => [c.code, c]));
|
|
171
|
+
|
|
172
|
+
export function codeInfo(code: string): CodeInfo {
|
|
173
|
+
const info = REGISTRY.get(code);
|
|
174
|
+
if (!info) throw new Error(`unregistered check code ${code}`);
|
|
175
|
+
return info;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export const ALL_CODES: readonly CodeInfo[] = CODES;
|
package/check/command.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { colorEnabled, colorFlag, paint, quietProgress, spinner, type Progress } from "../cli/term";
|
|
2
|
+
import { highlighter, type Highlight } from "./highlight";
|
|
3
|
+
import { exitCode, explain, formatPretty, listCodes, loadTypeScript, runCheck } from "./index";
|
|
4
|
+
|
|
5
|
+
export interface CheckFlags {
|
|
6
|
+
format?: string | boolean | undefined;
|
|
7
|
+
config?: string | boolean | undefined;
|
|
8
|
+
"deny-warnings"?: string | boolean | undefined;
|
|
9
|
+
"no-deps"?: string | boolean | undefined;
|
|
10
|
+
color?: string | boolean | undefined;
|
|
11
|
+
"no-color"?: string | boolean | undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface Io {
|
|
15
|
+
out: (text: string) => void;
|
|
16
|
+
err: (text: string) => void;
|
|
17
|
+
color: boolean;
|
|
18
|
+
progress: () => Progress;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const defaultIo = (flags: Pick<CheckFlags, "color" | "no-color"> = {}): Io => {
|
|
22
|
+
const color = colorEnabled(process.stdout, colorFlag(flags));
|
|
23
|
+
return {
|
|
24
|
+
out: (text) => process.stdout.write(`${text}\n`),
|
|
25
|
+
err: (text) => process.stderr.write(`${text}\n`),
|
|
26
|
+
color,
|
|
27
|
+
progress: () =>
|
|
28
|
+
process.stderr.isTTY ? spinner(process.stderr, colorEnabled(process.stderr, colorFlag(flags))) : quietProgress,
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
function codeHighlight(color: boolean, root: string): Highlight | undefined {
|
|
33
|
+
if (!color) return undefined;
|
|
34
|
+
try {
|
|
35
|
+
return highlighter(loadTypeScript(root), paint(true));
|
|
36
|
+
} catch {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** `sk check [app]`: 0 clean, 1 errors (or warnings with --deny-warnings), 2 usage, configuration or internal failure. */
|
|
42
|
+
export async function checkCommand(
|
|
43
|
+
target: string | undefined,
|
|
44
|
+
flags: CheckFlags,
|
|
45
|
+
toolVersion: string,
|
|
46
|
+
io = defaultIo(flags),
|
|
47
|
+
) {
|
|
48
|
+
const format = flags.format ?? "pretty";
|
|
49
|
+
const p = paint(colorEnabled(process.stderr, colorFlag(flags)));
|
|
50
|
+
const fail = (text: string) => io.err(`${p.red("error")}: ${text}`);
|
|
51
|
+
if (format !== "pretty" && format !== "json") {
|
|
52
|
+
fail("--format is pretty or json");
|
|
53
|
+
return 2;
|
|
54
|
+
}
|
|
55
|
+
if (flags.config !== undefined && (typeof flags.config !== "string" || !flags.config.trim())) {
|
|
56
|
+
fail("--config needs a path");
|
|
57
|
+
return 2;
|
|
58
|
+
}
|
|
59
|
+
const progress = io.progress();
|
|
60
|
+
try {
|
|
61
|
+
const report = await runCheck({
|
|
62
|
+
target: target ?? ".",
|
|
63
|
+
configPath: typeof flags.config === "string" ? flags.config : undefined,
|
|
64
|
+
deps: !flags["no-deps"],
|
|
65
|
+
toolVersion,
|
|
66
|
+
progress,
|
|
67
|
+
});
|
|
68
|
+
progress.end();
|
|
69
|
+
if (format === "json") io.out(JSON.stringify(report, null, 2));
|
|
70
|
+
else {
|
|
71
|
+
const highlight = codeHighlight(io.color, report.root);
|
|
72
|
+
io.out(formatPretty(report, { color: io.color, ...(highlight ? { highlight } : {}) }));
|
|
73
|
+
}
|
|
74
|
+
return exitCode(report, flags["deny-warnings"] === true);
|
|
75
|
+
} catch (e) {
|
|
76
|
+
progress.end();
|
|
77
|
+
fail(e instanceof Error ? e.message : String(e));
|
|
78
|
+
return 2;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** `sk explain <CODE>` and `sk explain --list`. */
|
|
83
|
+
export function explainCommand(
|
|
84
|
+
code: string | undefined,
|
|
85
|
+
list: boolean,
|
|
86
|
+
flags: Pick<CheckFlags, "color" | "no-color"> = {},
|
|
87
|
+
io = defaultIo(flags),
|
|
88
|
+
): number {
|
|
89
|
+
if (list) {
|
|
90
|
+
io.out(listCodes(io.color));
|
|
91
|
+
return 0;
|
|
92
|
+
}
|
|
93
|
+
if (!code) {
|
|
94
|
+
io.err("usage: sk explain <CODE> | sk explain --list");
|
|
95
|
+
return 2;
|
|
96
|
+
}
|
|
97
|
+
const highlight = codeHighlight(io.color, process.cwd());
|
|
98
|
+
const text = explain(code, { color: io.color, ...(highlight ? { highlight } : {}) });
|
|
99
|
+
if (!text) {
|
|
100
|
+
io.err(`sk explain: ${code} is not a check code (sk explain --list shows them)`);
|
|
101
|
+
return 2;
|
|
102
|
+
}
|
|
103
|
+
io.out(text);
|
|
104
|
+
return 0;
|
|
105
|
+
}
|
package/check/config.ts
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { REGISTRY } from "./codes";
|
|
2
|
+
|
|
3
|
+
export interface CheckConfig {
|
|
4
|
+
/** Code -> package names and app-relative globs whose diagnostics of that code are not reported. */
|
|
5
|
+
allow: Map<string, string[]>;
|
|
6
|
+
/** What is wrong, and the JSON key it is about. */
|
|
7
|
+
problems: { message: string; key: string }[];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function parseCheckConfig(raw: unknown): CheckConfig {
|
|
11
|
+
const allow = new Map<string, string[]>();
|
|
12
|
+
const problems: CheckConfig["problems"] = [];
|
|
13
|
+
if (raw === undefined) return { allow, problems };
|
|
14
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) {
|
|
15
|
+
problems.push({
|
|
16
|
+
message: '"check" must be an object, e.g. { "allow": { "SK2001": ["some-package"] } }',
|
|
17
|
+
key: "check",
|
|
18
|
+
});
|
|
19
|
+
return { allow, problems };
|
|
20
|
+
}
|
|
21
|
+
for (const key of Object.keys(raw)) {
|
|
22
|
+
if (key !== "allow" && !key.startsWith("$"))
|
|
23
|
+
problems.push({ message: `"check": unknown key ${JSON.stringify(key)}; the known key is allow`, key });
|
|
24
|
+
}
|
|
25
|
+
const list = (raw as Record<string, unknown>).allow;
|
|
26
|
+
if (list === undefined) return { allow, problems };
|
|
27
|
+
if (!list || typeof list !== "object" || Array.isArray(list)) {
|
|
28
|
+
problems.push({
|
|
29
|
+
message: '"check.allow" must map codes to lists, e.g. { "SK2001": ["some-package", "src/legacy/**"] }',
|
|
30
|
+
key: "allow",
|
|
31
|
+
});
|
|
32
|
+
return { allow, problems };
|
|
33
|
+
}
|
|
34
|
+
for (const [code, value] of Object.entries(list as Record<string, unknown>)) {
|
|
35
|
+
const info = REGISTRY.get(code);
|
|
36
|
+
if (!info) {
|
|
37
|
+
problems.push({
|
|
38
|
+
message: `"check.allow": ${JSON.stringify(code)} is not a check code (sk explain --list shows them)`,
|
|
39
|
+
key: code,
|
|
40
|
+
});
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
if (info.fatal) {
|
|
44
|
+
problems.push({ message: `"check.allow": ${code} stops the check and cannot be allowed`, key: code });
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
if (!Array.isArray(value) || value.some((v) => typeof v !== "string" || !v.trim())) {
|
|
48
|
+
problems.push({
|
|
49
|
+
message: `"check.allow.${code}" must be a list of package names or app-relative globs`,
|
|
50
|
+
key: code,
|
|
51
|
+
});
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
allow.set(
|
|
55
|
+
code,
|
|
56
|
+
value.map((v: string) => v.trim()),
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
return { allow, problems };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export class Suppressions {
|
|
63
|
+
private readonly globs = new Map<string, Bun.Glob>();
|
|
64
|
+
|
|
65
|
+
constructor(private readonly allow: Map<string, string[]>) {}
|
|
66
|
+
|
|
67
|
+
matches(code: string, file: string, packageName: string | null): boolean {
|
|
68
|
+
for (const pattern of this.allow.get(code) ?? []) {
|
|
69
|
+
if (packageName !== null && pattern === packageName) return true;
|
|
70
|
+
let glob = this.globs.get(pattern);
|
|
71
|
+
if (!glob) this.globs.set(pattern, (glob = new Bun.Glob(pattern.replace(/^\.\//, ""))));
|
|
72
|
+
if (glob.match(file)) return true;
|
|
73
|
+
}
|
|
74
|
+
return false;
|
|
75
|
+
}
|
|
76
|
+
}
|
package/check/finding.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type ts from "typescript";
|
|
2
|
+
import type { Range } from "./schema";
|
|
3
|
+
|
|
4
|
+
export interface FindingRelated {
|
|
5
|
+
/** Absolute. */
|
|
6
|
+
file: string;
|
|
7
|
+
range: Range;
|
|
8
|
+
message: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** A diagnostic before it is made relative, attributed and filtered. */
|
|
12
|
+
export interface Finding {
|
|
13
|
+
code: string;
|
|
14
|
+
message: string;
|
|
15
|
+
/** Absolute. */
|
|
16
|
+
file: string;
|
|
17
|
+
range: Range;
|
|
18
|
+
help?: string | undefined;
|
|
19
|
+
related: FindingRelated[];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const ZERO_RANGE: Range = { start: { line: 0, character: 0 }, end: { line: 0, character: 0 } };
|
|
23
|
+
|
|
24
|
+
export function rangeOf(file: ts.SourceFile, start: number, end: number): Range {
|
|
25
|
+
const a = file.getLineAndCharacterOfPosition(start);
|
|
26
|
+
const b = file.getLineAndCharacterOfPosition(end);
|
|
27
|
+
return { start: { line: a.line, character: a.character }, end: { line: b.line, character: b.character } };
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export const nodeRange = (file: ts.SourceFile, node: ts.Node) => rangeOf(file, node.getStart(file), node.getEnd());
|
package/check/graph.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { existsSync, mkdtempSync, readFileSync, rmSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
|
|
5
|
+
/** What `bun prod.ts --graph <file>` writes: the files whose code reaches the bundle. */
|
|
6
|
+
export interface ModuleGraph {
|
|
7
|
+
schema: "skathi.module-graph";
|
|
8
|
+
version: 1;
|
|
9
|
+
entry: string;
|
|
10
|
+
appRoot: string;
|
|
11
|
+
/** Bare module names the build maps to a file (vue, the runtime, sk.config.json "aliases"). */
|
|
12
|
+
aliases: Record<string, string>;
|
|
13
|
+
/** Ambient declarations the runtime contributes. */
|
|
14
|
+
declarations: string[];
|
|
15
|
+
/** Absolute paths of the .js/.ts/.vue files with code in the bundle. */
|
|
16
|
+
modules: string[];
|
|
17
|
+
errors: GraphError[];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface GraphError {
|
|
21
|
+
message: string;
|
|
22
|
+
file: string | null;
|
|
23
|
+
/** One-based. */
|
|
24
|
+
line: number | null;
|
|
25
|
+
/** Zero-based. */
|
|
26
|
+
column: number | null;
|
|
27
|
+
specifier: string | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export class GraphFailure extends Error {}
|
|
31
|
+
|
|
32
|
+
export async function buildGraph(options: {
|
|
33
|
+
prodTs: string;
|
|
34
|
+
entry: string;
|
|
35
|
+
appRoot: string;
|
|
36
|
+
configPath: string | null;
|
|
37
|
+
}): Promise<ModuleGraph> {
|
|
38
|
+
const work = mkdtempSync(join(tmpdir(), "sk-check-"));
|
|
39
|
+
try {
|
|
40
|
+
const out = join(work, "graph.json");
|
|
41
|
+
const child = Bun.spawn(
|
|
42
|
+
[
|
|
43
|
+
process.execPath,
|
|
44
|
+
options.prodTs,
|
|
45
|
+
options.entry,
|
|
46
|
+
join(work, "app.prod.js"),
|
|
47
|
+
`--app-root=${options.appRoot}`,
|
|
48
|
+
...(options.configPath ? [`--config=${options.configPath}`] : []),
|
|
49
|
+
`--graph=${out}`,
|
|
50
|
+
],
|
|
51
|
+
{ cwd: join(options.prodTs, ".."), stdout: "pipe", stderr: "pipe" },
|
|
52
|
+
);
|
|
53
|
+
const [stdout, stderr, status] = await Promise.all([
|
|
54
|
+
new Response(child.stdout).text(),
|
|
55
|
+
new Response(child.stderr).text(),
|
|
56
|
+
child.exited,
|
|
57
|
+
]);
|
|
58
|
+
if (status !== 0 || !existsSync(out)) {
|
|
59
|
+
const output = `${stderr}${stdout}`.trim();
|
|
60
|
+
throw new GraphFailure(output || `the production build exited with ${child.signalCode ?? status}`);
|
|
61
|
+
}
|
|
62
|
+
const graph = JSON.parse(readFileSync(out, "utf8")) as ModuleGraph;
|
|
63
|
+
if (graph.schema !== "skathi.module-graph" || graph.version !== 1)
|
|
64
|
+
throw new GraphFailure("the production builder wrote a module graph this sk check does not understand");
|
|
65
|
+
return graph;
|
|
66
|
+
} finally {
|
|
67
|
+
rmSync(work, { recursive: true, force: true });
|
|
68
|
+
}
|
|
69
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type ts from "typescript";
|
|
2
|
+
import type { Paint } from "../cli/term";
|
|
3
|
+
import type { TypeScript } from "./program";
|
|
4
|
+
|
|
5
|
+
export type Highlight = (code: string) => string;
|
|
6
|
+
|
|
7
|
+
/** Colours one line of TS/JS with the TypeScript scanner; a line cut mid-token still prints as is. */
|
|
8
|
+
export function highlighter(typescript: TypeScript, p: Paint): Highlight {
|
|
9
|
+
const k = typescript.SyntaxKind;
|
|
10
|
+
const style = (kind: ts.SyntaxKind): ((text: string) => string) | null => {
|
|
11
|
+
if (kind === k.SingleLineCommentTrivia || kind === k.MultiLineCommentTrivia) return p.dim;
|
|
12
|
+
if (
|
|
13
|
+
kind === k.StringLiteral ||
|
|
14
|
+
kind === k.NoSubstitutionTemplateLiteral ||
|
|
15
|
+
kind === k.TemplateHead ||
|
|
16
|
+
kind === k.TemplateMiddle ||
|
|
17
|
+
kind === k.TemplateTail
|
|
18
|
+
)
|
|
19
|
+
return p.green;
|
|
20
|
+
if (kind === k.NumericLiteral || kind === k.BigIntLiteral) return p.cyan;
|
|
21
|
+
if (kind >= k.FirstKeyword && kind <= k.LastKeyword) return p.magenta;
|
|
22
|
+
return null;
|
|
23
|
+
};
|
|
24
|
+
return (code) => {
|
|
25
|
+
if (!p.on) return code;
|
|
26
|
+
const scanner = typescript.createScanner(
|
|
27
|
+
typescript.ScriptTarget.Latest,
|
|
28
|
+
false,
|
|
29
|
+
typescript.LanguageVariant.Standard,
|
|
30
|
+
code,
|
|
31
|
+
);
|
|
32
|
+
let out = "";
|
|
33
|
+
for (let kind = scanner.scan(); kind !== k.EndOfFileToken; kind = scanner.scan()) {
|
|
34
|
+
const text = scanner.getTokenText();
|
|
35
|
+
const paint = style(kind);
|
|
36
|
+
out += paint ? paint(text) : text;
|
|
37
|
+
}
|
|
38
|
+
return out;
|
|
39
|
+
};
|
|
40
|
+
}
|