11
11
*/
12
12
13
13
import fs from "node:fs" ;
14
- import { relative , sep } from "node:path" ;
14
+ import { basename , relative , sep } from "node:path" ;
15
15
16
16
import postcss from "postcss" ;
17
17
import valuesParser from "postcss-values-parser" ;
18
18
import stylelint from "stylelint" ;
19
+ import { isString } from "stylelint/lib/utils/validateTypes.mjs" ;
19
20
20
21
const {
21
22
createPlugin,
@@ -34,7 +35,7 @@ const messages = ruleMessages(ruleName, {
34
35
} ) ;
35
36
36
37
/** @type {import('stylelint').Plugin } */
37
- const ruleFunction = ( enabled ) => {
38
+ const ruleFunction = ( enabled , options = { } ) => {
38
39
return ( root , result ) => {
39
40
const validOptions = validateOptions (
40
41
result ,
@@ -43,19 +44,29 @@ const ruleFunction = (enabled) => {
43
44
actual : enabled ,
44
45
possible : [ true ] ,
45
46
} ,
47
+ {
48
+ actual : options ,
49
+ possible : {
50
+ baseFilename : isString ,
51
+ } ,
52
+ optional : true ,
53
+ } ,
46
54
) ;
47
55
48
56
if ( ! validOptions ) return ;
49
57
58
+
59
+ const { baseFilename = "spectrum-two" } = options ;
60
+
50
61
const sourceFile = root . source . input . file ;
51
62
const parts = sourceFile ? sourceFile . split ( sep ) : [ ] ;
52
63
const isTheme = parts [ parts . length - 2 ] === "themes" ;
53
64
const filename = parts [ parts . length - 1 ] ;
54
65
55
- if ( ! isTheme || filename === "spectrum .css") return ;
66
+ if ( ! isTheme || basename ( filename , " .css") === baseFilename ) return ;
56
67
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 ) ;
59
70
const rootPath = parts . slice ( 0 , - 2 ) . join ( sep ) ;
60
71
61
72
// If the base file doesn't exist, throw an error
@@ -81,8 +92,10 @@ const ruleFunction = (enabled) => {
81
92
82
93
/* Iterate over selectors in the base root */
83
94
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
+ } ) ;
86
99
87
100
rule . walkDecls ( ( decl ) => {
88
101
// If this is a custom property, add it to the properties set
@@ -102,18 +115,20 @@ const ruleFunction = (enabled) => {
102
115
103
116
/* Iterate over selectors in the source root and validate that they align with the base */
104
117
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
+ } ) ;
117
132
118
133
rule . walkDecls ( ( decl ) => {
119
134
const isProperty = decl . prop . startsWith ( "--" ) ;
0 commit comments