Skip to content

Commit

Permalink
Add warning when writing to private method (#1067)
Browse files Browse the repository at this point in the history
  • Loading branch information
jridgewell authored Mar 27, 2021
1 parent d4bfad7 commit 09c79a9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3900,6 +3900,12 @@ func TestPrivateIdentifiers(t *testing.T) {
expectParseError(t, "class Foo { set #x(x) { this.#x += 1 } }",
"<stdin>: warning: Reading from setter-only property \"#x\" will throw\n")

// Writing to method warnings
expectParseError(t, "class Foo { #x() { this.#x = 1 } }",
"<stdin>: warning: Writing to readonly method \"#x\" will throw\n")
expectParseError(t, "class Foo { #x() { this.#x += 1 } }",
"<stdin>: warning: Writing to readonly method \"#x\" will throw\n")

expectPrinted(t, `class Foo {
#if
#im() { return this.#im(this.#if) }
Expand Down
10 changes: 9 additions & 1 deletion scripts/end-to-end-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2053,14 +2053,22 @@
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')
╵ ~~~~~~~
> in.js:24:30: warning: Writing to getter-only property "#getter" will throw
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'], {
Expand Down

0 comments on commit 09c79a9

Please sign in to comment.