@@ -46,12 +46,10 @@ const trimNodes = nodes => dropWhile(dropRightWhile(nodes, isSpace), isSpace);
46
46
const getPathValue = nodes =>
47
47
nodes . length === 1 && nodes [ 0 ] . type === "string" ? nodes [ 0 ] . value : null ;
48
48
49
- const expandValuesParentheses = valuesNodes =>
50
- valuesNodes . length === 1 &&
51
- valuesNodes [ 0 ] . type === "function" &&
52
- valuesNodes [ 0 ] . value === ""
53
- ? valuesNodes [ 0 ] . nodes
54
- : valuesNodes ;
49
+ const expandValuesParentheses = nodes =>
50
+ nodes . length === 1 && nodes [ 0 ] . type === "function" && nodes [ 0 ] . value === ""
51
+ ? nodes [ 0 ] . nodes
52
+ : nodes ;
55
53
56
54
const getAliasesPairs = valuesNodes =>
57
55
chunkBy ( expandValuesParentheses ( valuesNodes ) , isComma ) . map ( pairNodes => {
@@ -95,19 +93,14 @@ const parse = value => {
95
93
return null ;
96
94
} ;
97
95
98
- const getAliasName = ( name , index ) =>
99
- `__value__${ name . replace ( / \W / g, "_" ) } __${ index } ` ;
96
+ const isForbidden = name => name . includes ( "." ) || name . includes ( "#" ) ;
100
97
101
- // TODO forbig '.' and '#' in names
98
+ const createGenerator = ( i = 0 ) => name =>
99
+ `__value__${ name . replace ( / \W / g, "_" ) } __${ i ++ } ` ;
102
100
103
101
module . exports = postcss . plugin ( plugin , ( ) => ( css , result ) => {
104
102
const { icssImports, icssExports } = extractICSS ( css ) ;
105
- let importIndex = 0 ;
106
- const createImportedName = ( path , name ) => {
107
- const importedName = getAliasName ( name , importIndex ) ;
108
- importIndex += 1 ;
109
- return importedName ;
110
- } ;
103
+ const getAliasName = createGenerator ( ) ;
111
104
112
105
css . walkAtRules ( "value" , atRule => {
113
106
if ( atRule . params . indexOf ( "@value" ) !== - 1 ) {
@@ -118,33 +111,50 @@ module.exports = postcss.plugin(plugin, () => (css, result) => {
118
111
const parsed = parse ( atRule . params ) ;
119
112
if ( parsed ) {
120
113
if ( parsed . type === "value" ) {
121
- if ( icssExports [ parsed . name ] ) {
122
- result . warn ( `"${ parsed . name } " value already declared` , {
123
- node : atRule
124
- } ) ;
125
- }
126
- icssExports [ parsed . name ] = replaceValueSymbols (
127
- parsed . value ,
128
- icssExports
129
- ) ;
130
- }
131
- if ( parsed . type === "import" ) {
132
- const pairs = parsed . pairs . map ( ( [ key , value ] ) => {
133
- let importedName = createImportedName ( parsed . path , value ) ;
134
- if ( icssExports [ value ] ) {
135
- result . warn ( `"${ value } " value already declared` , {
114
+ const { name, value } = parsed ;
115
+ if ( isForbidden ( name ) ) {
116
+ result . warn (
117
+ `Dot and hash symbols are not allowed in value "${ name } "` ,
118
+ { node : atRule }
119
+ ) ;
120
+ } else {
121
+ if ( icssExports [ name ] ) {
122
+ result . warn ( `"${ name } " value already declared` , {
136
123
node : atRule
137
124
} ) ;
138
125
}
139
- icssExports [ value ] = importedName ;
140
- return [ importedName , key ] ;
141
- } ) ;
142
- const aliases = fromPairs ( pairs ) ;
143
- icssImports [ parsed . path ] = Object . assign (
144
- { } ,
145
- icssImports [ parsed . path ] ,
146
- aliases
147
- ) ;
126
+ icssExports [ name ] = replaceValueSymbols ( value , icssExports ) ;
127
+ }
128
+ }
129
+ if ( parsed . type === "import" ) {
130
+ const pairs = parsed . pairs
131
+ . filter ( ( [ , local ] ) => {
132
+ if ( isForbidden ( local ) ) {
133
+ result . warn (
134
+ `Dot and hash symbols are not allowed in value "${ local } "`
135
+ ) ;
136
+ return false ;
137
+ }
138
+ return true ;
139
+ } )
140
+ . map ( ( [ imported , local ] ) => {
141
+ const alias = getAliasName ( local ) ;
142
+ if ( icssExports [ local ] ) {
143
+ result . warn ( `"${ local } " value already declared` , {
144
+ node : atRule
145
+ } ) ;
146
+ }
147
+ icssExports [ local ] = alias ;
148
+ return [ alias , imported ] ;
149
+ } ) ;
150
+ if ( pairs . length ) {
151
+ const aliases = fromPairs ( pairs ) ;
152
+ icssImports [ parsed . path ] = Object . assign (
153
+ { } ,
154
+ icssImports [ parsed . path ] ,
155
+ aliases
156
+ ) ;
157
+ }
148
158
}
149
159
} else {
150
160
result . warn ( `Invalid value definition "${ atRule . params } "` , {
0 commit comments