Skip to content

Commit

Permalink
JS: fix memory bug in ast.JSString() function
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Jan 28, 2024
1 parent f1831b3 commit 11da24f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion js/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ func TestJS(t *testing.T) {
{"(t)=>{//!\n}", "(t) => { //! };"}, // space after //! is newline
{"import();", "import();"},
{"0\n.k", "(0).k;"},
{"do//!\n; while(1)", "//! do; while (1);"}, // space after //! is newline
{"do//!\n; while(1)", "//! do; while (1);"}, // space after //! is newline
{"//!\nn=>{ return n }", "//! (n) => { return n; };"}, // space after //! is newline
{"//!\n{//!\n}", "//! { //! }"}, // space after //! is newline
}

re := regexp.MustCompile("\n *")
Expand Down
10 changes: 7 additions & 3 deletions js/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ func (p *Parser) parseStmt(allowDeclaration bool) (stmt IStmt) {
}

func (p *Parser) parseStmtList(in string) (list []IStmt) {
comments := len(p.comments)
if !p.consume(in, OpenBraceToken) {
return
}
Expand All @@ -649,9 +650,12 @@ func (p *Parser) parseStmtList(in string) (list []IStmt) {
}
list = append(list, p.parseStmt(true))
}
if 0 < len(p.comments) {
list = append(p.comments, list...)
p.comments = p.comments[:0]
if comments < len(p.comments) {
list2 := make([]IStmt, 0, len(p.comments)-comments+len(list))
list2 = append(list2, p.comments[comments:]...)
list2 = append(list2, list...)
list = list2
p.comments = p.comments[:comments]
}
return
}
Expand Down

0 comments on commit 11da24f

Please sign in to comment.