22import postcss from "postcss" ;
33import Tokenizer from "css-selector-tokenizer" ;
44
5+ const plugin = "postcss-modules-local-by-default" ;
6+
57function normalizeNodeArray ( nodes ) {
68 var array = [ ] ;
79 nodes . forEach ( function ( x ) {
@@ -21,10 +23,10 @@ function normalizeNodeArray(nodes) {
2123
2224function localizeNode ( node , context ) {
2325 if ( context . ignoreNextSpacing && node . type !== "spacing" ) {
24- throw new Error ( " Missing whitespace after :" + context . ignoreNextSpacing ) ;
26+ throw Error ( ` Missing whitespace after :${ context . ignoreNextSpacing } ` ) ;
2527 }
2628 if ( context . enforceNoSpacing && node . type === "spacing" ) {
27- throw new Error ( " Missing whitespace before :" + context . enforceNoSpacing ) ;
29+ throw Error ( ` Missing whitespace before :${ context . enforceNoSpacing } ` ) ;
2830 }
2931
3032 var newNodes ;
@@ -43,10 +45,8 @@ function localizeNode(node, context) {
4345 if ( typeof resultingGlobal === "undefined" ) {
4446 resultingGlobal = nContext . global ;
4547 } else if ( resultingGlobal !== nContext . global ) {
46- throw new Error (
47- 'Inconsistent rule global/local result in rule "' +
48- Tokenizer . stringify ( node ) +
49- '" (multiple selectors must result in the same mode for the rule)'
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)`
5050 ) ;
5151 }
5252 if ( ! nContext . hasLocals ) {
@@ -80,7 +80,7 @@ function localizeNode(node, context) {
8080 case "pseudo-class" :
8181 if ( node . name === "local" || node . name === "global" ) {
8282 if ( context . inside ) {
83- throw new Error (
83+ throw Error (
8484 "A :" +
8585 node . name +
8686 " is not allowed inside of a :" +
@@ -100,7 +100,7 @@ function localizeNode(node, context) {
100100 var subContext ;
101101 if ( node . name === "local" || node . name === "global" ) {
102102 if ( context . inside ) {
103- throw new Error (
103+ throw Error (
104104 "A :" +
105105 node . name +
106106 "(...) is not allowed inside of a :" +
@@ -159,50 +159,41 @@ function localizeNode(node, context) {
159159 return node ;
160160}
161161
162- module . exports = postcss . plugin (
163- "postcss-modules-local-by-default" ,
164- ( options = { } ) => css => {
165- if (
166- options . mode &&
167- options . mode !== "global" &&
168- options . mode !== "local" &&
169- options . mode !== "pure"
170- ) {
171- throw Error (
172- 'options.mode must be either "global", "local" or "pure" (default "local")'
162+ module . exports = postcss . plugin ( plugin , ( options = { } ) => css => {
163+ if (
164+ options . mode &&
165+ options . mode !== "global" &&
166+ options . mode !== "local" &&
167+ options . mode !== "pure"
168+ ) {
169+ throw Error (
170+ 'options.mode must be either "global", "local" or "pure" (default "local")'
171+ ) ;
172+ }
173+ var pureMode = options . mode === "pure" ;
174+ var globalMode = options . mode === "global" ;
175+ css . walkRules ( function ( rule ) {
176+ if ( rule . parent . type === "atrule" && / k e y f r a m e s $ / . test ( rule . parent . name ) ) {
177+ // ignore keyframe rules
178+ return ;
179+ }
180+ var selector = Tokenizer . parse ( rule . selector ) ;
181+ var context = {
182+ options : options ,
183+ global : globalMode ,
184+ hasPureGlobals : false
185+ } ;
186+ var newSelector ;
187+ try {
188+ newSelector = localizeNode ( selector , context ) ;
189+ } catch ( e ) {
190+ throw rule . error ( e . message ) ;
191+ }
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)`
173195 ) ;
174196 }
175- var pureMode = options . mode === "pure" ;
176- var globalMode = options . mode === "global" ;
177- css . walkRules ( function ( rule ) {
178- if (
179- rule . parent . type === "atrule" &&
180- / k e y f r a m e s $ / . test ( rule . parent . name )
181- ) {
182- // ignore keyframe rules
183- return ;
184- }
185- var selector = Tokenizer . parse ( rule . selector ) ;
186- var context = {
187- options : options ,
188- global : globalMode ,
189- hasPureGlobals : false
190- } ;
191- var newSelector ;
192- try {
193- newSelector = localizeNode ( selector , context ) ;
194- } catch ( e ) {
195- throw rule . error ( e . message ) ;
196- }
197- if ( pureMode && context . hasPureGlobals ) {
198- throw rule . error (
199- 'Selector "' +
200- Tokenizer . stringify ( selector ) +
201- '" is not pure ' +
202- "(pure selectors must contain at least one local class or id)"
203- ) ;
204- }
205- rule . selector = Tokenizer . stringify ( newSelector ) ;
206- } ) ;
207- }
208- ) ;
197+ rule . selector = Tokenizer . stringify ( newSelector ) ;
198+ } ) ;
199+ } ) ;
0 commit comments