From d259786780184102960f2193552f9963fda70aa4 Mon Sep 17 00:00:00 2001 From: Xavier Lozano Carreras Date: Fri, 30 Oct 2026 11:32:28 +0100 Subject: [PATCH 1/2] Stylelint: Flag the gutenberg-base-styles wrapper where @wordpress/base-styles is declared Add a local jetpack/no-redundant-base-styles-wrapper rule that reports @use, @forward and @import of @automattic/jetpack-base-styles/gutenberg-base-styles in files whose nearest package.json declares @wordpress/base-styles. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01K4t4VY7ojGc1sr9Cb1z84Z --- .../no-redundant-base-styles-wrapper.mjs | 79 +++++++++++++++++++ tools/js-tools/stylelint.config.base.mjs | 4 + 2 files changed, 83 insertions(+) create mode 100644 tools/js-tools/stylelint-plugins/no-redundant-base-styles-wrapper.mjs diff --git a/tools/js-tools/stylelint-plugins/no-redundant-base-styles-wrapper.mjs b/tools/js-tools/stylelint-plugins/no-redundant-base-styles-wrapper.mjs new file mode 100644 index 000000000000..66f8faec533f --- /dev/null +++ b/tools/js-tools/stylelint-plugins/no-redundant-base-styles-wrapper.mjs @@ -0,0 +1,79 @@ +import fs from 'node:fs'; +import path from 'node:path'; +import stylelint from 'stylelint'; + +const { + createPlugin, + utils: { report, ruleMessages, validateOptions }, +} = stylelint; + +const ruleName = 'jetpack/no-redundant-base-styles-wrapper'; + +const wrapperRegex = /^["']~?@automattic\/jetpack-base-styles\/gutenberg-base-styles["']/; + +const messages = ruleMessages( ruleName, { + rejected: () => + 'This project depends on @wordpress/base-styles, so load the modules it needs directly (e.g. `@use "@wordpress/base-styles/mixins";`) instead of through @automattic/jetpack-base-styles/gutenberg-base-styles.', +} ); + +const declaresBaseStylesCache = new Map(); + +/** + * Whether the nearest package.json above a directory declares `@wordpress/base-styles`. + * + * @param {string} dir - Directory to start from. + * @return {boolean} Whether the dependency is declared. + */ +function declaresBaseStyles( dir ) { + if ( declaresBaseStylesCache.has( dir ) ) { + return declaresBaseStylesCache.get( dir ); + } + let declares = false; + const pkgFile = path.join( dir, 'package.json' ); + if ( fs.existsSync( pkgFile ) ) { + const pkg = JSON.parse( fs.readFileSync( pkgFile, 'utf8' ) ); + declares = Boolean( + pkg.dependencies?.[ '@wordpress/base-styles' ] || + pkg.devDependencies?.[ '@wordpress/base-styles' ] + ); + } else if ( path.dirname( dir ) !== dir ) { + declares = declaresBaseStyles( path.dirname( dir ) ); + } + declaresBaseStylesCache.set( dir, declares ); + return declares; +} + +/** + * Report wrapper loads in files whose project declares `@wordpress/base-styles`. + * + * @type {import('stylelint').Rule} + */ +const ruleFunction = primary => { + return ( root, result ) => { + const validOptions = validateOptions( result, ruleName, { + actual: primary, + possible: [ true ], + } ); + const file = root.source?.input?.file; + if ( ! validOptions || ! file || ! declaresBaseStyles( path.dirname( file ) ) ) { + return; + } + + root.walkAtRules( /^(use|forward|import)$/, atRule => { + if ( wrapperRegex.test( atRule.params ) ) { + report( { + message: messages.rejected(), + node: atRule, + result, + ruleName, + } ); + } + } ); + }; +}; + +ruleFunction.ruleName = ruleName; +ruleFunction.messages = messages; + +/** @type {import('stylelint').Plugin} */ +export default createPlugin( ruleName, ruleFunction ); diff --git a/tools/js-tools/stylelint.config.base.mjs b/tools/js-tools/stylelint.config.base.mjs index b62a7fec043f..777d77effaf9 100644 --- a/tools/js-tools/stylelint.config.base.mjs +++ b/tools/js-tools/stylelint.config.base.mjs @@ -10,11 +10,15 @@ const baseConfig = { '@wordpress/theme/stylelint-plugins/no-unknown-ds-tokens', '@wordpress/theme/stylelint-plugins/no-setting-wpds-custom-properties', '@wordpress/theme/stylelint-plugins/no-token-fallback-values', + fileURLToPath( + import.meta.resolve( './stylelint-plugins/no-redundant-base-styles-wrapper.mjs' ) + ), ], rules: { 'plugin-wpds/no-unknown-ds-tokens': true, 'plugin-wpds/no-setting-wpds-custom-properties': true, 'plugin-wpds/no-token-fallback-values': true, + 'jetpack/no-redundant-base-styles-wrapper': true, // In addition to what `@wordpress/stylelint-config/scss-stylistic` does by default, also ignore comments containing /stylelint-disable/. '@stylistic/max-line-length': [ From 2d7096190ef682f69809e65e7cfde5829bfceea8 Mon Sep 17 00:00:00 2001 From: Xavier Lozano Carreras Date: Fri, 30 Oct 2026 11:40:14 +0100 Subject: [PATCH 2/2] Stylelint: Check package.json files up to the project root Nested package.json files (wp-build routes, packages/init) list no dependencies, so stopping at the nearest one skipped every stylesheet under them. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01K4t4VY7ojGc1sr9Cb1z84Z --- .../no-redundant-base-styles-wrapper.mjs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tools/js-tools/stylelint-plugins/no-redundant-base-styles-wrapper.mjs b/tools/js-tools/stylelint-plugins/no-redundant-base-styles-wrapper.mjs index 66f8faec533f..6168ecc8440e 100644 --- a/tools/js-tools/stylelint-plugins/no-redundant-base-styles-wrapper.mjs +++ b/tools/js-tools/stylelint-plugins/no-redundant-base-styles-wrapper.mjs @@ -19,7 +19,10 @@ const messages = ruleMessages( ruleName, { const declaresBaseStylesCache = new Map(); /** - * Whether the nearest package.json above a directory declares `@wordpress/base-styles`. + * Whether any package.json from a directory up to its project root declares `@wordpress/base-styles`. + * + * Nested package.json files (wp-build routes, `packages/init`) list no dependencies, so the + * walk continues to the project root, which is the directory holding `composer.json`. * * @param {string} dir - Directory to start from. * @return {boolean} Whether the dependency is declared. @@ -36,7 +39,9 @@ function declaresBaseStyles( dir ) { pkg.dependencies?.[ '@wordpress/base-styles' ] || pkg.devDependencies?.[ '@wordpress/base-styles' ] ); - } else if ( path.dirname( dir ) !== dir ) { + } + const isProjectRoot = fs.existsSync( path.join( dir, 'composer.json' ) ); + if ( ! declares && ! isProjectRoot && path.dirname( dir ) !== dir ) { declares = declaresBaseStyles( path.dirname( dir ) ); } declaresBaseStylesCache.set( dir, declares );