Skip to content

Commit 306a7a0

Browse files
committed
Merge pull request #32 from GitScrum/milestone-0.0.18
Milestone 0.0.18
2 parents a6a02b5 + 5208ae2 commit 306a7a0

File tree

7 files changed

+126
-127
lines changed

7 files changed

+126
-127
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
node_modules/
22
lib/
33
coverage/
4+
.nyc_output/
45
npm-debug.log
56
*.sublime*

.travis.yml

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
sudo: false
22
language: node_js
33
node_js:
4-
- iojs
54
- "5.1.0"
65
- "4.2.2"
7-
- "0.12"
8-
- "0.10"
6+
97
after_script:
10-
- npm run coveralls
8+
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'

CHANGELOG.md

+22
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
## [0.0.17] - 18-03-2016
2+
### Added
3+
- Added #1, ava
4+
- Added #19, coverage to travis config
5+
- Adeed test after transpiling
6+
7+
### Change
8+
- Change #21, isparta to nyc
9+
- Rename index.es6 to index
10+
- Update #28, move updtr to prepublish
11+
- Change #1, test to ava
12+
- Update #31, matcher and core
13+
14+
### Removed
15+
- Removed #30, ild node version in .travis config
16+
- Removed #1, mocha chai
17+
- Remove #19, coverage runs in package.json
18+
- Remove config for babel from cli
19+
20+
### Fixed
21+
- Fixed report coverage
22+
123
## [0.0.17] - 15-03-2016
224
### Added
325
- Added #18, babel config to package.json

package.json

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
{
22
"name": "postcss-at-rules-variables",
3-
"version": "0.0.17",
3+
"version": "0.0.18",
44
"description": "PostCss plugin to use CSS Custom Properties in at-rule @each, @for, @if, @else ",
55
"main": "./lib/index.js",
66
"scripts": {
7-
"test": "xo ./src/*.js ./test/*.js && mocha --compilers js:babel-register ./test/*.js",
7+
"test": "xo ./src/*.js ./test/*.js && nyc ava",
88
"clean": "rm -rf lib && mkdir lib",
9-
"build": "npm run clean && babel --presets node5 src/index.es6.js --out-file lib/index.js",
10-
"prepublish": "npm run build",
11-
"coverage": "babel-node ./node_modules/isparta/bin/isparta cover _mocha --",
12-
"coveralls": "coveralls < coverage/lcov.info",
13-
"precoveralls": "npm run coverage",
14-
"update": "updtr"
9+
"build": "npm run clean && babel src/index.js --out-file lib/index.js && npm t",
10+
"prepublish": "updtr && npm run build"
1511
},
1612
"keywords": [
1713
"postcss",
@@ -38,13 +34,12 @@
3834
"postcss": "^5.0.19"
3935
},
4036
"devDependencies": {
37+
"ava": "^0.13.0",
4138
"babel-cli": "^6.6.5",
42-
"babel-preset-node5": "^10.8.0",
39+
"babel-preset-node5": "^11.0.0",
4340
"babel-register": "^6.7.2",
44-
"chai": "^3.5.0",
4541
"coveralls": "^2.11.8",
46-
"isparta": "^4.0.0",
47-
"mocha": "^2.4.5",
42+
"nyc": "^6.1.1",
4843
"updtr": "^0.1.7",
4944
"xo": "^0.13.0"
5045
},
@@ -53,6 +48,11 @@
5348
"node5"
5449
]
5550
},
51+
"ava": {
52+
"require": [
53+
"babel-register"
54+
]
55+
},
5656
"bugs": {
5757
"url": "https://github.com/gitscrum/postcss-at-rules-variables/issues"
5858
},

src/index.es6.js

-55
This file was deleted.

src/index.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import postcss from 'postcss';
2+
import {merge} from 'lodash';
3+
4+
const VAR_FUNC_IDENTIFIER = 'var';
5+
const maps = {};
6+
7+
function resolveValue(value) {
8+
const hasVar = value.indexOf(`${VAR_FUNC_IDENTIFIER}(`);
9+
if (hasVar === -1) {
10+
return value;
11+
}
12+
13+
return value.replace(/var\(--.*?\)/g, (match) => maps[match.slice(4, -1)] || match);
14+
}
15+
16+
export default postcss.plugin('postcss-at-rules-variables', options => {
17+
const DEFAULT = {
18+
atRules: ['for', 'if', 'else', 'each']
19+
};
20+
21+
const mergeOptions = merge(DEFAULT, options, (a, b) => {
22+
if (Array.isArray(a)) {
23+
return a.concat(b);
24+
}
25+
});
26+
27+
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+
});
41+
42+
nodes.walkAtRules(reg, rules => {
43+
rules.params = resolveValue(rules.params);
44+
});
45+
};
46+
});

test/test.js

