Skip to content

Commit

Permalink
release notes for #1067
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 27, 2021
1 parent 09c79a9 commit d06bfdb
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# Changelog

## Unreleased

* Warn about mutation of private methods ([#1067](https://github.com/evanw/esbuild/pull/1067))

Mutating a private method in JavaScript is not allowed, and will throw at run-time:

```js
class Foo {
#method() {}
mutate() {
this.#method = () => {}
}
}
```

This is the case both when esbuild passes the syntax through untransformed and when esbuild transforms the syntax into the equivalent code that uses a `WeakSet` to emulate private methods in older browsers. However, it's clear from this code that doing this will always throw, so this code is almost surely a mistake. With this release, esbuild will now warn when you do this. This change was contributed by [@jridgewell](https://github.com/jridgewell).

## 0.10.2

* Fix a crash that was introduced in the previous release ([#1064](https://github.com/evanw/esbuild/issues/1064))
Expand Down
2 changes: 1 addition & 1 deletion internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10554,7 +10554,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
} else if !p.options.suppressWarningsAboutWeirdCode {
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))
p.log.AddRangeWarning(&p.source, r, fmt.Sprintf("Writing to read-only 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))
Expand Down
4 changes: 2 additions & 2 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3902,9 +3902,9 @@ func TestPrivateIdentifiers(t *testing.T) {

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

expectPrinted(t, `class Foo {
#if
Expand Down
4 changes: 2 additions & 2 deletions scripts/end-to-end-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2053,7 +2053,7 @@
new Foo().bar()
`,
}, {
expectedStderr: ` > in.js:22:29: warning: Writing to readonly method "#method" will throw
expectedStderr: ` > in.js:22:29: warning: Writing to read-only method "#method" will throw
22 │ expect(() => obj.#method = 1, 'Cannot write to private f...
╵ ~~~~~~~
Expand All @@ -2065,7 +2065,7 @@
24 │ expect(() => this.#getter = 1, 'member.set is not a func...
╵ ~~~~~~~
> in.js:25:30: warning: Writing to readonly method "#method" will throw
> in.js:25:30: warning: Writing to read-only method "#method" will throw
25 │ expect(() => this.#method = 1, 'member.set is not a func...
╵ ~~~~~~~
Expand Down

0 comments on commit d06bfdb

Please sign in to comment.