-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixed computed keys for class expression #10029
Merged
nicolo-ribaudo
merged 6 commits into
babel:master
from
tanhauhau:tanhauhau/fix/computed-keys
May 28, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7fd8eb5
test case for insertBefore for jsx
tanhauhau aaf48aa
fix unshiftContainer and insertBefore
tanhauhau afa64a5
use path.scope.push
tanhauhau 47b0e0f
add test making sure computedKeys var declaration at the right block
tanhauhau c36f544
add comment
tanhauhau ff1cf32
nit [skip ci]
existentialism File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
27 changes: 11 additions & 16 deletions
27
...ages/babel-plugin-proposal-class-properties/test/fixtures/public-loose/computed/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
4 changes: 3 additions & 1 deletion
4
...l-plugin-proposal-class-properties/test/fixtures/public-loose/instance-computed/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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
function test(x) { | ||
var _x = x; | ||
var _x; | ||
|
||
_x = x; | ||
|
||
var F = function F() { | ||
"use strict"; | ||
|
5 changes: 5 additions & 0 deletions
5
...abel-plugin-proposal-class-properties/test/fixtures/public/computed-without-block/exec.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,5 @@ | ||
const createClass = (k) => class { [k()] = 2 }; | ||
|
||
const clazz = createClass(() => 'foo'); | ||
const instance = new clazz(); | ||
expect(instance.foo).toBe(2); |
1 change: 1 addition & 0 deletions
1
...bel-plugin-proposal-class-properties/test/fixtures/public/computed-without-block/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 @@ | ||
const createClass = (k) => class { [k()] = 2 }; |
18 changes: 18 additions & 0 deletions
18
...el-plugin-proposal-class-properties/test/fixtures/public/computed-without-block/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,18 @@ | ||
var createClass = k => { | ||
var _temp; | ||
|
||
var _k; | ||
|
||
return _temp = (_k = k(), | ||
/*#__PURE__*/ | ||
function () { | ||
"use strict"; | ||
|
||
function _class2() { | ||
babelHelpers.classCallCheck(this, _class2); | ||
babelHelpers.defineProperty(this, _k, 2); | ||
} | ||
|
||
return _class2; | ||
}()), _temp; | ||
}; |
27 changes: 11 additions & 16 deletions
27
packages/babel-plugin-proposal-class-properties/test/fixtures/public/computed/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
4 changes: 3 additions & 1 deletion
4
...s/babel-plugin-proposal-class-properties/test/fixtures/public/instance-computed/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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
function test(x) { | ||
var _x = x; | ||
var _x; | ||
|
||
_x = x; | ||
|
||
var F = function F() { | ||
"use strict"; | ||
|
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
23 changes: 23 additions & 0 deletions
23
packages/babel-plugin-proposal-class-properties/test/fixtures/regression/8882/exec.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,23 @@ | ||
const classes = []; | ||
for (let i = 0; i <= 10; ++i) { | ||
classes.push( | ||
class A { | ||
[i] = `computed field ${i}`; | ||
static foo = `static field ${i}`; | ||
#bar = `private field ${i}`; | ||
getBar() { | ||
return this.#bar; | ||
} | ||
} | ||
); | ||
} | ||
|
||
for(let i=0; i<= 10; ++i) { | ||
const clazz = classes[i]; | ||
expect(clazz.foo).toBe('static field ' + i); | ||
|
||
const instance = new clazz(); | ||
expect(Object.getOwnPropertyNames(instance)).toEqual([String(i)]) | ||
expect(instance[i]).toBe('computed field ' + i); | ||
expect(instance.getBar()).toBe('private field ' + i); | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/babel-plugin-proposal-class-properties/test/fixtures/regression/8882/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,13 @@ | ||
const classes = []; | ||
for (let i = 0; i <= 10; ++i) { | ||
classes.push( | ||
class A { | ||
[i] = `computed field ${i}`; | ||
static foo = `static field ${i}`; | ||
#bar = `private field ${i}`; | ||
getBar() { | ||
return this.#bar; | ||
} | ||
} | ||
); | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/babel-plugin-proposal-class-properties/test/fixtures/regression/8882/options.json
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,3 @@ | ||
{ | ||
"plugins": ["external-helpers", "proposal-class-properties"] | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/babel-plugin-proposal-class-properties/test/fixtures/regression/8882/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,23 @@ | ||
const classes = []; | ||
|
||
for (let i = 0; i <= 10; ++i) { | ||
var _class, _temp, _bar; | ||
|
||
let _i; | ||
|
||
classes.push((_temp = (_i = i, _class = class A { | ||
constructor() { | ||
babelHelpers.defineProperty(this, _i, `computed field ${i}`); | ||
|
||
_bar.set(this, { | ||
writable: true, | ||
value: `private field ${i}` | ||
}); | ||
} | ||
|
||
getBar() { | ||
return babelHelpers.classPrivateFieldGet(this, _bar); | ||
} | ||
|
||
}), _bar = new WeakMap(), babelHelpers.defineProperty(_class, "foo", `static field ${i}`), _temp)); | ||
} |
4 changes: 3 additions & 1 deletion
4
...n-proposal-class-properties/test/fixtures/static-property-tdz/decorator-interop/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
4 changes: 3 additions & 1 deletion
4
...-plugin-proposal-class-properties/test/fixtures/static-property-tdz/edgest-case/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
4 changes: 3 additions & 1 deletion
4
...abel-plugin-proposal-class-properties/test/fixtures/static-property-tdz/general/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
4 changes: 3 additions & 1 deletion
4
.../babel-plugin-proposal-class-properties/test/fixtures/static-property-tdz/loose/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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see any advantage in using
let
here? Usually we try to generate an output as low-level as possible, if it isn't harder to do.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is for this scenario: microsoft/TypeScript#27864
if it is set as
var
, it will behave the same as pointed out in the issue link above, which will break this newly added test case (https://github.com/babel/babel/pull/10029/files#diff-fbbdd83e7a9c998721c1484529c2ce92)maybe it should be determined by whether the computedKeys original declaration kind, to preserve the original scoping intention?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh ok; it's ok like this then.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like adding a comment explaining this might be useful?