Skip to content

Commit 4e463b4

Browse files
committed
Merge pull request #1467 from mgreter/bugfix/arglist-in-each
Fix argument list handling for `each` function
2 parents 9387623 + 0334212 commit 4e463b4

File tree

2 files changed

+50
-29
lines changed

2 files changed

+50
-29
lines changed

src/eval.cpp

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -258,25 +258,35 @@ namespace Sass {
258258
}
259259
}
260260
else {
261+
bool arglist = list->is_arglist();
261262
for (size_t i = 0, L = list->length(); i < L; ++i) {
262-
List* variable = 0;
263-
if ((*list)[i]->concrete_type() != Expression::LIST || variables.size() == 1) {
264-
variable = new (ctx.mem) List((*list)[i]->pstate(), 1, SASS_COMMA);
265-
*variable << (*list)[i];
266-
}
267-
else {
268-
variable = static_cast<List*>((*list)[i]);
269-
}
270-
for (size_t j = 0, K = variables.size(); j < K; ++j) {
271-
if (j < variable->length()) {
272-
env->set_local(variables[j], (*variable)[j]);
263+
Expression* e = (*list)[i];
264+
// unwrap value if the expression is an argument
265+
if (Argument* arg = dynamic_cast<Argument*>(e)) e = arg->value();
266+
// check if we got passed a list of args (investigate)
267+
if (List* scalars = dynamic_cast<List*>(e)) {
268+
if (variables.size() == 1) {
269+
Expression* var = scalars;
270+
if (arglist) var = (*scalars)[0];
271+
env->set_local(variables[0], var);
272+
} else {
273+
for (size_t j = 0, K = variables.size(); j < K; ++j) {
274+
Expression* res = j >= scalars->length()
275+
? new (ctx.mem) Null(expr->pstate())
276+
: (*scalars)[j];
277+
env->set_local(variables[j], res);
278+
}
273279
}
274-
else {
275-
env->set_local(variables[j], new (ctx.mem) Null(expr->pstate()));
280+
} else {
281+
if (variables.size() > 0) {
282+
env->set_local(variables[0], e);
283+
for (size_t j = 1, K = variables.size(); j < K; ++j) {
284+
Expression* res = new (ctx.mem) Null(expr->pstate());
285+
env->set_local(variables[j], res);
286+
}
276287
}
277-
val = body->perform(this);
278-
if (val) break;
279288
}
289+
val = body->perform(this);
280290
if (val) break;
281291
}
282292
}
@@ -395,7 +405,7 @@ namespace Sass {
395405
std::string cwd(ctx.get_cwd());
396406
std::string result(unquote(message->perform(&to_string)));
397407
std::string rel_path(Sass::File::resolve_relative_path(d->pstate().path, cwd, cwd));
398-
std::cerr << rel_path << ":" << d->pstate().line << ":" << " DEBUG: " << result;
408+
std::cerr << rel_path << ":" << d->pstate().line+1 << " DEBUG: " << result;
399409
std::cerr << std::endl;
400410
return 0;
401411
}

src/expand.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -433,21 +433,32 @@ namespace Sass {
433433
}
434434
}
435435
else {
436+
bool arglist = list->is_arglist();
436437
for (size_t i = 0, L = list->length(); i < L; ++i) {
437-
List* variable = 0;
438-
if ((*list)[i]->concrete_type() != Expression::LIST || variables.size() == 1) {
439-
variable = new (ctx.mem) List((*list)[i]->pstate(), 1, SASS_COMMA);
440-
*variable << (*list)[i];
441-
}
442-
else {
443-
variable = static_cast<List*>((*list)[i]);
444-
}
445-
for (size_t j = 0, K = variables.size(); j < K; ++j) {
446-
if (j < variable->length()) {
447-
env->set_local(variables[j], (*variable)[j]->perform(&eval));
438+
Expression* e = (*list)[i];
439+
// unwrap value if the expression is an argument
440+
if (Argument* arg = dynamic_cast<Argument*>(e)) e = arg->value();
441+
// check if we got passed a list of args (investigate)
442+
if (List* scalars = dynamic_cast<List*>(e)) {
443+
if (variables.size() == 1) {
444+
Expression* var = scalars;
445+
if (arglist) var = (*scalars)[0];
446+
env->set_local(variables[0], var);
447+
} else {
448+
for (size_t j = 0, K = variables.size(); j < K; ++j) {
449+
Expression* res = j >= scalars->length()
450+
? new (ctx.mem) Null(expr->pstate())
451+
: (*scalars)[j]->perform(&eval);
452+
env->set_local(variables[j], res);
453+
}
448454
}
449-
else {
450-
env->set_local(variables[j], new (ctx.mem) Null(expr->pstate()));
455+
} else {
456+
if (variables.size() > 0) {
457+
env->set_local(variables[0], e);
458+
for (size_t j = 1, K = variables.size(); j < K; ++j) {
459+
Expression* res = new (ctx.mem) Null(expr->pstate());
460+
env->set_local(variables[j], res);
461+
}
451462
}
452463
}
453464
append_block(body);

0 commit comments

Comments
 (0)