Support optional deref post-refactor

This commit is contained in:
jaina heartles 2022-12-14 01:36:58 -08:00
parent 54748b4c07
commit 25d6ee0245
2 changed files with 21 additions and 9 deletions

View file

@ -234,7 +234,7 @@ fn EvaluateExpression(
.isTag => bool,
.slice => |sl| []const std.meta.Elem(EvaluateExpression(sl.iterable, Args, Captures, Context)),
},
.optional_unwrap => |expr| std.meta.Child(EvaluateExpression(expr, Args, Captures, Context)),
.optional_unwrap => |expr| std.meta.Child(EvaluateExpression(expr.*, Args, Captures, Context)),
};
}
@ -279,7 +279,10 @@ fn evaluateExpression(
return iterable[start..end];
},
},
.optional_unwrap => unreachable,
.optional_unwrap => |expr| {
const val = try evaluateExpression(expr.*, args, captures, context);
return val orelse error.NullOptional;
},
};
}
@ -561,15 +564,24 @@ fn parseExpression(comptime tokens: ControlTokenIter) ParseResult(ControlTokenIt
if (expr == null) {
expr = .{ .args = {} };
if (iter.peek()) |n| if (n == .text) iter.putBack(.{ .period = {} });
} else {
const field = iter.next();
expectToken(field, .text);
expr = .{
} else switch (iter.next() orelse break) {
.text => |text| expr = .{
.deref = &.{
.container = expr.?,
.field = field.?.text,
.field = text,
},
};
},
.question_mark => expr = .{
.optional_unwrap = blk: {
const e = expr.?;
break :blk &e;
},
},
else => |t2| {
iter.pushBack(t2);
iter.pushBack(token);
break;
},
}
last_valid_iter = iter;
},

View file

@ -49,7 +49,7 @@
{#if .maybe_foo |$v|}{$v}{#else}null{/if}
{#if .maybe_bar |$v|}{$v}{#else}null{/if}
{#if .maybe_foo |$_|}abcd{#else}null{/if}
{{.maybe_foo.?}}
{.maybe_foo.?}
This causes an error: {{.maybe_bar.?}}
<template>{#template test_tmpl .bar}</template>