Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/stylelint-config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ There are additional configs for specific language features and strict mode:
| Config | Description |
|--------|-------------|
| @trigen/stylelint-config/logical | Rules with CSS logical properties support. |
| @trigen/stylelint-config/strict | Strict rules for enhanced code quality. |
| @trigen/stylelint-config/strict | Strict rules for enhanced code quality: colors, fonts, layers and lengths through tokens. |
| @trigen/stylelint-config/flat-layers | Rules for the files of Flat Layers Design: tokens, breakpoints, container names, mixins and global styles each in their own file (`variables.css`, `media.css`, `containers.module.css`, `mixins.module.css`, `globals.css`, `styles.css`). |

Example:

Expand Down
5 changes: 5 additions & 0 deletions packages/stylelint-config/src/flat-layers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/**
* Export Flat Layers Design subconfig
*/

export { default } from './subconfigs/flat-layers.js'
8 changes: 7 additions & 1 deletion packages/stylelint-config/src/subconfigs/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ export default {
'color-function-notation': 'legacy',
'color-named': 'always-where-possible',
// Length
'length-zero-no-unit': true,
// A custom property keeps its unit: `0px` in `calc()` is a length, a bare `0` is not
'length-zero-no-unit': [
true,
{
ignore: ['custom-properties']
}
],
// Font weight
'font-weight-notation': 'named-where-possible',
// Function
Expand Down
131 changes: 131 additions & 0 deletions packages/stylelint-config/src/subconfigs/flat-layers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* Flat Layers Design config
*/

import {
strictCustomPropertyMatcher,
strictMediaQueryMatcher
} from './matchers.js'

// The files the flat-layers-design and css-containers skills keep the tokens, the breakpoints,
// the mixins, the container names and the global styles in
const variablesFiles = ['**/variables.css']
const mediaFiles = ['**/media.css']
const mixinsFiles = ['**/mixins.module.css']
const containersFiles = ['**/containers.module.css']
const globalFiles = [
'**/styles.css',
'**/globals.css',
...variablesFiles,
...mixinsFiles
]
const mediaFeatures = [
'width',
'min-width',
'max-width',
'height',
'min-height',
'max-height',
'hover',
'any-hover',
'pointer',
'any-pointer',
'prefers-reduced-motion',
'prefers-contrast',
'forced-colors',
'orientation',
'aspect-ratio',
'resolution'
]

export default {
rules: {
// Breakpoints and features are custom media of `media.css`, and the theme is the tokens' or,
// for a picture drawn per theme, the mixins'
'media-feature-name-disallowed-list': [...mediaFeatures, 'prefers-color-scheme'],
'at-rule-disallowed-list': ['custom-media'],
'custom-media-pattern': [
strictMediaQueryMatcher,
{
message: 'Expected custom media query name to be in camelCase and start with "screen" (e.g. "--screenSmUp", "--screenReducedMotion")'
}
],
// A container's name is a token of `containers.module.css`: an empty list takes no value
'declaration-property-value-allowed-list': {
'/^container(-name)?$/': []
},
// A module styles its own classes, and the root only while one of them stands, by `:has()`;
// the chosen theme is read by the mixins alone
'selector-disallowed-list': [
[
'/^:global(?!\\(html\\):has\\()/',
'/^(:root|html|body)\\b/',
'/data-theme/'
],
{
splitList: true
}
],
// Only a mixin, a utility, has to win over what it stands beside
'declaration-no-important': true,
// Colors and easings are tokens
'color-no-hex': true,
'function-disallowed-list': [
'rgb',
'rgba',
'hsl',
'hsla',
'hwb',
'lab',
'lch',
'oklab',
'oklch',
'color',
'color-mix',
'cubic-bezier',
'steps'
]
},
overrides: [
{
files: variablesFiles,
rules: {
'color-no-hex': null,
'function-disallowed-list': null,
// A token's name starts with its type: `--colorText`, `--sizeSpace4`
'custom-property-pattern': [
strictCustomPropertyMatcher,
{
message: 'Expected token name to be in camelCase and start with its type (e.g. "--colorPrimary", "--sizeFont")'
}
]
}
},
{
files: mediaFiles,
rules: {
'media-feature-name-disallowed-list': null,
'at-rule-disallowed-list': null
}
},
{
files: mixinsFiles,
rules: {
'media-feature-name-disallowed-list': mediaFeatures,
'declaration-no-important': null
}
},
{
files: containersFiles,
rules: {
'declaration-property-value-allowed-list': null
}
},
{
files: globalFiles,
rules: {
'selector-disallowed-list': null
}
}
]
}
4 changes: 2 additions & 2 deletions packages/stylelint-config/src/subconfigs/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const pascalCaseMatcher = /^([A-Z][a-z\d]*)+$/

export const camelCaseMatcher = /^[a-z\d]+([A-Z][a-z\d]*)*$/

export const strictMediaQueryMatcher = /^media([A-Z][a-z\d]*)+$/
export const strictMediaQueryMatcher = /^screen([A-Z][a-z\d]*)+$/

export const strictCustomPropertyMatcher = /^(size|color|duration|shadow|opacity|zIndex|font|animation)([A-Z][a-z\d]*)+$/
export const strictCustomPropertyMatcher = /^(duration|opacity|color|size|shadow|font|fontWeight|easing|zIndex|transition)([A-Z][a-z\d]*)+$/
121 changes: 95 additions & 26 deletions packages/stylelint-config/src/subconfigs/strict.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,111 @@
* Strict config
*/

import {
strictCustomPropertyMatcher,
strictMediaQueryMatcher
} from './matchers.js'
const systemColors = '/^(AccentColor|AccentColorText|ActiveText|ButtonBorder|ButtonFace|ButtonText|Canvas|CanvasText|Field|FieldText|GrayText|Highlight|HighlightText|LinkText|Mark|MarkText|SelectedItem|SelectedItemText|VisitedText)$/'
const colorValues = [
'currentcolor',
'currentColor',
'transparent',
'inherit',
'none',
systemColors
]
// Values a length needs no token for
const lengthValues = [
'0',
'1',
'auto',
'none',
'inherit',
'normal',
'/^-?\\d+%$/'
]
// A property here takes a token, a calc of tokens or a value that needs none
const strictValue = [
[
'/color$/',
'fill',
'stroke',
'font-family',
'font-weight',
'z-index',
'/^padding/',
'/^margin/',
'/^(gap|row-gap|column-gap)$/',
'/^(inset|top|right|bottom|left)(-|$)/',
'/radius$/',
'font-size',
'line-height',
'outline-offset',
'text-underline-offset',
'text-decoration-thickness',
'/^scroll-(padding|margin)/'
],
{
disableFix: true,
ignoreValues: {
'': lengthValues,
'/color$/': colorValues,
fill: colorValues,
stroke: colorValues,
'font-family': ['inherit'],
'font-weight': ['inherit'],
// A local order inside a component: the layers across components are tokens
'z-index': ['auto', '/^-?[0-2]$/']
}
}
]

export default {
plugins: ['stylelint-declaration-strict-value', 'stylelint-plugin-a11y'],
rules: {
'scale-unlimited/declaration-strict-value': [
['font-family', '/color([^A-Z]|$)/'],
{
disableFix: true,
ignoreKeywords: [
'currentColor',
'transparent',
'inherit'
]
}
'scale-unlimited/declaration-strict-value': strictValue,
'declaration-property-value-disallowed-list': {
// A color reads a role, which is a `light-dark()` pair of the tokens
'/^(?!--)/': ['/light-dark\\(/'],
// No pulls: a box stands where its box stands
'/^margin/': [
'/\\*\\s*-1\\b/',
'/-1\\s*\\*/',
'/(^|[\\s(,])-(\\d|var)/'
]
},
'property-disallowed-list': [
// The shorthand alone: the logical autofix turns `overflow-y` into `overflow-block`, which
// Safari 18 drops
'overflow-x',
'overflow-y',
'overflow-block',
'overflow-inline',
// A mixin is a class in the markup
'composes'
],
'rule-selector-property-disallowed-list': {
// The root keeps its scrollbar's room and its scroll anchoring
'/^(html|:root)\\b|:global\\(html\\)/': [
'overflow-anchor',
'scrollbar-gutter',
'overflow-x',
'overflow-y',
'overflow-block'
],
// A block does not place itself: where it stands is its consumer's. Its margin is left out:
// a root resets the one its element has with `0`, which a list of properties cannot tell
'/^\\.root(?![\\w-])[^\\s>+~]*$/': [
'inset',
'/^inset-/',
'top',
'right',
'bottom',
'left',
'z-index'
]
},
'a11y/line-height-is-vertical-rhythmed': [
true,
{
severity: 'warning'
}
],
'custom-property-pattern': [
strictCustomPropertyMatcher,
{
message: 'Expected custom property name to be in camelCase (e.g. "--colorPrimary", "--sizeFont")'
}
],
'custom-media-pattern': [
strictMediaQueryMatcher,
{
message: 'Expected custom media query name to be in camelCase (e.g. "--mediaSmallScreen", "--mediaTabletAndUp")'
}
]
}
}