@alis-build/design-system 0.18.0 → 0.21.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/rules/styling.md CHANGED
@@ -76,12 +76,29 @@ global class or invent a role: the system changes once, by decision.
76
76
 
77
77
  ## Enforce it
78
78
 
79
- Lint for what can be linted: hex literals, off-scale spacing and radius, shadows,
80
- local font declarations and forced-priority overrides in component code. Run it
81
- as a ratchet, failing when a file gains violations, so an existing backlog
82
- doesn't block new work. Re-record the baseline only after an intentional change,
83
- never to clear a failure, and say in the commit why the number moved.
84
-
85
- Check by hand what a linter can't see: a static `style=""` attribute in a
86
- template, and a case transform in code (`toUpperCase()` on displayed text),
87
- which shouts exactly as `text-transform` would.
79
+ The package ships what can be linted, so every app checks the same things
80
+ (setup: the package README).
81
+
82
+ - **`stylelint.mjs`** restates this page as a stylelint config an app extends:
83
+ a literal colour, a role at an alpha outside a drawing or the scrim, spacing,
84
+ radius and shadow written without their tokens, local type, a forced
85
+ priority, and a selector on a system class. It reads the token and class
86
+ names from the package, so a new token needs no edit to the check. In a
87
+ Svelte or Vue file it reads the `<style>` block and a static `style=""`.
88
+ - **A gap is said where it is**, by rule and with a reason:
89
+ `/* stylelint-disable-next-line <rule> -- GAP: what this needs */`. A disable
90
+ is itself reported when it names no rule, gives no reason, or silences
91
+ nothing.
92
+ - **`alisx-check styles`** runs it as a gate. An app with a backlog records a
93
+ baseline and it becomes a ratchet, failing when a file gains violations of a
94
+ rule, so the backlog doesn't block new work. Re-record the baseline only
95
+ after an intentional change, never to clear a failure, and say in the commit
96
+ why the number moved.
97
+ - **`alisx-check names`** fails on a system class or a custom property the
98
+ installed version doesn't have (`classes.json`): a misspelt class renders
99
+ unstyled and says nothing. A property that arrives at run time (a client's
100
+ theme) is known once the file that declares it is named.
101
+
102
+ Check by hand what a linter can't see: a computed style binding, a value behind
103
+ a variable of your own, and a case transform in code (`toUpperCase()` on
104
+ displayed text), which shouts exactly as `text-transform` would.
package/stylelint.mjs ADDED
@@ -0,0 +1,167 @@
1
+ // The style check for an app built on @alis-build/design-system.
2
+ //
3
+ // Every rule restates a row of rules/styling.md: appearance comes from the
4
+ // system, a site supplies layout, motion and drawing. Nothing here is a new
5
+ // opinion. The names it accepts (tokens, classes) are read from classes.json,
6
+ // so a token added to the system is accepted without an edit here.
7
+ //
8
+ // Use it from your stylelint config (stylelint 16.11 or later):
9
+ //
10
+ // export default {
11
+ // extends: ['@alis-build/design-system/stylelint'],
12
+ // overrides: [{ files: ['**/*.svelte', '**/*.vue'], customSyntax: 'postcss-html' }],
13
+ // }
14
+ //
15
+ // A rule you set under the same name REPLACES the system's: stylelint never
16
+ // merges two lists. To add to one, or to keep one part of it for a file, carry
17
+ // the system's parts along (the named exports below) and build the rule with
18
+ // refuse() or allow().
19
+ //
20
+ // When nothing in styling.md fits, the system has a gap. Say so where it is:
21
+ //
22
+ // /* stylelint-disable-next-line <rule> -- GAP: what this needs */
23
+ //
24
+ // A disable is itself reported when it names no rule (it would silence every
25
+ // rule on that line), gives no reason after `--`, or silences nothing.
26
+ import { readFileSync } from 'node:fs'
27
+
28
+ export const guide = 'rules/styling.md in @alis-build/design-system'
29
+
30
+ const inventory = JSON.parse(readFileSync(new URL('./classes.json', import.meta.url), 'utf8'))
31
+ const escaped = (names) => names.map((name) => name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')).join('|')
32
+ const tokens = (prefix) =>
33
+ inventory.properties
34
+ .filter((name) => name.startsWith(prefix))
35
+ .map((name) => name.slice(2))
36
+ .sort((a, b) => a.localeCompare(b, undefined, { numeric: true }))
37
+ const spaces = tokens('--space-')
38
+ const radii = tokens('--radius-')
39
+ const elevations = tokens('--elevation-')
40
+ // Every class the system defines, except its state classes (is-selected …),
41
+ // which an app also sets on its own elements.
42
+ const systemClasses = inventory.classes.filter((name) => !name.startsWith('is-'))
43
+
44
+ // A colour written out: #006383, rgb(0 99 131), color(srgb 0 .4 .5). A hash
45
+ // inside url() is a reference, not a colour.
46
+ const literalColour =
47
+ /(?<!url\([^)]*)#(?:[0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})(?![\w-])|\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch)\(\s*[-+.\d]|\bcolor\(\s*(?!from\b)[\w-]+\s+[-+.\d]/i
48
+ // A role at an alpha or a blend: color-mix(…), rgb(var(--x) / 50%),
49
+ // oklch(from var(--x) …). The scrim at an alpha is the one styling.md allows.
50
+ const blendedColour =
51
+ /^(?!.*var\(--scrim\)).*(?:\bcolor-mix\(|\b(?:rgba?|hsla?|hwb|lab|lch|oklab|oklch|color)\(\s*(?:from\b|var\())/is
52
+ // A length written as a number (12px, -8px, 0.75rem), or a spacing token the
53
+ // system doesn't have. A token's own name (var(--space-12)) is not a length.
54
+ const literalLength = /(?<!--[\w-]*)(?<![\w.])\d*\.?\d+(?:px|r?em|ch|ex|pt|pc|cm|mm|in|vw|vh|vmin|vmax|[sld]v[wh])\b/i
55
+ const unknownSpace = new RegExp(`var\\(\\s*--space-(?!(?:${escaped(spaces.map((name) => name.slice('space-'.length)))})\\s*[,)])`)
56
+
57
+ // The parts, exported so an app can add to a rule, or keep one part of it for
58
+ // a file, without losing the rest. Each part has a name, so an app picks it by
59
+ // that name and never by its message or its place in a list.
60
+ // A key is a property name or a "/regex/" string, as stylelint reads them.
61
+
62
+ /** Values no declaration may carry, each part by name. */
63
+ export const refused = {
64
+ literalColour: {
65
+ // A mask reads alpha only, and a filter's shadow is the same in both themes.
66
+ properties: '/^(?!(?:-webkit-)?(?:mask|filter|backdrop-filter))/',
67
+ values: [literalColour],
68
+ message: `A literal colour is the same in both themes: name a role (${guide}).`,
69
+ },
70
+ blend: {
71
+ // "A role at an alpha is allowed for an area or a band in a drawing, and
72
+ // for the scrim behind text on an image. Nowhere else."
73
+ properties: '/^(?!(?:fill|stroke|stop-color)$)/',
74
+ values: [blendedColour],
75
+ message: `A role at an alpha or a blend belongs to a drawing (fill, stroke) or the scrim. Elsewhere, name the nearest role (${guide}).`,
76
+ },
77
+ spacing: {
78
+ properties: '/^(?:(?:margin|padding|scroll-margin|scroll-padding)(?:-|$)|(?:grid-)?(?:row-|column-)?gap$)/',
79
+ values: [literalLength, unknownSpace],
80
+ message: (property) => `Spacing comes from the scale: write ${property} with ${spaces.map((name) => `var(--${name})`).join(', ')}, or 0 (${guide}).`,
81
+ },
82
+ }
83
+ /** Every refused part, as the default config uses them. */
84
+ export const refusedValues = Object.values(refused)
85
+
86
+ /** Properties whose every value must be one of these, each part by name. */
87
+ export const allowed = {
88
+ radius: {
89
+ properties: '/radius$/',
90
+ values: [new RegExp(`^(?:(?:var\\(--(?:${escaped(radii)})\\)|0|50%|inherit)(?:\\s*\\/\\s*|\\s+|$))+$`)],
91
+ message: `A radius is ${radii.map((name) => `var(--${name})`).join(', ')}, 0, or 50% for a circle (${guide}).`,
92
+ },
93
+ shadow: {
94
+ properties: '/^(?:-webkit-|-moz-)?box-shadow$/',
95
+ values: ['none', 'inherit', new RegExp(`^var\\(--(?:${escaped(elevations)})\\)$`)],
96
+ message: `A shadow is ${elevations.map((name) => `var(--${name})`).join(', ')}, or none (${guide}).`,
97
+ },
98
+ }
99
+ /** Every allowed part, as the default config uses them. */
100
+ export const allowedValues = Object.values(allowed)
101
+
102
+ /** Type belongs to a text-* class. Refused in a rule; an @font-face is not one. */
103
+ export const refusedProperties = {
104
+ properties: ['font', 'font-family', 'font-size', 'font-weight', 'font-variation-settings', 'line-height', 'letter-spacing', 'text-transform', 'text-shadow'],
105
+ message: (selector, property) => `${property} is appearance: a text-* class carries type (${guide}).`,
106
+ }
107
+
108
+ /** A site never restyles a system class. Exact names, so .app-bar-search of your own is yours. */
109
+ export const refusedSelectors = {
110
+ selectors: [new RegExp(`\\.(?:${escaped(systemClasses)})(?![\\w-])`)],
111
+ message: `A site never restyles a system component, a type class or prose (${guide}).`,
112
+ }
113
+
114
+ const applies = (key, property) => {
115
+ const pattern = /^\/.*\/$/.test(key) ? new RegExp(key.slice(1, -1)) : null
116
+ // stylelint 16 matches a key against the property without its vendor prefix.
117
+ return [property, property.replace(/^-(?:webkit|moz|ms|o)-/, '')].some((name) => (pattern ? pattern.test(name) : key === name))
118
+ }
119
+ const matches = (pattern, value) => (typeof pattern === 'string' ? pattern === value : pattern.test(value))
120
+ const said = (entry, property, value) => (typeof entry.message === 'function' ? entry.message(property, value) : entry.message)
121
+ const listed = (entries) => {
122
+ // A part this version doesn't have (a name a release changed) must stop the
123
+ // check, never thin the rule out quietly.
124
+ if (entries.some((entry) => !entry?.properties || !entry.values))
125
+ throw new Error(`@alis-build/design-system/stylelint: refuse() or allow() was given a part this version doesn't have. Its parts are refused.{${Object.keys(refused).join(', ')}} and allowed.{${Object.keys(allowed).join(', ')}}.`)
126
+ const list = {}
127
+ for (const entry of entries) (list[entry.properties] ??= []).push(...entry.values)
128
+ return list
129
+ }
130
+
131
+ /** declaration-property-value-disallowed-list from entries, each with its own message. */
132
+ export function refuse(entries) {
133
+ const message = (property, value) => {
134
+ const entry = entries.find((each) => applies(each.properties, property) && each.values.some((pattern) => matches(pattern, value)))
135
+ return entry ? said(entry, property, value) : `${property}: ${value} is not from the system (${guide}).`
136
+ }
137
+ return [listed(entries), { message }]
138
+ }
139
+
140
+ /** declaration-property-value-allowed-list from entries, each with its own message. */
141
+ export function allow(entries) {
142
+ const message = (property, value) => {
143
+ const entry = entries.find((each) => applies(each.properties, property))
144
+ return entry ? said(entry, property, value) : `${property}: ${value} is not from the system (${guide}).`
145
+ }
146
+ return [listed(entries), { message }]
147
+ }
148
+
149
+ export default {
150
+ // An exemption names its rule and says why: `<rule> -- GAP: …`. One that
151
+ // names no rule would silence every rule on its line; one that silences
152
+ // nothing (the code changed, the rule is off for this file, the name is
153
+ // misspelt) is a leftover.
154
+ reportDescriptionlessDisables: true,
155
+ reportUnscopedDisables: true,
156
+ reportNeedlessDisables: true,
157
+ rules: {
158
+ // A named colour is a hex by another name. Hex is refused with the other
159
+ // literal colours below, so a mask may use either.
160
+ 'color-named': ['never', { ignoreProperties: ['/mask/', '/filter/'], message: `A named colour is the same in both themes: name a role (${guide}).` }],
161
+ 'declaration-property-value-disallowed-list': refuse(refusedValues),
162
+ 'declaration-property-value-allowed-list': allow(allowedValues),
163
+ 'rule-selector-property-disallowed-list': [{ '/.*/': refusedProperties.properties }, { message: refusedProperties.message }],
164
+ 'declaration-no-important': [true, { message: `!important fights the system: settle it once, in the theme, or not at all (${guide}).` }],
165
+ 'selector-disallowed-list': [refusedSelectors.selectors, { message: refusedSelectors.message }],
166
+ },
167
+ }