2
2
import postcss from "postcss" ;
3
3
import Tokenizer from "css-selector-tokenizer" ;
4
4
5
+ const plugin = "postcss-modules-local-by-default" ;
6
+
5
7
function normalizeNodeArray ( nodes ) {
6
8
var array = [ ] ;
7
9
nodes . forEach ( function ( x ) {
@@ -21,10 +23,10 @@ function normalizeNodeArray(nodes) {
21
23
22
24
function localizeNode ( node , context ) {
23
25
if ( context . ignoreNextSpacing && node . type !== "spacing" ) {
24
- throw new Error ( " Missing whitespace after :" + context . ignoreNextSpacing ) ;
26
+ throw Error ( ` Missing whitespace after :${ context . ignoreNextSpacing } ` ) ;
25
27
}
26
28
if ( context . enforceNoSpacing && node . type === "spacing" ) {
27
- throw new Error ( " Missing whitespace before :" + context . enforceNoSpacing ) ;
29
+ throw Error ( ` Missing whitespace before :${ context . enforceNoSpacing } ` ) ;
28
30
}
29
31
30
32
var newNodes ;
@@ -43,10 +45,8 @@ function localizeNode(node, context) {
43
45
if ( typeof resultingGlobal === "undefined" ) {
44
46
resultingGlobal = nContext . global ;
45
47
} 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)`
50
50
) ;
51
51
}
52
52
if ( ! nContext . hasLocals ) {
@@ -80,7 +80,7 @@ function localizeNode(node, context) {
80
80
case "pseudo-class" :
81
81
if ( node . name === "local" || node . name === "global" ) {
82
82
if ( context . inside ) {
83
- throw new Error (
83
+ throw Error (
84
84
"A :" +
85
85
node . name +
86
86
" is not allowed inside of a :" +
@@ -100,7 +100,7 @@ function localizeNode(node, context) {
100
100
var subContext ;
101
101
if ( node . name === "local" || node . name === "global" ) {
102
102
if ( context . inside ) {
103
- throw new Error (
103
+ throw Error (
104
104
"A :" +
105
105
node . name +
106
106
"(...) is not allowed inside of a :" +
@@ -159,50 +159,41 @@ function localizeNode(node, context) {
159
159
return node ;
160
160
}
161
161
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)`
173
195
) ;
174
196
}
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