Skip to content

Commit 5363729

Browse files
committed
[INTERNAL] Variables: improved tests and coverage to be 100%
1 parent 1f41cc1 commit 5363729

File tree

6 files changed

+112
-0
lines changed

6 files changed

+112
-0
lines changed

lib/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,8 @@ Builder.prototype.build = function(options) {
193193
const libName = config.libName = options.library.name;
194194
config.libPath = libName.replace(/\./g, "/");
195195
config.prefix = "_" + libName.replace(/\./g, "_") + "_";
196+
} else {
197+
config.prefix = ""; // no library, no prefix
196198
}
197199

198200
// Keep filename for later usage (see ImportCollectorPlugin) as less modifies the parserOptions.filename

lib/plugin/css-variables-collector.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ CSSVariablesCollectorPlugin.prototype = {
107107

108108
visitOperation(node, visitArgs) {
109109
if (this._isRelevant()) {
110+
// console.log("visitOperation", this.ruleStack[this.ruleStack.length - 1], this._getCSS(node));
110111
return new less.tree.Call("calc", [new less.tree.Expression([node.operands[0], new less.tree.Anonymous(node.op), node.operands[1]])]);
111112
}
112113
return node;
@@ -116,6 +117,7 @@ CSSVariablesCollectorPlugin.prototype = {
116117
// if variables are used inside rules, generate a new calculated variable for it!
117118
const isRelevantFunction = typeof less.tree.functions[node.name] === "function" && ["rgba"].indexOf(node.name) === -1;
118119
if (this._isRelevant() && isRelevantFunction) {
120+
// console.log("visitCall", this.ruleStack[this.ruleStack.length - 1], this._getCSS(node));
119121
const css = this._getCSS(node);
120122
let newName = this.config.prefix + "function_" + node.name + Object.keys(this.vars).length;
121123
// check for duplicate value in vars already
@@ -139,6 +141,7 @@ CSSVariablesCollectorPlugin.prototype = {
139141
visitNegative(node, visitArgs) {
140142
// convert negative into calc function
141143
if (this._isRelevant()) {
144+
console.log("visitNegative", this.ruleStack[this.ruleStack.length - 1], this._getCSS(node));
142145
return new less.tree.Call("calc", [new less.tree.Expression([new less.tree.Anonymous("-1"), new less.tree.Anonymous("*"), node.value])]);
143146
}
144147
return node;

test/expected/complex/test-cssvars-skeleton.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,28 @@ a:hover {
1212
.lazy-eval {
1313
width: var(--var);
1414
}
15+
.nested .calc-vars {
16+
height: var(--c);
17+
width: var(--d);
18+
color: var(--function_lighten12);
19+
top: calc(-1 * var(--top));
20+
line-height: calc(var(--lineHeight) / 2);
21+
background: #428bca;
22+
border-color: #92bce0;
23+
background: #5697d0;
24+
}
25+
.nested .calc-vars-duplicate {
26+
color: var(--function_lighten12);
27+
padding-left: calc(20px + 0.5rem);
28+
border-color: #ffffff;
29+
background: #ffffff;
30+
}
31+
.nested .calc-vars-duplicate somePrefix.somePadding {
32+
padding: 1rem;
33+
box-sizing: border-box;
34+
}
35+
@media (max-width: 599px) {
36+
.nested .calc-vars-duplicate somePrefix.somePadding {
37+
padding: 0;
38+
}
39+
}

test/expected/complex/test-cssvars-variables.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,11 @@
55
--link-color-active: var(--link-color);
66
--a: 100%;
77
--var: var(--a);
8+
--b: #245682;
9+
--padLeft: 20px;
10+
--top: 5rem;
11+
--lineHeight: 2rem;
12+
--c: calc(20%);
13+
--d: -100%;
14+
--function_lighten12: #3071a9;
815
}

test/expected/complex/test.css

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,28 @@ a:hover {
1212
.lazy-eval {
1313
width: 9%;
1414
}
15+
.nested .calc-vars {
16+
height: calc(20%);
17+
width: -100%;
18+
color: #3071a9;
19+
top: -5rem;
20+
line-height: 1rem;
21+
background: #428bca;
22+
border-color: #92bce0;
23+
background: #5697d0;
24+
}
25+
.nested .calc-vars-duplicate {
26+
color: #3071a9;
27+
padding-left: calc(20px + 0.5rem);
28+
border-color: #ffffff;
29+
background: #ffffff;
30+
}
31+
.nested .calc-vars-duplicate somePrefix.somePadding {
32+
padding: 1rem;
33+
box-sizing: border-box;
34+
}
35+
@media (max-width: 599px) {
36+
.nested .calc-vars-duplicate somePrefix.somePadding {
37+
padding: 0;
38+
}
39+
}

test/fixtures/complex/test.less

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,53 @@ a:hover {
2525

2626
@var: @a;
2727
@a: 100%;
28+
29+
.nested {
30+
31+
@b: darken(@link-color-active, 20%);
32+
@padLeft: 20px;
33+
@top: 5rem;
34+
@lineHeight: 2rem;
35+
36+
.my-mixin(@bgColor: #fff) {
37+
background: @bgColor;
38+
border-color: lighten(@bgColor, 20%);
39+
& when not (@bgColor = #000) {
40+
background: lighten(@bgColor, 5%);
41+
}
42+
}
43+
44+
45+
.other-mixin(@rootClass) {
46+
@{rootClass}.somePadding {
47+
padding: 1rem;
48+
box-sizing: border-box;
49+
}
50+
51+
@media (max-width:599px) {
52+
@{rootClass}.somePadding {
53+
padding: 0;
54+
}
55+
}
56+
}
57+
58+
.calc-vars {
59+
@c: calc(100% - 80px);
60+
@d: -@a;
61+
height: @c;
62+
width: @d;
63+
color: lighten(@b, 10%);
64+
top: -@top;
65+
line-height: @lineHeight / 2;
66+
.my-mixin(@link-color)
67+
}
68+
69+
.calc-vars-duplicate {
70+
color: lighten(@b, 10%);
71+
padding-left: ~"calc(@{padLeft} + 0.5rem)";
72+
.my-mixin(#fff);
73+
.other-mixin(somePrefix);
74+
}
75+
76+
}
77+

0 commit comments

Comments
 (0)