From dc27cae3a836c1b886c4bba1e722e548c3b626ad Mon Sep 17 00:00:00 2001 From: Robert Masen Date: Sun, 27 Nov 2022 10:42:07 -0600 Subject: [PATCH] fix: parsing a variable export decl with two statements on one line --- src/spanned/mod.rs | 6 +- .../everything_js__es2015_module.snap | 64 +++++++++---------- tests/snippets.rs | 19 ++++++ 3 files changed, 55 insertions(+), 34 deletions(-) diff --git a/src/spanned/mod.rs b/src/spanned/mod.rs index 6d2fd0d..25adf09 100644 --- a/src/spanned/mod.rs +++ b/src/spanned/mod.rs @@ -737,9 +737,11 @@ where || self.look_ahead.token.matches_keyword(Keyword::Const(())) { let _start = self.look_ahead_position; - let lex = self.parse_lexical_decl(false)?; + let mut lex = self.parse_lexical_decl(false)?; + if let Decl::Var { semi_colon, .. } = &mut lex { + semi = semi_colon.take(); + } let decl = NamedExportDecl::Decl(lex); - semi = self.consume_semicolon()?; let spec = ModExportSpecifier::Named(decl); ModExport { keyword, spec } } else if self.look_ahead.token.matches_keyword(Keyword::Var(())) { diff --git a/tests/snapshots/everything_js__es2015_module.snap b/tests/snapshots/everything_js__es2015_module.snap index 24d46fe..137e284 100644 --- a/tests/snapshots/everything_js__es2015_module.snap +++ b/tests/snapshots/everything_js__es2015_module.snap @@ -2340,26 +2340,26 @@ Mod( }, ], }, - semi_colon: Some( - Slice { - source: ";", - loc: SourceLocation { - start: Position { - line: 18, - column: 22, - }, - end: Position { - line: 18, - column: 23, - }, - }, - }, - ), + semi_colon: None, }, ), ), }, - semi_colon: None, + semi_colon: Some( + Slice { + source: ";", + loc: SourceLocation { + start: Position { + line: 18, + column: 22, + }, + end: Position { + line: 18, + column: 23, + }, + }, + }, + ), }, ), Decl( @@ -2526,26 +2526,26 @@ Mod( }, ], }, - semi_colon: Some( - Slice { - source: ";", - loc: SourceLocation { - start: Position { - line: 19, - column: 29, - }, - end: Position { - line: 19, - column: 30, - }, - }, - }, - ), + semi_colon: None, }, ), ), }, - semi_colon: None, + semi_colon: Some( + Slice { + source: ";", + loc: SourceLocation { + start: Position { + line: 19, + column: 29, + }, + end: Position { + line: 19, + column: 30, + }, + }, + }, + ), }, ), Decl( diff --git a/tests/snippets.rs b/tests/snippets.rs index 91561c8..711a130 100644 --- a/tests/snippets.rs +++ b/tests/snippets.rs @@ -865,6 +865,17 @@ fn class_method_export() { run_test(js, true).unwrap(); } +#[test] +fn export_then_assign_two_lines() { + run_spanned_test("export let a; + a = 0;", true).unwrap(); +} + +#[test] +fn export_then_assign_one_line() { + run_spanned_test("export let a; a = 0;", true).unwrap(); +} + #[test] fn redecl_error_in_nested_arrow() { let js = r#"(() => { @@ -1293,3 +1304,11 @@ fn run_test(js: &str, as_mod: bool) -> Result<(), ressa::Error> { p.parse()?; Ok(()) } + +fn run_spanned_test<'a>(js: &'a str, as_mod: bool) -> Result<(), ressa::Error> { + use ressa::spanned::Parser; + env_logger::builder().is_test(true).try_init().ok(); + let mut p = Parser::builder().js(js).module(as_mod).build()?; + p.parse()?; + Ok(()) +}