Skip to content

Add ellipsis support when calling a mixin #1857

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions lib/less/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,7 +1122,7 @@ less.Parser = function Parser(env) {
var parsers = parser.parsers, entities = parsers.entities,
returner = { args:null, variadic: false },
expressions = [], argsSemiColon = [], argsComma = [],
isSemiColonSeperated, expressionContainsNamed, name, nameLoop, value, arg;
isSemiColonSeperated, expressionContainsNamed, name, nameLoop, value, arg, expands;

while (true) {
if (isCall) {
Expand Down Expand Up @@ -1171,14 +1171,20 @@ less.Parser = function Parser(env) {
}
value = expect(parsers.expression);
nameLoop = (name = val.name);
} else if (!isCall && $re(/^\.{3}/)) {
returner.variadic = true;
if ($char(";") && !isSemiColonSeperated) {
isSemiColonSeperated = true;
} else if ($re(/^\.{3}/)) {
if (isCall) {
name = nameLoop = val.name;
expands = true;
} else {
returner.variadic = true;
if ($char(";") && !isSemiColonSeperated) {
isSemiColonSeperated = true;
}
(isSemiColonSeperated ? argsSemiColon : argsComma)
.push({ name: arg.name, variadic: true });
break;
}
(isSemiColonSeperated ? argsSemiColon : argsComma)
.push({ name: arg.name, variadic: true });
break;

} else if (!isCall) {
name = nameLoop = val.name;
value = null;
Expand All @@ -1189,7 +1195,7 @@ less.Parser = function Parser(env) {
expressions.push(value);
}

argsComma.push({ name:nameLoop, value:value });
argsComma.push({ name:nameLoop, value:value, expands:expands });

if ($char(',')) {
continue;
Expand All @@ -1206,9 +1212,10 @@ less.Parser = function Parser(env) {
if (expressions.length > 1) {
value = new(tree.Value)(expressions);
}
argsSemiColon.push({ name:name, value:value });
argsSemiColon.push({ name:name, value:value, expands:expands });

name = null;
expands = null;
expressions = [];
expressionContainsNamed = false;
}
Expand Down
27 changes: 22 additions & 5 deletions lib/less/tree/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,30 @@ tree.mixin.Call.prototype = {
}
},
eval: function (env) {
var mixins, mixin, args, rules = [], match = false, i, m, f, isRecursive, isOneFound, rule,
var mixins, mixin, args = [], rules = [], match = false, i, m, f, isRecursive, isOneFound, rule,
candidates = [], candidate, conditionResult = [], defaultFunc = tree.defaultFunc,
defaultResult, defNone = 0, defTrue = 1, defFalse = 2, count;
defaultResult, defNone = 0, defTrue = 1, defFalse = 2, count, arg, argValue;

args = this.arguments && this.arguments.map(function (a) {
return { name: a.name, value: a.value.eval(env) };
});
this.arguments = this.arguments || [];

// To call a mixin and "expand" a variable containing a list into multiple arguments we loop
// over `expanded` variables and add each entry in the list.
// Example: `.mixin(@rest...);`
for (i = 0; i < this.arguments.length; i++) {
arg = this.arguments[i];
argValue = arg.value.eval(env);
if (arg.expands) {
if (argValue.value instanceof Array) {
for (m = 0; m < argValue.value.length; m++) {
args.push({value: argValue.value[m]});
}
} else {
args.push({ value: argValue});
}
} else {
args.push({ name: arg.name, value: argValue });
}
}

for (i = 0; i < env.frames.length; i++) {
if ((mixins = env.frames[i].find(this.selector)).length > 0) {
Expand Down
17 changes: 16 additions & 1 deletion test/less/mixins-args.less
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,19 @@ body {

div {
.test-calling-one-arg-mixin(1);
}
}

.test-calling-with-ellipsis-variable {
.mixin-multi-arg(1; 2; 3; 4) { }

@all: 1, 2, 3, 4;
.mixin-multi-arg(@all...);

@first: 1, 2;
@last: 3, 4;
.mixin-multi-arg(@first...; @last...);

@single: 1;
.mixin-multi-arg(@single...; 2; 3; 4);
.mixin-multi-arg(@single...; 2; @last...);
}