Skip to content
Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
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 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.
*/
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' ]
);
}
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 );
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 );
4 changes: 4 additions & 0 deletions tools/js-tools/stylelint.config.base.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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': [
Expand Down
Loading