@sixbell-telco/eslint-plugin-sdk 0.0.0 → 0.1.1
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/CHANGELOG.md +29 -0
- package/README.md +249 -2
- package/package.json +26 -4
- package/src/index.mjs +149 -0
- package/src/manifest.mjs +216 -0
- package/src/rules/app-rules.mjs +353 -0
- package/src/rules/app-support.mjs +131 -0
- package/src/rules/composition-rules.mjs +373 -0
- package/src/rules/import-rules.mjs +42 -0
- package/src/rules/style-rules.mjs +294 -0
- package/src/rules/template-rules.mjs +461 -0
- package/src/types.d.ts +122 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Design-system class rules for templates (DESIGN.md "Typography" and "Geometry, surfaces, and
|
|
3
|
+
* effects"), ported from the MFE design-system contract with their original rule ids.
|
|
4
|
+
*
|
|
5
|
+
* One owner per case: on an `st-*` element, shadcn/no-restyle (run by `sdk-tools check`) owns every
|
|
6
|
+
* class that is not layout or spacing, so these rules read only non-kit elements, except
|
|
7
|
+
* `kit-override`, which reports the layout and spacing `!` and `[&...]` tokens no-restyle lets through. CSS
|
|
8
|
+
* declarations (`box-shadow`, `font-family` in stylesheets) belong to `check`'s styles step.
|
|
9
|
+
*/
|
|
10
|
+
import { getTemplateParserServices } from '@angular-eslint/utils';
|
|
11
|
+
import { idRule } from './app-support.mjs';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Template AST nodes are read by shape (Element, Template, TextAttribute, BoundAttribute).
|
|
15
|
+
* @typedef {any} Node
|
|
16
|
+
* @typedef {import('eslint').Rule.RuleContext} RuleContext
|
|
17
|
+
* @typedef {import('../types.js').TemplateSpan} TemplateSpan
|
|
18
|
+
* @typedef {{ raw: string, base: string, variants: string[] }} StyleToken
|
|
19
|
+
* @typedef {{ tag: string, kit: boolean, textHost: boolean, dragPreview: boolean }} StyleElement
|
|
20
|
+
* @typedef {{ tokens: StyleToken[], span: TemplateSpan }} ClassSource
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
// Arbitrary `[...]` values are left out of every pattern: shadcn/no-arbitrary-values (and
|
|
24
|
+
// no-raw-colors for `/[0.1]` opacity) report them in `sdk-tools check`.
|
|
25
|
+
// Raw Tailwind radius once variants and `!` are stripped: the default and scale steps on every side
|
|
26
|
+
// or corner, and `rounded-(--x)` unless it names a theme radius role.
|
|
27
|
+
const RAW_RADIUS =
|
|
28
|
+
/^rounded(?:-(?:t|r|b|l|tl|tr|bl|br|s|e|ss|se|ee|es))?(?:-(?:xs|sm|md|lg|xl|2xl|3xl|4xl|\((?!--radius-(?:box|field|selector)\))[^)]+\)))?$/;
|
|
29
|
+
const TEXT_TYPOGRAPHY =
|
|
30
|
+
/^(?:text-(?:xs|sm|base|lg|[2-9]?xl)|font-(?:thin|extralight|light|normal|medium|semibold|bold|extrabold|black)|leading-\S+|tracking-\S+|uppercase|lowercase|capitalize)$/;
|
|
31
|
+
const TEXT_TONE = /^(?:text|border|divide)-base-content\/\d+$/;
|
|
32
|
+
const SURFACE_TINT = /^bg-(?:primary|secondary|accent|success|error|warning|info|base-\d+)\/\d+$/;
|
|
33
|
+
const RAW_SHADOW = /^(?:drop-)?shadow(?:-(?:2xs|xs|sm|md|lg|xl|2xl))?$/;
|
|
34
|
+
const FONT_FAMILY = /^font-(?:sans|serif|\([^)]+\))$/;
|
|
35
|
+
const BORDER_WIDTH = /^(?:border|divide)(?:-[xytrblse]{1,2})?-[2-8]$/;
|
|
36
|
+
// Accent stripes are the documented border-2 use outside dashed drop targets.
|
|
37
|
+
const BORDER_WIDTH_ALLOWED = new Set(['border-s-2']);
|
|
38
|
+
const SMALL_CHIP_PADDING_Y = /^py-(?:0|0\.5|1)$/;
|
|
39
|
+
const SMALL_CHIP_PADDING_X = /^px-(?:0|0\.5|1|1\.5|2|2\.5)$/;
|
|
40
|
+
const SMALL_CHIP_SIZE = /^size-(?:[0-7](?:\.5)?|8)$/;
|
|
41
|
+
const FLOATING_SHADOW = new Set(['shadow-overlay', 'shadow-floating', 'shadow-modal', 'shadow-main', 'shadow-main-lg']);
|
|
42
|
+
const TEXT_HOST_ATTRS = new Set(['stText']);
|
|
43
|
+
const DRAG_PREVIEW_ATTRS = new Set(['cdkDragPreview']);
|
|
44
|
+
const KIT_ELEMENT = /^st-/;
|
|
45
|
+
// Color, typography, shape, border and effect utilities: shadcn/no-restyle reports these on an st-*
|
|
46
|
+
// element with or without `!`, so kit-override leaves them to it (one owner per finding).
|
|
47
|
+
const RESTYLE_OWNED =
|
|
48
|
+
/^(?:bg-|from-|via-|to-|fill-|stroke-|ring|outline|decoration-|caret-|accent-|rounded|shadow|drop-shadow|opacity-|border(?:$|-)|divide-|font-|leading-|tracking-|uppercase|lowercase|capitalize|italic|text-(?!left$|right$|center$|justify$|start$|end$|wrap$|nowrap$|pretty$|balance$|ellipsis$|clip$))/;
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Splits `md:hover:[&_svg]:size-4` on the `:` that are outside brackets and parentheses.
|
|
52
|
+
* @param {string} token
|
|
53
|
+
*/
|
|
54
|
+
function splitVariants(token) {
|
|
55
|
+
const parts = [];
|
|
56
|
+
let depth = 0;
|
|
57
|
+
let current = '';
|
|
58
|
+
for (const char of token) {
|
|
59
|
+
if (char === '[' || char === '(') depth += 1;
|
|
60
|
+
else if ((char === ']' || char === ')') && depth > 0) depth -= 1;
|
|
61
|
+
if (char === ':' && depth === 0) {
|
|
62
|
+
parts.push(current);
|
|
63
|
+
current = '';
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
current += char;
|
|
67
|
+
}
|
|
68
|
+
parts.push(current);
|
|
69
|
+
return parts;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* The utility a class token applies once variants and the `!` modifier are stripped.
|
|
74
|
+
* @param {string} raw
|
|
75
|
+
* @returns {StyleToken}
|
|
76
|
+
*/
|
|
77
|
+
function normalizeClassToken(raw) {
|
|
78
|
+
const parts = splitVariants(raw);
|
|
79
|
+
const last = parts[parts.length - 1];
|
|
80
|
+
return { raw, base: last.replace(/^!/, '').replace(/!$/, ''), variants: parts.slice(0, -1) };
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** @param {string} text */
|
|
84
|
+
const tokensOf = (text) => [...text.matchAll(/\S+/g)].map((match) => normalizeClassToken(match[0]));
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* The class strings of an element: the static `class`, the string literals of `[class]` and
|
|
88
|
+
* `[ngClass]`, and the class names of `[class.x]` bindings.
|
|
89
|
+
* @param {Node} element
|
|
90
|
+
* @returns {ClassSource[]}
|
|
91
|
+
*/
|
|
92
|
+
function classSources(element) {
|
|
93
|
+
/** @type {ClassSource[]} */
|
|
94
|
+
const sources = [];
|
|
95
|
+
for (const attribute of element.attributes ?? []) {
|
|
96
|
+
if (attribute.name === 'class' && attribute.value)
|
|
97
|
+
sources.push({ tokens: tokensOf(attribute.value), span: attribute.valueSpan ?? attribute.sourceSpan });
|
|
98
|
+
}
|
|
99
|
+
for (const input of element.inputs ?? []) {
|
|
100
|
+
// `[class.rounded-lg]="on"`: the key span keeps the binding's `class.` prefix.
|
|
101
|
+
if (String(input.keySpan?.details ?? '').startsWith('class.')) {
|
|
102
|
+
sources.push({ tokens: tokensOf(input.name), span: input.keySpan ?? input.sourceSpan });
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (input.name !== 'class' && input.name !== 'ngClass') continue;
|
|
106
|
+
const source = input.value?.source ?? '';
|
|
107
|
+
const literals = [...source.matchAll(/'([^']+)'|"([^"]+)"/g)].map((match) => match[1] ?? match[2]);
|
|
108
|
+
if (literals.length) sources.push({ tokens: tokensOf(literals.join(' ')), span: input.valueSpan ?? input.sourceSpan });
|
|
109
|
+
}
|
|
110
|
+
return sources;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* What the rules need to know about the element a class sits on. `*cdkDragPreview` puts the
|
|
115
|
+
* attribute on the wrapping template node, so the parent is read too.
|
|
116
|
+
* @param {Node} element
|
|
117
|
+
* @returns {StyleElement}
|
|
118
|
+
*/
|
|
119
|
+
function describe(element) {
|
|
120
|
+
/** @param {Node | undefined} node */
|
|
121
|
+
const names = (node) => [...(node?.attributes ?? []), ...(node?.inputs ?? []), ...(node?.templateAttrs ?? [])].map((a) => a.name);
|
|
122
|
+
const own = names(element);
|
|
123
|
+
const parent = element.parent?.type === 'Template' || element.parent?.tagName ? names(element.parent) : [];
|
|
124
|
+
return {
|
|
125
|
+
tag: element.name,
|
|
126
|
+
kit: KIT_ELEMENT.test(element.name),
|
|
127
|
+
textHost: element.name === 'st-text' || own.some((name) => TEXT_HOST_ATTRS.has(name)),
|
|
128
|
+
dragPreview: [...own, ...parent].some((name) => DRAG_PREVIEW_ATTRS.has(name)),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* A rule that visits every element's class tokens.
|
|
134
|
+
* @param {{ name: string, type?: 'problem' | 'suggestion', description: string, messages: Record<string, string>, kit?: boolean,
|
|
135
|
+
* check: (tokens: StyleToken[], element: StyleElement, report: (token: StyleToken, messageId: string, data?: Record<string, string>) => void) => void }} definition
|
|
136
|
+
*/
|
|
137
|
+
function classRule({ name, type, description, messages, kit = false, check }) {
|
|
138
|
+
return idRule({
|
|
139
|
+
name,
|
|
140
|
+
type,
|
|
141
|
+
description,
|
|
142
|
+
messages,
|
|
143
|
+
create(context) {
|
|
144
|
+
const services = /** @type {{ convertNodeSourceSpanToLoc(span: TemplateSpan): import('eslint').AST.SourceLocation }} */ (
|
|
145
|
+
/** @type {unknown} */ (getTemplateParserServices(/** @type {any} */ (context)))
|
|
146
|
+
);
|
|
147
|
+
return {
|
|
148
|
+
/** @param {Node} node */
|
|
149
|
+
Element(node) {
|
|
150
|
+
const element = describe(node);
|
|
151
|
+
if (element.kit !== kit) return;
|
|
152
|
+
for (const source of classSources(node)) {
|
|
153
|
+
check(source.tokens, element, (token, messageId, data = {}) =>
|
|
154
|
+
context.report({ loc: services.convertNodeSourceSpanToLoc(source.span), messageId, data: { token: token.raw, ...data } }),
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
},
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/** Each token whose utility matches `pattern`. */
|
|
164
|
+
const eachMatch =
|
|
165
|
+
(/** @type {RegExp} */ pattern, /** @type {string} */ messageId) =>
|
|
166
|
+
(/** @type {StyleToken[]} */ tokens, /** @type {StyleElement} */ _element, /** @type {(t: StyleToken, id: string) => void} */ report) => {
|
|
167
|
+
for (const token of tokens) if (pattern.test(token.base)) report(token, messageId);
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
export const radiusRaw = classRule({
|
|
171
|
+
name: 'radius-raw',
|
|
172
|
+
description: 'Use a semantic radius token (rounded-box, rounded-field, rounded-selector) instead of a Tailwind radius step',
|
|
173
|
+
messages: {
|
|
174
|
+
DS_RADIUS_RAW:
|
|
175
|
+
'"{{token}}" is a raw radius: use rounded-box (cards, panels, dialogs), rounded-field (controls), rounded-selector (badges, chips) or rounded-full (circles only).',
|
|
176
|
+
},
|
|
177
|
+
check: eachMatch(RAW_RADIUS, 'DS_RADIUS_RAW'),
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
export const radiusRole = classRule({
|
|
181
|
+
name: 'radius-role',
|
|
182
|
+
type: 'suggestion',
|
|
183
|
+
description: 'Match the radius token to the role of the element (box on surfaces, selector on chips)',
|
|
184
|
+
messages: {
|
|
185
|
+
DS_RADIUS_ROLE: '"{{token}}" on {{what}}: {{use}}. The radius follows the role of the element (DESIGN.md "Geometry, surfaces, and effects").',
|
|
186
|
+
},
|
|
187
|
+
check(tokens, element, report) {
|
|
188
|
+
const bases = new Set(tokens.map((token) => token.base));
|
|
189
|
+
const box = tokens.find((token) => token.base === 'rounded-box');
|
|
190
|
+
if (box) {
|
|
191
|
+
const smallChip =
|
|
192
|
+
([...bases].some((base) => SMALL_CHIP_PADDING_Y.test(base)) && [...bases].some((base) => SMALL_CHIP_PADDING_X.test(base))) ||
|
|
193
|
+
[...bases].some((base) => SMALL_CHIP_SIZE.test(base));
|
|
194
|
+
if (['span', 'code', 'kbd'].includes(element.tag) || bases.has('badge') || smallChip) {
|
|
195
|
+
report(box, 'DS_RADIUS_ROLE', {
|
|
196
|
+
what: 'a chip or compact status',
|
|
197
|
+
use: 'use rounded-selector (badges, chips) or rounded-field (icon tiles, controls)',
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const control = tokens.find((token) => token.base === 'rounded-selector' || token.base === 'rounded-field');
|
|
202
|
+
if (control && ([...bases].some((base) => FLOATING_SHADOW.has(base)) || element.dragPreview)) {
|
|
203
|
+
report(control, 'DS_RADIUS_ROLE', { what: 'a floating surface', use: 'use rounded-box (floating surfaces, panels, drag previews)' });
|
|
204
|
+
}
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
export const textRaw = classRule({
|
|
209
|
+
name: 'text-raw',
|
|
210
|
+
description: 'Set typography through st-text or [stText] presets and kit text slots, not raw size and weight utilities',
|
|
211
|
+
messages: {
|
|
212
|
+
DS_TEXT_RAW:
|
|
213
|
+
'"{{token}}" is raw typography: use <st-text> or [stText] with preset, weight and color, or the kit slot that owns the text (st-card-title, st-card-subtitle, st-description-item, st-form-hint).',
|
|
214
|
+
},
|
|
215
|
+
check(tokens, element, report) {
|
|
216
|
+
if (element.textHost) return;
|
|
217
|
+
for (const token of tokens) if (TEXT_TYPOGRAPHY.test(token.base)) report(token, 'DS_TEXT_RAW');
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
export const textTone = classRule({
|
|
222
|
+
name: 'text-tone',
|
|
223
|
+
description: 'Use the muted and secondary text tokens instead of base-content opacity steps',
|
|
224
|
+
messages: {
|
|
225
|
+
DS_TEXT_TONE:
|
|
226
|
+
'"{{token}}" fades base-content with opacity: use text-base-content-muted or text-base-content-secondary for text, border-base-300 or a plain border for lines.',
|
|
227
|
+
},
|
|
228
|
+
check: eachMatch(TEXT_TONE, 'DS_TEXT_TONE'),
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
export const surfaceTint = classRule({
|
|
232
|
+
name: 'surface-tint',
|
|
233
|
+
type: 'suggestion',
|
|
234
|
+
description: 'Use a theme surface or the kit component that owns a tint instead of an opacity background',
|
|
235
|
+
messages: {
|
|
236
|
+
DS_SURFACE_TINT:
|
|
237
|
+
'"{{token}}" tints a background with opacity: use a surface step (bg-base-100, bg-base-200, bg-base-300) or the kit component that owns the tint (st-alert, a soft st-badge).',
|
|
238
|
+
},
|
|
239
|
+
check(tokens, _element, report) {
|
|
240
|
+
// `selection:bg-primary/20` colors selected text, not a surface.
|
|
241
|
+
for (const token of tokens) if (SURFACE_TINT.test(token.base) && !token.variants.includes('selection')) report(token, 'DS_SURFACE_TINT');
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
export const shadowRaw = classRule({
|
|
246
|
+
name: 'shadow-raw',
|
|
247
|
+
description: 'Use a theme shadow token instead of a Tailwind shadow step',
|
|
248
|
+
messages: {
|
|
249
|
+
DS_SHADOW_RAW:
|
|
250
|
+
'"{{token}}" is a raw shadow: use shadow-raised (raised surfaces), shadow-overlay (overlay panels), shadow-floating (toast, drag preview) or shadow-modal (dialog, sticky footer).',
|
|
251
|
+
},
|
|
252
|
+
check: eachMatch(RAW_SHADOW, 'DS_SHADOW_RAW'),
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
export const fontFamily = classRule({
|
|
256
|
+
name: 'font-family',
|
|
257
|
+
description: 'Use the theme font tokens (font-body, font-heading) instead of a raw font family',
|
|
258
|
+
messages: {
|
|
259
|
+
DS_FONT_FAMILY: '"{{token}}" is a raw font family: the theme font is Nunito Sans, through font-body or font-heading (font-mono for code).',
|
|
260
|
+
},
|
|
261
|
+
check: eachMatch(FONT_FAMILY, 'DS_FONT_FAMILY'),
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
export const borderWidth = classRule({
|
|
265
|
+
name: 'border-width',
|
|
266
|
+
type: 'suggestion',
|
|
267
|
+
description: 'Keep boundaries at the theme border width',
|
|
268
|
+
messages: {
|
|
269
|
+
DS_BORDER_WIDTH:
|
|
270
|
+
'"{{token}}" widens a boundary: use a plain border (the theme --border). Wide borders are for dashed drop targets (border-dashed) and accent stripes (border-s-2).',
|
|
271
|
+
},
|
|
272
|
+
check(tokens, _element, report) {
|
|
273
|
+
if (tokens.some((token) => token.base === 'border-dashed')) return;
|
|
274
|
+
for (const token of tokens) {
|
|
275
|
+
if (BORDER_WIDTH.test(token.base) && !BORDER_WIDTH_ALLOWED.has(token.base)) report(token, 'DS_BORDER_WIDTH');
|
|
276
|
+
}
|
|
277
|
+
},
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
export const kitOverride = classRule({
|
|
281
|
+
name: 'kit-override',
|
|
282
|
+
description: 'Do not force classes onto st-* internals with ! or [&...] selectors',
|
|
283
|
+
kit: true,
|
|
284
|
+
messages: {
|
|
285
|
+
DS_KIT_OVERRIDE:
|
|
286
|
+
'"{{token}}" forces a style onto <{{tag}}> internals: use the component input (variant, size, color) or ask for a kit change; never ! or [&...] on st-* elements.',
|
|
287
|
+
},
|
|
288
|
+
check(tokens, element, report) {
|
|
289
|
+
for (const token of tokens) {
|
|
290
|
+
if (RESTYLE_OWNED.test(token.base)) continue;
|
|
291
|
+
if (token.raw.includes('!') || token.raw.includes('[&')) report(token, 'DS_KIT_OVERRIDE', { tag: element.tag });
|
|
292
|
+
}
|
|
293
|
+
},
|
|
294
|
+
});
|