@kerf-js/cli 0.0.0-stage → 0.1.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/LICENSE +16 -0
- package/README.md +554 -2
- package/bin/kerf.mjs +15 -0
- package/docs/async.md +50 -0
- package/docs/browser.md +53 -0
- package/docs/config.md +127 -0
- package/docs/core.md +30 -0
- package/docs/deploy.md +108 -0
- package/docs/design.md +106 -0
- package/docs/entrypoints.md +83 -0
- package/docs/fonts.md +118 -0
- package/docs/icons.md +108 -0
- package/docs/lists.md +25 -0
- package/docs/meta.md +92 -0
- package/docs/plugins.md +166 -0
- package/docs/props.md +88 -0
- package/docs/reactivity.md +26 -0
- package/docs/rules.md +39 -0
- package/docs/rust.md +253 -0
- package/docs/start-short.md +10 -0
- package/docs/start.md +642 -0
- package/docs/state.md +122 -0
- package/docs/style.md +82 -0
- package/docs/surface.md +158 -0
- package/docs/testing.md +81 -0
- package/docs/theme.md +263 -0
- package/docs/ui-state.md +105 -0
- package/docs/verify.md +61 -0
- package/package.json +47 -3
- package/src/add/config.mjs +93 -0
- package/src/add/errors.mjs +2 -0
- package/src/add/setup.mjs +88 -0
- package/src/add-rust.mjs +178 -0
- package/src/add.mjs +392 -0
- package/src/agents-md.mjs +240 -0
- package/src/alias.mjs +70 -0
- package/src/audit/page.mjs +313 -0
- package/src/audit/report.mjs +69 -0
- package/src/audit/utils.mjs +5 -0
- package/src/audit.mjs +319 -0
- package/src/browser/cli.mjs +53 -0
- package/src/browser/client.mjs +299 -0
- package/src/browser/commands.mjs +258 -0
- package/src/capture.mjs +21 -0
- package/src/catalog/audit.mjs +129 -0
- package/src/catalog/auth.mjs +40 -0
- package/src/catalog/compiler.mjs +337 -0
- package/src/catalog/contracts.mjs +18 -0
- package/src/catalog/database.mjs +52 -0
- package/src/catalog/deploy.mjs +117 -0
- package/src/catalog/design.mjs +159 -0
- package/src/catalog/forms.mjs +25 -0
- package/src/catalog/hydration.mjs +29 -0
- package/src/catalog/icons.mjs +24 -0
- package/src/catalog/media.mjs +163 -0
- package/src/catalog/metadata.mjs +116 -0
- package/src/catalog/plugins.mjs +228 -0
- package/src/catalog/realtime.mjs +195 -0
- package/src/catalog/rust.mjs +75 -0
- package/src/catalog/server-functions.mjs +100 -0
- package/src/catalog/state.mjs +39 -0
- package/src/catalog/style-groups.mjs +54 -0
- package/src/catalog/tests.mjs +127 -0
- package/src/catalog/text.mjs +200 -0
- package/src/catalog/ui-state.mjs +42 -0
- package/src/catalog.mjs +103 -0
- package/src/check/runtime.mjs +280 -0
- package/src/check.mjs +321 -0
- package/src/cli/authoring.mjs +213 -0
- package/src/cli/project.mjs +302 -0
- package/src/cli.mjs +340 -0
- package/src/config.d.ts +1 -0
- package/src/config.mjs +5 -0
- package/src/contributions.mjs +119 -0
- package/src/db-layer.mjs +41 -0
- package/src/deploy/cdn.mjs +142 -0
- package/src/deploy/files.mjs +266 -0
- package/src/deploy/fit.mjs +166 -0
- package/src/deploy/hosts.mjs +126 -0
- package/src/deploy/index/cli.mjs +20 -0
- package/src/deploy/index/environment.mjs +69 -0
- package/src/deploy/index/steps.mjs +87 -0
- package/src/deploy/index.mjs +387 -0
- package/src/deploy/needs.mjs +149 -0
- package/src/design-checks/accessible-names.mjs +62 -0
- package/src/design-checks/contrast.mjs +43 -0
- package/src/design-checks/csp.mjs +60 -0
- package/src/design-checks/files.mjs +59 -0
- package/src/design-checks/heading-links.mjs +61 -0
- package/src/design-checks/interaction.mjs +65 -0
- package/src/design-checks/jsx.mjs +67 -0
- package/src/design-checks/layout-values.mjs +68 -0
- package/src/design-checks/native-controls.mjs +48 -0
- package/src/design-checks/outline.mjs +39 -0
- package/src/design-checks/reporter.mjs +37 -0
- package/src/design-checks/rules.mjs +274 -0
- package/src/design-checks/values.mjs +262 -0
- package/src/design-checks.mjs +49 -0
- package/src/diag.mjs +98 -0
- package/src/docs.mjs +228 -0
- package/src/doctor.mjs +42 -0
- package/src/files.mjs +60 -0
- package/src/fix.mjs +48 -0
- package/src/gate/after.mjs +40 -0
- package/src/gate/before.mjs +54 -0
- package/src/gate/classify.mjs +72 -0
- package/src/gate/mutation.mjs +209 -0
- package/src/gate/resolve-cases.mjs +22 -0
- package/src/gate/result.mjs +63 -0
- package/src/gate/run.mjs +75 -0
- package/src/gate.mjs +178 -0
- package/src/index.mjs +9 -0
- package/src/infer/box-props.cjs +140 -0
- package/src/infer/core-boxes.cjs +46 -0
- package/src/infer/derived-writes.cjs +95 -0
- package/src/infer/engine/checks.cjs +193 -0
- package/src/infer/engine/flow.cjs +293 -0
- package/src/infer/engine/report.cjs +158 -0
- package/src/infer/engine/utils.cjs +15 -0
- package/src/infer/engine.cjs +265 -0
- package/src/infer/facts/sites.cjs +114 -0
- package/src/infer/facts.cjs +345 -0
- package/src/infer/hosts.cjs +161 -0
- package/src/infer/lsp.mjs +240 -0
- package/src/infer/session.cjs +161 -0
- package/src/infer/shadow.cjs +54 -0
- package/src/infer/worker.cjs +145 -0
- package/src/jsx-scan.mjs +128 -0
- package/src/kerf.mjs +98 -0
- package/src/load-config/schema.mjs +300 -0
- package/src/load-config.mjs +302 -0
- package/src/map.mjs +177 -0
- package/src/mcp/browser-tools.mjs +155 -0
- package/src/mcp.mjs +273 -0
- package/src/meta-table.mjs +63 -0
- package/src/mutate.mjs +224 -0
- package/src/plugin-checks.mjs +118 -0
- package/src/plugins.mjs +258 -0
- package/src/project.mjs +310 -0
- package/src/props-checks.mjs +64 -0
- package/src/props-listener.mjs +56 -0
- package/src/react-checks.mjs +67 -0
- package/src/server-state-checks.mjs +34 -0
- package/src/share-checks.mjs +158 -0
- package/src/site.mjs +46 -0
- package/src/slots.mjs +181 -0
- package/src/start/build.mjs +116 -0
- package/src/start/routes.mjs +38 -0
- package/src/start.mjs +394 -0
- package/src/state-checks/core-state.mjs +223 -0
- package/src/state-checks/modules.mjs +17 -0
- package/src/state-checks/scan.mjs +232 -0
- package/src/state-checks/server-graph.mjs +65 -0
- package/src/state-checks.mjs +189 -0
- package/src/style/find.mjs +121 -0
- package/src/style.mjs +327 -0
- package/src/surface-checks.mjs +384 -0
- package/src/tailwind.mjs +23 -0
- package/src/tasks.mjs +261 -0
- package/src/test-rules/context.mjs +109 -0
- package/src/test-rules/file-rules.mjs +279 -0
- package/src/test-rules/rule-helpers.mjs +244 -0
- package/src/test-rules/syntax.mjs +223 -0
- package/src/test-rules/test-rules.mjs +127 -0
- package/src/testdiff.mjs +219 -0
- package/src/tests.mjs +120 -0
- package/src/text-checks.mjs +332 -0
- package/src/tokens.mjs +33 -0
- package/src/trust/scan.mjs +95 -0
- package/src/trust.mjs +394 -0
- package/src/workspace.mjs +182 -0
- package/ts-plugin/context.js +57 -0
- package/ts-plugin/index.js +17 -0
- package/ts-plugin/mapping.js +65 -0
- package/ts-plugin/package.json +1 -0
- package/ts-plugin/proxy.js +144 -0
package/src/style.mjs
ADDED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
// `kerf style find|explain|matrix` and the style D-codes (research 23 §2.4, §3). The matrix comes from the installed
|
|
2
|
+
// kerf (`@kerf-js/core/style/matrix`): the project is scanned with its compiler, so the CLI, the MCP tools and the Vite
|
|
3
|
+
// plugin's `.kerf/styles.json` agree. D020 (class soup) is found here, on the source text: groups never write a class.
|
|
4
|
+
|
|
5
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs'
|
|
6
|
+
import { join } from 'node:path'
|
|
7
|
+
import { pathToFileURL } from 'node:url'
|
|
8
|
+
import { diagnostic, locate, positions } from './diag.mjs'
|
|
9
|
+
import { discover } from './files.mjs'
|
|
10
|
+
import { loadConfig } from './load-config.mjs'
|
|
11
|
+
import { isTestFile } from './tests.mjs'
|
|
12
|
+
import { loadCompiler, resolveKerf } from './kerf.mjs'
|
|
13
|
+
|
|
14
|
+
import { closest, usageError } from './style/find.mjs'
|
|
15
|
+
|
|
16
|
+
export { closest, findGroups } from './style/find.mjs'
|
|
17
|
+
|
|
18
|
+
/** The installed kerf's matrix module, or null when this kerf has no style groups. */
|
|
19
|
+
export async function styleModule(kerf) {
|
|
20
|
+
const target = kerf.pkg.exports?.['./style/matrix']
|
|
21
|
+
if (typeof target !== 'string') return null
|
|
22
|
+
return import(pathToFileURL(join(kerf.dir, target)).href)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Source files that can define or use groups: components and plain modules, not tests. */
|
|
26
|
+
export function styleFiles(root, config = {}) {
|
|
27
|
+
const exclude = [...(config.check?.exclude ?? []), ...(config.scope?.forbid ?? [])]
|
|
28
|
+
return discover(root, { include: ['**/*.{ts,tsx,jsx,mts}'], exclude }).filter(
|
|
29
|
+
(f) => !isTestFile(f) && !f.endsWith('.d.ts'),
|
|
30
|
+
)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Scan the project and build the matrix. `write`: also write `.kerf/styles.json` (when the project has groups, or the
|
|
35
|
+
* file is already there).
|
|
36
|
+
*/
|
|
37
|
+
export async function styleMatrix(root, { write = false } = {}) {
|
|
38
|
+
const kerf = resolveKerf(root)
|
|
39
|
+
const mod = await styleModule(kerf)
|
|
40
|
+
if (!mod)
|
|
41
|
+
throw Object.assign(
|
|
42
|
+
new Error(`kerf@${kerf.version} has no style groups (@kerf-js/core/style/matrix)`),
|
|
43
|
+
{ code: 'E_NO_STYLE' },
|
|
44
|
+
)
|
|
45
|
+
const { config } = await loadConfig(root)
|
|
46
|
+
const compiler = loadCompiler(kerf)
|
|
47
|
+
const scan = mod.scanProject(root, styleFiles(root, config), (...a) => compiler.transform(...a))
|
|
48
|
+
const matrix = mod.buildMatrix(scan.groups, scan.uses)
|
|
49
|
+
const file = join(root, '.kerf', 'styles.json')
|
|
50
|
+
if (write && (scan.groups.length || existsSync(file))) writeMatrixFile(root, matrix)
|
|
51
|
+
return { matrix, scan, mod, file }
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function writeMatrixFile(root, matrix) {
|
|
55
|
+
const file = join(root, '.kerf', 'styles.json')
|
|
56
|
+
const text = `${JSON.stringify(matrix, null, 2)}\n`
|
|
57
|
+
if (existsSync(file) && readFileSync(file, 'utf8') === text) return false
|
|
58
|
+
mkdirSync(join(root, '.kerf'), { recursive: true })
|
|
59
|
+
writeFileSync(file, text)
|
|
60
|
+
return true
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const ARIA = new Set(['selected', 'pressed', 'checked', 'expanded', 'current', 'invalid', 'busy'])
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Read `order-card`, `order-card.title`, `order-card[status=late][selected]`, or a DOM selector
|
|
67
|
+
* (`[data-g="order-card"][data-status="late"] [data-part="title"]`, `[aria-selected="true"]`, `[data-accent~="radius:round"]`).
|
|
68
|
+
*/
|
|
69
|
+
export function parseTarget(target) {
|
|
70
|
+
const t = (target ?? '').trim()
|
|
71
|
+
if (!t)
|
|
72
|
+
throw usageError(
|
|
73
|
+
'kerf style explain <group|selector>: a group name (`order-card`), `order-card.title`, or a selector',
|
|
74
|
+
)
|
|
75
|
+
const out = { group: null, part: null, axes: {}, states: [], accent: {} }
|
|
76
|
+
const head = /^([a-z][\w-]*)(?:\.([\w-]+))?/i.exec(t)
|
|
77
|
+
if (head && !t.startsWith('[')) {
|
|
78
|
+
out.group = head[1]
|
|
79
|
+
out.part = head[2] ?? null
|
|
80
|
+
}
|
|
81
|
+
for (const m of t.matchAll(
|
|
82
|
+
/\[\s*([\w-]+)\s*(?:[~|^$*]?=\s*(?:"([^"]*)"|'([^']*)'|([^\]\s]*)))?\s*\]/g,
|
|
83
|
+
)) {
|
|
84
|
+
const [, attr, ...vals] = m
|
|
85
|
+
const value = vals.find((v) => v !== undefined)
|
|
86
|
+
if (attr === 'data-g') out.group = value
|
|
87
|
+
else if (attr === 'data-part') out.part = value
|
|
88
|
+
else if (attr === 'data-accent')
|
|
89
|
+
for (const w of (value ?? '').split(/\s+/).filter(Boolean)) {
|
|
90
|
+
const [k, v] = w.split(':')
|
|
91
|
+
out.accent[k] = v
|
|
92
|
+
}
|
|
93
|
+
else if (attr.startsWith('aria-')) {
|
|
94
|
+
if (value !== 'false') out.states.push(attr.slice(5))
|
|
95
|
+
} else {
|
|
96
|
+
const name = attr.replace(/^data-/, '')
|
|
97
|
+
if (value === undefined || value === '') out.states.push(name)
|
|
98
|
+
else out.axes[name] = value
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!out.group) throw usageError(`kerf style explain: no group in \`${t}\``)
|
|
102
|
+
return out
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* `kerf style explain <target>`: the group, and for the selection (defaults filled in) every declaration on the root
|
|
107
|
+
* and its parts in cascade order, with where each came from, plus the CSS that selection emits.
|
|
108
|
+
* @param {{ axes?: Record<string, string>, states?: string[], accent?: Record<string, string>, part?: string }} [extra]
|
|
109
|
+
*/
|
|
110
|
+
export function explainTarget({ matrix, scan, mod }, target, extra = {}) {
|
|
111
|
+
const t = parseTarget(target)
|
|
112
|
+
const g = matrix.groups[t.group]
|
|
113
|
+
if (!g) {
|
|
114
|
+
const near = closest(t.group, Object.keys(matrix.groups))
|
|
115
|
+
throw Object.assign(
|
|
116
|
+
new Error(
|
|
117
|
+
`D025 no group \`${t.group}\`${near.length ? `; did you mean ${near.map((n) => `\`${n}\``).join(' or ')}?` : ''} (\`kerf style find\` searches by what it is)`,
|
|
118
|
+
),
|
|
119
|
+
{ code: 'E_UNKNOWN_GROUP' },
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
const source = scan.groups.find((s) => s.name === t.group)
|
|
123
|
+
// A state written as data-x="…" in a selector, or an axis passed as --x: sort them by what the group has.
|
|
124
|
+
const axes = { ...t.axes, ...extra.axes }
|
|
125
|
+
const states = [...t.states, ...(extra.states ?? [])]
|
|
126
|
+
for (const [k, v] of Object.entries(axes))
|
|
127
|
+
if (!(k in g.axes) && g.states.includes(k) && (v === 'true' || v === '')) {
|
|
128
|
+
delete axes[k]
|
|
129
|
+
states.push(k)
|
|
130
|
+
}
|
|
131
|
+
const selection = {
|
|
132
|
+
axes,
|
|
133
|
+
states: [...new Set(states)],
|
|
134
|
+
accent: { ...t.accent, ...extra.accent },
|
|
135
|
+
part: extra.part ?? t.part ?? undefined,
|
|
136
|
+
}
|
|
137
|
+
let explained
|
|
138
|
+
try {
|
|
139
|
+
explained = mod.explainGroup(source, selection)
|
|
140
|
+
} catch (e) {
|
|
141
|
+
if (e.constructor?.name === 'GroupError')
|
|
142
|
+
throw Object.assign(new Error(`D025 ${e.message}`), { code: 'E_UNKNOWN_NAME' })
|
|
143
|
+
throw e
|
|
144
|
+
}
|
|
145
|
+
const sel = explained.selection
|
|
146
|
+
const sites = g.used.filter(
|
|
147
|
+
(u) =>
|
|
148
|
+
!u.part &&
|
|
149
|
+
Object.entries(sel.axes).every(
|
|
150
|
+
([a, v]) => !u.axes[a] || u.axes[a] === 'dynamic' || u.axes[a].includes(v),
|
|
151
|
+
),
|
|
152
|
+
)
|
|
153
|
+
return {
|
|
154
|
+
group: t.group,
|
|
155
|
+
doc: g.doc,
|
|
156
|
+
file: g.file,
|
|
157
|
+
axes: g.axes,
|
|
158
|
+
defaults: g.defaults,
|
|
159
|
+
parts: g.parts,
|
|
160
|
+
states: g.states,
|
|
161
|
+
contextTone: g.contextTone,
|
|
162
|
+
selection: sel,
|
|
163
|
+
attributes: attributesOf(t.group, sel, selection.part),
|
|
164
|
+
elements: explained.elements,
|
|
165
|
+
css: explained.css,
|
|
166
|
+
sites,
|
|
167
|
+
accents: g.accents,
|
|
168
|
+
similar: matrix.similar.filter((s) => s[0] === t.group || s[1] === t.group),
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** What the selection looks like in the DOM, as a person would find it in devtools. */
|
|
173
|
+
function attributesOf(group, sel, part) {
|
|
174
|
+
const root = [
|
|
175
|
+
`data-g="${group}"`,
|
|
176
|
+
...Object.entries(sel.axes).map(([a, v]) => `data-${a}="${v}"`),
|
|
177
|
+
...sel.states.map((s) => (ARIA.has(s) ? `aria-${s}="true"` : `data-${s}`)),
|
|
178
|
+
]
|
|
179
|
+
const accent = Object.entries(sel.accent)
|
|
180
|
+
.sort(([a], [b]) => (a < b ? -1 : 1))
|
|
181
|
+
.map(([k, v]) => `${k}:${v}`)
|
|
182
|
+
.join(' ')
|
|
183
|
+
if (accent) root.push(`data-accent="${accent}"`)
|
|
184
|
+
return { root: root.join(' '), ...(part ? { part: `data-part="${part}"` } : {}) }
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export function formatFind(r) {
|
|
188
|
+
if (!r.groups.length)
|
|
189
|
+
return `no group fits "${r.query}" (${r.total} groups). Define one with group() from @kerf-js/core/style; \`kerf docs style\` has the rules.`
|
|
190
|
+
const lines = r.groups.map((g) => {
|
|
191
|
+
const axes = Object.entries(g.axes)
|
|
192
|
+
.map(([a, v]) => `${a}: ${Array.isArray(v) ? v.join('|') : v}`)
|
|
193
|
+
.join(' · ')
|
|
194
|
+
return `${g.name.padEnd(18)} ${g.file}${g.doc ? `\n ${g.doc}` : ''}\n ${[axes, g.parts.length ? `parts: ${g.parts.join(', ')}` : '', g.states.length ? `states: ${g.states.join(', ')}` : '', `${g.uses} uses`].filter(Boolean).join(' · ')}`
|
|
195
|
+
})
|
|
196
|
+
return (
|
|
197
|
+
(r.partial ? `no group matches every word of "${r.query}"; closest:\n` : '') + lines.join('\n')
|
|
198
|
+
)
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function formatExplain(r) {
|
|
202
|
+
const out = [
|
|
203
|
+
`${r.group} ${r.file}${r.doc ? ` — ${r.doc}` : ''}`,
|
|
204
|
+
` <el ${r.attributes.root}>${r.attributes.part ? ` part: <el ${r.attributes.part}>` : ''}`,
|
|
205
|
+
]
|
|
206
|
+
for (const el of r.elements) {
|
|
207
|
+
if (!el.decls.length) continue
|
|
208
|
+
out.push(` ${el.element}:`)
|
|
209
|
+
for (const d of el.decls)
|
|
210
|
+
out.push(
|
|
211
|
+
` ${d.overridden ? '~' : ' '} ${`${d.prop}: ${d.value}`.padEnd(64)} ← ${d.from} (${d.at})${d.overridden ? ', overridden' : ''}`,
|
|
212
|
+
)
|
|
213
|
+
}
|
|
214
|
+
out.push('', r.css.trimEnd())
|
|
215
|
+
if (r.contextTone)
|
|
216
|
+
out.push('', 'Reads its tone from the nearest data-tone (veneer context tone).')
|
|
217
|
+
return out.join('\n')
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Long class lists are how Tailwind is written, so D020 stays out of an app that chose it. */
|
|
221
|
+
export function usesTailwind(root) {
|
|
222
|
+
const file = join(root, 'package.json')
|
|
223
|
+
if (!existsSync(file)) return false
|
|
224
|
+
const { dependencies, devDependencies } = JSON.parse(readFileSync(file, 'utf8'))
|
|
225
|
+
return 'tailwindcss' in { ...dependencies, ...devDependencies }
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* D020: class soup. A static `class`/`className` with more than 8 classes, or one class list (4+ classes, any order)
|
|
230
|
+
* in two or more places. The fix names a group after the component and element.
|
|
231
|
+
* @param {{ rel: string, text: string }[]} files
|
|
232
|
+
*/
|
|
233
|
+
export function classSoup(files) {
|
|
234
|
+
const out = []
|
|
235
|
+
const lists = new Map()
|
|
236
|
+
const attr = /\b(class|className)\s*=\s*(?:"([^"]*)"|'([^']*)'|\{\s*(["'`])([^"'`$]*)\4\s*\})/g
|
|
237
|
+
for (const { rel, text } of files) {
|
|
238
|
+
const pos = positions(text)
|
|
239
|
+
const named = new Map()
|
|
240
|
+
for (const m of text.matchAll(attr)) {
|
|
241
|
+
const value = m[2] ?? m[3] ?? m[5] ?? ''
|
|
242
|
+
const classes = value.split(/\s+/).filter(Boolean)
|
|
243
|
+
if (!classes.length) continue
|
|
244
|
+
const before = text.slice(0, m.index)
|
|
245
|
+
const component = [
|
|
246
|
+
...before.matchAll(/(?:function\s+([A-Z]\w*)|(?:const|let)\s+([A-Z]\w*)\s*=)/g),
|
|
247
|
+
].at(-1)
|
|
248
|
+
const tag = [...before.matchAll(/<([a-z][\w-]*)\b/g)].at(-1)?.[1] ?? 'div'
|
|
249
|
+
const base = component
|
|
250
|
+
? (component[1] ?? component[2]).replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase()
|
|
251
|
+
: rel
|
|
252
|
+
.replace(/^.*\//, '')
|
|
253
|
+
.replace(/\.\w+$/, '')
|
|
254
|
+
.toLowerCase()
|
|
255
|
+
const n = (named.get(base) ?? 0) + 1
|
|
256
|
+
named.set(base, n)
|
|
257
|
+
const name = n === 1 ? base : `${base}-${tag}`
|
|
258
|
+
const at = {
|
|
259
|
+
span: pos.span(m.index, m.index + m[0].length),
|
|
260
|
+
offset: [m.index, m.index + m[0].length],
|
|
261
|
+
}
|
|
262
|
+
const site = { rel, at, name, count: classes.length }
|
|
263
|
+
if (classes.length > 8)
|
|
264
|
+
out.push(
|
|
265
|
+
diagnostic({
|
|
266
|
+
code: 'D020',
|
|
267
|
+
severity: 'warning',
|
|
268
|
+
file: rel,
|
|
269
|
+
...at,
|
|
270
|
+
why: `<${tag}> carries ${classes.length} classes: nobody can read what it is or which variation it's in.`,
|
|
271
|
+
help: groupHelp(name),
|
|
272
|
+
}),
|
|
273
|
+
)
|
|
274
|
+
if (classes.length >= 4) {
|
|
275
|
+
const key = [...classes].sort().join(' ')
|
|
276
|
+
lists.set(key, [...(lists.get(key) ?? []), site])
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
for (const sites of lists.values()) {
|
|
281
|
+
if (sites.length < 2) continue
|
|
282
|
+
for (const s of sites.slice(1)) {
|
|
283
|
+
const first = sites[0]
|
|
284
|
+
out.push(
|
|
285
|
+
diagnostic({
|
|
286
|
+
code: 'D020',
|
|
287
|
+
severity: 'warning',
|
|
288
|
+
file: s.rel,
|
|
289
|
+
...s.at,
|
|
290
|
+
why: `The same ${s.count} classes are written in ${sites.length} places (first at ${first.rel}:${first.at.span[0]}): one look, copied by hand.`,
|
|
291
|
+
help: groupHelp(first.name),
|
|
292
|
+
related: sites.filter((x) => x !== s).map((x) => ({ file: x.rel, span: x.at.span })),
|
|
293
|
+
}),
|
|
294
|
+
)
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return out
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
const groupHelp = (name) =>
|
|
301
|
+
`Extract a group: \`export const ${name.replace(/-(\w)/g, (_, c) => c.toUpperCase())} = group('${name}', { base: { … } })\` and spread it (\`{...${name.replace(/-(\w)/g, (_, c) => c.toUpperCase())}()}\`). \`kerf style find\` first: one may already fit.`
|
|
302
|
+
|
|
303
|
+
/** Matrix diagnostics (D021–D025) as `kerf check` diagnostics, located on the group or the site. */
|
|
304
|
+
export function matrixDiagnostics(root, matrix) {
|
|
305
|
+
const texts = new Map()
|
|
306
|
+
const text = (file) => {
|
|
307
|
+
if (!texts.has(file))
|
|
308
|
+
texts.set(file, existsSync(join(root, file)) ? readFileSync(join(root, file), 'utf8') : '')
|
|
309
|
+
return texts.get(file)
|
|
310
|
+
}
|
|
311
|
+
return matrix.diagnostics.map((d) => {
|
|
312
|
+
const lines = text(d.file).split('\n')
|
|
313
|
+
const line = Math.max(1, Math.min(d.line, lines.length))
|
|
314
|
+
const col = (lines[line - 1]?.search(/\S/) ?? 0) + 1
|
|
315
|
+
const loc = d.line
|
|
316
|
+
? { span: [line, col, line, (lines[line - 1]?.length ?? 0) + 1] }
|
|
317
|
+
: locate(text(d.file), d.group)
|
|
318
|
+
return diagnostic({
|
|
319
|
+
code: d.code,
|
|
320
|
+
severity: d.severity,
|
|
321
|
+
file: d.file,
|
|
322
|
+
...loc,
|
|
323
|
+
why: d.message,
|
|
324
|
+
help: d.help,
|
|
325
|
+
})
|
|
326
|
+
})
|
|
327
|
+
}
|