-
-
Notifications
You must be signed in to change notification settings - Fork 483
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): only rename symbols if necessary (#…
…7896) #7872 implements renaming symbols in constructor which shadow references in instance property initializers. But we don't need to rename where the reference in initializer references a symbol which is bound within the initializer itself. Input: ```js class C { double = n => n * 2; constructor(n) { console.log(n); } } ``` Output: ```js class C { constructor(n) { // <-- not renamed this.double = n => n * 2; // <-- moved into constructor console.log(n); // <-- not renamed } } ``` This produces better output, and avoids a traversal of constructor's AST renaming symbols.
- Loading branch information
1 parent
e76fbb0
commit feac02e
Showing
4 changed files
with
98 additions
and
23 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
20 changes: 20 additions & 0 deletions
20
...gin-transform-class-properties/test/fixtures/instance-prop-initializer-var-clash/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,20 @@ | ||
let bound, bound2, bound3; | ||
|
||
class C { | ||
clash1 = bound; | ||
clash2 = unbound; | ||
noClash1 = bound2; | ||
noClash2 = unbound2; | ||
noClash3 = bound3; | ||
noClash4 = unbound3; | ||
noClash5 = x => x; | ||
noClash6 = () => { let y; return y; }; | ||
|
||
constructor(bound, x, y) { | ||
{ | ||
var unbound; | ||
let bound3, unbound3; | ||
} | ||
console.log(bound, unbound, x, y); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...in-transform-class-properties/test/fixtures/instance-prop-initializer-var-clash/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,22 @@ | ||
let bound, bound2, bound3; | ||
|
||
class C { | ||
constructor(_bound, x, y) { | ||
babelHelpers.defineProperty(this, "clash1", bound); | ||
babelHelpers.defineProperty(this, "clash2", unbound); | ||
babelHelpers.defineProperty(this, "noClash1", bound2); | ||
babelHelpers.defineProperty(this, "noClash2", unbound2); | ||
babelHelpers.defineProperty(this, "noClash3", bound3); | ||
babelHelpers.defineProperty(this, "noClash4", unbound3); | ||
babelHelpers.defineProperty(this, "noClash5", (x) => x); | ||
babelHelpers.defineProperty(this, "noClash6", () => { | ||
let y; | ||
return y; | ||
}); | ||
{ | ||
var _unbound; | ||
let bound3, unbound3; | ||
} | ||
console.log(_bound, _unbound, x, y); | ||
} | ||
} |