@@ -116,13 +116,12 @@ function localizeNode(node, context) {
116116 return node ;
117117}
118118
119- const localizeSelectors = ( selectors , context ) => {
119+ const localizeSelectors = ( selectors , mode ) => {
120120 const node = Tokenizer . parse ( selectors ) ;
121121 var resultingGlobal ;
122- context . hasPureGlobals = false ;
123122 node . nodes = node . nodes . map ( n => {
124123 var nContext = {
125- global : context . global ,
124+ global : mode === " global" ,
126125 lastWasSpacing : true ,
127126 hasLocals : false ,
128127 explicit : false
@@ -136,46 +135,26 @@ const localizeSelectors = (selectors, context) => {
136135 ` (multiple selectors must result in the same mode for the rule)`
137136 ) ;
138137 }
139- if ( ! nContext . hasLocals ) {
140- context . hasPureGlobals = true ;
141- }
142138 return n ;
143139 } ) ;
144- context . global = resultingGlobal ;
145140 return Tokenizer . stringify ( node ) ;
146141} ;
147142
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 ) => {
161144 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 ) ;
165147 }
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 => {
171153 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+ ) ;
179158 } catch ( e ) {
180159 throw rule . error ( e . message ) ;
181160 }
0 commit comments