From 09c79a900f25b687b246efbaad34abc976c5b2a0 Mon Sep 17 00:00:00 2001 From: Justin Ridgewell Date: Sat, 27 Mar 2021 18:33:42 -0400 Subject: [PATCH] Add warning when writing to private method (#1067) --- internal/js_parser/js_parser.go | 5 ++++- internal/js_parser/js_parser_test.go | 6 ++++++ scripts/end-to-end-tests.js | 10 +++++++++- 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index 0b1a49998a6..00416588abf 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -10552,7 +10552,10 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO r := logger.Range{Loc: e.Index.Loc, Len: int32(len(name))} p.log.AddRangeError(&p.source, r, fmt.Sprintf("Private name %q must be declared in an enclosing class", name)) } else if !p.options.suppressWarningsAboutWeirdCode { - if in.assignTarget != js_ast.AssignTargetNone && (kind == js_ast.SymbolPrivateGet || kind == js_ast.SymbolPrivateStaticGet) { + if in.assignTarget != js_ast.AssignTargetNone && (kind == js_ast.SymbolPrivateMethod || kind == js_ast.SymbolPrivateStaticMethod) { + r := logger.Range{Loc: e.Index.Loc, Len: int32(len(name))} + p.log.AddRangeWarning(&p.source, r, fmt.Sprintf("Writing to readonly method %q will throw", name)) + } else if in.assignTarget != js_ast.AssignTargetNone && (kind == js_ast.SymbolPrivateGet || kind == js_ast.SymbolPrivateStaticGet) { r := logger.Range{Loc: e.Index.Loc, Len: int32(len(name))} p.log.AddRangeWarning(&p.source, r, fmt.Sprintf("Writing to getter-only property %q will throw", name)) } else if in.assignTarget != js_ast.AssignTargetReplace && (kind == js_ast.SymbolPrivateSet || kind == js_ast.SymbolPrivateStaticSet) { diff --git a/internal/js_parser/js_parser_test.go b/internal/js_parser/js_parser_test.go index 4f11f901e93..afe752f4ad2 100644 --- a/internal/js_parser/js_parser_test.go +++ b/internal/js_parser/js_parser_test.go @@ -3900,6 +3900,12 @@ func TestPrivateIdentifiers(t *testing.T) { expectParseError(t, "class Foo { set #x(x) { this.#x += 1 } }", ": warning: Reading from setter-only property \"#x\" will throw\n") + // Writing to method warnings + expectParseError(t, "class Foo { #x() { this.#x = 1 } }", + ": warning: Writing to readonly method \"#x\" will throw\n") + expectParseError(t, "class Foo { #x() { this.#x += 1 } }", + ": warning: Writing to readonly method \"#x\" will throw\n") + expectPrinted(t, `class Foo { #if #im() { return this.#im(this.#if) } diff --git a/scripts/end-to-end-tests.js b/scripts/end-to-end-tests.js index 51463581fd2..55c57303426 100644 --- a/scripts/end-to-end-tests.js +++ b/scripts/end-to-end-tests.js @@ -2053,7 +2053,11 @@ new Foo().bar() `, }, { - expectedStderr: ` > in.js:23:30: warning: Reading from setter-only property "#setter" will throw + expectedStderr: ` > in.js:22:29: warning: Writing to readonly method "#method" will throw + 22 │ expect(() => obj.#method = 1, 'Cannot write to private f... + ╵ ~~~~~~~ + + > in.js:23:30: warning: Reading from setter-only property "#setter" will throw 23 │ expect(() => this.#setter, 'member.get is not a function') ╵ ~~~~~~~ @@ -2061,6 +2065,10 @@ 24 │ expect(() => this.#getter = 1, 'member.set is not a func... ╵ ~~~~~~~ + > in.js:25:30: warning: Writing to readonly method "#method" will throw + 25 │ expect(() => this.#method = 1, 'member.set is not a func... + ╵ ~~~~~~~ + `, }), test(['in.js', '--outfile=node.js', '--target=es6'], {