// CSS Baseline 2024 stepped-value functions not yet recognized by Stylelint. const CSS_BASELINE_2024_FUNCTIONS = [ 'round', 'rem', 'mod' ]; // Sass treats hyphens and underscores in identifiers as equivalent. const FONT_FAMILY_VARIABLE = /\$font[-_]family[-_](body|headings|mono)(?![\w-])/; const FONT_FAMILY_VARIABLE_REPLACEMENTS = { body: 'var(--wpds-typography-font-family-body)', headings: 'var(--wpds-typography-font-family-heading)', mono: 'var(--wpds-typography-font-family-mono)', }; const FONT_SIZE_VARIABLE = /\$(?:font[-_]size[-_](x[-_]small|small|medium|large|x[-_]large|2x[-_]large)|default[-_]font[-_]size)(?![\w-])/; const FONT_SIZE_VARIABLE_REPLACEMENTS = { 'x-small': 'var(--wpds-typography-font-size-xs)', small: 'var(--wpds-typography-font-size-sm)', medium: 'var(--wpds-typography-font-size-md)', large: 'var(--wpds-typography-font-size-lg)', 'x-large': 'var(--wpds-typography-font-size-xl)', '2x-large': 'var(--wpds-typography-font-size-2xl)', }; const FONT_LINE_HEIGHT_VARIABLE = /\$font[-_]line[-_]height[-_](x[-_]small|small|medium|large|x[-_]large|2x[-_]large)(?![\w-])/; const FONT_LINE_HEIGHT_VARIABLE_REPLACEMENTS = { 'x-small': 'var(--wpds-typography-line-height-xs)', small: 'var(--wpds-typography-line-height-sm)', medium: 'var(--wpds-typography-line-height-md)', large: 'var(--wpds-typography-line-height-lg)', 'x-large': 'var(--wpds-typography-line-height-xl)', '2x-large': 'var(--wpds-typography-line-height-2xl)', }; /** @type {import('stylelint').Config} */ module.exports = { extends: '@wordpress/stylelint-config/scss-stylistic', plugins: [ 'stylelint-plugin-logical-css', '@wordpress/theme-build-tools/stylelint-plugins/no-token-fallback-values', ], reportNeedlessDisables: true, rules: { 'at-rule-no-unknown': null, 'comment-empty-line-before': null, 'declaration-property-value-disallowed-list': [ { '/.*/': [ '/--wp-components-color-/', '/\\$font-weight-regular/', '/\\$font-weight-medium/', `/${ FONT_FAMILY_VARIABLE.source }/`, `/${ FONT_SIZE_VARIABLE.source }/`, `/${ FONT_LINE_HEIGHT_VARIABLE.source }/`, ], cursor: [ 'pointer' ], 'flex-direction': [ '/^(row|column)-reverse/' ], }, { message: ( property, value ) => { switch ( property ) { case 'flex-direction': return `Avoid "${ value }" value for the "${ property }" property. For accessibility reasons, visual, reading, and DOM order must match. Only use the reverse values when they do not affect reading order, meaning, and interaction.`; case 'cursor': return 'Use the `var( --wpds-cursor-control )` token for interactive non-link controls. If this is for a link, you can disable this rule.'; } // The remaining patterns apply to every property, so tell them apart by value. if ( value.includes( '$font-weight-regular' ) || value.includes( '$font-weight-medium' ) ) { const variable = value.includes( '$font-weight-regular' ) ? '$font-weight-regular' : '$font-weight-medium'; return `\`${ variable }\` has been removed. Use \`var(--wpds-typography-font-weight-default)\` or \`var(--wpds-typography-font-weight-emphasis)\` based on the intended emphasis.`; } const [ , fontFamily ] = value.match( FONT_FAMILY_VARIABLE ) ?? []; if ( fontFamily ) { return `Avoid using the \`$font-family-${ fontFamily }\` Sass variable. Use \`${ FONT_FAMILY_VARIABLE_REPLACEMENTS[ fontFamily ] }\` instead.`; } const fontSizeMatch = value.match( FONT_SIZE_VARIABLE ); if ( fontSizeMatch ) { const size = fontSizeMatch[ 1 ]?.replace( /_/g, '-' ); const variable = size ? `$font-size-${ size }` : '$default-font-size'; return `Avoid using the \`${ variable }\` Sass variable. Use \`${ FONT_SIZE_VARIABLE_REPLACEMENTS[ size ?? 'medium' ] }\` instead.`; } const [ , lineHeight ] = value.match( FONT_LINE_HEIGHT_VARIABLE ) ?? []; if ( lineHeight ) { const size = lineHeight.replace( /_/g, '-' ); return `Avoid using the \`$font-line-height-${ size }\` Sass variable. Use \`${ FONT_LINE_HEIGHT_VARIABLE_REPLACEMENTS[ size ] }\` instead.`; } return `Avoid using "${ value }" in "${ property }". --wp-components-color-* variables are being removed from the components package.`; }, }, ], 'font-weight-notation': 'numeric', '@stylistic/max-line-length': null, 'no-descending-specificity': null, 'property-disallowed-list': [ [ 'order' ], { message: 'Avoid the order property. For accessibility reasons, visual, reading, and DOM order must match. Only use the order property when it does not affect reading order, meaning, and interaction.', }, ], 'rule-empty-line-before': null, 'selector-class-pattern': [ '^[a-z][a-z0-9]*(?:(?:__|--|-)[a-z0-9]+)*$', { message: 'Selector should use lowercase class segments separated with hyphens, double hyphens, or double underscores (selector-class-pattern)', }, ], 'value-keyword-case': null, 'scss/operator-no-unspaced': null, 'scss/selector-no-redundant-nesting-selector': null, 'scss/load-partial-extension': null, 'scss/no-global-function-names': null, 'scss/comment-no-empty': null, 'scss/at-extend-no-missing-placeholder': null, 'scss/operator-no-newline-after': null, 'scss/at-if-closing-brace-newline-after': null, 'scss/at-else-empty-line-before': null, 'scss/at-if-closing-brace-space-after': null, 'no-invalid-position-at-import-rule': null, 'plugin-wpds/no-token-fallback-values': true, }, overrides: [ { files: [ '**/*.module.{css,scss}', // Can be removed when all `routes/` stylesheets are converted to CSS modules. 'routes/**/*.{css,scss}', ], rules: { 'function-no-unknown': [ true, { ignoreFunctions: CSS_BASELINE_2024_FUNCTIONS }, ], 'declaration-property-max-values': { // Prevents left/right values with shorthand property names (unclear for RTL) margin: 3, padding: 3, 'border-width': 3, 'border-color': 3, 'border-style': 3, 'border-radius': 3, inset: 3, }, 'logical-css/require-logical-keywords': [ true ], 'logical-css/require-logical-properties': [ true, { ignore: [ // Doesn't affect RTL styles 'border-bottom', 'border-top', 'width', 'min-width', 'max-width', 'height', 'min-height', 'max-height', 'margin-top', 'margin-bottom', 'overflow-x', 'overflow-y', 'padding-top', 'padding-bottom', 'scroll-margin-top', 'scroll-margin-bottom', 'top', 'bottom', ], }, ], 'property-no-unknown': [ true, { ignoreProperties: [ // https://github.com/css-modules/css-modules/blob/master/docs/composition.md 'composes', ], }, ], 'selector-pseudo-class-no-unknown': [ true, { ignorePseudoClasses: [ // CSS Modules global escape hatch. 'global', ], }, ], }, }, { // SCSS-only: use the Sass-aware `function-no-unknown` variant. files: [ '**/*.module.scss', 'routes/**/*.scss' ], rules: { 'function-no-unknown': null, 'scss/function-no-unknown': [ true, { ignoreFunctions: [ ...CSS_BASELINE_2024_FUNCTIONS, // Sass helpers from `@wordpress/base-styles`. 'z-index', ], }, ], }, }, { files: [ '**/*.css' ], rules: { // Re-add no-duplicate-selectors rule for CSS because it is disabled by stylelint-config-recommended-scss v17 'no-duplicate-selectors': true, }, }, { /* * The admin design tokens experiment ships inside a cascade layer, and * layer priority REVERSES for `!important`: a layered `!important` * beats a third-party plugin's *unlayered* `!important`, even when the * plugin's stylesheet loads later. Allowing one would silently break * overrides the ecosystem has relied on for years. Normal declarations * are safe, since unlayered plugin CSS still wins. * * DELETE WHEN: `lib/experimental/wpds-admin/` is removed. */ files: [ 'lib/experimental/wpds-admin/**/*.css' ], rules: { 'declaration-no-important': true, }, }, { /* * The single exception. `99-overrides.css` exists only to cancel * `!important` declarations in WordPress's admin CSS, which survive * being demoted into a lower layer because importance is compared * before layer order. Every rule in that file must cancel a specific * WordPress `!important` and record what would let it be deleted. Do * not widen this exception to any other file. * * DELETE WHEN: `lib/experimental/wpds-admin/` is removed. */ files: [ 'lib/experimental/wpds-admin/css/99-overrides.css' ], rules: { 'declaration-no-important': null, }, }, ], reportDescriptionlessDisables: true, };