Skip to content

Commit 7a19ff3

Browse files
committed
no more multiple definitions on one line
1 parent bb69ed6 commit 7a19ff3

File tree

3 files changed

+43
-17
lines changed

3 files changed

+43
-17
lines changed

lib/index.js

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var _icssReplaceSymbols = require('icss-replace-symbols');
1717
var _icssReplaceSymbols2 = _interopRequireDefault(_icssReplaceSymbols);
1818

1919
var matchImports = /^(.+?)\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/;
20-
var matchValueDefinition = /(?:,\s+|^)([\w-]+):?\s+("[^"]*"|'[^']*'|\w+\([^\)]+\)|[^,]+)\s?/g;
20+
var matchValueDefinition = /^([\w-]+):?\s+(.*)\s*$/;
2121
var matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/;
2222

2323
var processor = _postcss2['default'].plugin('postcss-modules-values', function () {
@@ -34,14 +34,12 @@ var processor = _postcss2['default'].plugin('postcss-modules-values', function (
3434
var definitions = {};
3535

3636
var addDefinition = function addDefinition(atRule) {
37-
var matches = undefined;
38-
while (matches = matchValueDefinition.exec(atRule.params)) {
39-
var _matches = matches;
40-
41-
var _matches2 = _slicedToArray(_matches, 3);
37+
var matches = matchValueDefinition.exec(atRule.params);
38+
if (matches) {
39+
var _matches = _slicedToArray(matches, 3);
4240

43-
var /*match*/key = _matches2[1];
44-
var value = _matches2[2];
41+
var /*match*/key = _matches[1];
42+
var value = _matches[2];
4543

4644
// Add to the definitions, knowing that values can refer to each other
4745
definitions[key] = (0, _icssReplaceSymbols.replaceAll)(definitions, value);
@@ -52,10 +50,10 @@ var processor = _postcss2['default'].plugin('postcss-modules-values', function (
5250
var addImport = function addImport(atRule) {
5351
var matches = matchImports.exec(atRule.params);
5452
if (matches) {
55-
var _matches3 = _slicedToArray(matches, 3);
53+
var _matches2 = _slicedToArray(matches, 3);
5654

57-
var /*match*/aliases = _matches3[1];
58-
var path = _matches3[2];
55+
var /*match*/aliases = _matches2[1];
56+
var path = _matches2[2];
5957

6058
// We can use constants for path names
6159
if (definitions[path]) path = definitions[path];

src/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import postcss from 'postcss'
22
import replaceSymbols, { replaceAll } from 'icss-replace-symbols'
33

44
const matchImports = /^(.+?)\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/
5-
const matchValueDefinition = /(?:,\s+|^)([\w-]+):?\s+("[^"]*"|'[^']*'|\w+\([^\)]+\)|[^,]+)\s?/g
5+
const matchValueDefinition = /^([\w-]+):?\s+(.*)\s*$/
66
const matchImport = /^([\w-]+)(?:\s+as\s+([\w-]+))?/
77

88
const processor = postcss.plugin('postcss-modules-values', function (opts = {}) {
@@ -16,8 +16,8 @@ const processor = postcss.plugin('postcss-modules-values', function (opts = {})
1616
let definitions = {}
1717

1818
const addDefinition = atRule => {
19-
let matches
20-
while (matches = matchValueDefinition.exec(atRule.params)) {
19+
let matches = matchValueDefinition.exec(atRule.params)
20+
if (matches) {
2121
let [/*match*/, key, value] = matches
2222
// Add to the definitions, knowing that values can refer to each other
2323
definitions[key] = replaceAll(definitions, value)

test/index.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,23 @@ describe('constants', () => {
4141

4242
it('should import multiple from a single file', () => {
4343
test(
44-
`@value blue, red from "./colors.css";
44+
`@value blue, red, green as three from "./colors.css";
4545
.foo { color: red; }
46-
.bar { color: blue }`,
46+
.bar { color: blue; }
47+
.baz { color: three; }`,
4748
`:import("./colors.css") {
4849
i__const_blue_0: blue;
4950
i__const_red_1: red;
51+
i__const_three_2: green;
5052
}
5153
:export {
5254
blue: i__const_blue_0;
5355
red: i__const_red_1;
56+
three: i__const_three_2;
5457
}
5558
.foo { color: i__const_red_1; }
56-
.bar { color: i__const_blue_0 }`)
59+
.bar { color: i__const_blue_0; }
60+
.baz { color: i__const_three_2; }`)
5761
})
5862

5963
it('should import from a definition', () => {
@@ -64,6 +68,13 @@ describe('constants', () => {
6468
)
6569
})
6670

71+
it('should compose from a definition', () => {
72+
test(
73+
'@value colors: "./colors.css"; .foo { composes: red from colors; }',
74+
':export {\n colors: "./colors.css";\n}\n.foo { composes: red from "./colors.css"; }'
75+
)
76+
})
77+
6778
it('should only allow values for paths if defined in the right order', () => {
6879
test(
6980
'@value red from colors; @value colors: "./colors.css";',
@@ -110,4 +121,21 @@ describe('constants', () => {
110121
':export {\n named: red;\n 3char: #0f0;\n 6char: #00ff00;\n rgba: rgba(34, 12, 64, 0.3);\n hsla: hsla(220, 13.0%, 18.0%, 1);\n}\n' +
111122
'.foo { color: red; background-color: #0f0; border-top-color: #00ff00; border-bottom-color: rgba(34, 12, 64, 0.3); outline-color: hsla(220, 13.0%, 18.0%, 1); }')
112123
})
124+
125+
it('should allow box shadow definitions with rgba', () => {
126+
test(
127+
'@value okShadow: 0 11px 15px -7px rgba(0,0,0,.2);\n' +
128+
'.foo { box-shadow: okShadow; }',
129+
':export {\n okShadow: 0 11px 15px -7px rgba(0,0,0,.2);\n}\n' +
130+
'.foo { box-shadow: 0 11px 15px -7px rgba(0,0,0,.2); }'
131+
)
132+
})
133+
134+
it('should allow definitions with commas in them if quoted', () => {
135+
test(
136+
'@value coolShadow: 0 11px 15px -7px rgba(0,0,0,.2), 0 24px 38px 3px rgba(0,0,0,.14);\n' +
137+
'.foo { box-shadow: coolShadow; }',
138+
':export {\n coolShadow: 0 11px 15px -7px rgba(0,0,0,.2), 0 24px 38px 3px rgba(0,0,0,.14);\n}\n' +
139+
'.foo { box-shadow: 0 11px 15px -7px rgba(0,0,0,.2), 0 24px 38px 3px rgba(0,0,0,.14); }')
140+
})
113141
})

0 commit comments

Comments
 (0)