Skip to content

Commit 659398e

Browse files
committed
style: according linter
1 parent 523421a commit 659398e

File tree

3 files changed

+80
-77
lines changed

3 files changed

+80
-77
lines changed

.editorconfig

+4-1
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@ charset = utf-8
88
trim_trailing_whitespace = true
99
insert_final_newline = true
1010

11-
[{package.json,*.yml}]
11+
[*.{json,yml,jade,pss,css,html,js}]
1212
indent_size = 2
1313

1414
[changelog.md]
1515
insert_final_newline = false
1616

17+
[*.html]
18+
insert_final_newline = false
19+
1720
[*.md]
1821
trim_trailing_whitespace = false

src/index.js

+32-32
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
import postcss from 'postcss';
22

3-
function hasVar(str) {
4-
return str.includes('var(');
3+
function hasVar(string) {
4+
return string.includes('var(');
55
}
66

77
function resolveValue(value, maps) {
8-
return hasVar(value) ? value.replace(/var\(--.*?\)/g, match => maps[match.slice(4, -1)] || match) : value;
8+
return hasVar(value) ? value.replace(/var\(--.*?\)/g, match => maps[match.slice(4, -1)] || match) : value;
99
}
1010

1111
function getProperty(nodes) {
12-
let propertys = {};
13-
14-
nodes.walkRules(rule => {
15-
if (rule.selector !== ':root') {
16-
return;
17-
}
18-
19-
rule.each(({type, prop, value}) => {
20-
if (type === 'decl') {
21-
propertys[prop] = value;
22-
}
23-
});
12+
let propertys = {};
13+
14+
nodes.walkRules(rule => {
15+
if (rule.selector !== ':root') {
16+
return;
17+
}
18+
19+
rule.each(({type, prop: property, value}) => {
20+
if (type === 'decl') {
21+
propertys[property] = value;
22+
}
2423
});
24+
});
2525

26-
return propertys;
26+
return propertys;
2727
}
2828

2929
function circularReference(maps) {
30-
return Object.keys(maps).reduce((prevMaps, property) => {
31-
prevMaps[property] = resolveValue(maps[property], maps);
32-
return prevMaps;
33-
}, maps);
30+
return Object.keys(maps).reduce((previousMaps, property) => {
31+
previousMaps[property] = resolveValue(maps[property], maps);
32+
return previousMaps;
33+
}, maps);
3434
}
3535

3636
export default postcss.plugin('postcss-at-rules-variables', (options = {}) => {
37-
options = {
38-
atRules: [...new Set(['for', 'if', 'else', 'each', 'mixin', 'custom-media', ...options.atRules || ''])],
39-
variables: options.variables || {}
40-
};
41-
42-
return nodes => {
43-
const maps = circularReference(Object.assign(getProperty(nodes), options.variables));
44-
45-
nodes.walkAtRules(new RegExp(options.atRules.join('|')), rules => {
46-
rules.params = resolveValue(rules.params, maps);
47-
});
48-
};
37+
options = {
38+
atRules: [...new Set(['for', 'if', 'else', 'each', 'mixin', 'custom-media', ...options.atRules || ''])],
39+
variables: options.variables || {}
40+
};
41+
42+
return nodes => {
43+
const maps = circularReference(Object.assign(getProperty(nodes), options.variables));
44+
45+
nodes.walkAtRules(new RegExp(options.atRules.join('|')), rules => {
46+
rules.params = resolveValue(rules.params, maps);
47+
});
48+
};
4949
});

test/test.js

+44-44
Original file line numberDiff line numberDiff line change
@@ -2,87 +2,87 @@ import postcss from 'postcss';
22
import test from 'ava';
33
import plugin from '../src';
44

5-
const processing = (input, opts) => {
6-
return postcss([plugin(opts)]).process(input).css;
5+
const processing = (input, options) => {
6+
return postcss([plugin(options)]).process(input).css;
77
};
88

99
test('it change circular reference', t => {
10-
const expected = ':root{ --from: 1; --to: var(--from)} @for $i from 1 to 1';
11-
const value = ':root{ --from: 1; --to: var(--from)} @for $i from var(--from) to var(--to)';
12-
t.is(processing(value), expected);
10+
const expected = ':root{ --from: 1; --to: var(--from)} @for $i from 1 to 1';
11+
const value = ':root{ --from: 1; --to: var(--from)} @for $i from var(--from) to var(--to)';
12+
t.is(processing(value), expected);
1313
});
1414

1515
test('it should not throw error if comment exists', t => {
16-
const expected = ':root{ --from: 1; /* comment */ }';
17-
const value = ':root{ --from: 1; /* comment */ }';
18-
t.is(processing(value), expected);
16+
const expected = ':root{ --from: 1; /* comment */ }';
17+
const value = ':root{ --from: 1; /* comment */ }';
18+
t.is(processing(value), expected);
1919
});
2020

2121
test('it should not throw error if comment exists with rule', t => {
22-
const expected = ':root{ --from: 1; /* comment */ } @for $i from 1 to 2';
23-
const value = ':root{ --from: 1; /* comment */ } @for $i from var(--from) to 2';
24-
t.is(processing(value), expected);
22+
const expected = ':root{ --from: 1; /* comment */ } @for $i from 1 to 2';
23+
const value = ':root{ --from: 1; /* comment */ } @for $i from var(--from) to 2';
24+
t.is(processing(value), expected);
2525
});
2626

2727
test('it change first properties for @for', t => {
28-
const expected = ':root{ --from: 1; } @for $i from 1 to 2';
29-
const value = ':root{ --from: 1; } @for $i from var(--from) to 2';
30-
t.is(processing(value), expected);
28+
const expected = ':root{ --from: 1; } @for $i from 1 to 2';
29+
const value = ':root{ --from: 1; } @for $i from var(--from) to 2';
30+
t.is(processing(value), expected);
3131
});
3232

3333
test('it change second properties for @for', t => {
34-
const expected = ':root{ --to: 2; } @for $i from 1 to 2';
35-
const value = ':root{ --to: 2; } @for $i from 1 to var(--to)';
36-
t.is(processing(value), expected);
34+
const expected = ':root{ --to: 2; } @for $i from 1 to 2';
35+
const value = ':root{ --to: 2; } @for $i from 1 to var(--to)';
36+
t.is(processing(value), expected);
3737
});
3838

3939
test('it change two properties for @for', t => {
40-
const expected = ':root{ --from: 1; --to: 2; } @for $i from 1 to 2';
41-
const value = ':root{ --from: 1; --to: 2; } @for $i from var(--from) to var(--to)';
42-
t.is(processing(value), expected);
40+
const expected = ':root{ --from: 1; --to: 2; } @for $i from 1 to 2';
41+
const value = ':root{ --from: 1; --to: 2; } @for $i from var(--from) to var(--to)';
42+
t.is(processing(value), expected);
4343
});
4444

4545
test('it change three properties for @for', t => {
46-
const expected = ':root{ --from: 1; --to: 2; --step: 5 } @for $i from 1 to 2 by 5';
47-
const value = ':root{ --from: 1; --to: 2; --step: 5 } @for $i from var(--from) to var(--to) by var(--step)';
48-
t.is(processing(value), expected);
46+
const expected = ':root{ --from: 1; --to: 2; --step: 5 } @for $i from 1 to 2 by 5';
47+
const value = ':root{ --from: 1; --to: 2; --step: 5 } @for $i from var(--from) to var(--to) by var(--step)';
48+
t.is(processing(value), expected);
4949
});
5050

5151
test('it change two properties for @if', t => {
52-
const expected = ':root{ --first: 1; --second: 2; } @if 1 < 2';
53-
const value = ':root{ --first: 1; --second: 2; } @if var(--first) < var(--second)';
54-
t.is(processing(value, {atRules: ['if']}), expected);
52+
const expected = ':root{ --first: 1; --second: 2; } @if 1 < 2';
53+
const value = ':root{ --first: 1; --second: 2; } @if var(--first) < var(--second)';
54+
t.is(processing(value, {atRules: ['if']}), expected);
5555
});
5656

5757
test('it change two properties for @if, @else if', t => {
58-
const expected = ':root{ --first: 1; --second: 2; } @if 1 < 2 { color: olive; } @else if 1 > 2 { color: red; }';
59-
const value = ':root{ --first: 1; --second: 2; } @if var(--first) < var(--second) { color: olive; } @else if var(--first) > var(--second) { color: red; }';
60-
t.is(processing(value, {atRules: ['if', 'else']}), expected);
58+
const expected = ':root{ --first: 1; --second: 2; } @if 1 < 2 { color: olive; } @else if 1 > 2 { color: red; }';
59+
const value = ':root{ --first: 1; --second: 2; } @if var(--first) < var(--second) { color: olive; } @else if var(--first) > var(--second) { color: red; }';
60+
t.is(processing(value, {atRules: ['if', 'else']}), expected);
6161
});
6262

6363
test('it change multi properties for @each', t => {
64-
const expected = ':root{ --array: foo, bar, baz; } @each $val in foo, bar, baz {} @for foo, bar, baz {}';
65-
const value = ':root{ --array: foo, bar, baz; } @each $val in var(--array) {} @for var(--array) {}';
66-
t.is(processing(value, {atRules: ['each']}), expected);
64+
const expected = ':root{ --array: foo, bar, baz; } @each $val in foo, bar, baz {} @for foo, bar, baz {}';
65+
const value = ':root{ --array: foo, bar, baz; } @each $val in var(--array) {} @for var(--array) {}';
66+
t.is(processing(value, {atRules: ['each']}), expected);
6767
});
6868

6969
test('it without variables', t => {
70-
const expected = ':root{ --red: red; } @if var(--green) { color: var(--green) }';
71-
const value = ':root{ --red: red; } @if var(--green) { color: var(--green) }';
72-
t.is(processing(value), expected);
70+
const expected = ':root{ --red: red; } @if var(--green) { color: var(--green) }';
71+
const value = ':root{ --red: red; } @if var(--green) { color: var(--green) }';
72+
t.is(processing(value), expected);
7373
});
7474

7575
test('chould change from options variables', t => {
76-
const expected = '@if green { .text-green { color: var(--green) }}';
77-
const value = '@if var(--green) { .text-green { color: var(--green) }}';
78-
const variables = {
79-
'--green': 'green'
80-
};
81-
t.is(processing(value, {variables: variables}), expected);
76+
const expected = '@if green { .text-green { color: var(--green) }}';
77+
const value = '@if var(--green) { .text-green { color: var(--green) }}';
78+
const variables = {
79+
'--green': 'green'
80+
};
81+
t.is(processing(value, {variables: variables}), expected);
8282
});
8383

8484
test('should change for @custom-media', t => {
85-
const expected = ':root{ --breakpoint-xs: 29.25em } @custom-media --viewport-xs (width > 29.25em)';
86-
const value = ':root{ --breakpoint-xs: 29.25em } @custom-media --viewport-xs (width > var(--breakpoint-xs))';
87-
t.is(processing(value, {atRules: ['custom-media']}), expected);
85+
const expected = ':root{ --breakpoint-xs: 29.25em } @custom-media --viewport-xs (width > 29.25em)';
86+
const value = ':root{ --breakpoint-xs: 29.25em } @custom-media --viewport-xs (width > var(--breakpoint-xs))';
87+
t.is(processing(value, {atRules: ['custom-media']}), expected);
8888
});

0 commit comments

Comments
 (0)