1111 */
1212
1313import fs from "node:fs" ;
14- import { relative , sep } from "node:path" ;
14+ import { basename , relative , sep } from "node:path" ;
1515
1616import postcss from "postcss" ;
1717import valuesParser from "postcss-values-parser" ;
1818import stylelint from "stylelint" ;
19+ import { isString } from "stylelint/lib/utils/validateTypes.mjs" ;
1920
2021const {
2122 createPlugin,
@@ -34,7 +35,7 @@ const messages = ruleMessages(ruleName, {
3435} ) ;
3536
3637/** @type {import('stylelint').Plugin } */
37- const ruleFunction = ( enabled ) => {
38+ const ruleFunction = ( enabled , options = { } ) => {
3839 return ( root , result ) => {
3940 const validOptions = validateOptions (
4041 result ,
@@ -43,19 +44,29 @@ const ruleFunction = (enabled) => {
4344 actual : enabled ,
4445 possible : [ true ] ,
4546 } ,
47+ {
48+ actual : options ,
49+ possible : {
50+ baseFilename : isString ,
51+ } ,
52+ optional : true ,
53+ } ,
4654 ) ;
4755
4856 if ( ! validOptions ) return ;
4957
58+
59+ const { baseFilename = "spectrum-two" } = options ;
60+
5061 const sourceFile = root . source . input . file ;
5162 const parts = sourceFile ? sourceFile . split ( sep ) : [ ] ;
5263 const isTheme = parts [ parts . length - 2 ] === "themes" ;
5364 const filename = parts [ parts . length - 1 ] ;
5465
55- if ( ! isTheme || filename === "spectrum .css") return ;
66+ if ( ! isTheme || basename ( filename , " .css") === baseFilename ) return ;
5667
57- // All the parts of the source file but replace the filename with spectrum-two.css
58- const baseFile = [ ...parts . slice ( 0 , - 1 ) , "spectrum .css" ] . join ( sep ) ;
68+ // All the parts of the source file but replace the filename with the baseFilename
69+ const baseFile = [ ...parts . slice ( 0 , - 1 ) , ` ${ baseFilename } .css` ] . join ( sep ) ;
5970 const rootPath = parts . slice ( 0 , - 2 ) . join ( sep ) ;
6071
6172 // If the base file doesn't exist, throw an error
@@ -81,8 +92,10 @@ const ruleFunction = (enabled) => {
8192
8293 /* Iterate over selectors in the base root */
8394 baseRoot . walkRules ( ( rule ) => {
84- // Add this selector to the selectors set
85- baseSelectors . add ( rule . selector ) ;
95+ rule . selectors . forEach ( ( selector ) => {
96+ // Add this selector to the selectors set
97+ baseSelectors . add ( selector ) ;
98+ } ) ;
8699
87100 rule . walkDecls ( ( decl ) => {
88101 // If this is a custom property, add it to the properties set
@@ -102,18 +115,20 @@ const ruleFunction = (enabled) => {
102115
103116 /* Iterate over selectors in the source root and validate that they align with the base */
104117 root . walkRules ( ( rule ) => {
105- // Check if this selector exists in the base
106- if ( ! baseSelectors . has ( rule . selector ) ) {
107- // Report any selectors that don't exist in the base
108- report ( {
109- message : messages . expected ,
110- messageArgs : [ rule . selector , baseFile , rootPath ] ,
111- node : rule ,
112- result,
113- ruleName,
114- } ) ;
115- return ;
116- }
118+ rule . selectors . forEach ( ( selector ) => {
119+ // Check if this selector exists in the base
120+ if ( ! baseSelectors . has ( selector ) ) {
121+ // Report any selectors that don't exist in the base
122+ report ( {
123+ message : messages . expected ,
124+ messageArgs : [ selector , baseFile , rootPath ] ,
125+ node : rule ,
126+ result,
127+ ruleName,
128+ } ) ;
129+ return ;
130+ }
131+ } ) ;
117132
118133 rule . walkDecls ( ( decl ) => {
119134 const isProperty = decl . prop . startsWith ( "--" ) ;
0 commit comments