Skip to content

Commit

Permalink
avoid var declaration overwritten
Browse files Browse the repository at this point in the history
- fix evanw#2080 regression introduced in 4fa3d7a
  • Loading branch information
magic-akari committed Mar 5, 2022
1 parent 536ebd3 commit d277b8b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
14 changes: 8 additions & 6 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1147,16 +1147,18 @@ func (p *parser) canMergeSymbols(scope *js_ast.Scope, existing js_ast.SymbolKind
}
}

// "var foo; var foo;"
// "var foo; function foo() {}"
// "function foo() {} var foo;"
// "function foo() {} function foo() {}"
// "function *foo() {} function *foo() {}" but not "{ function *foo() {} function *foo() {} }"
if new.IsHoistedOrFunction() && existing.IsHoistedOrFunction() &&
(scope.Kind == js_ast.ScopeEntry || scope.Kind == js_ast.ScopeFunctionBody ||
(new.IsHoisted() && existing.IsHoisted())) {
if new.IsFunction() && existing.IsFunction() &&
(scope.Kind == js_ast.ScopeEntry || scope.Kind == js_ast.ScopeFunctionBody) {
return mergeReplaceWithNew
}

if new.IsHoisted() && existing.IsHoisted() &&
(scope.Kind == js_ast.ScopeEntry || scope.Kind == js_ast.ScopeFunctionBody) {
return mergeKeepExisting
}

// "get #foo() {} set #foo() {}"
// "set #foo() {} get #foo() {}"
if (existing == js_ast.SymbolPrivateGet && new == js_ast.SymbolPrivateSet) ||
Expand Down
6 changes: 4 additions & 2 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1355,10 +1355,12 @@ func TestFunction(t *testing.T) {
expectPrintedMangle(t, "async function f() { x() } async function f() { y() }", "async function f() {\n y();\n}\n")
expectPrintedMangle(t, "var f; function f() {}", "var f;\nfunction f() {\n}\n")
expectPrintedMangle(t, "function f() {} var f", "function f() {\n}\nvar f;\n")
expectPrintedMangle(t, "var f; function f() { x() } function f() { y() }", "var f;\nfunction f() {\n y();\n}\n")
expectPrintedMangle(t, "var f; function f() { x() } function f() { y() }", "var f;\nfunction f() {\n x();\n}\nfunction f() {\n y();\n}\n")
expectPrintedMangle(t, "function f() { x() } function f() { y() } var f", "function f() {\n y();\n}\nvar f;\n")
expectPrintedMangle(t, "function f() { x() } var f; function f() { y() }", "function f() {\n x();\n}\nvar f;\nfunction f() {\n y();\n}\n")
expectPrintedMangle(t, "function f() { x() } var f; function f() { y() }", "var f;\nfunction f() {\n y();\n}\n")
expectPrintedMangle(t, "export function f() { x() } function f() { y() }", "export function f() {\n x();\n}\nfunction f() {\n y();\n}\n")

expectPrintedMangle(t, "var x = x || {}; console.log(x); var x = x || {};", "var x = x || {};\nconsole.log(x);\nvar x = x || {};\n")
}

func TestClass(t *testing.T) {
Expand Down

0 comments on commit d277b8b

Please sign in to comment.