diff --git a/src/parse/control_flow.rs b/src/parse/control_flow.rs index d61320d..308a764 100644 --- a/src/parse/control_flow.rs +++ b/src/parse/control_flow.rs @@ -32,6 +32,7 @@ impl<'a> Parser<'a> { self.whitespace_or_comment(); if init_cond.is_true() { + self.scopes.enter_new_scope(); found_true = true; body = Parser { toks: self.toks, @@ -52,6 +53,7 @@ impl<'a> Parser<'a> { module_config: self.module_config, } .parse_stmt()?; + self.scopes.exit_scope(); } else { self.throw_away_until_closing_curly_brace()?; } @@ -89,6 +91,7 @@ impl<'a> Parser<'a> { }; if cond { found_true = true; + self.scopes.enter_new_scope(); body = Parser { toks: self.toks, map: self.map, @@ -108,6 +111,7 @@ impl<'a> Parser<'a> { module_config: self.module_config, } .parse_stmt()?; + self.scopes.exit_scope(); } else { self.throw_away_until_closing_curly_brace()?; } @@ -119,7 +123,8 @@ impl<'a> Parser<'a> { self.throw_away_until_closing_curly_brace()?; break; } else { - return Parser { + self.scopes.enter_new_scope(); + let tmp = Parser { toks: self.toks, map: self.map, path: self.path, @@ -138,6 +143,8 @@ impl<'a> Parser<'a> { module_config: self.module_config, } .parse_stmt(); + self.scopes.exit_scope(); + return tmp; } } _ => { diff --git a/src/parse/variable.rs b/src/parse/variable.rs index 1656db5..9e12aed 100644 --- a/src/parse/variable.rs +++ b/src/parse/variable.rs @@ -55,25 +55,35 @@ impl<'a> Parser<'a> { } self.parse_value_from_vec(val_toks, true)?.node - } else if !self.at_root { + } else if self.at_root { + self.parse_value_from_vec(val_toks, true)?.node + } else { if self.scopes.default_var_exists(ident) { return Ok(()); } - self.parse_value_from_vec(val_toks, true)?.node - } else { self.parse_value_from_vec(val_toks, true)?.node }; - if self.at_root && !self.flags.in_control_flow() { - self.global_scope.insert_var(ident, value); - return Ok(()); + if self.at_root && self.global_scope.var_exists(ident) { + if !self.global_scope.default_var_exists(ident) { + self.global_scope.insert_var(ident, value.clone()); + } + } else if self.at_root + && !self.flags.in_control_flow() + && !self.global_scope.default_var_exists(ident) + { + self.global_scope.insert_var(ident, value.clone()); } if global { self.global_scope.insert_var(ident, value.clone()); } + if self.at_root && !self.flags.in_control_flow() { + return Ok(()); + } + self.scopes.insert_var(ident, value); return Ok(()); diff --git a/tests/variables.rs b/tests/variables.rs index f5be38f..8a779b5 100644 --- a/tests/variables.rs +++ b/tests/variables.rs @@ -156,7 +156,7 @@ test!( default_var_overrides_when_null_declared_global, "$a: null; $a: red !default; - + a { color: $a; }", @@ -208,6 +208,57 @@ test!( }", "a {\n color: outer;\n}\n" ); +test!( + variable_declared_at_root_inside_if, + "@if true { + $a: outer; + } + + a { + color: variable-exists(a); + }", + "a {\n color: false;\n}\n" +); +test!( + variable_declared_at_root_inside_if_default, + "@if true { + $a: outer !default; + } + + a { + color: variable-exists(a); + }", + "a {\n color: false;\n}\n" +); +test!( + variable_declared_at_root_inside_if_global, + "@if true { + $a: outer !global; + } + + a { + color: variable-exists(a); + }", + "a {\n color: true;\n}\n" +); +test!( + variable_declared_at_root_and_globally_inside_if_default, + "$a: null; + + @if true { + $a: null; + $a: outer !default; + + a { + color: $a; + } + } + + a { + color: $a; + }", + "a {\n color: outer;\n}\n\na {\n color: outer;\n}\n" +); // https://github.com/Kixiron/lasso/issues/7 test!( regression_test_for_lasso_0_3_0,