From 5f6b11348abb49fa9f1d93185f8505ca9db9feb5 Mon Sep 17 00:00:00 2001 From: David Sherret Date: Thu, 2 Jan 2020 02:46:56 -0500 Subject: [PATCH] Fix span of the switch statement. (#553) --- ecmascript/parser/src/parser/stmt.rs | 70 ++++++++++++---------------- 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/ecmascript/parser/src/parser/stmt.rs b/ecmascript/parser/src/parser/stmt.rs index eff46a1cc53..ff8bce93c7a 100644 --- a/ecmascript/parser/src/parser/stmt.rs +++ b/ecmascript/parser/src/parser/stmt.rs @@ -415,57 +415,49 @@ impl<'a, I: Tokens> Parser<'a, I> { let discriminant = self.include_in_expr(true).parse_expr()?; expect!(')'); - let mut cur = None; let mut cases = vec![]; let mut span_of_previous_default = None; expect!('{'); - while !eof!() && !is!('}') { - let case_start = cur_pos!(); - let ctx = Context { - is_break_allowed: true, - ..self.ctx() - }; + let ctx = Context { + is_break_allowed: true, + ..self.ctx() + }; - self.with_ctx(ctx).parse_with(|p| { - if is_one_of!("case", "default") { - let is_case = is!("case"); - let start_of_case = cur_pos!(); - bump!(); - cases.extend(cur.take()); - let test = if is_case { - p.include_in_expr(true).parse_expr().map(Some)? - } else { - if let Some(previous) = span_of_previous_default { - syntax_error!(SyntaxError::MultipleDefault { previous }); - } - span_of_previous_default = Some(span!(start_of_case)); - - None - }; - expect!(':'); - - cur = Some(SwitchCase { - span: span!(case_start), - test, - cons: vec![], - }); + self.with_ctx(ctx).parse_with(|p| { + while is_one_of!("case", "default") { + let mut cons = vec![]; + let is_case = is!("case"); + let case_start = cur_pos!(); + bump!(); + let test = if is_case { + p.include_in_expr(true).parse_expr().map(Some)? } else { - match cur { - Some(ref mut cur) => { - cur.cons.push(p.parse_stmt_list_item(false)?); - } - None => unexpected!(), + if let Some(previous) = span_of_previous_default { + syntax_error!(SyntaxError::MultipleDefault { previous }); } + span_of_previous_default = Some(span!(case_start)); + + None + }; + expect!(':'); + + while !eof!() && !is_one_of!("case", "default", '}') { + cons.push(p.parse_stmt_list_item(false)?); } - Ok(()) - })? - } + cases.push(SwitchCase { + span: Span::new(case_start, p.input.prev_span().hi(), Default::default()), + test, + cons, + }); + } + + Ok(()) + })?; // eof or rbrace expect!('}'); - cases.extend(cur); Ok(Stmt::Switch(SwitchStmt { span: span!(switch_start),