-
-
Notifications
You must be signed in to change notification settings - Fork 480
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(transformer/class-properties): exit faster from super replacemen…
…t visitor (#8028) When inserting instance property initializers into class constructor, need to search for and transform `super()`. Exit that visit earlier in some cases, for better performance and smaller output.
- Loading branch information
1 parent
de4c772
commit 897a1a8
Showing
6 changed files
with
145 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...bel-plugin-transform-class-properties/test/fixtures/super-in-constructor-missing/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class C extends S { | ||
prop = 1; | ||
constructor() { | ||
return {}; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...el-plugin-transform-class-properties/test/fixtures/super-in-constructor-missing/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
class C extends S { | ||
constructor() { | ||
var _super = (..._args) => (super(..._args), babelHelpers.defineProperty(this, "prop", 1), this); | ||
return {}; | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...abel-plugin-transform-class-properties/test/fixtures/super-in-constructor-nested/input.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
class C extends S { | ||
prop = 1; | ||
constructor() { | ||
// Insert prop initializer after `super()` | ||
super(); | ||
this.self = this; | ||
} | ||
} | ||
|
||
class C2 extends S { | ||
prop = 1; | ||
constructor() { | ||
class Inner extends S { | ||
constructor() { | ||
// Don't transform - different `super` | ||
super(); | ||
} | ||
} | ||
|
||
// Insert prop initializer after `super()` | ||
super(); | ||
this.self = this; | ||
} | ||
} | ||
|
||
class C3 extends S { | ||
prop = 1; | ||
constructor() { | ||
if (condition) { | ||
// Transform to `_super()` | ||
super(1, 2); | ||
return this; | ||
} | ||
|
||
// Transform to `_super()` | ||
super(3, 4); | ||
|
||
if (condition2) { | ||
// Don't transform | ||
super(5, 6); | ||
} | ||
|
||
// Don't transform | ||
super(7, 8); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...bel-plugin-transform-class-properties/test/fixtures/super-in-constructor-nested/output.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class C extends S { | ||
constructor() { | ||
// Insert prop initializer after `super()` | ||
super(); | ||
babelHelpers.defineProperty(this, "prop", 1); | ||
this.self = this; | ||
} | ||
} | ||
|
||
class C2 extends S { | ||
constructor() { | ||
class Inner extends S { | ||
constructor() { | ||
// Don't transform - different `super` | ||
super(); | ||
} | ||
} | ||
|
||
// Insert prop initializer after `super()` | ||
super(); | ||
babelHelpers.defineProperty(this, "prop", 1); | ||
this.self = this; | ||
} | ||
} | ||
|
||
class C3 extends S { | ||
constructor() { | ||
var _super = (..._args) => (super(..._args), babelHelpers.defineProperty(this, "prop", 1), this); | ||
|
||
if (condition) { | ||
// Transform to `_super()` | ||
_super(1, 2); | ||
return this; | ||
} | ||
|
||
// Transform to `_super()` | ||
_super(3, 4); | ||
|
||
if (condition2) { | ||
// Don't transform | ||
super(5, 6); | ||
} | ||
|
||
// Don't transform | ||
super(7, 8); | ||
} | ||
} |