@@ -45,7 +45,7 @@ function trimSelectors(selector) {
45
45
}
46
46
}
47
47
48
- function localizeNodez ( rule , mode , options ) {
48
+ function localizeNode ( rule , mode , options ) {
49
49
const isScopePseudo = node =>
50
50
node . value === ':local' || node . value === ':global' ;
51
51
@@ -87,27 +87,26 @@ function localizeNodez(rule, mode, options) {
87
87
context . global = resultingGlobal ;
88
88
89
89
node . nodes = normalizeNodeArray ( newNodes ) ;
90
- // console.log(node.nodes);
91
90
break ;
92
91
}
93
92
case 'selector' : {
94
93
newNodes = node . map ( childNode => transform ( childNode , context ) ) ;
95
94
96
95
node = node . clone ( ) ;
97
96
node . nodes = normalizeNodeArray ( newNodes ) ;
98
- console . log ( 'SECLE' , node . toString ( ) ) ;
99
97
break ;
100
98
}
101
99
case 'combinator' : {
102
- if ( ! isSpacing ( node ) ) break ;
103
-
104
- if ( context . ignoreNextSpacing ) {
105
- context . ignoreNextSpacing = false ;
106
- context . lastWasSpacing = false ;
107
- context . enforceNoSpacing = false ;
108
- return null ;
100
+ if ( isSpacing ( node ) ) {
101
+ if ( context . ignoreNextSpacing ) {
102
+ context . ignoreNextSpacing = false ;
103
+ context . lastWasSpacing = false ;
104
+ context . enforceNoSpacing = false ;
105
+ return null ;
106
+ }
107
+ context . lastWasSpacing = true ;
108
+ return node ;
109
109
}
110
- context . lastWasSpacing = true ;
111
110
break ;
112
111
}
113
112
case 'pseudo' : {
@@ -132,7 +131,6 @@ function localizeNodez(rule, mode, options) {
132
131
hasLocals : false ,
133
132
explicit : true ,
134
133
} ;
135
- // console.log('PSUDI', node.nodes);
136
134
137
135
newNodes = node
138
136
. map ( childNode => transform ( childNode , childContext ) )
@@ -147,12 +145,9 @@ function localizeNodez(rule, mode, options) {
147
145
first . spaces = { before, after : first . spaces . after } ;
148
146
last . spaces = { before : last . spaces . before , after } ;
149
147
}
150
- console . log ( 'PSUDI' , node ) ;
148
+
151
149
node = newNodes ;
152
150
153
- // // don't leak spacing
154
- // node[0].spaces.before = '';
155
- // node[node.length - 1].spaces.after = '';
156
151
break ;
157
152
} else {
158
153
childContext = {
@@ -185,36 +180,41 @@ function localizeNodez(rule, mode, options) {
185
180
) ;
186
181
}
187
182
188
- const next = node . next ( ) ;
189
- console . log ( 'SPACESS' , next , node . spaces ) ;
190
- if ( next ) next . spaces = node . spaces ;
183
+ const next = node . parent ;
184
+ const addBackSpacing = ! ! node . spaces . before ;
191
185
192
186
context . ignoreNextSpacing = context . lastWasSpacing
193
187
? node . value
194
188
: false ;
189
+
195
190
context . enforceNoSpacing = context . lastWasSpacing
196
191
? false
197
192
: node . value ;
193
+
198
194
context . global = node . value === ':global' ;
199
195
context . explicit = true ;
200
- return null ;
196
+
197
+ // because this node has spacing that is lost when we remove it
198
+ // we make up for it by adding an extra combinator in since adding
199
+ // spacing on the parent selector doesn't work
200
+ return addBackSpacing
201
+ ? selectorParser . combinator ( { value : ' ' } )
202
+ : null ;
201
203
}
202
204
break ;
203
205
}
204
206
case 'id' :
205
207
case 'class' : {
206
208
if ( ! context . global ) {
207
- console . log ( 'REPLCE' , node . spaces ) ;
208
209
const innerNode = node . clone ( ) ;
209
- console . log ( innerNode ) ;
210
210
innerNode . spaces = { before : '' , after : '' } ;
211
211
212
212
node = selectorParser . pseudo ( {
213
213
value : ':local' ,
214
214
nodes : [ innerNode ] ,
215
215
spaces : node . spaces ,
216
216
} ) ;
217
- // console.log('HERE');
217
+
218
218
context . hasLocals = true ;
219
219
}
220
220
@@ -233,11 +233,11 @@ function localizeNodez(rule, mode, options) {
233
233
hasPureGlobals : false ,
234
234
} ;
235
235
236
- const updatedRule = selectorParser ( root => {
236
+ const selector = selectorParser ( root => {
237
237
transform ( root , rootContext ) ;
238
- } ) . processSync ( rule , { updateSelector : true , lossless : true } ) ;
238
+ } ) . processSync ( rule , { updateSelector : false , lossless : true } ) ;
239
239
240
- // console.log('HERE', rule. selector) ;
240
+ rootContext . selector = selector ;
241
241
return rootContext ;
242
242
}
243
243
@@ -479,7 +479,9 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
479
479
return ;
480
480
}
481
481
482
- const context = localizeNodez ( rule , options . mode , options ) ;
482
+ const context = localizeNode ( rule , options . mode ) ;
483
+
484
+ context . options = options ;
483
485
484
486
if ( pureMode && context . hasPureGlobals ) {
485
487
throw rule . error (
@@ -489,12 +491,10 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
489
491
'(pure selectors must contain at least one local class or id)'
490
492
) ;
491
493
}
494
+ rule . selector = context . selector ;
492
495
// Less-syntax mixins parse as rules with no nodes
493
496
if ( rule . nodes ) {
494
- rule . nodes . forEach ( function ( decl ) {
495
- console . log ( context ) ;
496
- localizeDecl ( decl , context ) ;
497
- } ) ;
497
+ rule . nodes . forEach ( decl => localizeDecl ( decl , context ) ) ;
498
498
}
499
499
} ) ;
500
500
} ;
0 commit comments