Skip to content
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

Add handling of boolean literals to condition evaluation #91

Merged
merged 2 commits into from
Jul 27, 2024
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
13 changes: 9 additions & 4 deletions rinja_derive/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ impl<'a> Generator<'a> {
only_contains_is_defined: &mut bool,
) -> EvaluatedResult {
match **expr {
Expr::BoolLit(_)
| Expr::NumLit(_)
Expr::NumLit(_)
| Expr::StrLit(_)
| Expr::CharLit(_)
| Expr::Var(_)
Expand All @@ -392,6 +391,8 @@ impl<'a> Generator<'a> {
*only_contains_is_defined = false;
EvaluatedResult::Unknown
}
Expr::BoolLit(true) => EvaluatedResult::AlwaysTrue,
Expr::BoolLit(false) => EvaluatedResult::AlwaysFalse,
Expr::Unary("!", ref inner) => {
match self.evaluate_condition(inner, only_contains_is_defined) {
EvaluatedResult::AlwaysTrue => EvaluatedResult::AlwaysFalse,
Expand Down Expand Up @@ -1888,8 +1889,12 @@ impl<'a> Generator<'a> {
DisplayWrap::Wrapped
}

fn visit_bool_lit(&mut self, buf: &mut Buffer, s: &str) -> DisplayWrap {
buf.write(s);
fn visit_bool_lit(&mut self, buf: &mut Buffer, s: bool) -> DisplayWrap {
if s {
buf.write("true");
} else {
buf.write("false");
}
DisplayWrap::Unwrapped
}

Expand Down
92 changes: 92 additions & 0 deletions rinja_derive/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,95 @@ writer.write_str("12")?;
3,
);
}

#[test]
fn check_bool_conditions() {
// Checks that it removes conditions if we know at compile-time that they always return false.
//
// We're forced to add `bla` otherwise `compare` assert fails in weird ways...
compare(
"{% if false %}{{query}}{% endif %}bla",
r#"writer.write_str("bla")?;"#,
&[],
3,
);
compare(
"{% if false && false %}{{query}}{% endif %}bla",
r#"writer.write_str("bla")?;"#,
&[],
3,
);
compare(
"{% if false && true %}{{query}}{% endif %}bla",
r#"writer.write_str("bla")?;"#,
&[],
3,
);
compare(
"{% if true && false %}{{query}}{% endif %}bla",
r#"writer.write_str("bla")?;"#,
&[],
3,
);
compare(
"{% if false || true %}bli{% endif %}bla",
r#"writer.write_str("blibla")?;"#,
&[],
6,
);
compare(
"{% if true || false %}bli{% endif %}bla",
r#"writer.write_str("blibla")?;"#,
&[],
6,
);

compare(
"{% if true || x == 12 %}{{x}}{% endif %}",
r#"if *(&(true || self.x == 12) as &bool) {
match (
&((&&::rinja::filters::AutoEscaper::new(&(self.x), ::rinja::filters::Text)).rinja_auto_escape()?),
) {
(expr0,) => {
(&&::rinja::filters::Writable(expr0)).rinja_write(writer)?;
}
}
}
"#,
&[("x", "u32")],
3,
);
compare(
"{% if false || x == 12 %}{{x}}{% endif %}",
r#"if *(&(false || self.x == 12) as &bool) {
match (
&((&&::rinja::filters::AutoEscaper::new(
&(self.x),
::rinja::filters::Text,
))
.rinja_auto_escape()?),
) {
(expr0,) => {
(&&::rinja::filters::Writable(expr0)).rinja_write(writer)?;
}
}
}
"#,
&[("x", "u32")],
3,
);

// Some funny cases.
compare(
"{% if !(false) %}bla{% endif %}",
r#"writer.write_str("bla")?;"#,
&[],
3,
);
compare(
"{% if !(true) %}{{query}}{% endif %}bla",
r#"writer.write_str("bla")?;"#,
&[],
3,
);
}
6 changes: 3 additions & 3 deletions rinja_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ macro_rules! expr_prec_layer {

#[derive(Clone, Debug, PartialEq)]
pub enum Expr<'a> {
BoolLit(&'a str),
BoolLit(bool),
NumLit(&'a str),
StrLit(&'a str),
CharLit(&'a str),
Expand Down Expand Up @@ -334,8 +334,8 @@ impl<'a> Expr<'a> {
let start = i;
map(path_or_identifier, |v| match v {
PathOrIdentifier::Path(v) => Self::Path(v),
PathOrIdentifier::Identifier(v @ "true") => Self::BoolLit(v),
PathOrIdentifier::Identifier(v @ "false") => Self::BoolLit(v),
PathOrIdentifier::Identifier("true") => Self::BoolLit(true),
PathOrIdentifier::Identifier("false") => Self::BoolLit(false),
PathOrIdentifier::Identifier(v) => Self::Var(v),
})(i)
.map(|(i, expr)| (i, WithSpan::new(expr, start)))
Expand Down