Skip to content

Commit 3fb4fe8

Browse files
committed
Merge pull request #49 from GitScrum/milestone-0.0.20
Milestone 0.0.20
2 parents 8e6e193 + 235351d commit 3fb4fe8

File tree

4 files changed

+55
-29
lines changed

4 files changed

+55
-29
lines changed

CHANGELOG.md

+19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
## [0.0.20] - 07-04-2016
2+
### Added
3+
- Added #41, trevor
4+
- Added #46, ava-codemods
5+
- Added #44, lodash.merge
6+
7+
### Change
8+
- Change #48, updateDev to update
9+
- Update #45, devDep
10+
- Update #33, core
11+
12+
### Remove
13+
- Remove #47, run task postinstall
14+
- Remove #41, trevor need nvm
15+
- Remove #44, deep-assign, not merge array
16+
17+
### Fixed
18+
- Fixed #44, broken test, deep-assign not merge array. Change to lodash
19+
120
## [0.0.19] - 02-04-2016
221
### Added
322
- Added #34, husky

package.json

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
{
22
"name": "postcss-at-rules-variables",
3-
"version": "0.0.19",
3+
"version": "0.0.20",
44
"description": "PostCss plugin to use CSS Custom Properties in at-rule @each, @for, @if, @else ",
55
"main": "./lib/index.js",
66
"scripts": {
77
"test": "xo ./src/*.js ./test/*.js && nyc ava",
88
"clean": "rm -rf lib && mkdir lib",
99
"build": "npm run clean && babel src/index.js --out-file lib/index.js",
10-
"postinstall": "npm run updateDev",
1110
"prepublish": "npm run build",
1211
"prepush": "npm test",
13-
"updateDev": "updtr"
12+
"update": "updtr && ava-codemods"
1413
},
1514
"keywords": [
1615
"postcss",
@@ -36,15 +35,17 @@
3635
"postcss": "^5.0.19"
3736
},
3837
"devDependencies": {
39-
"ava": "^0.13.0",
38+
"ava": "^0.14.0",
39+
"ava-codemods": "^0.2.1",
4040
"babel-cli": "^6.6.5",
4141
"babel-plugin-add-module-exports": "^0.1.2",
4242
"babel-preset-node5": "^11.0.0",
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",
47-
"updtr": "^0.1.7",
48+
"updtr": "^0.1.10",
4849
"xo": "^0.13.0"
4950
},
5051
"babel": {

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)