From bc0a14a18ad37d7c0ec60a23d7b2ff140fb38a1c Mon Sep 17 00:00:00 2001 From: Nevkontakte Date: Wed, 13 Oct 2021 02:05:04 +0100 Subject: [PATCH] Don't emit "duplicate label" error across function scopes. (#1671) --- internal/js_parser/js_parser.go | 4 ++++ internal/js_parser/js_parser_test.go | 1 + 2 files changed, 5 insertions(+) diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index f1d23cd3b81..a131a5ce5ed 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -8784,6 +8784,10 @@ func (p *parser) visitAndAppendStmt(stmts []js_ast.Stmt, stmt js_ast.Stmt) []js_ fmt.Sprintf("The original label %q is here", name))}) break } + if scope.Kind == js_ast.ScopeFunctionBody { + // Labels are only visible within the function they are defined in. + break + } } p.currentScope.Label = js_ast.LocRef{Loc: s.Name.Loc, Ref: ref} diff --git a/internal/js_parser/js_parser_test.go b/internal/js_parser/js_parser_test.go index 5f06ace7994..7ab69eea41b 100644 --- a/internal/js_parser/js_parser_test.go +++ b/internal/js_parser/js_parser_test.go @@ -1903,6 +1903,7 @@ func TestLabels(t *testing.T) { expectPrinted(t, "x: y: z: 1", "x:\n y:\n z:\n 1;\n") expectPrinted(t, "x: 1; y: 2; x: 3", "x:\n 1;\ny:\n 2;\nx:\n 3;\n") expectParseError(t, "x: y: x: 1", ": error: Duplicate label \"x\"\n: note: The original label \"x\" is here\n") + expectPrinted(t, "x: (function(){ x: 1; })()", "x:\n (function() {\n x:\n 1;\n })();\n") } func TestArrow(t *testing.T) {