From c20176820ef586259cfed67b1a281b6397b65f11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EB=8F=99=EC=9C=A4?= Date: Fri, 20 Sep 2019 23:07:24 +0900 Subject: [PATCH] Fix #415 --- ecmascript/codegen/src/lib.rs | 16 +++++++++++++++- ecmascript/parser/src/lexer/state.rs | 7 +++++++ ecmascript/parser/src/lexer/util.rs | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/ecmascript/codegen/src/lib.rs b/ecmascript/codegen/src/lib.rs index 778b462b47c..6c3af5baed4 100644 --- a/ecmascript/codegen/src/lib.rs +++ b/ecmascript/codegen/src/lib.rs @@ -1636,8 +1636,22 @@ impl<'a> Emitter<'a> { keyword!("return"); if let Some(ref arg) = node.arg { + let need_paren = if let Some(cmt) = self.comments { + !self.pos_of_leading_comments.contains(&arg.span().lo()) && { + // see #415 + cmt.leading_comments(arg.span().lo()).is_some() + } + } else { + false + }; + if need_paren { + punct!("("); + } space!(); - emit!(arg) + emit!(arg); + if need_paren { + punct!(")"); + } } semi!(); } diff --git a/ecmascript/parser/src/lexer/state.rs b/ecmascript/parser/src/lexer/state.rs index b0ba64961d0..bb2c4eb251c 100644 --- a/ecmascript/parser/src/lexer/state.rs +++ b/ecmascript/parser/src/lexer/state.rs @@ -246,6 +246,13 @@ impl State { .unwrap_or(false) } + pub fn last_was_return(&self) -> bool { + match self.token_type { + Some(TokenType::Keyword(Keyword::Return)) => true, + _ => false, + } + } + pub fn last_was_tpl_element(&self) -> bool { match self.token_type { Some(TokenType::Template) => true, diff --git a/ecmascript/parser/src/lexer/util.rs b/ecmascript/parser/src/lexer/util.rs index 88660c861ef..789f6694662 100644 --- a/ecmascript/parser/src/lexer/util.rs +++ b/ecmascript/parser/src/lexer/util.rs @@ -205,7 +205,7 @@ impl<'a, I: Input> Lexer<'a, I> { false }; - let is_for_next = self.state.had_line_break; + let is_for_next = self.state.had_line_break || self.state.last_was_return(); while let Some(c) = self.cur() { if was_star && c == '/' {