@@ -116,13 +116,12 @@ function localizeNode(node, context) {
116
116
return node ;
117
117
}
118
118
119
- const localizeSelectors = ( selectors , context ) => {
119
+ const localizeSelectors = ( selectors , mode ) => {
120
120
const node = Tokenizer . parse ( selectors ) ;
121
121
var resultingGlobal ;
122
- context . hasPureGlobals = false ;
123
122
node . nodes = node . nodes . map ( n => {
124
123
var nContext = {
125
- global : context . global ,
124
+ global : mode === " global" ,
126
125
lastWasSpacing : true ,
127
126
hasLocals : false ,
128
127
explicit : false
@@ -136,46 +135,26 @@ const localizeSelectors = (selectors, context) => {
136
135
` (multiple selectors must result in the same mode for the rule)`
137
136
) ;
138
137
}
139
- if ( ! nContext . hasLocals ) {
140
- context . hasPureGlobals = true ;
141
- }
142
138
return n ;
143
139
} ) ;
144
- context . global = resultingGlobal ;
145
140
return Tokenizer . stringify ( node ) ;
146
141
} ;
147
142
148
- module . exports = postcss . plugin ( plugin , ( options = { } ) => css => {
149
- if (
150
- options . mode &&
151
- options . mode !== "global" &&
152
- options . mode !== "local" &&
153
- options . mode !== "pure"
154
- ) {
155
- throw Error (
156
- 'options.mode must be either "global", "local" or "pure" (default "local")'
157
- ) ;
158
- }
159
- var pureMode = options . mode === "pure" ;
160
- var globalMode = options . mode === "global" ;
143
+ const walkRules = ( css , callback ) => {
161
144
css . walkRules ( rule => {
162
- if ( rule . parent . type === "atrule" && / k e y f r a m e s $ / . test ( rule . parent . name ) ) {
163
- // ignore keyframe rules
164
- return ;
145
+ if ( rule . parent . type !== "atrule" || ! / k e y f r a m e s $ / . test ( rule . parent . name ) ) {
146
+ callback ( rule ) ;
165
147
}
166
- var context = {
167
- options : options ,
168
- global : globalMode ,
169
- hasPureGlobals : false
170
- } ;
148
+ } ) ;
149
+ } ;
150
+
151
+ module . exports = postcss . plugin ( plugin , ( options = { } ) => css => {
152
+ walkRules ( css , rule => {
171
153
try {
172
- const newSelector = localizeSelectors ( rule . selector , context ) ;
173
- if ( pureMode && context . hasPureGlobals ) {
174
- throw Error (
175
- `Selector "${ rule . selector } " is not pure (pure selectors must contain at least one local class or id)`
176
- ) ;
177
- }
178
- rule . selector = newSelector ;
154
+ rule . selector = localizeSelectors (
155
+ rule . selector ,
156
+ options . mode === "global" ? "global" : "local"
157
+ ) ;
179
158
} catch ( e ) {
180
159
throw rule . error ( e . message ) ;
181
160
}
0 commit comments