@panomc/plugin-kit 0.0.0-stage → 1.0.0-dev.61
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/assets/pano-bootstrap.css +17903 -0
- package/bin/__tests__/new/e2e.test.js +111 -0
- package/bin/__tests__/new/helpers.js +76 -0
- package/bin/__tests__/new/new.test.js +472 -0
- package/bin/new.js +530 -0
- package/bin/pano-plugin.js +162 -0
- package/bin/templates/plugin/README.md +30 -0
- package/bin/templates/plugin/_gitignore +15 -0
- package/bin/templates/plugin/build.gradle.kts +389 -0
- package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/bin/templates/plugin/gradle/wrapper/gradle-wrapper.properties +7 -0
- package/bin/templates/plugin/gradle.properties +55 -0
- package/bin/templates/plugin/gradlew +249 -0
- package/bin/templates/plugin/gradlew.bat +92 -0
- package/bin/templates/plugin/package.json.tpl +14 -0
- package/bin/templates/plugin/rollup.config.js +2 -0
- package/bin/templates/plugin/settings.gradle.kts +5 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/__CLASS__Plugin.kt +50 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/frontend/ExampleFallbackPage.kt +28 -0
- package/bin/templates/plugin/src/main/kotlin/__PKGPATH__/routes/GetHelloAPI.kt +19 -0
- package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/LicenseGuard.kt +30 -0
- package/bin/templates/plugin/src/main/kotlin/com/panomc/plugins/license/PluginLicenseClient.kt +144 -0
- package/bin/templates/plugin/src/main/resources/config.conf +1 -0
- package/bin/templates/plugin/src/main/resources/frontend-targets.json +1 -0
- package/bin/templates/plugin/src/main/resources/locales/en-US.json +3 -0
- package/bin/templates/plugin/src/main/resources/logo.png +0 -0
- package/bin/templates/plugin/src/theme/views/HelloPage.svelte +26 -0
- package/bin/ui.js +96 -0
- package/package.json +46 -4
- package/src/config.js +191 -0
- package/src/controller/__tests__/controller.test.js +647 -0
- package/src/controller/index.js +604 -0
- package/src/dev.js +208 -0
- package/src/index.js +22 -0
- package/src/rollup/__tests__/controllers/controllers.test.js +418 -0
- package/src/rollup/__tests__/controllers/fixture.js +143 -0
- package/src/rollup/__tests__/controllers/golden/controllers.json +34 -0
- package/src/rollup/__tests__/controllers/golden/controllers.types.js +69 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.json +16 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.lock.json +27 -0
- package/src/rollup/__tests__/controllers/golden/pano-plugin.with-views.json +54 -0
- package/src/rollup/__tests__/preset/preset.test.js +418 -0
- package/src/rollup/__tests__/rules/_build.js +39 -0
- package/src/rollup/__tests__/rules/_fixture.js +83 -0
- package/src/rollup/__tests__/rules/helpers.test.js +148 -0
- package/src/rollup/__tests__/rules/rules.test.js +259 -0
- package/src/rollup/__tests__/rules/stamp.test.js +113 -0
- package/src/rollup/__tests__/samples/samples.test.js +481 -0
- package/src/rollup/__tests__/seams/config.test.js +78 -0
- package/src/rollup/__tests__/seams/lock.test.js +124 -0
- package/src/rollup/__tests__/seams/sdk-seams.test.js +93 -0
- package/src/rollup/__tests__/seams/stubs.test.js +52 -0
- package/src/rollup/__tests__/views/helpers.js +82 -0
- package/src/rollup/__tests__/views/views.test.js +522 -0
- package/src/rollup/__tests__/widgets/helpers.js +117 -0
- package/src/rollup/__tests__/widgets/widgets.test.js +422 -0
- package/src/rollup/cli-build.js +90 -0
- package/src/rollup/cli-check.js +280 -0
- package/src/rollup/cli-classes.js +75 -0
- package/src/rollup/client-gen.js +72 -0
- package/src/rollup/controllers.js +434 -0
- package/src/rollup/entry.js +250 -0
- package/src/rollup/helpers.js +193 -0
- package/src/rollup/index.js +601 -0
- package/src/rollup/integrate.js +205 -0
- package/src/rollup/lock.js +230 -0
- package/src/rollup/meta.js +479 -0
- package/src/rollup/rules.js +950 -0
- package/src/rollup/samples.js +667 -0
- package/src/rollup/stamp.js +410 -0
- package/src/rollup/types.js +248 -0
- package/src/rollup/views.js +1205 -0
- package/src/rollup/widgets.js +1000 -0
- package/src/styles/__tests__/fallback/__snapshots__/build-fallback-css.test.js.snap +173 -0
- package/src/styles/__tests__/fallback/build-fallback-css.test.js +526 -0
- package/src/styles/__tests__/fallback/fixtures/base.css +247 -0
- package/src/styles/__tests__/fallback/plugins.test.js +301 -0
- package/src/styles/__tests__/lint/check-styles.test.js +423 -0
- package/src/styles/__tests__/lint/fixtures/ProductCard.golden.svelte +13 -0
- package/src/styles/__tests__/lint/fixtures/ProductCard.in.svelte +13 -0
- package/src/styles/__tests__/lint/semantic-classes.test.js +364 -0
- package/src/styles/bootstrap-classes.js +155 -0
- package/src/styles/check-styles.js +626 -0
- package/src/styles/fallback-css.js +1182 -0
- package/src/styles/plugin-css.js +237 -0
- package/src/styles/scan.js +206 -0
- package/src/styles/semantic-classes.js +436 -0
- package/src/styles/tokens.map.js +53 -0
- package/README.md +0 -3
|
@@ -0,0 +1,626 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The plugin style lint (doc 03 section 3): the generalised form of market's `theme-class` /
|
|
3
|
+
* `theme-style` rules. `pano-plugin check` runs it at level `core`, `--styles badge` at `badge`.
|
|
4
|
+
*
|
|
5
|
+
* Every message is `<what> — <next step>.` so the author always sees what to do next.
|
|
6
|
+
*/
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import postcss from 'postcss';
|
|
10
|
+
import { parse } from 'svelte/compiler';
|
|
11
|
+
import { classTokens, parseTags, splitSvelte } from './scan.js';
|
|
12
|
+
import { isAllowedThemeClass } from './bootstrap-classes.js';
|
|
13
|
+
import { PART_TABLE, inspectRoots, rootClassOf } from './semantic-classes.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* @typedef {{ rule: string, file: string, line: number, message: string, level: 'error'|'warn' }} StyleFinding
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Severity of each rule per level; `null` = rule off.
|
|
21
|
+
*
|
|
22
|
+
* @type {Readonly<Record<string, { core: 'error'|'warn'|null, badge: 'error'|'warn'|null }>>}
|
|
23
|
+
*/
|
|
24
|
+
export const RULE_LEVELS = Object.freeze({
|
|
25
|
+
'class-allowed': { core: 'warn', badge: 'error' },
|
|
26
|
+
'style-block-scope': { core: null, badge: 'error' },
|
|
27
|
+
'style-attr': { core: null, badge: 'error' },
|
|
28
|
+
'no-bs-var': { core: 'warn', badge: 'error' },
|
|
29
|
+
'dynamic-class': { core: 'warn', badge: 'warn' },
|
|
30
|
+
'root-class': { core: null, badge: 'error' },
|
|
31
|
+
'part-coverage': { core: null, badge: 'error' },
|
|
32
|
+
'part-prefix': { core: 'warn', badge: 'error' },
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
const escapeRe = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
36
|
+
|
|
37
|
+
function lineOf(source, index) {
|
|
38
|
+
let line = 1;
|
|
39
|
+
for (let i = 0; i < index && i < source.length; i++) if (source.charCodeAt(i) === 10) line++;
|
|
40
|
+
return line;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function walkSvelte(dir) {
|
|
44
|
+
const out = [];
|
|
45
|
+
if (!fs.existsSync(dir)) return out;
|
|
46
|
+
const visit = (d) => {
|
|
47
|
+
for (const entry of fs.readdirSync(d, { withFileTypes: true })) {
|
|
48
|
+
if (entry.name === 'node_modules') continue;
|
|
49
|
+
const full = path.join(d, entry.name);
|
|
50
|
+
if (entry.isDirectory()) visit(full);
|
|
51
|
+
else if (entry.name.endsWith('.svelte')) out.push(full);
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
visit(dir);
|
|
55
|
+
return out.sort();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function walkJs(dir) {
|
|
59
|
+
const out = [];
|
|
60
|
+
if (!fs.existsSync(dir)) return out;
|
|
61
|
+
const visit = (d) => {
|
|
62
|
+
for (const entry of fs.readdirSync(d, { withFileTypes: true })) {
|
|
63
|
+
if (entry.name === 'node_modules' || entry.name === '__tests__') continue;
|
|
64
|
+
const full = path.join(d, entry.name);
|
|
65
|
+
if (entry.isDirectory()) visit(full);
|
|
66
|
+
else if (/\.(js|mjs)$/.test(entry.name) && !/\.test\.m?js$/.test(entry.name)) out.push(full);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
visit(dir);
|
|
70
|
+
return out.sort();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Reads the value of the attribute whose name starts at `pos` (just after `name=`).
|
|
75
|
+
*
|
|
76
|
+
* @returns {{ kind: 'quoted'|'brace'|'bare', raw: string }}
|
|
77
|
+
*/
|
|
78
|
+
function readAttrValue(attrs, pos) {
|
|
79
|
+
const c = attrs[pos];
|
|
80
|
+
if (c === '"' || c === "'") {
|
|
81
|
+
const end = attrs.indexOf(c, pos + 1);
|
|
82
|
+
return { kind: 'quoted', raw: attrs.slice(pos + 1, end === -1 ? attrs.length : end) };
|
|
83
|
+
}
|
|
84
|
+
if (c === '{') {
|
|
85
|
+
let depth = 0;
|
|
86
|
+
let quote = '';
|
|
87
|
+
for (let i = pos; i < attrs.length; i++) {
|
|
88
|
+
const ch = attrs[i];
|
|
89
|
+
if (quote) {
|
|
90
|
+
if (ch === '\\') i++;
|
|
91
|
+
else if (ch === quote) quote = '';
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
if (ch === '"' || ch === "'" || ch === '`') quote = ch;
|
|
95
|
+
else if (ch === '{') depth++;
|
|
96
|
+
else if (ch === '}' && --depth === 0) return { kind: 'brace', raw: attrs.slice(pos + 1, i) };
|
|
97
|
+
}
|
|
98
|
+
return { kind: 'brace', raw: attrs.slice(pos + 1) };
|
|
99
|
+
}
|
|
100
|
+
const m = /^[^\s>]*/.exec(attrs.slice(pos));
|
|
101
|
+
return { kind: 'bare', raw: m ? m[0] : '' };
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** `{…}` chunks of a quoted attribute replaced by `0`. */
|
|
105
|
+
function stripBraces(raw) {
|
|
106
|
+
let out = '';
|
|
107
|
+
let depth = 0;
|
|
108
|
+
for (const ch of raw) {
|
|
109
|
+
if (ch === '{') {
|
|
110
|
+
if (depth++ === 0) out += '0';
|
|
111
|
+
} else if (ch === '}') depth = Math.max(0, depth - 1);
|
|
112
|
+
else if (depth === 0) out += ch;
|
|
113
|
+
}
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** True when every declaration in the text is a custom property (`--x: …`) and there is one. */
|
|
118
|
+
function onlyCustomProperties(text) {
|
|
119
|
+
const decls = text
|
|
120
|
+
.split(';')
|
|
121
|
+
.map((d) => d.trim())
|
|
122
|
+
.filter(Boolean);
|
|
123
|
+
return decls.length > 0 && decls.every((d) => /^--[\w-]+\s*:/.test(d));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Style attributes of one tag: `[{ offset, raw, custom }]` and `style:` directives.
|
|
128
|
+
*
|
|
129
|
+
* @param {string} attrs
|
|
130
|
+
*/
|
|
131
|
+
function styleAttributes(attrs) {
|
|
132
|
+
const found = [];
|
|
133
|
+
for (const m of attrs.matchAll(/(?:^|\s)style\s*=\s*/g)) {
|
|
134
|
+
const valuePos = m.index + m[0].length;
|
|
135
|
+
const { kind, raw } = readAttrValue(attrs, valuePos);
|
|
136
|
+
let custom;
|
|
137
|
+
if (kind === 'brace') {
|
|
138
|
+
const literals = [...raw.matchAll(/(["'`])((?:\\.|(?!\1).)*)\1/g)].map((s) =>
|
|
139
|
+
s[2].replace(/\$\{[^}]*\}/g, '0'),
|
|
140
|
+
);
|
|
141
|
+
custom = literals.length > 0 && onlyCustomProperties(literals.join(';'));
|
|
142
|
+
} else custom = onlyCustomProperties(stripBraces(raw));
|
|
143
|
+
found.push({ offset: m.index, raw, custom });
|
|
144
|
+
}
|
|
145
|
+
for (const m of attrs.matchAll(/(?:^|\s)style:([\w-]+)/g))
|
|
146
|
+
found.push({ offset: m.index, raw: m[1], custom: m[1].startsWith('--'), directive: m[1] });
|
|
147
|
+
return found;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// ---- dynamic-class -----------------------------------------------------------------------------
|
|
151
|
+
|
|
152
|
+
function literalStrings(node, out) {
|
|
153
|
+
if (!node || typeof node !== 'object') return;
|
|
154
|
+
if (Array.isArray(node)) {
|
|
155
|
+
for (const n of node) literalStrings(n, out);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
if (node.type === 'Literal' && typeof node.value === 'string') out.push(node.value);
|
|
159
|
+
else if (node.type === 'TemplateElement') out.push(String(node.value?.cooked ?? ''));
|
|
160
|
+
for (const key of Object.keys(node)) {
|
|
161
|
+
if (key === 'loc' || key === 'type') continue;
|
|
162
|
+
literalStrings(node[key], out);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const tokensOf = (strings) => strings.flatMap((s) => s.split(/\s+/)).filter(Boolean);
|
|
167
|
+
|
|
168
|
+
/** Declared name -> class-like literal tokens of its initialiser, plus every literal token seen. */
|
|
169
|
+
function collectDeclarations(scripts, into) {
|
|
170
|
+
const visit = (node) => {
|
|
171
|
+
if (!node || typeof node !== 'object') return;
|
|
172
|
+
if (Array.isArray(node)) {
|
|
173
|
+
for (const n of node) visit(n);
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
let name = null;
|
|
177
|
+
let body = null;
|
|
178
|
+
if (node.type === 'VariableDeclarator' && node.id?.type === 'Identifier') {
|
|
179
|
+
name = node.id.name;
|
|
180
|
+
body = node.init;
|
|
181
|
+
} else if (node.type === 'FunctionDeclaration' && node.id) {
|
|
182
|
+
name = node.id.name;
|
|
183
|
+
body = node.body;
|
|
184
|
+
} else if (node.type === 'Property' && !node.computed && node.key?.type === 'Identifier') {
|
|
185
|
+
name = node.key.name;
|
|
186
|
+
body = node.value;
|
|
187
|
+
}
|
|
188
|
+
if (name && body) {
|
|
189
|
+
const strings = [];
|
|
190
|
+
literalStrings(body, strings);
|
|
191
|
+
const tokens = tokensOf(strings);
|
|
192
|
+
const entry = into.declared.get(name) ?? new Set();
|
|
193
|
+
for (const t of tokens) entry.add(t);
|
|
194
|
+
into.declared.set(name, entry);
|
|
195
|
+
}
|
|
196
|
+
for (const key of Object.keys(node)) {
|
|
197
|
+
if (key === 'loc' || key === 'type') continue;
|
|
198
|
+
visit(node[key]);
|
|
199
|
+
}
|
|
200
|
+
};
|
|
201
|
+
for (const script of scripts) {
|
|
202
|
+
const strings = [];
|
|
203
|
+
literalStrings(script, strings);
|
|
204
|
+
for (const t of tokensOf(strings)) into.literals.add(t);
|
|
205
|
+
visit(script);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
function parseScripts(source) {
|
|
210
|
+
try {
|
|
211
|
+
const ast = parse(source, { modern: true });
|
|
212
|
+
return { ast, scripts: [ast.instance?.content, ast.module?.content].filter(Boolean) };
|
|
213
|
+
} catch {
|
|
214
|
+
return { ast: null, scripts: [] };
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function helperScripts(files) {
|
|
219
|
+
const scripts = [];
|
|
220
|
+
for (const file of files) {
|
|
221
|
+
try {
|
|
222
|
+
const wrapped = parse(`<script module>${fs.readFileSync(file, 'utf8')}\n</script>`, {
|
|
223
|
+
modern: true,
|
|
224
|
+
});
|
|
225
|
+
if (wrapped.module?.content) scripts.push(wrapped.module.content);
|
|
226
|
+
} catch {
|
|
227
|
+
// an unparsable helper only makes the lint less precise
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
return scripts;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Root identifier names an expression reads (`a.b.c(d)` -> a). */
|
|
234
|
+
function headIdentifiers(node, out) {
|
|
235
|
+
if (!node || typeof node !== 'object') return;
|
|
236
|
+
if (node.type === 'Identifier') out.add(node.name);
|
|
237
|
+
else if (node.type === 'MemberExpression') headIdentifiers(node.object, out);
|
|
238
|
+
else if (node.type === 'CallExpression') headIdentifiers(node.callee, out);
|
|
239
|
+
else if (node.type === 'ChainExpression') headIdentifiers(node.expression, out);
|
|
240
|
+
else if (node.type === 'TemplateLiteral') for (const e of node.expressions) headIdentifiers(e, out);
|
|
241
|
+
else if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') {
|
|
242
|
+
headIdentifiers(node.left, out);
|
|
243
|
+
headIdentifiers(node.right, out);
|
|
244
|
+
} else if (node.type === 'ConditionalExpression') {
|
|
245
|
+
headIdentifiers(node.consequent, out);
|
|
246
|
+
headIdentifiers(node.alternate, out);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/** Trailing token of literal text (`btn btn-` -> `btn-`). */
|
|
251
|
+
const trailingToken = (text) => /(\S*)$/.exec(text)?.[1] ?? '';
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Non-literal members of a class expression.
|
|
255
|
+
*
|
|
256
|
+
* @param {any} node
|
|
257
|
+
* @param {{ node: any, prefix: string, text: string }[]} out
|
|
258
|
+
* @param {string} source
|
|
259
|
+
*/
|
|
260
|
+
function dynamicMembers(node, out, source) {
|
|
261
|
+
if (!node) return;
|
|
262
|
+
switch (node.type) {
|
|
263
|
+
case 'Literal':
|
|
264
|
+
return;
|
|
265
|
+
case 'ArrayExpression':
|
|
266
|
+
for (const e of node.elements) dynamicMembers(e, out, source);
|
|
267
|
+
return;
|
|
268
|
+
case 'SpreadElement':
|
|
269
|
+
dynamicMembers(node.argument, out, source);
|
|
270
|
+
return;
|
|
271
|
+
case 'ConditionalExpression':
|
|
272
|
+
dynamicMembers(node.consequent, out, source);
|
|
273
|
+
dynamicMembers(node.alternate, out, source);
|
|
274
|
+
return;
|
|
275
|
+
case 'LogicalExpression':
|
|
276
|
+
if (node.operator !== '&&') dynamicMembers(node.left, out, source);
|
|
277
|
+
dynamicMembers(node.right, out, source);
|
|
278
|
+
return;
|
|
279
|
+
case 'ObjectExpression':
|
|
280
|
+
for (const p of node.properties) {
|
|
281
|
+
if (p.type === 'Property' && p.computed) dynamicMembers(p.key, out, source);
|
|
282
|
+
}
|
|
283
|
+
return;
|
|
284
|
+
case 'TemplateLiteral':
|
|
285
|
+
if (node.expressions.length)
|
|
286
|
+
out.push({
|
|
287
|
+
node,
|
|
288
|
+
prefix: trailingToken(String(node.quasis[0].value.cooked ?? '')),
|
|
289
|
+
text: source.slice(node.start, node.end),
|
|
290
|
+
});
|
|
291
|
+
return;
|
|
292
|
+
case 'BinaryExpression': {
|
|
293
|
+
let left = node;
|
|
294
|
+
while (left.type === 'BinaryExpression' && left.operator === '+') left = left.left;
|
|
295
|
+
const prefix =
|
|
296
|
+
left.type === 'Literal' && typeof left.value === 'string' ? trailingToken(left.value) : '';
|
|
297
|
+
out.push({ node, prefix, text: source.slice(node.start, node.end) });
|
|
298
|
+
return;
|
|
299
|
+
}
|
|
300
|
+
case 'Identifier':
|
|
301
|
+
if (node.name === 'undefined') return;
|
|
302
|
+
// falls through
|
|
303
|
+
default:
|
|
304
|
+
out.push({ node, prefix: '', text: source.slice(node.start, node.end) });
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/** @param {any} root parsed component @param {string} source */
|
|
309
|
+
function classAttributes(root) {
|
|
310
|
+
const found = [];
|
|
311
|
+
const seen = new WeakSet();
|
|
312
|
+
const visit = (node) => {
|
|
313
|
+
if (!node || typeof node !== 'object' || seen.has(node)) return;
|
|
314
|
+
seen.add(node);
|
|
315
|
+
if (Array.isArray(node)) {
|
|
316
|
+
for (const n of node) visit(n);
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
if (
|
|
320
|
+
(node.type === 'RegularElement' || node.type === 'Component' || node.type === 'SvelteElement') &&
|
|
321
|
+
node.attributes
|
|
322
|
+
) {
|
|
323
|
+
for (const a of node.attributes)
|
|
324
|
+
if (a.type === 'Attribute' && a.name === 'class' && a.value !== true) found.push(a);
|
|
325
|
+
}
|
|
326
|
+
for (const key of Object.keys(node)) {
|
|
327
|
+
if (key === 'loc' || key === 'name_loc' || key === 'metadata') continue;
|
|
328
|
+
visit(node[key]);
|
|
329
|
+
}
|
|
330
|
+
};
|
|
331
|
+
visit(root.fragment);
|
|
332
|
+
return found;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** @returns {{ attr: any, text: string, prefix: string, node: any }[]} */
|
|
336
|
+
function dynamicClassMembers(ast, source) {
|
|
337
|
+
const out = [];
|
|
338
|
+
for (const attr of classAttributes(ast)) {
|
|
339
|
+
const parts = Array.isArray(attr.value) ? attr.value : [attr.value];
|
|
340
|
+
parts.forEach((part, i) => {
|
|
341
|
+
if (part.type !== 'ExpressionTag') return;
|
|
342
|
+
const members = [];
|
|
343
|
+
dynamicMembers(part.expression, members, source);
|
|
344
|
+
const before = parts[i - 1];
|
|
345
|
+
const textPrefix = before?.type === 'Text' ? trailingToken(before.data) : '';
|
|
346
|
+
for (const m of members) {
|
|
347
|
+
// `btn-{variant}`: the literal text in front of the tag is the prefix
|
|
348
|
+
const prefix = Array.isArray(attr.value) && m.node === part.expression ? textPrefix : m.prefix;
|
|
349
|
+
out.push({ attr, text: m.text, prefix, node: m.node });
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
return out;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// ---- main --------------------------------------------------------------------------------------
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* @param {{ root: string, ns: string, viewsDir?: string, views: { name: string, file: string }[],
|
|
360
|
+
* styleAttrAllow?: string[], dynamicClassAllow?: string[], safelist?: string[],
|
|
361
|
+
* level?: 'core'|'badge' }} options
|
|
362
|
+
* @returns {StyleFinding[]} viewsDir default 'src/theme'
|
|
363
|
+
*/
|
|
364
|
+
export function checkStyles(options) {
|
|
365
|
+
const {
|
|
366
|
+
root,
|
|
367
|
+
ns,
|
|
368
|
+
viewsDir = 'src/theme',
|
|
369
|
+
views = [],
|
|
370
|
+
styleAttrAllow = [],
|
|
371
|
+
dynamicClassAllow = [],
|
|
372
|
+
safelist = [],
|
|
373
|
+
level = 'core',
|
|
374
|
+
} = options;
|
|
375
|
+
if (level !== 'core' && level !== 'badge')
|
|
376
|
+
throw new Error(`checkStyles: unknown level "${level}" — use "core" or "badge".`);
|
|
377
|
+
|
|
378
|
+
const absRoot = path.resolve(root);
|
|
379
|
+
const absViews = path.resolve(absRoot, viewsDir);
|
|
380
|
+
const toRel = (file) => path.relative(absRoot, file).split(path.sep).join('/');
|
|
381
|
+
const viewByFile = new Map(views.map((v) => [path.resolve(absRoot, v.file), v.name]));
|
|
382
|
+
const ownClass = new RegExp(`^${escapeRe(ns)}-[a-z0-9-]+(__[a-z0-9-]+)?$`);
|
|
383
|
+
const partClass = new RegExp(`^${escapeRe(ns)}-[a-z0-9-]+__[a-z0-9-]+$`);
|
|
384
|
+
const safe = new Set(safelist);
|
|
385
|
+
|
|
386
|
+
/** @type {StyleFinding[]} */
|
|
387
|
+
const findings = [];
|
|
388
|
+
const addAt = (rule, file, line, what, next) => {
|
|
389
|
+
const severity = RULE_LEVELS[rule][level];
|
|
390
|
+
if (!severity) return;
|
|
391
|
+
findings.push({ rule, file, line, message: `${what} — ${next}.`, level: severity });
|
|
392
|
+
};
|
|
393
|
+
const add = (rule, file, source, index, what, next) =>
|
|
394
|
+
addAt(rule, file, lineOf(source, index), what, next);
|
|
395
|
+
|
|
396
|
+
const files = walkSvelte(absViews);
|
|
397
|
+
const helperDecls = { declared: new Map(), literals: new Set() };
|
|
398
|
+
collectDeclarations(helperScripts(walkJs(absViews)), helperDecls);
|
|
399
|
+
|
|
400
|
+
for (const abs of files) {
|
|
401
|
+
const file = toRel(abs);
|
|
402
|
+
const source = fs.readFileSync(abs, 'utf8');
|
|
403
|
+
const baseName = path.basename(abs, '.svelte');
|
|
404
|
+
const viewName = viewByFile.get(abs);
|
|
405
|
+
const rootClass = rootClassOf(ns, viewName ?? baseName);
|
|
406
|
+
const { markup, styles } = splitSvelte(source);
|
|
407
|
+
const allowedStyleAttr = styleAttrAllow.some(
|
|
408
|
+
(a) => a === baseName || a === path.basename(abs) || a === file || file.endsWith(`/${a}`),
|
|
409
|
+
);
|
|
410
|
+
|
|
411
|
+
// style-block-scope (the plugin's own look lives in its views, decision 77) and no-bs-var in <style> blocks
|
|
412
|
+
for (const m of source.matchAll(/<style\b[^>]*>([\s\S]*?)<\/style>/g)) {
|
|
413
|
+
const bodyStart = m.index + m[0].indexOf('>') + 1;
|
|
414
|
+
const bodyLine = lineOf(source, bodyStart);
|
|
415
|
+
const body = m[1];
|
|
416
|
+
const atLine = (node) => bodyLine + (node.source?.start?.line ?? 1) - 1;
|
|
417
|
+
|
|
418
|
+
for (const v of body.matchAll(/var\(\s*(--bs-[\w-]+)/g))
|
|
419
|
+
add(
|
|
420
|
+
'no-bs-var',
|
|
421
|
+
file,
|
|
422
|
+
source,
|
|
423
|
+
bodyStart + v.index,
|
|
424
|
+
`var(${v[1]}) in a <style> block`,
|
|
425
|
+
'use the matching --pano-* variable (for example --pano-color-primary) so Bootstrap-free themes keep working',
|
|
426
|
+
);
|
|
427
|
+
|
|
428
|
+
let sheet = null;
|
|
429
|
+
try {
|
|
430
|
+
sheet = postcss.parse(body);
|
|
431
|
+
} catch {
|
|
432
|
+
// CSS the lint cannot read (a preprocessor dialect) is the compiler's to report
|
|
433
|
+
}
|
|
434
|
+
if (!sheet) continue;
|
|
435
|
+
|
|
436
|
+
const ownSelector = new RegExp(`\\.${escapeRe(ns)}-[a-z0-9_-]+`);
|
|
437
|
+
const ownName = new RegExp(`^${escapeRe(ns)}-[a-z0-9_-]+$`);
|
|
438
|
+
const ownProperty = new RegExp(`^--${escapeRe(ns)}-[a-z0-9_-]+$`);
|
|
439
|
+
const rule = 'style-block-scope';
|
|
440
|
+
|
|
441
|
+
sheet.walk((node) => {
|
|
442
|
+
const line = atLine(node);
|
|
443
|
+
|
|
444
|
+
if (node.type === 'rule') {
|
|
445
|
+
if (/:global\b/.test(node.selector))
|
|
446
|
+
addAt(
|
|
447
|
+
rule,
|
|
448
|
+
file,
|
|
449
|
+
line,
|
|
450
|
+
`${baseName}: "${node.selector.trim()}" uses :global`,
|
|
451
|
+
`drop :global and style an element of this view through a class of its own (.${ns}-...)`,
|
|
452
|
+
);
|
|
453
|
+
|
|
454
|
+
const inKeyframes = node.parent?.type === 'atrule' && /keyframes$/i.test(node.parent.name);
|
|
455
|
+
const nested = node.parent?.type === 'rule';
|
|
456
|
+
|
|
457
|
+
if (!inKeyframes && !nested && !node.selectors.every((sel) => ownSelector.test(sel)))
|
|
458
|
+
addAt(
|
|
459
|
+
rule,
|
|
460
|
+
file,
|
|
461
|
+
line,
|
|
462
|
+
`${baseName}: selector "${node.selector.trim().replace(/\s+/g, ' ')}" has no class of the plugin namespace`,
|
|
463
|
+
`start every selector with a class like .${ns}-name (optionally with .is-state) so the rule cannot reach other markup`,
|
|
464
|
+
);
|
|
465
|
+
} else if (node.type === 'atrule' && /keyframes$/i.test(node.name)) {
|
|
466
|
+
const name = node.params.trim();
|
|
467
|
+
|
|
468
|
+
if (!ownName.test(name))
|
|
469
|
+
addAt(
|
|
470
|
+
rule,
|
|
471
|
+
file,
|
|
472
|
+
line,
|
|
473
|
+
`${baseName}: keyframes "${name}" is not named after the plugin`,
|
|
474
|
+
`rename it to ${ns}-${name.replace(/^-+/, '') || 'name'} and update the animation that uses it`,
|
|
475
|
+
);
|
|
476
|
+
} else if (node.type === 'decl' && node.prop.startsWith('--') && !ownProperty.test(node.prop)) {
|
|
477
|
+
addAt(
|
|
478
|
+
rule,
|
|
479
|
+
file,
|
|
480
|
+
line,
|
|
481
|
+
`${baseName}: custom property "${node.prop}" is not named after the plugin`,
|
|
482
|
+
`rename it to --${ns}-${node.prop.replace(/^-+/, '')} (read theme values through var(--pano-*) instead of redefining them)`,
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
});
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
for (const tag of parseTags(markup)) {
|
|
489
|
+
const attrsStart = tag.index + 1 + tag.name.length;
|
|
490
|
+
|
|
491
|
+
// class-allowed, part-prefix
|
|
492
|
+
for (const token of classTokens(tag.attrs)) {
|
|
493
|
+
const isOwn = ownClass.test(token);
|
|
494
|
+
if (!isOwn && !/^is-[a-z0-9-]+$/.test(token) && !isAllowedThemeClass(token) && !safe.has(token))
|
|
495
|
+
add(
|
|
496
|
+
'class-allowed',
|
|
497
|
+
file,
|
|
498
|
+
source,
|
|
499
|
+
tag.index,
|
|
500
|
+
`own class "${token}": not covered by fallback.css in a Bootstrap-free theme`,
|
|
501
|
+
`rename it to ${ns}-${token} (a semantic class), use a Bootstrap class, or add it to styles.safelist in pano.plugin.js`,
|
|
502
|
+
);
|
|
503
|
+
if (isOwn && token.includes('__') && !token.startsWith(`${rootClass}__`))
|
|
504
|
+
add(
|
|
505
|
+
'part-prefix',
|
|
506
|
+
file,
|
|
507
|
+
source,
|
|
508
|
+
tag.index,
|
|
509
|
+
`class "${token}" in ${baseName}.svelte does not start with its view's root class "${rootClass}"`,
|
|
510
|
+
`rename it to ${rootClass}__${token.split('__').pop()}`,
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// style-attr, no-bs-var in style=
|
|
515
|
+
if (/(?:^|\s)style(?::|\s*=)/.test(tag.attrs)) {
|
|
516
|
+
for (const st of styleAttributes(tag.attrs)) {
|
|
517
|
+
const index = attrsStart + st.offset;
|
|
518
|
+
if (!st.custom && !allowedStyleAttr)
|
|
519
|
+
add(
|
|
520
|
+
'style-attr',
|
|
521
|
+
file,
|
|
522
|
+
source,
|
|
523
|
+
index,
|
|
524
|
+
st.directive
|
|
525
|
+
? `style:${st.directive} sets a regular property on <${tag.name}>`
|
|
526
|
+
: `style= on <${tag.name}> sets more than custom properties`,
|
|
527
|
+
`set only --custom-properties there and read them in a class, move the rules into a class, or list ${baseName} in styleAttrAllow`,
|
|
528
|
+
);
|
|
529
|
+
for (const v of st.raw.matchAll(/var\(\s*(--bs-[\w-]+)/g))
|
|
530
|
+
add(
|
|
531
|
+
'no-bs-var',
|
|
532
|
+
file,
|
|
533
|
+
source,
|
|
534
|
+
index,
|
|
535
|
+
`var(${v[1]}) in style=`,
|
|
536
|
+
'use the matching --pano-* variable (for example --pano-color-primary) so Bootstrap-free themes keep working',
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
// part-coverage
|
|
542
|
+
const tokens = classTokens(tag.attrs);
|
|
543
|
+
const tableEntry = PART_TABLE.find(([c]) => tokens.includes(c));
|
|
544
|
+
if (tableEntry && !tokens.some((t) => partClass.test(t))) {
|
|
545
|
+
const part = tableEntry[1];
|
|
546
|
+
add(
|
|
547
|
+
'part-coverage',
|
|
548
|
+
file,
|
|
549
|
+
source,
|
|
550
|
+
tag.index,
|
|
551
|
+
`<${tag.name}> with a Bootstrap component class has no part class`,
|
|
552
|
+
`run pano-plugin classes --fix to add ${rootClass}__${part}`,
|
|
553
|
+
);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
// root-class
|
|
558
|
+
if (viewName !== undefined) {
|
|
559
|
+
try {
|
|
560
|
+
const roots = inspectRoots(source);
|
|
561
|
+
if (!roots.length) {
|
|
562
|
+
if (RULE_LEVELS['root-class'][level])
|
|
563
|
+
findings.push({
|
|
564
|
+
rule: 'root-class',
|
|
565
|
+
file,
|
|
566
|
+
line: 1,
|
|
567
|
+
message: `${viewName} has no root element and cannot carry "${rootClass}" — wrap it in an element so themes can target it.`,
|
|
568
|
+
level: 'warn',
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
for (const r of roots)
|
|
572
|
+
if (!r.tokens.includes(rootClass))
|
|
573
|
+
addAt(
|
|
574
|
+
'root-class',
|
|
575
|
+
file,
|
|
576
|
+
r.line,
|
|
577
|
+
`${viewName}: root <${r.tag}> lacks the class "${rootClass}" in source`,
|
|
578
|
+
'run pano-plugin classes --fix (the build adds it, but badge level wants it committed)',
|
|
579
|
+
);
|
|
580
|
+
} catch {
|
|
581
|
+
// a Svelte syntax error is the compiler's to report
|
|
582
|
+
}
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
// dynamic-class
|
|
586
|
+
const { ast, scripts } = parseScripts(source);
|
|
587
|
+
if (ast) {
|
|
588
|
+
const decls = { declared: new Map(), literals: new Set() };
|
|
589
|
+
collectDeclarations(scripts, decls);
|
|
590
|
+
for (const m of dynamicClassMembers(ast, source)) {
|
|
591
|
+
let resolved = false;
|
|
592
|
+
if (m.prefix) {
|
|
593
|
+
const hit = (t) => t.startsWith(m.prefix) && t !== m.prefix;
|
|
594
|
+
resolved =
|
|
595
|
+
[...safe].some(hit) ||
|
|
596
|
+
[...decls.literals].some(hit) ||
|
|
597
|
+
[...helperDecls.literals].some(hit);
|
|
598
|
+
} else {
|
|
599
|
+
const names = new Set();
|
|
600
|
+
headIdentifiers(m.node, names);
|
|
601
|
+
for (const name of names) {
|
|
602
|
+
const known = decls.declared.get(name) ?? helperDecls.declared.get(name);
|
|
603
|
+
if (known && [...known].some((t) => isAllowedThemeClass(t) || ownClass.test(t)))
|
|
604
|
+
resolved = true;
|
|
605
|
+
}
|
|
606
|
+
}
|
|
607
|
+
if (!resolved && !m.prefix && dynamicClassAllow.includes(baseName)) resolved = true;
|
|
608
|
+
if (!resolved)
|
|
609
|
+
add(
|
|
610
|
+
'dynamic-class',
|
|
611
|
+
file,
|
|
612
|
+
source,
|
|
613
|
+
m.attr.start,
|
|
614
|
+
`class expression "${m.text}" has values the build cannot see`,
|
|
615
|
+
m.prefix
|
|
616
|
+
? `write the possible classes as literals in the view or add them to styles.safelist in pano.plugin.js (they start with "${m.prefix}")`
|
|
617
|
+
: 'write the possible classes as literals in the view (a map of full class names), add them to styles.safelist in pano.plugin.js, or list the view in styles.dynamicClassAllow',
|
|
618
|
+
);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
return findings.sort(
|
|
624
|
+
(a, b) => a.file.localeCompare(b.file) || a.line - b.line || a.rule.localeCompare(b.rule),
|
|
625
|
+
);
|
|
626
|
+
}
|