Get operation on private accessor without getter should throw
See original GitHub issueBug Report
- I would like to work on a fix!
Current behavior
Transpiled code doesn’t throw an error in foo()
and returns undefined
.
Input Code
class C {
foo() { return this.#bar; }
set #bar(a) {}
}
new C().foo();
Expected behavior
new C().foo();
should throw a TypeError
.
- Example from Google Chrome Version 90.0.4393.0 Canary:
VM490:2 Uncaught TypeError: '#bar' was defined without a getter at C.foo (<anonymous>:2:11) at <anonymous>:6:9
- Related
test262
test: get-access-of-missing-private-getter.js.
Babel Configuration: See the REPL link
Environment
- Babel version(s): v7.12.12
- Node/npm version: 10.13
- OS: Using REPL
- Monorepo: Using REPL
- How you are using Babel: Using REPL
Possible Solution
_classPrivateFieldGet
helper should not fallback to returning descriptor.value
if descriptor.get
is false-y. If get
property exists but is undefined
, it can throw this particular error.
Additional context
See PrivateFieldGet-6.b
from the spec:
PrivateFieldGet ( P, O )
1. Assert: P is a Private Name.
2. If O is not an object, throw a TypeError exception.
3. If P.[[Kind]] is "field",
a. Let entry be PrivateFieldFind(P, O).
b. If entry is empty, throw a TypeError exception.
c. Return entry.[[PrivateFieldValue]].
4. Perform ? PrivateBrandCheck(O, P).
5. If P.[[Kind]] is "method",
a. Return P.[[Value]].
6. Else,
a. Assert: P.[[Kind]] is "accessor".
b. If P does not have a [[Get]] field, throw a TypeError exception.
c. Let getter be P.[[Get]].
d. Return ? Call(getter, O).
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Bad design decision to throw an exception from an accessor?
Yes, that's bad. I suggest initializing the strings variable in the declaration, like: private List<String> strings = new ...
Read more >Using Properties - C# Programming Guide - Microsoft Learn
A property without a get accessor is considered write-only. ... The get accessor must end in a return or throw statement, and control...
Read more >Property getters and setters - The Modern JavaScript Tutorial
Accessor properties are represented by “getter” and “setter” methods. In an object literal they are denoted by get and set :.
Read more >Accessor and Mutator Methods in Java [Practical Examples]
Accessor method must be declared as public methods. Although, Accessor methods do not accept any parameters. But, they return a value of a...
Read more >Avoid getters and setters whenever possible
In both classes, Car1.java and Car2.java, we get essentially the same ... This realization that the private member should not have been ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ll take this, seems to be a quick fix.
@nicolo-ribaudo
writeOnlyError
sounds good. I don’t have the bandwidth to pick this up right now so happy for someone else to take it. 🙂