@skathi-native/cli 0.1.0-beta.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/aot/host-globals.json +1 -1
- package/aot/pin.ts +1 -1
- package/aot/toolchain.lock.json +5 -5
- package/aot/transform.ts +9 -1
- package/aot-inputs.ts +21 -2
- package/build.ts +284 -64
- package/check/builtins.json +11 -11
- package/check/bundle.ts +265 -0
- package/check/codes.ts +46 -0
- package/check/css-catalog.json +451 -0
- package/check/css.ts +512 -0
- package/check/finding.ts +63 -1
- package/check/graph.ts +14 -1
- package/check/index.ts +30 -49
- package/check/program.ts +26 -6
- package/cli/aot-build.ts +2 -6
- package/cli/build-app.ts +19 -0
- package/cli/create.ts +70 -12
- package/cli/dev.ts +11 -6
- package/cli/effects.ts +39 -18
- package/cli/gallery.ts +106 -0
- package/cli/sk.ts +5 -2
- package/devtools/commands.ts +26 -1
- package/devtools/ctl.ts +4 -3
- package/effects/compile.ts +160 -31
- package/effects/sk.slang +359 -30
- package/gallery.json +1 -0
- package/package.json +9 -5
- package/prod.ts +78 -10
- package/svelte.ts +76 -0
- package/templates/icon.png +0 -0
- package/templates/showcase/assets/textures/golden.png +0 -0
- package/templates/showcase/assets/textures/hammered.png +0 -0
- package/templates/showcase/assets/textures/noir.png +0 -0
- package/templates/showcase/assets/textures/reeded.png +0 -0
- package/templates/showcase/assets/textures/teal-orange.png +0 -0
- package/templates/showcase/shaders/dissolve.slang +40 -0
- package/templates/showcase/shaders/fireflies.slang +58 -0
- package/templates/showcase/shaders/glass.slang +31 -0
- package/templates/showcase/shaders/grade.slang +34 -0
- package/templates/showcase/shaders/halftone.slang +42 -0
- package/templates/showcase/shaders/lens.slang +51 -0
- package/templates/showcase/shaders/nebula.slang +57 -0
- package/templates/showcase/shaders/orbit.slang +52 -0
- package/templates/showcase/shaders/pond.slang +76 -0
- package/templates/showcase/shaders/ripples.slang +71 -0
- package/templates/showcase/shaders/shatter.slang +116 -0
- package/templates/showcase/shaders/snow.slang +57 -0
- package/templates/showcase/shaders/spotlight.slang +52 -0
- package/templates/showcase/src/App.vue +11 -16
- package/templates/showcase/src/components/FullWindow.vue +77 -0
- package/templates/showcase/src/components/Page.vue +2 -0
- package/templates/showcase/src/fullwindow.ts +36 -0
- package/templates/showcase/src/icons.ts +2 -0
- package/templates/showcase/src/pages/GraphicsPage.vue +122 -0
- package/templates/showcase/src/pages/MotionPage.vue +71 -1
- package/templates/showcase/src/pages/ShadersPage.vue +443 -0
- package/templates/showcase/src/router.ts +32 -1
- package/templates/svelte/.vscode/settings.json +3 -0
- package/templates/svelte/assets/hello.txt +1 -0
- package/templates/svelte/src/App.svelte +142 -0
- package/templates/svelte/src/main.ts +4 -0
- package/vitecompat.ts +2 -0
package/check/css.ts
ADDED
|
@@ -0,0 +1,512 @@
|
|
|
1
|
+
import { parse as parseSfc } from "@vue/compiler-sfc";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { join, resolve } from "node:path";
|
|
4
|
+
import postcss, { type AtRule, type ChildNode, type Container, type Declaration, type Root, type Rule } from "postcss";
|
|
5
|
+
import { diagnosticsOf, type Finding, type FindingRelated } from "./finding";
|
|
6
|
+
import { Packages } from "./packages";
|
|
7
|
+
import type { Diagnostic, Range } from "./schema";
|
|
8
|
+
|
|
9
|
+
interface CatalogEntry {
|
|
10
|
+
name: string;
|
|
11
|
+
status: "supported" | "partial" | "ignored" | "unsupported" | "dropped";
|
|
12
|
+
note: string;
|
|
13
|
+
applied: string[];
|
|
14
|
+
refused: string[];
|
|
15
|
+
webkit: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const catalog = JSON.parse(readFileSync(join(import.meta.dir, "css-catalog.json"), "utf8")) as CatalogEntry[];
|
|
19
|
+
const ENTRIES = new Map(catalog.map((e) => [e.name.toLowerCase(), e]));
|
|
20
|
+
|
|
21
|
+
const squash = (value: string) => value.trim().replace(/\s+/g, " ").toLowerCase();
|
|
22
|
+
const functionsOf = (value: string) =>
|
|
23
|
+
[...value.matchAll(/(?<![\w-])([a-z][\w-]*)\(/gi)].map((m) => m[1]!.toLowerCase());
|
|
24
|
+
|
|
25
|
+
/** `name` when the value is one function call and nothing else. */
|
|
26
|
+
function singleCall(value: string): string | null {
|
|
27
|
+
const text = value.trim();
|
|
28
|
+
const m = /^([a-z][\w-]*)\(/i.exec(text);
|
|
29
|
+
if (!m) return null;
|
|
30
|
+
let depth = 0;
|
|
31
|
+
for (let i = m[0].length - 1; i < text.length; i++) {
|
|
32
|
+
if (text[i] === "(") depth++;
|
|
33
|
+
else if (text[i] === ")" && --depth === 0) return i === text.length - 1 ? m[1]!.toLowerCase() : null;
|
|
34
|
+
}
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Functions a partial property refuses whatever their arguments: a refused sample that is one call, in no applied sample. */
|
|
39
|
+
const REFUSED_FUNCTIONS = new Map(
|
|
40
|
+
catalog.map((e) => {
|
|
41
|
+
const applied = new Set(e.applied.flatMap(functionsOf));
|
|
42
|
+
const refused = e.refused.map(singleCall).filter((f): f is string => f !== null && !applied.has(f));
|
|
43
|
+
return [e.name.toLowerCase(), new Set(refused)];
|
|
44
|
+
}),
|
|
45
|
+
);
|
|
46
|
+
|
|
47
|
+
const CSS_WIDE = new Set(["inherit", "initial", "unset", "revert", "revert-layer"]);
|
|
48
|
+
/** Unsupported properties set to what a browser would compute anyway: no difference to report. */
|
|
49
|
+
const INERT_DEFAULTS = new Set(["none", "normal", "auto"]);
|
|
50
|
+
const INERT_FOR: Record<string, string> = {
|
|
51
|
+
direction: "ltr",
|
|
52
|
+
"writing-mode": "horizontal-tb",
|
|
53
|
+
"backface-visibility": "visible",
|
|
54
|
+
"transform-style": "flat",
|
|
55
|
+
"border-collapse": "separate",
|
|
56
|
+
"text-orientation": "mixed",
|
|
57
|
+
};
|
|
58
|
+
const SUBSTITUTED = /(?<![\w-])(var|env|theme|screen)\(|#\{|\$[\w-]/i;
|
|
59
|
+
|
|
60
|
+
const UNSUPPORTED_AT_RULES: Record<string, string> = {
|
|
61
|
+
container: "Use a `@media` query on the window size, or set a class from script.",
|
|
62
|
+
layer: "Move the rules out of the `@layer` block; their order in the sheet decides the cascade.",
|
|
63
|
+
scope: "Scope the rules with a class selector instead.",
|
|
64
|
+
"starting-style": "Start the transition from script: set the start values, then change them on the next frame.",
|
|
65
|
+
"counter-style": "Use one of the predefined list styles (`disc`, `decimal`, …) or draw the marker yourself.",
|
|
66
|
+
"font-feature-values": "Use `font-feature-settings` with the feature tags.",
|
|
67
|
+
namespace: "Drop the namespace and select by element name or class.",
|
|
68
|
+
};
|
|
69
|
+
const RECURSE_AT_RULES = new Set(["media", "keyframes", "-webkit-keyframes", "-moz-keyframes"]);
|
|
70
|
+
|
|
71
|
+
const STATE_FIX = "Set a class or data attribute from script and select on that.";
|
|
72
|
+
const PSEUDO_CLASSES: Record<string, string> = Object.fromEntries([
|
|
73
|
+
...[
|
|
74
|
+
"placeholder-shown",
|
|
75
|
+
"read-only",
|
|
76
|
+
"read-write",
|
|
77
|
+
"required",
|
|
78
|
+
"optional",
|
|
79
|
+
"indeterminate",
|
|
80
|
+
"default",
|
|
81
|
+
"autofill",
|
|
82
|
+
"valid",
|
|
83
|
+
"invalid",
|
|
84
|
+
"user-valid",
|
|
85
|
+
"user-invalid",
|
|
86
|
+
"in-range",
|
|
87
|
+
"out-of-range",
|
|
88
|
+
"blank",
|
|
89
|
+
"link",
|
|
90
|
+
"visited",
|
|
91
|
+
"any-link",
|
|
92
|
+
"local-link",
|
|
93
|
+
"target",
|
|
94
|
+
"target-within",
|
|
95
|
+
"lang",
|
|
96
|
+
"dir",
|
|
97
|
+
"modal",
|
|
98
|
+
"popover-open",
|
|
99
|
+
"open",
|
|
100
|
+
"closed",
|
|
101
|
+
"defined",
|
|
102
|
+
"picture-in-picture",
|
|
103
|
+
"playing",
|
|
104
|
+
"paused",
|
|
105
|
+
"seeking",
|
|
106
|
+
"buffering",
|
|
107
|
+
"stalled",
|
|
108
|
+
"muted",
|
|
109
|
+
"volume-locked",
|
|
110
|
+
"current",
|
|
111
|
+
"past",
|
|
112
|
+
"future",
|
|
113
|
+
].map((name) => [name, STATE_FIX]),
|
|
114
|
+
]);
|
|
115
|
+
const LADDER_STATES = /:(hover|active|focus|focus-visible|focus-within)(?![\w-])/i;
|
|
116
|
+
const FUNCTIONAL = /:(not|is|where|matches|any|-webkit-any|-moz-any)\(/gi;
|
|
117
|
+
|
|
118
|
+
interface Block {
|
|
119
|
+
css: string;
|
|
120
|
+
/** Offset of `css` in the file. */
|
|
121
|
+
offset: number;
|
|
122
|
+
/** Declarations only (a `style` attribute). */
|
|
123
|
+
inline: boolean;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** The CSS of one file at its own offsets: a stylesheet, the `<style>` blocks of a component or page, its static `style` attributes. */
|
|
127
|
+
export function styleBlocks(path: string, source: string): Block[] {
|
|
128
|
+
if (/\.css$/i.test(path)) return [{ css: source, offset: 0, inline: false }];
|
|
129
|
+
if (/\.vue$/i.test(path)) return vueBlocks(path, source);
|
|
130
|
+
if (/\.(svelte|html?)$/i.test(path)) return markupBlocks(source, /\.svelte$/i.test(path));
|
|
131
|
+
return [];
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
const plainCss = (lang: string | undefined) => !lang || lang === "css" || lang === "postcss";
|
|
135
|
+
|
|
136
|
+
function vueBlocks(path: string, source: string): Block[] {
|
|
137
|
+
const { descriptor } = parseSfc(source, { filename: path });
|
|
138
|
+
const blocks: Block[] = descriptor.styles
|
|
139
|
+
.filter((s) => !s.src && plainCss(s.lang))
|
|
140
|
+
.map((s) => ({
|
|
141
|
+
css: s.content,
|
|
142
|
+
offset: s.loc.start.offset,
|
|
143
|
+
inline: false,
|
|
144
|
+
}));
|
|
145
|
+
type Node = { type: number; props?: unknown[]; children?: unknown[] };
|
|
146
|
+
type Attr = {
|
|
147
|
+
type: number;
|
|
148
|
+
name: string;
|
|
149
|
+
value?: { loc: { start: { offset: number }; end: { offset: number } } };
|
|
150
|
+
};
|
|
151
|
+
const visit = (node: Node) => {
|
|
152
|
+
for (const prop of (node.props ?? []) as Attr[]) {
|
|
153
|
+
if (prop.type !== 6 || prop.name !== "style" || !prop.value) continue;
|
|
154
|
+
let start = prop.value.loc.start.offset;
|
|
155
|
+
let end = prop.value.loc.end.offset;
|
|
156
|
+
if (/["']/.test(source[start] ?? "")) {
|
|
157
|
+
start++;
|
|
158
|
+
end--;
|
|
159
|
+
}
|
|
160
|
+
blocks.push({
|
|
161
|
+
css: source.slice(start, end),
|
|
162
|
+
offset: start,
|
|
163
|
+
inline: true,
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
for (const child of (node.children ?? []) as Node[]) if (child && typeof child === "object") visit(child);
|
|
167
|
+
};
|
|
168
|
+
if (descriptor.template?.ast) visit(descriptor.template.ast as unknown as Node);
|
|
169
|
+
return blocks;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const blank = (text: string) => text.replace(/[^\r\n]/g, " ");
|
|
173
|
+
|
|
174
|
+
function markupBlocks(source: string, attributes: boolean): Block[] {
|
|
175
|
+
const blocks: Block[] = [];
|
|
176
|
+
let markup = source.replace(/<!--[\s\S]*?-->/g, blank);
|
|
177
|
+
for (const m of markup.matchAll(/<style(\s[^>]*)?>([\s\S]*?)<\/style\s*>/gi)) {
|
|
178
|
+
const lang = /\blang\s*=\s*["']?([\w-]+)/i.exec(m[1] ?? "")?.[1]?.toLowerCase();
|
|
179
|
+
if (plainCss(lang))
|
|
180
|
+
blocks.push({
|
|
181
|
+
css: m[2]!,
|
|
182
|
+
offset: m.index + m[0].indexOf(">") + 1,
|
|
183
|
+
inline: false,
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
if (!attributes) return blocks;
|
|
187
|
+
markup = markup.replace(/<(script|style)\b[\s\S]*?<\/\1\s*>/gi, blank);
|
|
188
|
+
for (const m of markup.matchAll(/<[a-zA-Z][^\s/>]*\s[^>]*?\bstyle\s*=\s*(["'])([^"'{}]*)\1/g)) {
|
|
189
|
+
const offset = m.index + m[0].length - 1 - m[2]!.length;
|
|
190
|
+
blocks.push({ css: m[2]!, offset, inline: true });
|
|
191
|
+
}
|
|
192
|
+
return blocks;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
class Lines {
|
|
196
|
+
private readonly starts: number[] = [0];
|
|
197
|
+
|
|
198
|
+
constructor(text: string) {
|
|
199
|
+
for (let i = 0; i < text.length; i++) if (text[i] === "\n") this.starts.push(i + 1);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
position(offset: number) {
|
|
203
|
+
let lo = 0;
|
|
204
|
+
let hi = this.starts.length - 1;
|
|
205
|
+
while (lo < hi) {
|
|
206
|
+
const mid = (lo + hi + 1) >> 1;
|
|
207
|
+
if (this.starts[mid]! <= offset) lo = mid;
|
|
208
|
+
else hi = mid - 1;
|
|
209
|
+
}
|
|
210
|
+
return { line: lo, character: offset - this.starts[lo]! };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
range(start: number, end: number): Range {
|
|
214
|
+
return {
|
|
215
|
+
start: this.position(start),
|
|
216
|
+
end: this.position(Math.max(start, end)),
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
const sentence = (text: string) => (text && !/[.!?)]$/.test(text) ? `${text}.` : text);
|
|
222
|
+
|
|
223
|
+
function entryOf(property: string): CatalogEntry | undefined {
|
|
224
|
+
const own = ENTRIES.get(property);
|
|
225
|
+
if (own) return own;
|
|
226
|
+
const base = property.startsWith("-webkit-") ? ENTRIES.get(property.slice(8)) : undefined;
|
|
227
|
+
return base?.webkit ? base : undefined;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** What the engine does not apply in one declaration: a code, what is wrong and what to do. */
|
|
231
|
+
export function declarationProblem(
|
|
232
|
+
property: string,
|
|
233
|
+
value: string,
|
|
234
|
+
): { code: string; message: string; help: string } | null {
|
|
235
|
+
const prop = property.trim().toLowerCase();
|
|
236
|
+
if (prop.startsWith("--")) return null;
|
|
237
|
+
const entry = entryOf(prop);
|
|
238
|
+
if (!entry) return null;
|
|
239
|
+
const v = squash(value);
|
|
240
|
+
if (!v || CSS_WIDE.has(v) || SUBSTITUTED.test(value)) return null;
|
|
241
|
+
const keep = `Remove it, or keep it for browsers inside \`@supports (${prop}: ${value.trim()})\`.`;
|
|
242
|
+
if (entry.status === "unsupported") {
|
|
243
|
+
if (INERT_DEFAULTS.has(v) || INERT_FOR[prop] === v) return null;
|
|
244
|
+
return {
|
|
245
|
+
code: "SK4001",
|
|
246
|
+
message: `\`${prop}\` is not supported: this declaration has no effect`,
|
|
247
|
+
help: [sentence(entry.note), keep].filter(Boolean).join(" "),
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
if (entry.status !== "partial") return null;
|
|
251
|
+
const refused = entry.refused.some((r) => squash(r) === v)
|
|
252
|
+
? v
|
|
253
|
+
: functionsOf(v).find((f) => REFUSED_FUNCTIONS.get(entry.name.toLowerCase())?.has(f));
|
|
254
|
+
if (!refused) return null;
|
|
255
|
+
const what = refused === v ? `\`${prop}: ${value.trim()}\`` : `\`${refused}()\` in \`${prop}\``;
|
|
256
|
+
return {
|
|
257
|
+
code: "SK4002",
|
|
258
|
+
message: `${what} is not applied`,
|
|
259
|
+
help: [sentence(entry.note), keep].filter(Boolean).join(" "),
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/** Selector parts no rule can match here, each with what to do instead. */
|
|
264
|
+
export function selectorProblems(selector: string): { what: string; help: string }[] {
|
|
265
|
+
const text = selector
|
|
266
|
+
.replace(/\\./g, " ")
|
|
267
|
+
.replace(/\/\*[\s\S]*?\*\//g, blank)
|
|
268
|
+
.replace(/"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'/g, blank)
|
|
269
|
+
.replace(/\[[^\]]*\]/g, blank);
|
|
270
|
+
const out = new Map<string, string>();
|
|
271
|
+
for (const m of text.matchAll(/(?<!:):(?!:)(-webkit-)?([a-z][\w-]*)(\()?/gi)) {
|
|
272
|
+
const name = m[2]!.toLowerCase();
|
|
273
|
+
if (m[1] && name !== "autofill") continue;
|
|
274
|
+
const help = PSEUDO_CLASSES[name];
|
|
275
|
+
if (help) out.set(`the pseudo-class \`:${name}${m[3] ? "()" : ""}\``, help);
|
|
276
|
+
}
|
|
277
|
+
for (const m of text.matchAll(
|
|
278
|
+
/::?(first-line|first-letter)(?![\w-])|::(marker|-webkit-scrollbar[\w-]*)(?![\w-])/gi,
|
|
279
|
+
)) {
|
|
280
|
+
const name = (m[1] ?? m[2]!).toLowerCase();
|
|
281
|
+
out.set(
|
|
282
|
+
`the pseudo-element \`::${name}\``,
|
|
283
|
+
name.startsWith("-webkit-scrollbar")
|
|
284
|
+
? "Style scrollbars with `scrollbar-width` and `scrollbar-color`."
|
|
285
|
+
: "Wrap the part in its own element and style that.",
|
|
286
|
+
);
|
|
287
|
+
}
|
|
288
|
+
if (/:nth-(last-)?child\([^)]*\sof\s/i.test(text))
|
|
289
|
+
out.set("`:nth-child(… of S)`", "Select the subset with a class and use `:nth-child()` without `of`.");
|
|
290
|
+
for (const m of text.matchAll(FUNCTIONAL)) {
|
|
291
|
+
let depth = 1;
|
|
292
|
+
let i = m.index + m[0].length;
|
|
293
|
+
const from = i;
|
|
294
|
+
for (; i < text.length && depth > 0; i++) {
|
|
295
|
+
if (text[i] === "(") depth++;
|
|
296
|
+
else if (text[i] === ")") depth--;
|
|
297
|
+
}
|
|
298
|
+
const state = LADDER_STATES.exec(text.slice(from, i - 1));
|
|
299
|
+
if (state)
|
|
300
|
+
out.set(
|
|
301
|
+
`\`:${state[1]!.toLowerCase()}\` inside \`:${m[1]!.toLowerCase()}()\``,
|
|
302
|
+
`Put \`:${state[1]!.toLowerCase()}\` on the compound itself, outside the parentheses, or set a class from script.`,
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
return [...out].map(([what, help]) => ({ what, help }));
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
interface Context {
|
|
309
|
+
file: string;
|
|
310
|
+
lines: Lines;
|
|
311
|
+
offset: number;
|
|
312
|
+
tailwind: boolean;
|
|
313
|
+
out: Finding[];
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
function push(ctx: Context, code: string, message: string, help: string, start: number, end: number) {
|
|
317
|
+
ctx.out.push({
|
|
318
|
+
code,
|
|
319
|
+
message,
|
|
320
|
+
file: ctx.file,
|
|
321
|
+
range: ctx.lines.range(ctx.offset + start, ctx.offset + end),
|
|
322
|
+
help,
|
|
323
|
+
related: [],
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
const startOf = (node: ChildNode) => node.source?.start?.offset ?? 0;
|
|
328
|
+
|
|
329
|
+
function visit(ctx: Context, container: Container, keyframes: boolean) {
|
|
330
|
+
for (const node of container.nodes ?? []) {
|
|
331
|
+
if (node.type === "decl") declaration(ctx, node);
|
|
332
|
+
else if (node.type === "rule") rule(ctx, node, keyframes);
|
|
333
|
+
else if (node.type === "atrule") atRule(ctx, node);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function declaration(ctx: Context, decl: Declaration) {
|
|
338
|
+
const problem = declarationProblem(decl.prop, decl.value);
|
|
339
|
+
if (!problem) return;
|
|
340
|
+
const start = startOf(decl);
|
|
341
|
+
push(ctx, problem.code, problem.message, problem.help, start, start + decl.toString().length);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
function rule(ctx: Context, node: Rule, keyframes: boolean) {
|
|
345
|
+
if (!keyframes) {
|
|
346
|
+
const start = startOf(node);
|
|
347
|
+
for (const { what, help } of selectorProblems(node.selector))
|
|
348
|
+
push(
|
|
349
|
+
ctx,
|
|
350
|
+
"SK4003",
|
|
351
|
+
`${what} never matches here, so this rule is dropped`,
|
|
352
|
+
help,
|
|
353
|
+
start,
|
|
354
|
+
start + node.selector.length,
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
visit(ctx, node, keyframes);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
function atRule(ctx: Context, node: AtRule) {
|
|
361
|
+
const name = node.name.toLowerCase();
|
|
362
|
+
if (RECURSE_AT_RULES.has(name)) {
|
|
363
|
+
visit(ctx, node, name !== "media");
|
|
364
|
+
return;
|
|
365
|
+
}
|
|
366
|
+
// Tailwind consumes `@layer` blocks before the engine sees them; a bare `@layer a, b;` changes nothing.
|
|
367
|
+
if (name === "layer" && ctx.tailwind) {
|
|
368
|
+
visit(ctx, node, false);
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
const help = UNSUPPORTED_AT_RULES[name];
|
|
372
|
+
if (!help || (name === "layer" && node.nodes === undefined)) return;
|
|
373
|
+
const start = startOf(node);
|
|
374
|
+
push(
|
|
375
|
+
ctx,
|
|
376
|
+
"SK4004",
|
|
377
|
+
`\`@${name}\` is not supported: ${node.nodes === undefined ? "it is ignored" : "the rules inside it are dropped"}`,
|
|
378
|
+
help,
|
|
379
|
+
start,
|
|
380
|
+
start + name.length + 1,
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** One file's CSS findings. A block that does not parse is skipped: the build reports it. */
|
|
385
|
+
export function fileFindings(file: string, source: string, tailwind: boolean): Finding[] {
|
|
386
|
+
const out: Finding[] = [];
|
|
387
|
+
const lines = new Lines(source);
|
|
388
|
+
for (const block of styleBlocks(file, source)) {
|
|
389
|
+
let root: Root;
|
|
390
|
+
try {
|
|
391
|
+
root = postcss.parse(block.css);
|
|
392
|
+
} catch {
|
|
393
|
+
continue;
|
|
394
|
+
}
|
|
395
|
+
const ctx: Context = { file, lines, offset: block.offset, tailwind, out };
|
|
396
|
+
if (block.inline) for (const node of root.nodes) node.type === "decl" && declaration(ctx, node);
|
|
397
|
+
else visit(ctx, root, false);
|
|
398
|
+
}
|
|
399
|
+
return out;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
const MAX_RELATED = 50;
|
|
403
|
+
|
|
404
|
+
/** A stylesheet, component or page and its text; offsets inside it come from `styleBlocks`. */
|
|
405
|
+
export interface StyleSource {
|
|
406
|
+
/** Absolute. */
|
|
407
|
+
file: string;
|
|
408
|
+
text: string;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** Each readable file once; one that cannot be read is left out. */
|
|
412
|
+
export function readStyleSources(files: Iterable<string>): StyleSource[] {
|
|
413
|
+
const out: StyleSource[] = [];
|
|
414
|
+
for (const file of new Set(files)) {
|
|
415
|
+
try {
|
|
416
|
+
out.push({ file, text: readFileSync(file, "utf8").replace(/^/, "") });
|
|
417
|
+
} catch {}
|
|
418
|
+
}
|
|
419
|
+
return out;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
export interface CssPassOptions {
|
|
423
|
+
root: string;
|
|
424
|
+
/** sk.config.json `check.allow`. */
|
|
425
|
+
allow: Map<string, string[]>;
|
|
426
|
+
tailwind: boolean;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
/** The CSS pass of `sk check` on its own, dependencies included, as sk build and sk dev report it. */
|
|
430
|
+
export function cssDiagnostics(
|
|
431
|
+
options: CssPassOptions & { sources: StyleSource[]; packages?: Packages | undefined },
|
|
432
|
+
): Diagnostic[] {
|
|
433
|
+
const root = resolve(options.root);
|
|
434
|
+
const packages = options.packages ?? new Packages(root);
|
|
435
|
+
const findings = cssFindings({ sources: options.sources, packages, deps: true, tailwind: options.tailwind });
|
|
436
|
+
return diagnosticsOf(findings, root, packages, options.allow).diagnostics;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/** sk dev: the pass over the files whose text changed since the last call, each warning once per session. */
|
|
440
|
+
export class CssWarnings {
|
|
441
|
+
private readonly texts = new Map<string, string>();
|
|
442
|
+
private readonly shown = new Set<string>();
|
|
443
|
+
private readonly packages: Packages;
|
|
444
|
+
|
|
445
|
+
constructor(private readonly options: CssPassOptions) {
|
|
446
|
+
this.packages = new Packages(resolve(options.root));
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
update(files: Iterable<string>): Diagnostic[] {
|
|
450
|
+
const sources = readStyleSources(files).filter((s) => this.texts.get(s.file) !== s.text);
|
|
451
|
+
if (!sources.length) return [];
|
|
452
|
+
for (const s of sources) this.texts.set(s.file, s.text);
|
|
453
|
+
// Keyed by place in order rather than line, so an edit above a warning does not repeat it.
|
|
454
|
+
const nth = new Map<string, number>();
|
|
455
|
+
return cssDiagnostics({ ...this.options, sources, packages: this.packages }).filter((d) => {
|
|
456
|
+
const what = `${d.file}\0${d.code}\0${d.message}`;
|
|
457
|
+
const n = (nth.get(what) ?? 0) + 1;
|
|
458
|
+
nth.set(what, n);
|
|
459
|
+
const key = `${what}\0${n}`;
|
|
460
|
+
if (this.shown.has(key)) return false;
|
|
461
|
+
this.shown.add(key);
|
|
462
|
+
return true;
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* CSS the engine does not apply, in the app's components, pages and stylesheets. A dependency's
|
|
469
|
+
* findings become one SK4005 per package, its places listed as related locations.
|
|
470
|
+
*/
|
|
471
|
+
export function cssFindings(input: {
|
|
472
|
+
sources: StyleSource[];
|
|
473
|
+
packages: Packages;
|
|
474
|
+
deps: boolean;
|
|
475
|
+
tailwind: boolean;
|
|
476
|
+
}): Finding[] {
|
|
477
|
+
const out: Finding[] = [];
|
|
478
|
+
const byPackage = new Map<string, { name: string; found: Finding[] }>();
|
|
479
|
+
for (const { file, text } of input.sources) {
|
|
480
|
+
const owner = input.packages.ownerOf(file);
|
|
481
|
+
if (owner && !input.deps) continue;
|
|
482
|
+
let found: Finding[];
|
|
483
|
+
try {
|
|
484
|
+
found = fileFindings(file, text, input.tailwind);
|
|
485
|
+
} catch {
|
|
486
|
+
continue;
|
|
487
|
+
}
|
|
488
|
+
if (!owner) {
|
|
489
|
+
out.push(...found);
|
|
490
|
+
continue;
|
|
491
|
+
}
|
|
492
|
+
const group = byPackage.get(owner.dir) ?? { name: owner.name, found: [] };
|
|
493
|
+
group.found.push(...found);
|
|
494
|
+
byPackage.set(owner.dir, group);
|
|
495
|
+
}
|
|
496
|
+
for (const { name, found } of byPackage.values()) {
|
|
497
|
+
const first = found[0];
|
|
498
|
+
if (!first) continue;
|
|
499
|
+
const related: FindingRelated[] = found
|
|
500
|
+
.slice(0, MAX_RELATED)
|
|
501
|
+
.map((f) => ({ file: f.file, range: f.range, message: f.message }));
|
|
502
|
+
out.push({
|
|
503
|
+
code: "SK4005",
|
|
504
|
+
message: `${found.length === 1 ? "1 rule or declaration" : `${found.length} rules and declarations`} in ${name}'s CSS ${found.length === 1 ? "is" : "are"} not applied by Skathi`,
|
|
505
|
+
file: first.file,
|
|
506
|
+
range: first.range,
|
|
507
|
+
help: "Check that the parts of the package the app uses look right; allow SK4005 for the package when they do.",
|
|
508
|
+
related,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
return out;
|
|
512
|
+
}
|
package/check/finding.ts
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
import { relative, sep } from "node:path";
|
|
1
2
|
import type ts from "typescript";
|
|
2
|
-
import
|
|
3
|
+
import { codeInfo } from "./codes";
|
|
4
|
+
import { Suppressions } from "./config";
|
|
5
|
+
import type { Packages } from "./packages";
|
|
6
|
+
import type { Diagnostic, Range } from "./schema";
|
|
3
7
|
|
|
4
8
|
export interface FindingRelated {
|
|
5
9
|
/** Absolute. */
|
|
@@ -28,3 +32,61 @@ export function rangeOf(file: ts.SourceFile, start: number, end: number): Range
|
|
|
28
32
|
}
|
|
29
33
|
|
|
30
34
|
export const nodeRange = (file: ts.SourceFile, node: ts.Node) => rangeOf(file, node.getStart(file), node.getEnd());
|
|
35
|
+
|
|
36
|
+
/** Packages the SDK provides and answers for under AOT; an app cannot swap or retype them. */
|
|
37
|
+
export const PLATFORM = new Set([
|
|
38
|
+
"vue",
|
|
39
|
+
"@vue/runtime-core",
|
|
40
|
+
"@vue/runtime-dom",
|
|
41
|
+
"@vue/reactivity",
|
|
42
|
+
"@vue/shared",
|
|
43
|
+
"@skathi-native/runtime",
|
|
44
|
+
]);
|
|
45
|
+
|
|
46
|
+
export const posix = (path: string) => path.split(sep).join("/");
|
|
47
|
+
|
|
48
|
+
/** Findings as sorted diagnostics relative to `root`: the platform's own dropped, `check.allow` applied. */
|
|
49
|
+
export function diagnosticsOf(
|
|
50
|
+
findings: Finding[],
|
|
51
|
+
root: string,
|
|
52
|
+
packages: Packages,
|
|
53
|
+
allow: Map<string, string[]>,
|
|
54
|
+
): { diagnostics: Diagnostic[]; suppressed: number } {
|
|
55
|
+
const rel = (file: string) => posix(relative(root, file));
|
|
56
|
+
const suppress = new Suppressions(allow);
|
|
57
|
+
const diagnostics: Diagnostic[] = [];
|
|
58
|
+
let suppressed = 0;
|
|
59
|
+
for (const f of findings) {
|
|
60
|
+
const info = codeInfo(f.code);
|
|
61
|
+
const owner = packages.ownerOf(f.file);
|
|
62
|
+
if (owner && PLATFORM.has(owner.name)) continue;
|
|
63
|
+
const file = rel(f.file);
|
|
64
|
+
if (!info.fatal && suppress.matches(f.code, file, owner?.name ?? null)) {
|
|
65
|
+
suppressed++;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
diagnostics.push({
|
|
69
|
+
code: f.code,
|
|
70
|
+
severity: info.severity,
|
|
71
|
+
message: f.message,
|
|
72
|
+
file,
|
|
73
|
+
package: owner ? { name: owner.name, version: owner.version } : null,
|
|
74
|
+
range: f.range,
|
|
75
|
+
help: f.help ?? info.fix,
|
|
76
|
+
related: f.related.map((r) => ({ file: rel(r.file), range: r.range, message: r.message })),
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
diagnostics.sort(
|
|
80
|
+
(a, b) =>
|
|
81
|
+
(a.file < b.file ? -1 : a.file > b.file ? 1 : 0) ||
|
|
82
|
+
a.range.start.line - b.range.start.line ||
|
|
83
|
+
a.range.start.character - b.range.start.character ||
|
|
84
|
+
a.code.localeCompare(b.code),
|
|
85
|
+
);
|
|
86
|
+
return { diagnostics, suppressed };
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** One line per diagnostic, for warnings that do not stop a build. */
|
|
90
|
+
export function diagnosticLine(d: Diagnostic): string {
|
|
91
|
+
return `${d.file}:${d.range.start.line + 1}:${d.range.start.character + 1} ${d.code} ${d.message}`;
|
|
92
|
+
}
|
package/check/graph.ts
CHANGED
|
@@ -12,11 +12,24 @@ 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
|
+
/** Absolute paths of the .css files (and index.html, for its `<style>`) whose CSS the build includes. */
|
|
18
|
+
styles?: string[];
|
|
19
|
+
/** The build runs Tailwind over the app's CSS. */
|
|
20
|
+
tailwind?: boolean;
|
|
21
|
+
/** The production bundle and its sourcemap; a source is null when its positions are not the file's own. */
|
|
22
|
+
bundle?: { code: string; sources: (string | null)[]; mappings: string };
|
|
17
23
|
errors: GraphError[];
|
|
18
24
|
}
|
|
19
25
|
|
|
26
|
+
/** What `bun prod.ts` writes to styles.json beside the bundle. */
|
|
27
|
+
export interface BuildStyles {
|
|
28
|
+
/** Absolute paths of the components, stylesheets and pages whose CSS the bundle includes. */
|
|
29
|
+
files: string[];
|
|
30
|
+
tailwind: boolean;
|
|
31
|
+
}
|
|
32
|
+
|
|
20
33
|
export interface GraphError {
|
|
21
34
|
message: string;
|
|
22
35
|
file: string | null;
|