@@ -6,9 +6,9 @@ const plugin = "postcss-modules-local-by-default";
66
77function normalizeNodeArray ( nodes ) {
88 var array = [ ] ;
9- nodes . forEach ( function ( x ) {
9+ nodes . forEach ( x => {
1010 if ( Array . isArray ( x ) ) {
11- normalizeNodeArray ( x ) . forEach ( function ( item ) {
11+ normalizeNodeArray ( x ) . forEach ( item => {
1212 array . push ( item ) ;
1313 } ) ;
1414 } else if ( x ) {
@@ -29,14 +29,11 @@ function localizeNode(node, context) {
2929 throw Error ( `Missing whitespace before :${ context . enforceNoSpacing } ` ) ;
3030 }
3131
32- var newNodes ;
3332 switch ( node . type ) {
3433 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+ ) ;
4037 break ;
4138
4239 case "spacing" :
@@ -53,11 +50,7 @@ function localizeNode(node, context) {
5350 if ( node . name === "local" || node . name === "global" ) {
5451 if ( context . inside ) {
5552 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 } (...)`
6154 ) ;
6255 }
6356 context . ignoreNextSpacing = context . lastWasSpacing ? node . name : false ;
@@ -73,11 +66,7 @@ function localizeNode(node, context) {
7366 if ( node . name === "local" || node . name === "global" ) {
7467 if ( context . inside ) {
7568 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 } (...)`
8170 ) ;
8271 }
8372 subContext = {
@@ -86,9 +75,7 @@ function localizeNode(node, context) {
8675 hasLocals : false ,
8776 explicit : true
8877 } ;
89- node = node . nodes . map ( function ( n ) {
90- return localizeNode ( n , subContext ) ;
91- } ) ;
78+ node = node . nodes . map ( n => localizeNode ( n , subContext ) ) ;
9279 // don't leak spacing
9380 node [ 0 ] . before = undefined ;
9481 node [ node . length - 1 ] . after = undefined ;
@@ -100,15 +87,9 @@ function localizeNode(node, context) {
10087 hasLocals : false ,
10188 explicit : context . explicit
10289 } ;
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 ) ) ;
11191 }
92+ context . hasLocals = subContext . hasLocals ;
11293 break ;
11394
11495 case "id" :
@@ -135,7 +116,7 @@ const localizeSelectors = (selectors, context) => {
135116 const node = Tokenizer . parse ( selectors ) ;
136117 var resultingGlobal ;
137118 context . hasPureGlobals = false ;
138- const newNodes = node . nodes . map ( function ( n ) {
119+ node . nodes = node . nodes . map ( n => {
139120 var nContext = {
140121 global : context . global ,
141122 lastWasSpacing : true ,
@@ -147,7 +128,8 @@ const localizeSelectors = (selectors, context) => {
147128 resultingGlobal = nContext . global ;
148129 } else if ( resultingGlobal !== nContext . global ) {
149130 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)`
151133 ) ;
152134 }
153135 if ( ! nContext . hasLocals ) {
@@ -156,7 +138,6 @@ const localizeSelectors = (selectors, context) => {
156138 return n ;
157139 } ) ;
158140 context . global = resultingGlobal ;
159- node . nodes = normalizeNodeArray ( newNodes ) ;
160141 return Tokenizer . stringify ( node ) ;
161142} ;
162143
@@ -173,7 +154,7 @@ module.exports = postcss.plugin(plugin, (options = {}) => css => {
173154 }
174155 var pureMode = options . mode === "pure" ;
175156 var globalMode = options . mode === "global" ;
176- css . walkRules ( function ( rule ) {
157+ css . walkRules ( rule => {
177158 if ( rule . parent . type === "atrule" && / k e y f r a m e s $ / . test ( rule . parent . name ) ) {
178159 // ignore keyframe rules
179160 return ;
0 commit comments