Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions js/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,15 @@ func TestJS(t *testing.T) {
{"async\n()", "async();"},
{"{};;", "{} ;"},
{"{}\n;", "{} ;"},
{"if(a)b\n;else c", "if (a) b; else c;"},
{"if(a)var b=1\n;else c", "if (a) var b = 1; else c;"},
{"if(a)debugger\n;else c", "if (a) debugger; else c;"},
{"if(a)throw b\n;else c", "if (a) throw b; else c;"},
{"for(;;)if(a)break\n;else continue\n;", "for ( ; ; ) { if (a) break; else continue; }"},
{"function f(){if(a)return\n;else c}", "function f() { if (a) return; else c; }"},
{"do a()\n;while(b)", "do a(); while (b);"},
{"if(a)do;while(b)\n;else c", "if (a) do; while (b); else c;"},
{"a\n;\n;", "a; ;"},
{"- - --3", "- - --3;"},
{"([,,])=>P", "([,,]) => { return P; };"},
{"(t)=>{//!\n}", "(t) => { //! };"}, // space after //! is newline
Expand Down
11 changes: 10 additions & 1 deletion js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,13 +646,22 @@ func (p *Parser) parseStmt(allowDeclaration bool) (stmt IStmt) {
}
}
}
if !p.prevLT && p.tt == SemicolonToken {
if p.tt == SemicolonToken && (!p.prevLT || endsWithSemicolon(stmt)) {
p.next()
}
p.stmtLevel--
return
}

// endsWithSemicolon returns true for statements that are terminated by a semicolon. ASI only inserts a semicolon when the next token is not allowed, so a semicolon after a line terminator still ends these statements and is not an empty statement.
func endsWithSemicolon(stmt IStmt) bool {
switch stmt.(type) {
case *ExprStmt, *VarDecl, *DoWhileStmt, *BranchStmt, *ReturnStmt, *ThrowStmt, *DebuggerStmt, *DirectivePrologueStmt:
return true
}
return false
}

func (p *Parser) parseStmtList(in string) (list []IStmt) {
comments := len(p.comments)
if !p.consume(in, OpenBraceToken) {
Expand Down
Loading