Skip to content

Commit d974ef6

Browse files
committedJan 18, 2018
Fix regression, replace values in expressions without spaces, closes #23
1 parent e094c2c commit d974ef6

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed
 

‎index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const postcss = require('postcss');
22
const path = require('path');
33
const promisify = require('es6-promisify');
44
const { CachedInputFileSystem, NodeJsInputFileSystem, ResolverFactory } = require('enhanced-resolve');
5-
const valueParser = require('postcss-value-parser');
5+
const valuesParser = require('postcss-values-parser');
66

77
const matchImports = /^(.+?|\([\s\S]+?\))\s+from\s+("[^"]*"|'[^']*'|[\w-]+)$/;
88
const matchValueDefinition = /(?:\s+|^)([\w-]+)(:?\s+)(.+?)(\s*)$/g;
@@ -17,7 +17,7 @@ const nodeFs = new CachedInputFileSystem(new NodeJsInputFileSystem(), 4000);
1717
const concordContext = {};
1818

1919
const replaceValueSymbols = (valueString, replacements) => {
20-
const value = valueParser(valueString);
20+
const value = valuesParser(valueString, { loose: true }).parse();
2121

2222
value.walk((node) => {
2323
if (node.type !== 'word') return;

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"enhanced-resolve": "^3.1.0",
2323
"es6-promisify": "^5.0.0",
2424
"postcss": "^6.0.1",
25-
"postcss-value-parser": "^3.3.0"
25+
"postcss-values-parser": "^1.3.1"
2626
},
2727
"devDependencies": {
2828
"ava": "^0.24.0",

‎test.js

+51-3
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ test('gives an error when @value statement is invalid', async (t) => {
4848
await t.throws(processor.process(input, parserOpts));
4949
});
5050

51+
test('shouldn\'t break on draft spec syntax', async (t) => {
52+
await run(
53+
t,
54+
'.foo { width: calc(2+2); }',
55+
'.foo { width: calc(2+2); }',
56+
);
57+
});
58+
5159
test('should replace constants within the file', async (t) => {
5260
await run(
5361
t,
@@ -56,6 +64,14 @@ test('should replace constants within the file', async (t) => {
5664
);
5765
});
5866

67+
test('shouldn\'t replace number-like values', async (t) => {
68+
await run(
69+
t,
70+
'@value 3char #000; .foo { color: 3char; }',
71+
'@value 3char #000; .foo { color: 3char; }',
72+
);
73+
});
74+
5975
test('shouldn\'t replace selector', async (t) => {
6076
await run(
6177
t,
@@ -72,6 +88,22 @@ test('shouldn\'t replace inside url', async (t) => {
7288
);
7389
});
7490

91+
test('should replace within calc', async (t) => {
92+
await run(
93+
t,
94+
'@value base: 10px;\n.a { margin: calc(base * 2); }',
95+
'@value base: 10px;\n.a { margin: calc(10px * 2); }',
96+
);
97+
});
98+
99+
test('should replace within calc without spaces', async (t) => {
100+
await run(
101+
t,
102+
'@value base: 10px;\n.a { margin: calc(base*2); }',
103+
'@value base: 10px;\n.a { margin: calc(10px*2); }',
104+
);
105+
});
106+
75107
test('should replace two constants with same name within the file and the latter should win', async (t) => {
76108
await run(
77109
t,
@@ -191,6 +223,22 @@ test('should allow transitive values within calc', async (t) => {
191223
);
192224
});
193225

226+
test('should allow transitive values within calc without spaces', async (t) => {
227+
await run(
228+
t,
229+
'@value base: 10px;\n@value large: calc(base*2);\n.a { margin: large; }',
230+
'@value base: 10px;\n@value large: calc(10px*2);\n.a { margin: calc(10px*2); }',
231+
);
232+
});
233+
234+
test('should replace inside custom properties', async (t) => {
235+
await run(
236+
t,
237+
'@value path: test.png;\n:root {--path: path};\n.foo { background-image: url(var(--path)); }',
238+
'@value path: test.png;\n:root {--path: test.png};\n.foo { background-image: url(var(--path)); }',
239+
);
240+
});
241+
194242
test('should allow custom-property-style names', async (t) => {
195243
await run(
196244
t,
@@ -202,9 +250,9 @@ test('should allow custom-property-style names', async (t) => {
202250
test('should allow all colour types', async (t) => {
203251
await run(
204252
t,
205-
'@value named: red; @value 3char #0f0; @value 6char #00ff00; @value rgba rgba(34, 12, 64, 0.3); @value hsla hsla(220, 13.0%, 18.0%, 1);\n' +
206-
'.foo { color: named; background-color: 3char; border-top-color: 6char; border-bottom-color: rgba; outline-color: hsla; }',
207-
'@value named: red; @value 3char #0f0; @value 6char #00ff00; @value rgba rgba(34, 12, 64, 0.3); @value hsla hsla(220, 13.0%, 18.0%, 1);\n' +
253+
'@value named: red; @value hex3char #0f0; @value hex6char #00ff00; @value rgba rgba(34, 12, 64, 0.3); @value hsla hsla(220, 13.0%, 18.0%, 1);\n' +
254+
'.foo { color: named; background-color: hex3char; border-top-color: hex6char; border-bottom-color: rgba; outline-color: hsla; }',
255+
'@value named: red; @value hex3char #0f0; @value hex6char #00ff00; @value rgba rgba(34, 12, 64, 0.3); @value hsla hsla(220, 13.0%, 18.0%, 1);\n' +
208256
'.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); }',
209257
);
210258
});

0 commit comments

Comments
 (0)
Please sign in to comment.