8000 error when property is used in root. Fixes #442 · nullstring/less.js@c139c6e · GitHub
[go: up one dir, main page]

Skip to content {"props":{"docsUrl":"https://docs.github.com/get-started/accessibility/keyboard-shortcuts"}}

Commit c139c6e

Browse files
committed
error when property is used in root. Fixes less#442
1 parent 7778da9 commit c139c6e

File tree

11 files changed

+44
-9
lines changed

11 files changed

+44
-9
lines changed

lib/less/parser.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ less.Parser = function Parser(env) {
363363
try {
364364
root = new(tree.Ruleset)([], $(this.parsers.primary));
365365
root.root = true;
366+
root.firstRoot = true;
366367
} catch (e) {
367368
return callback(new(LessError)(e, env));
368369
}
@@ -1182,7 +1183,7 @@ less.Parser = function Parser(env) {
11821183
important = $(this.important);
11831184

11841185
if (value && $(this.end)) {
1185-
return new(tree.Rule)(name, value, important, memo);
1186+
return new(tree.Rule)(name, value, important, memo, env.currentFileInfo);
11861187
} else {
11871188
furthest = i;
11881189
restore();
@@ -1265,7 +1266,7 @@ less.Parser = function Parser(env) {
12651266
e = $(this.value);
12661267
if ($(')')) {
12671268
if (p && e) {
1268-
nodes.push(new(tree.Paren)(new(tree.Rule)(p, e, null, i, true)));
1269+
nodes.push(new(tree.Paren)(new(tree.Rule)(p, e, null, i, env.currentFileInfo, true)));
12691270
} else if (e) {
12701271
nodes.push(new(tree.Paren)(e));
12711272
} else {

lib/less/tree/rule.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
(function (tree) {
22

3-
tree.Rule = function (name, value, important, index, inline) {
3+
tree.Rule = function (name, value, important, index, currentFileInfo, inline) {
44
this.name = name;
55
this.value = (value instanceof tree.Value) ? value : new(tree.Value)([value]);
66
this.important = important ? ' ' + important.trim() : '';
77
this.index = index;
8+
this.currentFileInfo = currentFileInfo;
89
this.inline = inline || false;
910

1011
if (name.charAt(0) === '@') {
@@ -35,7 +36,7 @@ tree.Rule.prototype = {
3536
return new(tree.Rule)(this.name,
3637
this.value.eval(env),
3738
this.important,
38-
this.index, this.inline);
39+
this.index, this.currentFileInfo, this.inline);
3940
}
4041
finally {
4142
if (strictMathsBypass) {
@@ -47,7 +48,7 @@ tree.Rule.prototype = {
4748
return new(tree.Rule)(this.name,
4849
this.value,
4950
"!important",
50-
this.index, this.inline);
51+
this.index, this.currentFileInfo, this.inline);
5152
}
5253
};
5354

lib/less/tree/ruleset.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ tree.Ruleset.prototype = {
1919

2020
ruleset.originalRuleset = 67F4 this;
2121
ruleset.root = this.root;
22+
ruleset.firstRoot = this.firstRoot;
2223
ruleset.allowImports = this.allowImports;
2324

2425
if(this.debugInfo) {
@@ -211,6 +212,10 @@ tree.Ruleset.prototype = {
211212
}
212213
} else {
213214
if (rule.toCSS && !rule.variable) {
215+
if (this.firstRoot && rule instanceof tree.Rule) {
216+
throw { message: "properties must be inside selector blocks, they cannot be in the root.",
217+
index: rule.index, filename: rule.currentFileInfo ? rule.currentFileInfo.filename : null};
218+
}
214219
rules.push(rule.toCSS(env));
215220
} else if (rule.value && !rule.variable) {
216221
rules.push(rule.value.toString());

test/css/css.css

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ div#id {
2626
color: grey;
2727
}
2828
@media print {
29-
font-size: 3em;
29+
* {
30+
font-size: 3em;
31+
}
3032
}
3133
@media screen {
32-
font-size: 10px;
34+
* {
35+
font-size: 10px;
36+
}
3337
}
3438
@font-face {
3539
font-family: 'Garamond Pro';

test/less/css.less

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,15 @@ div#id {
2727
}
2828

2929
@media print {
30-
font-size: 3em;
30+
* {
31+
font-size: 3em;
32+
}
3133
}
3234

3335
@media screen {
34-
font-size: 10px;
36+
* {
37+
font-size: 10px;
38+
}
3539
}
3640

3741
@font-face {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.a() {
2+
prop:1;
3+
}
4+
.a();

test/less/errors/property-in-root.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SyntaxError: properties must be inside selector blocks, they cannot be in the root. in {path}property-in-root.less on line 2, column 3:
2+
1 .a() {
3+
2 prop:1;
4+
3 }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "property-in-root";
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SyntaxError: properties must be inside selector blocks, they cannot be in the root. in C:\git\less.js\test\less\errors\property-in-root.less on line 2, column 3:
2+
1 .a() {
3+
2 prop:1;
4+
3 }
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
prop:1;
2+
.a {
3+
prop:1;
4+
}

0 commit comments

Comments
 (0)
0