Skip to content

Commit e309bf6

Browse files
committed
Added the concept of inherited index / fileInfo for nodes
1 parent b39b36f commit e309bf6

31 files changed

+216
-150
lines changed

lib/less/environment/abstract-plugin-loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ AbstractPluginLoader.prototype.evalPlugin = function(contents, context, pluginOp
5252
registry = functionRegistry.create();
5353

5454
try {
55-
loader = new Function("module", "require", "functions", "tree", "fileInfo", "less", contents);
56-
pluginObj = loader(localModule, this.require, registry, this.less.tree, fileInfo, this.less);
55+
loader = new Function("module", "require", "functions", "tree", "fileInfo", contents);
56+
pluginObj = loader(localModule, this.require, registry, this.less.tree, fileInfo);
5757

5858
if (!pluginObj) {
5959
pluginObj = localModule.exports;

lib/less/functions/function-caller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ var functionCaller = function(name, context, index, currentFileInfo) {
1111
functionCaller.prototype.isValid = function() {
1212
return Boolean(this.func);
1313
};
14-
functionCaller.prototype.call = function(args) {
1514

15+
functionCaller.prototype.call = function(args) {
1616
// This code is terrible and should be replaced as per this issue...
1717
// https://github.com/less/less.js/issues/2477
1818
if (Array.isArray(args)) {

lib/less/tree/anonymous.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ var Node = require("./node");
22

33
var Anonymous = function (value, index, currentFileInfo, mapLines, rulesetLike, visibilityInfo) {
44
this.value = value;
5-
this.index = index;
5+
this._index = index;
6+
this._fileInfo = currentFileInfo;
67
this.mapLines = mapLines;
7-
this.currentFileInfo = currentFileInfo;
88
this.rulesetLike = (typeof rulesetLike === 'undefined') ? false : rulesetLike;
99
this.allowRoot = true;
1010
this.copyVisibilityInfo(visibilityInfo);
1111
};
1212
Anonymous.prototype = new Node();
1313
Anonymous.prototype.type = "Anonymous";
1414
Anonymous.prototype.eval = function () {
15-
return new Anonymous(this.value, this.index, this.currentFileInfo, this.mapLines, this.rulesetLike, this.visibilityInfo());
15+
return new Anonymous(this.value, this._index, this._fileInfo, this.mapLines, this.rulesetLike, this.visibilityInfo());
1616
};
1717
Anonymous.prototype.compare = function (other) {
1818
return other.toCSS && this.toCSS() === other.toCSS() ? 0 : undefined;
@@ -21,6 +21,6 @@ Anonymous.prototype.isRulesetLike = function() {
2121
return this.rulesetLike;
2222
};
2323
Anonymous.prototype.genCSS = function (context, output) {
24-
output.add(this.value, this.currentFileInfo, this.index, this.mapLines);
24+
output.add(this.value, this._fileInfo, this._index, this.mapLines);
2525
};
2626
module.exports = Anonymous;

lib/less/tree/atrule.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ var AtRule = function (name, value, rules, index, currentFileInfo, debugInfo, is
1212
this.rules = rules;
1313
} else {
1414
this.rules = [rules];
15-
this.rules[0].selectors = (new Selector([], null, null, this.index, currentFileInfo)).createEmptySelectors();
15+
this.rules[0].selectors = (new Selector([], null, null, index, currentFileInfo)).createEmptySelectors();
1616
}
1717
for (i = 0; i < this.rules.length; i++) {
1818
this.rules[i].allowImports = true;
1919
}
20+
this.setParent(this.rules, this);
2021
}
21-
this.index = index;
22-
this.currentFileInfo = currentFileInfo;
22+
this._index = index;
23+
this._fileInfo = currentFileInfo;
2324
this.debugInfo = debugInfo;
2425
this.isRooted = isRooted || false;
2526
this.copyVisibilityInfo(visibilityInfo);
@@ -45,7 +46,7 @@ AtRule.prototype.isCharset = function() {
4546
};
4647
AtRule.prototype.genCSS = function (context, output) {
4748
var value = this.value, rules = this.rules;
48-
output.add(this.name, this.currentFileInfo, this.index);
49+
output.add(this.name, this.fileInfo(), this.getIndex());
4950
if (value) {
5051
output.add(' ');
5152
value.genCSS(context, output);
@@ -80,7 +81,7 @@ AtRule.prototype.eval = function (context) {
8081
context.mediaBlocks = mediaBlocksBackup;
8182

8283
return new AtRule(this.name, value, rules,
83-
this.index, this.currentFileInfo, this.debugInfo, this.isRooted, this.visibilityInfo());
84+
this.getIndex(), this.fileInfo(), this.debugInfo, this.isRooted, this.visibilityInfo());
8485
};
8586
AtRule.prototype.variable = function (name) {
8687
if (this.rules) {

lib/less/tree/call.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ var Node = require("./node"),
66
var Call = function (name, args, index, currentFileInfo) {
77
this.name = name;
88
this.args = args;
9-
this.index = index;
10-
this.currentFileInfo = currentFileInfo;
9+
this._index = index;
10+
this._fileInfo = currentFileInfo;
1111
};
1212
Call.prototype = new Node();
1313
Call.prototype.type = "Call";
@@ -29,8 +29,8 @@ Call.prototype.accept = function (visitor) {
2929
//
3030
Call.prototype.eval = function (context) {
3131
var args = this.args.map(function (a) { return a.eval(context); }),
32-
result, funcCaller = new FunctionCaller(this.name, context, this.index, this.currentFileInfo);
33-
32+
result, funcCaller = new FunctionCaller(this.name, context, this.getIndex(), this.fileInfo());
33+
3434
if (funcCaller.isValid()) {
3535
try {
3636
result = funcCaller.call(args);
@@ -39,24 +39,24 @@ Call.prototype.eval = function (context) {
3939
type: e.type || "Runtime",
4040
message: "error evaluating function `" + this.name + "`" +
4141
(e.message ? ': ' + e.message : ''),
42-
index: this.index,
43-
filename: this.currentFileInfo.filename,
42+
index: this.getIndex(),
43+
filename: this.fileInfo().filename,
4444
line: e.lineNumber,
4545
column: e.columnNumber
4646
};
4747
}
4848

4949
if (result != null) {
50-
result.index = this.index;
51-
result.currentFileInfo = this.currentFileInfo;
50+
result._index = this._index;
51+
result._fileInfo = this._fileInfo;
5252
return result;
5353
}
5454
}
5555

56-
return new Call(this.name, args, this.index, this.currentFileInfo);
56+
return new Call(this.name, args, this.getIndex(), this.fileInfo());
5757
};
5858
Call.prototype.genCSS = function (context, output) {
59-
output.add(this.name + "(", this.currentFileInfo, this.index);
59+
output.add(this.name + "(", this.fileInfo(), this.getIndex());
6060

6161
for (var i = 0; i < this.args.length; i++) {
6262
this.args[i].genCSS(context, output);

lib/less/tree/color.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Color.prototype.toCSS = function (context, doNotCompress) {
9999
// we create a new Color node to hold the result.
100100
//
101101
Color.prototype.operate = function (context, op, other) {
102-
var rgb = [];
102+
var rgb = new Array(3);
103103
var alpha = this.alpha * (1 - other.alpha) + other.alpha;
104104
for (var c = 0; c < 3; c++) {
105105
rgb[c] = this._operate(context, op, this.rgb[c], other.rgb[c]);

lib/less/tree/comment.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ var Node = require("./node"),
44
var Comment = function (value, isLineComment, index, currentFileInfo) {
55
this.value = value;
66
this.isLineComment = isLineComment;
7-
this.index = index;
8-
this.currentFileInfo = currentFileInfo;
7+
this._index = index;
8+
this._fileInfo = currentFileInfo;
99
this.allowRoot = true;
1010
};
1111
Comment.prototype = new Node();
1212
Comment.prototype.type = "Comment";
1313
Comment.prototype.genCSS = function (context, output) {
1414
if (this.debugInfo) {
15-
output.add(getDebugInfo(context, this), this.currentFileInfo, this.index);
15+
output.add(getDebugInfo(context, this), this.fileInfo(), this.getIndex());
1616
}
1717
output.add(this.value);
1818
};

lib/less/tree/condition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var Condition = function (op, l, r, i, negate) {
44
this.op = op.trim();
55
this.lvalue = l;
66
this.rvalue = r;
7-
this.index = i;
7+
this._index = i;
88
this.negate = negate;
99
};
1010
Condition.prototype = new Node();

lib/less/tree/declaration.js

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ var Declaration = function (name, value, important, merge, index, currentFileInf
77
this.value = (value instanceof Node) ? value : new Value([value]); //value instanceof tree.Value || value instanceof tree.Ruleset ??
88
this.important = important ? ' ' + important.trim() : '';
99
this.merge = merge;
10-
this.index = index;
11-
this.currentFileInfo = currentFileInfo;
10+
this._index = index;
11+
this._fileInfo = currentFileInfo;
1212
this.inline = inline || false;
1313
this.variable = (variable !== undefined) ? variable
1414
: (name.charAt && (name.charAt(0) === '@'));
1515
this.allowRoot = true;
16+
this.setParent(this.value, this);
1617
};
1718

1819
function evalName(context, name) {
@@ -27,16 +28,16 @@ function evalName(context, name) {
2728
Declaration.prototype = new Node();
2829
Declaration.prototype.type = "Declaration";
2930
Declaration.prototype.genCSS = function (context, output) {
30-
output.add(this.name + (context.compress ? ':' : ': '), this.currentFileInfo, this.index);
31+
output.add(this.name + (context.compress ? ':' : ': '), this.fileInfo(), this.getIndex());
3132
try {
3233
this.value.genCSS(context, output);
3334
}
3435
catch(e) {
35-
e.index = this.index;
36-
e.filename = this.currentFileInfo.filename;
36+
e.index = this._index;
37+
e.filename = this._fileInfo.filename;
3738
throw e;
3839
}
39-
output.add(this.important + ((this.inline || (context.lastRule && context.compress)) ? "" : ";"), this.currentFileInfo, this.index);
40+
output.add(this.important + ((this.inline || (context.lastRule && context.compress)) ? "" : ";"), this._fileInfo, this._index);
4041
};
4142
Declaration.prototype.eval = function (context) {
4243
var strictMathBypass = false, name = this.name, evaldValue, variable = this.variable;
@@ -57,7 +58,7 @@ Declaration.prototype.eval = function (context) {
5758

5859
if (!this.variable && evaldValue.type === "DetachedRuleset") {
5960
throw { message: "Rulesets cannot be evaluated on a property.",
60-
index: this.index, filename: this.currentFileInfo.filename };
61+
index: this.getIndex(), filename: this.fileInfo().filename };
6162
}
6263
var important = this.important,
6364
importantResult = context.importantScope.pop();
@@ -69,13 +70,13 @@ Declaration.prototype.eval = function (context) {
6970
evaldValue,
7071
important,
7172
this.merge,
72-
this.index, this.currentFileInfo, this.inline,
73+
this.getIndex(), this.fileInfo(), this.inline,
7374
variable);
7475
}
7576
catch(e) {
7677
if (typeof e.index !== 'number') {
77-
e.index = this.index;
78-
e.filename = this.currentFileInfo.filename;
78+
e.index = this.getIndex();
79+
e.filename = this.fileInfo().filename;
7980
}
8081
throw e;
8182
}
@@ -90,7 +91,7 @@ Declaration.prototype.makeImportant = function () {
9091
this.value,
9192
"!important",
9293
this.merge,
93-
this.index, this.currentFileInfo, this.inline);
94+
this.getIndex(), this.fileInfo(), this.inline);
9495
};
9596

9697
module.exports = Declaration;

lib/less/tree/detached-ruleset.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
var Node = require("./node"),
2-
contexts = require("../contexts");
2+
contexts = require("../contexts"),
3+
utils = require("../utils");
34

45
var DetachedRuleset = function (ruleset, frames) {
56
this.ruleset = ruleset;
67
this.frames = frames;
8+
this.setParent(this.ruleset, this);
79
};
810
DetachedRuleset.prototype = new Node();
911
DetachedRuleset.prototype.type = "DetachedRuleset";
@@ -12,7 +14,7 @@ DetachedRuleset.prototype.accept = function (visitor) {
1214
this.ruleset = visitor.visit(this.ruleset);
1315
};
1416
DetachedRuleset.prototype.eval = function (context) {
15-
var frames = this.frames || context.frames.slice(0);
17+
var frames = this.frames || utils.copyArray(context.frames);
1618
return new DetachedRuleset(this.ruleset, frames);
1719
};
1820
DetachedRuleset.prototype.callEval = function (context) {

0 commit comments

Comments
 (0)