Skip to content

Commit 7a416b7

Browse files
author
Ivan Demidov
committedApr 7, 2016
Fixed #44, broken test, Fixed #33 update core
1 parent 91e86c2 commit 7a416b7

File tree

3 files changed

+31
-24
lines changed

3 files changed

+31
-24
lines changed
 

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
"babel-register": "^6.7.2",
4444
"coveralls": "^2.11.8",
4545
"husky": "^0.11.4",
46+
"lodash.merge": "^4.3.4",
4647
"nyc": "^6.1.1",
4748
"updtr": "^0.1.10",
4849
"xo": "^0.13.0"

‎src/index.js

+28-22
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
11
import postcss from 'postcss';
2-
import deepAssign from 'deep-assign';
2+
import {merge as assign} from 'lodash';
33

4-
const VAR_FUNC_IDENTIFIER = 'var';
5-
const maps = {};
4+
function hasVar(str) {
5+
return str.indexOf('var(');
6+
}
67

7-
function resolveValue(value) {
8-
const hasVar = value.indexOf(`${VAR_FUNC_IDENTIFIER}(`);
9-
if (hasVar === -1) {
8+
function resolveValue(value, maps) {
9+
if (hasVar(value) === -1) {
1010
return value;
1111
}
1212

1313
return value.replace(/var\(--.*?\)/g, (match) => maps[match.slice(4, -1)] || match);
1414
}
1515

16+
function getProperty(nodes) {
17+
let propertys = {};
18+
19+
nodes.walkRules(rule => {
20+
if (rule.selector !== ':root') {
21+
return;
22+
}
23+
24+
rule.each(decl => {
25+
const {prop, value} = decl;
26+
propertys[prop] = value;
27+
});
28+
});
29+
30+
return propertys;
31+
}
32+
1633
export default postcss.plugin('postcss-at-rules-variables', options => {
1734
const DEFAULT = {
1835
atRules: ['for', 'if', 'else', 'each']
1936
};
2037

21-
const mergeOptions = deepAssign(DEFAULT, options, (a, b) => {
38+
options = assign(DEFAULT, options, (a, b) => {
2239
if (Array.isArray(a)) {
2340
return a.concat(b);
2441
}
2542
});
2643

2744
return nodes => {
28-
const reg = new RegExp(mergeOptions.atRules.join('|'));
29-
30-
nodes.walkRules(rule => {
31-
if (rule.selector !== ':root') {
32-
return;
33-
}
34-
35-
rule.each(decl => {
36-
const prop = decl.prop;
37-
const value = decl.value;
38-
maps[prop] = value;
39-
});
40-
});
45+
const maps = getProperty(nodes);
46+
const {atRules} = options;
4147

42-
nodes.walkAtRules(reg, rules => {
43-
rules.params = resolveValue(rules.params);
48+
nodes.walkAtRules(new RegExp(atRules.join('|')), rules => {
49+
rules.params = resolveValue(rules.params, maps);
4450
});
4551
};
4652
});

‎test/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ test('it change two properties for @if, @else if', t => {
4343
});
4444

4545
test('it change multi properties for @each', t => {
46-
const expected = ':root{ --array: foo, bar, baz; } @each $val in foo, bar, baz @for foo, bar, baz';
47-
const value = ':root{ --array: foo, bar, baz; } @each $val in var(--array) @for var(--array)';
46+
const expected = ':root{ --array: foo, bar, baz; } @each $val in foo, bar, baz {} @for foo, bar, baz {}';
47+
const value = ':root{ --array: foo, bar, baz; } @each $val in var(--array) {} @for var(--array) {}';
4848
t.is(processing(value, {atRules: ['each']}), expected);
4949
});
5050

0 commit comments

Comments
 (0)
Please sign in to comment.