@@ -4,7 +4,7 @@ const postcss = require('postcss');
4
4
const selectorParser = require ( 'postcss-selector-parser' ) ;
5
5
const valueParser = require ( 'postcss-value-parser' ) ;
6
6
7
- const IS_GLOBAL = Symbol ( 'is global' ) ;
7
+ const isSpacing = node => node . type === 'combinator' && node . value === ' ' ;
8
8
9
9
function normalizeNodeArray ( nodes ) {
10
10
const array = [ ] ;
@@ -25,27 +25,7 @@ function normalizeNodeArray(nodes) {
25
25
return array ;
26
26
}
27
27
28
- function checkForInconsistentRule ( node , current , context ) {
29
- if ( context . global !== context . lastIsGlobal )
30
- throw new Error (
31
- 'Inconsistent rule global/local result in rule "' +
32
- String ( node ) +
33
- '" (multiple selectors must result in the same mode for the rule)'
34
- ) ;
35
- }
36
- const isSpacing = node => node . type === 'combinator' && node . value === ' ' ;
37
-
38
- function trimSelectors ( selector ) {
39
- let last ;
40
- while (
41
- ( last = selector . last ) &&
42
- ( last . type === 'combinator' && last . value === ' ' )
43
- ) {
44
- last . remove ( ) ;
45
- }
46
- }
47
-
48
- function localizeNodez ( rule , mode , options ) {
28
+ function localizeNode ( rule , mode , options ) {
49
29
const isScopePseudo = node =>
50
30
node . value === ':local' || node . value === ':global' ;
51
31
@@ -87,27 +67,26 @@ function localizeNodez(rule, mode, options) {
87
67
context . global = resultingGlobal ;
88
68
89
69
node . nodes = normalizeNodeArray ( newNodes ) ;
90
- // console.log(node.nodes);
91
70
break ;
92
71
}
93
72
case 'selector' : {
94
73
newNodes = node . map ( childNode => transform ( childNode , context ) ) ;
95
74
96
75
node = node . clone ( ) ;
97
76
node . nodes = normalizeNodeArray ( newNodes ) ;
98
- console . log ( 'SECLE' , node . toString ( ) ) ;
99
77
break ;
100
78
}
101
79
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 ;
80
+ if ( isSpacing ( node ) ) {
81
+ if ( context . ignoreNextSpacing ) {
82
+ context . ignoreNextSpacing = false ;
83
+ context . lastWasSpacing = false ;
84
+ context . enforceNoSpacing = false ;
85
+ return null ;
86
+ }
87
+ context . lastWasSpacing = true ;
88
+ return node ;
109
89
}
110
- context . lastWasSpacing = true ;
111
90
break ;
112
91
}
113
92
case 'pseudo' : {
@@ -132,7 +111,6 @@ function localizeNodez(rule, mode, options) {
132
111
hasLocals : false ,
133
112
explicit : true ,
134
113
} ;
135
- // console.log('PSUDI', node.nodes);
136
114
137
115
newNodes = node
138
116
. map ( childNode => transform ( childNode , childContext ) )
@@ -147,12 +125,9 @@ function localizeNodez(rule, mode, options) {
147
125
first . spaces = { before, after : first . spaces . after } ;
148
126
last . spaces = { before : last . spaces . before , after } ;
149
127
}
150
- console . log ( 'PSUDI' , node ) ;
128
+
151
129
node = newNodes ;
152
130
153
- // // don't leak spacing
154
- // node[0].spaces.before = '';
155
- // node[node.length - 1].spaces.after = '';
156
131
break ;
157
132
} else {
158
133
childContext = {
@@ -185,36 +160,41 @@ function localizeNodez(rule, mode, options) {
185
160
) ;
186
161
}
187
162
188
- const next = node . next ( ) ;
189
- console . log ( 'SPACESS' , next , node . spaces ) ;
190
- if ( next ) next . spaces = node . spaces ;
163
+ const next = node . parent ;
164
+ const addBackSpacing = ! ! node . spaces . before ;
191
165
192
166
context . ignoreNextSpacing = context . lastWasSpacing
193
167
? node . value
194
168
: false ;
169
+
195
170
context . enforceNoSpacing = context . lastWasSpacing
196
171
? false
197
172
: node . value ;
173
+
198
174
context . global = node . value === ':global' ;
199
175
context . explicit = true ;
200
- return null ;
176
+
177
+ // because this node has spacing that is lost when we remove it
178
+ // we make up for it by adding an extra combinator in since adding
179
+ // spacing on the parent selector doesn't work
180
+ return addBackSpacing
181
+ ? selectorParser . combinator ( { value : ' ' } )
182
+ : null ;
201
183
}
202
184
break ;
203
185
}
204
186
case 'id' :
205
187
case 'class' : {
206
188
if ( ! context . global ) {
207
- console . log ( 'REPLCE' , node . spaces ) ;
208
189
const innerNode = node . clone ( ) ;
209
- console . log ( innerNode ) ;
210
190
innerNode . spaces = { before : '' , after : '' } ;
211
191
212
192
node = selectorParser . pseudo ( {
213
193
value : ':local' ,
214
194
nodes : [ innerNode ] ,
215
195
spaces : node . spaces ,
216
196
} ) ;
217
- // console.log('HERE');
197
+
218
198
context . hasLocals = true ;
219
199
}
220
200
@@ -233,11 +213,11 @@ function localizeNodez(rule, mode, options) {
233
213
hasPureGlobals : false ,
234
214
} ;
235
215
236
- const updatedRule = selectorParser ( root => {
216
+ const selector = selectorParser ( root => {
237
217
transform ( root , rootContext ) ;
238
- } ) . processSync ( rule , { updateSelector : true , lossless : true } ) ;
218
+ } ) . processSync ( rule , { updateSelector : false , lossless : true } ) ;
239
219
240
- // console.log('HERE', rule. selector) ;
220
+ rootContext . selector = selector ;
241
221
return rootContext ;
242
222
}
243
223
@@ -479,7 +459,9 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
479
459
return ;
480
460
}
481
461
482
- const context = localizeNodez ( rule , options . mode , options ) ;
462
+ const context = localizeNode ( rule , options . mode ) ;
463
+
464
+ context . options = options ;
483
465
484
466
if ( pureMode && context . hasPureGlobals ) {
485
467
throw rule . error (
@@ -489,12 +471,10 @@ module.exports = postcss.plugin('postcss-modules-local-by-default', function(
489
471
'(pure selectors must contain at least one local class or id)'
490
472
) ;
491
473
}
474
+ rule . selector = context . selector ;
492
475
// Less-syntax mixins parse as rules with no nodes
493
476
if ( rule . nodes ) {
494
- rule . nodes . forEach ( function ( decl ) {
495
- console . log ( context ) ;
496
- localizeDecl ( decl , context ) ;
497
- } ) ;
477
+ rule . nodes . forEach ( decl => localizeDecl ( decl , context ) ) ;
498
478
}
499
479
} ) ;
500
480
} ;
0 commit comments