@@ -6,9 +6,9 @@ const plugin = "postcss-modules-local-by-default";
6
6
7
7
function normalizeNodeArray ( nodes ) {
8
8
var array = [ ] ;
9
- nodes . forEach ( function ( x ) {
9
+ nodes . forEach ( x => {
10
10
if ( Array . isArray ( x ) ) {
11
- normalizeNodeArray ( x ) . forEach ( function ( item ) {
11
+ normalizeNodeArray ( x ) . forEach ( item => {
12
12
array . push ( item ) ;
13
13
} ) ;
14
14
} else if ( x ) {
@@ -29,14 +29,11 @@ function localizeNode(node, context) {
29
29
throw Error ( `Missing whitespace before :${ context . enforceNoSpacing } ` ) ;
30
30
}
31
31
32
- var newNodes ;
33
32
switch ( node . type ) {
34
33
case "selector" :
35
- newNodes = node . nodes . map ( function ( n ) {
36
- return localizeNode ( n , context ) ;
37
- } ) ;
38
- node = Object . create ( node ) ;
39
- node . nodes = normalizeNodeArray ( newNodes ) ;
34
+ node . nodes = normalizeNodeArray (
35
+ node . nodes . map ( n => localizeNode ( n , context ) )
36
+ ) ;
40
37
break ;
41
38
42
39
case "spacing" :
@@ -53,11 +50,7 @@ function localizeNode(node, context) {
53
50
if ( node . name === "local" || node . name === "global" ) {
54
51
if ( context . inside ) {
55
52
throw Error (
56
- "A :" +
57
- node . name +
58
- " is not allowed inside of a :" +
59
- context . inside +
60
- "(...)"
53
+ `A :${ node . name } is not allowed inside of a :${ context . inside } (...)`
61
54
) ;
62
55
}
63
56
context . ignoreNextSpacing = context . lastWasSpacing ? node . name : false ;
@@ -73,11 +66,7 @@ function localizeNode(node, context) {
73
66
if ( node . name === "local" || node . name === "global" ) {
74
67
if ( context . inside ) {
75
68
throw Error (
76
- "A :" +
77
- node . name +
78
- "(...) is not allowed inside of a :" +
79
- context . inside +
80
- "(...)"
69
+ `A :${ node . name } (...) is not allowed inside of a :${ context . inside } (...)`
81
70
) ;
82
71
}
83
72
subContext = {
@@ -86,9 +75,7 @@ function localizeNode(node, context) {
86
75
hasLocals : false ,
87
76
explicit : true
88
77
} ;
89
- node = node . nodes . map ( function ( n ) {
90
- return localizeNode ( n , subContext ) ;
91
- } ) ;
78
+ node = node . nodes . map ( n => localizeNode ( n , subContext ) ) ;
92
79
// don't leak spacing
93
80
node [ 0 ] . before = undefined ;
94
81
node [ node . length - 1 ] . after = undefined ;
@@ -100,15 +87,9 @@ function localizeNode(node, context) {
100
87
hasLocals : false ,
101
88
explicit : context . explicit
102
89
} ;
103
- newNodes = node . nodes . map ( function ( n ) {
104
- return localizeNode ( n , subContext ) ;
105
- } ) ;
106
- node = Object . create ( node ) ;
107
- node . nodes = normalizeNodeArray ( newNodes ) ;
108
- }
109
- if ( subContext . hasLocals ) {
110
- context . hasLocals = true ;
90
+ node . nodes = node . nodes . map ( n => localizeNode ( n , subContext ) ) ;
111
91
}
92
+ context . hasLocals = subContext . hasLocals ;
112
93
break ;
113
94
114
95
case "id" :
@@ -135,7 +116,7 @@ const localizeSelectors = (selectors, context) => {
135
116
const node = Tokenizer . parse ( selectors ) ;
136
117
var resultingGlobal ;
137
118
context . hasPureGlobals = false ;
138
- const newNodes = node . nodes . map ( function ( n ) {
119
+ node . nodes = node . nodes . map ( n => {
139
120
var nContext = {
140
121
global : context . global ,
141
122
lastWasSpacing : true ,
@@ -147,7 +128,8 @@ const localizeSelectors = (selectors, context) => {
147
128
resultingGlobal = nContext . global ;
148
129
} else if ( resultingGlobal !== nContext . global ) {
149
130
throw Error (
150
- `Inconsistent rule global/local result in rule "${ Tokenizer . stringify ( node ) } " (multiple selectors must result in the same mode for the rule)`
131
+ `Inconsistent rule global/local result in rule "${ Tokenizer . stringify ( node ) } "` +
132
+ ` (multiple selectors must result in the same mode for the rule)`
151
133
) ;
152
134
}
153
135
if ( ! nContext . hasLocals ) {
@@ -156,7 +138,6 @@ const localizeSelectors = (selectors, context) => {
156
138
return n ;
157
139
} ) ;
158
140
context . global = resultingGlobal ;
159
- node . nodes = normalizeNodeArray ( newNodes ) ;
160
141
return Tokenizer . stringify ( node ) ;
161
142
} ;
162
143
@@ -173,7 +154,7 @@ module.exports = postcss.plugin(plugin, (options = {}) => css => {
173
154
}
174
155
var pureMode = options . mode === "pure" ;
175
156
var globalMode = options . mode === "global" ;
176
- css . walkRules ( function ( rule ) {
157
+ css . walkRules ( rule => {
177
158
if ( rule . parent . type === "atrule" && / k e y f r a m e s $ / . test ( rule . parent . name ) ) {
178
159
// ignore keyframe rules
179
160
return ;
0 commit comments