@@ -31,34 +31,6 @@ function localizeNode(node, context) {
31
31
32
32
var newNodes ;
33
33
switch ( node . type ) {
34
- case "selectors" :
35
- var resultingGlobal ;
36
- context . hasPureGlobals = false ;
37
- newNodes = node . nodes . map ( function ( n ) {
38
- var nContext = {
39
- global : context . global ,
40
- lastWasSpacing : true ,
41
- hasLocals : false ,
42
- explicit : false
43
- } ;
44
- n = localizeNode ( n , nContext ) ;
45
- if ( typeof resultingGlobal === "undefined" ) {
46
- resultingGlobal = nContext . global ;
47
- } else if ( resultingGlobal !== nContext . global ) {
48
- throw Error (
49
- `Inconsistent rule global/local result in rule "${ Tokenizer . stringify ( node ) } " (multiple selectors must result in the same mode for the rule)`
50
- ) ;
51
- }
52
- if ( ! nContext . hasLocals ) {
53
- context . hasPureGlobals = true ;
54
- }
55
- return n ;
56
- } ) ;
57
- context . global = resultingGlobal ;
58
- node = Object . create ( node ) ;
59
- node . nodes = normalizeNodeArray ( newNodes ) ;
60
- break ;
61
-
62
34
case "selector" :
63
35
newNodes = node . nodes . map ( function ( n ) {
64
36
return localizeNode ( n , context ) ;
@@ -159,6 +131,35 @@ function localizeNode(node, context) {
159
131
return node ;
160
132
}
161
133
134
+ const localizeSelectors = ( selectors , context ) => {
135
+ const node = Tokenizer . parse ( selectors ) ;
136
+ var resultingGlobal ;
137
+ context . hasPureGlobals = false ;
138
+ const newNodes = node . nodes . map ( function ( n ) {
139
+ var nContext = {
140
+ global : context . global ,
141
+ lastWasSpacing : true ,
142
+ hasLocals : false ,
143
+ explicit : false
144
+ } ;
145
+ n = localizeNode ( n , nContext ) ;
146
+ if ( typeof resultingGlobal === "undefined" ) {
147
+ resultingGlobal = nContext . global ;
148
+ } else if ( resultingGlobal !== nContext . global ) {
149
+ throw Error (
150
+ `Inconsistent rule global/local result in rule "${ Tokenizer . stringify ( node ) } " (multiple selectors must result in the same mode for the rule)`
151
+ ) ;
152
+ }
153
+ if ( ! nContext . hasLocals ) {
154
+ context . hasPureGlobals = true ;
155
+ }
156
+ return n ;
157
+ } ) ;
158
+ context . global = resultingGlobal ;
159
+ node . nodes = normalizeNodeArray ( newNodes ) ;
160
+ return Tokenizer . stringify ( node ) ;
161
+ } ;
162
+
162
163
module . exports = postcss . plugin ( plugin , ( options = { } ) => css => {
163
164
if (
164
165
options . mode &&
@@ -177,23 +178,21 @@ module.exports = postcss.plugin(plugin, (options = {}) => css => {
177
178
// ignore keyframe rules
178
179
return ;
179
180
}
180
- var selector = Tokenizer . parse ( rule . selector ) ;
181
181
var context = {
182
182
options : options ,
183
183
global : globalMode ,
184
184
hasPureGlobals : false
185
185
} ;
186
- var newSelector ;
187
186
try {
188
- newSelector = localizeNode ( selector , context ) ;
187
+ const newSelector = localizeSelectors ( rule . selector , context ) ;
188
+ if ( pureMode && context . hasPureGlobals ) {
189
+ throw Error (
190
+ `Selector "${ rule . selector } " is not pure (pure selectors must contain at least one local class or id)`
191
+ ) ;
192
+ }
193
+ rule . selector = newSelector ;
189
194
} catch ( e ) {
190
195
throw rule . error ( e . message ) ;
191
196
}
192
- if ( pureMode && context . hasPureGlobals ) {
193
- throw rule . error (
194
- `Selector "${ Tokenizer . stringify ( selector ) } " is not pure (pure selectors must contain at least one local class or id)`
195
- ) ;
196
- }
197
- rule . selector = Tokenizer . stringify ( newSelector ) ;
198
197
} ) ;
199
198
} ) ;
0 commit comments