Skip to content
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
3 changes: 3 additions & 0 deletions common/jinja/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,9 @@ class parser {
statement_ptr step = slices.size() > 2 ? std::move(slices[2]) : nullptr;
return mk_stmt<slice_expression>(start_pos, std::move(start), std::move(stop), std::move(step));
}
if (slices.empty()) {
return mk_stmt<blank_expression>(start_pos);
}
return std::move(slices[0]);
}

Expand Down
9 changes: 7 additions & 2 deletions common/jinja/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,10 +769,15 @@ value member_expression::execute_impl(context & ctx) {
}

JJ_DEBUG("Member expression on object type %s, property type %s", object->type().c_str(), property->type().c_str());
ensure_key_type_allowed(property);

value val = mk_val<value_undefined>("object_property");

if (property->is_undefined()) {
JJ_DEBUG("%s", "Member expression property is undefined, returning undefined");
return val;
}

ensure_key_type_allowed(property);

if (is_val<value_undefined>(object)) {
JJ_DEBUG("%s", "Accessing property on undefined object, returning undefined");
return val;
Expand Down
8 changes: 8 additions & 0 deletions common/jinja/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,14 @@ struct comment_statement : public statement {

// Expressions

// Represents an omitted expression in a computed member, e.g. `a[]`.
struct blank_expression : public expression {
std::string type() const override { return "BlankExpression"; }
value execute_impl(context &) override {
return mk_val<value_undefined>();
}
};

struct member_expression : public expression {
statement_ptr object;
statement_ptr property;
Expand Down
18 changes: 18 additions & 0 deletions tests/test-jinja.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,24 @@ static void test_expressions(testing & t) {
"Bob"
);

test_template(t, "empty computed member defaults to undefined",
"{{ a[]|default('fallback') }}",
{{"a", {{"name", "Bob"}}}},
"fallback"
);

test_template(t, "empty computed member is undefined",
"{{ a[] is undefined }}",
{{"a", {{"name", "Bob"}}}},
"True"
);

test_template(t, "undefined computed member is undefined",
"{{ a[undefined] is undefined }}",
{{"a", {{"name", "Bob"}}}},
"True"
);

test_template(t, "array access",
"{{ items[1] }}",
{{"items", json::array({"a", "b", "c"})}},
Expand Down
Loading