Skip to content

Commit 48e829c

Browse files
committed
Add working test and change error range
I was putting the error on the `return` to match how it's on the `if` when missing the `end`. But the code review made me realize that where the `end` can be is arbitrary with an if statement, but not with a return statement.
1 parent 779a040 commit 48e829c

2 files changed

Lines changed: 15 additions & 5 deletions

File tree

crates/emmylua_parser/src/grammar/doc/test.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3733,7 +3733,8 @@ Syntax(Chunk)@0..47
37333733

37343734
assert_eq!(errors.len(), 1);
37353735
assert_eq!(errors[0].kind, LuaParseErrorKind::SyntaxError);
3736-
assert_eq!(errors[0].message, "expected end of block after return")
3736+
assert_eq!(errors[0].message, "expected end of block after return");
3737+
assert_eq!(u32::from(errors[0].range.start()) as usize, 12);
37373738
}
37383739

37393740
#[test]
@@ -3744,6 +3745,16 @@ Syntax(Chunk)@0..47
37443745

37453746
assert_eq!(errors.len(), 1);
37463747
assert_eq!(errors[0].kind, LuaParseErrorKind::SyntaxError);
3747-
assert_eq!(errors[0].message, "expected end of block after return")
3748+
assert_eq!(errors[0].message, "expected end of block after return");
3749+
assert_eq!(u32::from(errors[0].range.start()) as usize, 8);
3750+
}
3751+
3752+
#[test]
3753+
fn test_do_return_end() {
3754+
let code = "do return end";
3755+
let tree = LuaParser::parse(code, ParserConfig::default());
3756+
let errors = tree.get_errors();
3757+
3758+
assert!(errors.is_empty());
37483759
}
37493760
}

crates/emmylua_parser/src/grammar/lua/stat.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,6 @@ fn parse_attrib(p: &mut LuaParser) -> ParseResult {
752752

753753
fn parse_return(p: &mut LuaParser) -> ParseResult {
754754
let m = p.mark(LuaSyntaxKind::ReturnStat);
755-
let return_start_range = p.current_token_range();
756755
p.bump();
757756
if !block_follow(p)
758757
&& p.current_token() != LuaTokenKind::TkSemicolon
@@ -766,9 +765,9 @@ fn parse_return(p: &mut LuaParser) -> ParseResult {
766765
if !block_follow(p) {
767766
p.push_error(LuaParseError::syntax_error_from(
768767
&t!("expected end of block after return"),
769-
return_start_range,
768+
p.current_token_range(),
770769
));
771-
};
770+
}
772771

773772
Ok(m.complete(p))
774773
}

0 commit comments

Comments
 (0)