Skip to content

Commit

Permalink
don't throw classNameTDZError if referenced identifier is within a Ge…
Browse files Browse the repository at this point in the history
…nericTypeAnnotation

fix babel#9189

Obviously this code is intended to throw an error if someone tries to reference a class before it's defined, like:
```js
class Foo {
  someField = Foo;
}
```
But there's no problem with referencing the class in a type annotation before it's defined, and this is often necessary for tree structures:
```js
class Foo {
  [someSymbol]: Foo;
}
```
  • Loading branch information
jedwards1211 committed Dec 14, 2018
1 parent 47da5cf commit 1f83828
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const classFieldDefinitionEvaluationTDZVisitor = traverse.visitors.merge([
{
ReferencedIdentifier(path) {
if (
path.parentPath.node.type !== "GenericTypeAnnotation" &&
path.parentPath.node.type !== "QualifiedTypeIdentifier" &&
this.classBinding &&
this.classBinding === path.scope.getBinding(path.node.name)
) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const sym = Symbol();
const sym1 = Symbol();

class A {
[sym]: A.B;
[sym1]: Array<A>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": [
"flow"
],
"plugins": [
"proposal-class-properties"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const sym = Symbol();
const sym1 = Symbol();

class A {
constructor() {
_defineProperty(this, sym, void 0);

_defineProperty(this, sym1, void 0);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const sym = Symbol();

class A {
[sym]: A;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"presets": [
"flow"
],
"plugins": [
"proposal-class-properties"
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

const sym = Symbol();

class A {
constructor() {
_defineProperty(this, sym, void 0);
}

}

0 comments on commit 1f83828

Please sign in to comment.