+43-56
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,55 @@
1-
/* global describe it */
21
import postcss from 'postcss';
3-
import {expect} from 'chai';
2+
import test from 'ava';
43
import plugin from '../lib/index';
5-
const test = (input, output, opts) => {
6-
expect(postcss([plugin(opts)]).process(input).css).to.eql(output);
4+
5+
const processing = (input, opts) => {
6+
return postcss([plugin(opts)]).process(input).css;
77
};
88

9-
describe('postcss-at-rules-variables', () => {
10-
it('it change first properties for @for', () => {
11-
test(
12-
':root{ --from: 1; } @for $i from var(--from) to 2',
13-
':root{ --from: 1; } @for $i from 1 to 2'
14-
);
15-
});
9+
test('it change first properties for @for', t => {
10+
const expected = ':root{ --from: 1; } @for $i from 1 to 2';
11+
const value = ':root{ --from: 1; } @for $i from var(--from) to 2';
12+
t.is(processing(value), expected);
13+
});
1614

17-
it('it change second properties for @for', () => {
18-
test(
19-
':root{ --to: 2; } @for $i from 1 to var(--to)',
20-
':root{ --to: 2; } @for $i from 1 to 2'
21-
);
22-
});
15+
test('it change second properties for @for', t => {
16+
const expected = ':root{ --to: 2; } @for $i from 1 to 2';
17+
const value = ':root{ --to: 2; } @for $i from 1 to var(--to)';
18+
t.is(processing(value), expected);
19+
});
2320

24-
it('it change two properties for @for', () => {
25-
test(
26-
':root{ --from: 1; --to: 2; } @for $i from var(--from) to var(--to)',
27-
':root{ --from: 1; --to: 2; } @for $i from 1 to 2'
28-
);
29-
});
21+
test('it change two properties for @for', t => {
22+
const expected = ':root{ --from: 1; --to: 2; } @for $i from 1 to 2';
23+
const value = ':root{ --from: 1; --to: 2; } @for $i from var(--from) to var(--to)';
24+
t.is(processing(value), expected);
25+
});
3026

31-
it('it change three properties for @for', () => {
32-
test(
33-
':root{ --from: 1; --to: 2; --step: 5 } @for $i from var(--from) to var(--to) by var(--step)',
34-
':root{ --from: 1; --to: 2; --step: 5 } @for $i from 1 to 2 by 5'
35-
);
36-
});
27+
test('it change three properties for @for', t => {
28+
const expected = ':root{ --from: 1; --to: 2; --step: 5 } @for $i from 1 to 2 by 5';
29+
const value = ':root{ --from: 1; --to: 2; --step: 5 } @for $i from var(--from) to var(--to) by var(--step)';
30+
t.is(processing(value), expected);
31+
});
3732

38-
it('it change two properties for @if', () => {
39-
test(
40-
':root{ --first: 1; --second: 2; } @if var(--first) < var(--second)',
41-
':root{ --first: 1; --second: 2; } @if 1 < 2',
42-
{atRules: ['if']}
43-
);
44-
});
33+
test('it change two properties for @if', t => {
34+
const expected = ':root{ --first: 1; --second: 2; } @if 1 < 2';
35+
const value = ':root{ --first: 1; --second: 2; } @if var(--first) < var(--second)';
36+
t.is(processing(value, {atRules: ['if']}), expected);
37+
});
4538

46-
it('it change two properties for @if, @else if', () => {
47-
test(
48-
':root{ --first: 1; --second: 2; } @if var(--first) < var(--second) { color: olive; } @else if var(--first) > var(--second) { color: red; }',
49-
':root{ --first: 1; --second: 2; } @if 1 < 2 { color: olive; } @else if 1 > 2 { color: red; }',
50-
{atRules: ['if', 'else']}
51-
);
52-
});
39+
test('it change two properties for @if, @else if', t => {
40+
const expected = ':root{ --first: 1; --second: 2; } @if 1 < 2 { color: olive; } @else if 1 > 2 { color: red; }';
41+
const value = ':root{ --first: 1; --second: 2; } @if var(--first) < var(--second) { color: olive; } @else if var(--first) > var(--second) { color: red; }';
42+
t.is(processing(value, {atRules: ['if', 'else']}), expected);
43+
});
5344

54-
it('it change multi properties for @each', () => {
55-
test(
56-
':root{ --array: foo, bar, baz; } @each $val in var(--array)',
57-
':root{ --array: foo, bar, baz; } @each $val in foo, bar, baz',
58-
{atRules: ['each']}
59-
);
60-
});
45+
test('it change multi properties for @each', t => {
46+
const expected = ':root{ --array: foo, bar, baz; } @each $val in foo, bar, baz';
47+
const value = ':root{ --array: foo, bar, baz; } @each $val in var(--array)';
48+
t.is(processing(value, {atRules: ['each']}), expected);
49+
});
6150

62-
it('it without variables', () => {
63-
test(
64-
':root{ --red: red; } @if var(--green) { color: var(--green) }',
65-
':root{ --red: red; } @if var(--green) { color: var(--green) }'
66-
);
67-
});
51+
test('it without variables', t => {
52+
const expected = ':root{ --red: red; } @if var(--green) { color: var(--green) }';
53+
const value = ':root{ --red: red; } @if var(--green) { color: var(--green) }';
54+
t.is(processing(value), expected);
6855
});

0 commit comments

Comments
 (0)