forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 14
Transform private named instance fields #15
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
Merged
Neuroboy23
merged 4 commits into
bloomberg:es-private-fields
from
joeywatts:esnext-private-instance-fields
Dec 18, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b7ea7f4
Downlevel private named field initializers
229f3ae
Transform property access for private named instance fields
7bf2171
Transform private name bindings in destructuring assignments
fa17cdf
Transform call expressions on private names to properly bind `this`
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 hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 @@ | ||
//// [privateNameFieldAccess.ts] | ||
class A { | ||
#myField = "hello world"; | ||
constructor() { | ||
console.log(this.#myField); | ||
} | ||
} | ||
|
||
|
||
//// [privateNameFieldAccess.js] | ||
var _classPrivateFieldGet = function (receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return privateMap.get(receiver); }; | ||
var _myField; | ||
var A = /** @class */ (function () { | ||
function A() { | ||
_myField.set(this, "hello world"); | ||
console.log(_classPrivateFieldGet(this, _myField)); | ||
} | ||
return A; | ||
}()); | ||
_myField = new WeakMap(); |
This file contains hidden or 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,17 @@ | ||
=== tests/cases/conformance/classes/members/privateNames/privateNameFieldAccess.ts === | ||
class A { | ||
>A : Symbol(A, Decl(privateNameFieldAccess.ts, 0, 0)) | ||
|
||
#myField = "hello world"; | ||
>#myField : Symbol(A.#myField, Decl(privateNameFieldAccess.ts, 0, 9)) | ||
|
||
constructor() { | ||
console.log(this.#myField); | ||
>console.log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) | ||
>log : Symbol(Console.log, Decl(lib.dom.d.ts, --, --)) | ||
>this.#myField : Symbol(A.#myField, Decl(privateNameFieldAccess.ts, 0, 9)) | ||
>this : Symbol(A, Decl(privateNameFieldAccess.ts, 0, 0)) | ||
} | ||
} | ||
|
This file contains hidden or 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,19 @@ | ||
=== tests/cases/conformance/classes/members/privateNames/privateNameFieldAccess.ts === | ||
class A { | ||
>A : A | ||
|
||
#myField = "hello world"; | ||
>#myField : string | ||
>"hello world" : "hello world" | ||
|
||
constructor() { | ||
console.log(this.#myField); | ||
>console.log(this.#myField) : void | ||
>console.log : (message?: any, ...optionalParams: any[]) => void | ||
>console : Console | ||
>log : (message?: any, ...optionalParams: any[]) => void | ||
>this.#myField : string | ||
>this : this | ||
} | ||
} | ||
|
This file contains hidden or 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,78 @@ | ||
//// [privateNameFieldAssignment.ts] | ||
class A { | ||
#field = 0; | ||
constructor() { | ||
this.#field = 1; | ||
this.#field += 2; | ||
this.#field -= 3; | ||
this.#field /= 4; | ||
this.#field *= 5; | ||
this.#field **= 6; | ||
this.#field %= 7; | ||
this.#field <<= 8; | ||
this.#field >>= 9; | ||
this.#field >>>= 10; | ||
this.#field &= 11; | ||
this.#field |= 12; | ||
this.#field ^= 13; | ||
A.getInstance().#field = 1; | ||
A.getInstance().#field += 2; | ||
A.getInstance().#field -= 3; | ||
A.getInstance().#field /= 4; | ||
A.getInstance().#field *= 5; | ||
A.getInstance().#field **= 6; | ||
A.getInstance().#field %= 7; | ||
A.getInstance().#field <<= 8; | ||
A.getInstance().#field >>= 9; | ||
A.getInstance().#field >>>= 10; | ||
A.getInstance().#field &= 11; | ||
A.getInstance().#field |= 12; | ||
A.getInstance().#field ^= 13; | ||
} | ||
static getInstance() { | ||
return new A(); | ||
} | ||
} | ||
|
||
|
||
//// [privateNameFieldAssignment.js] | ||
var _classPrivateFieldSet = function (receiver, privateMap, value) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to set private field on non-instance"); } privateMap.set(receiver, value); return value; }; | ||
var _classPrivateFieldGet = function (receiver, privateMap) { if (!privateMap.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return privateMap.get(receiver); }; | ||
var _field; | ||
var A = /** @class */ (function () { | ||
function A() { | ||
_field.set(this, 0); | ||
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m; | ||
_classPrivateFieldSet(this, _field, 1); | ||
_classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) + 2); | ||
_classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) - 3); | ||
_classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) / 4); | ||
_classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) * 5); | ||
_classPrivateFieldSet(this, _field, Math.pow(_classPrivateFieldGet(this, _field), 6)); | ||
_classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) % 7); | ||
_classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) << 8); | ||
_classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) >> 9); | ||
_classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) >>> 10); | ||
_classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) & 11); | ||
_classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) | 12); | ||
_classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) ^ 13); | ||
_classPrivateFieldSet(A.getInstance(), _field, 1); | ||
_classPrivateFieldSet(_a = A.getInstance(), _field, _classPrivateFieldGet(_a, _field) + 2); | ||
_classPrivateFieldSet(_b = A.getInstance(), _field, _classPrivateFieldGet(_b, _field) - 3); | ||
_classPrivateFieldSet(_c = A.getInstance(), _field, _classPrivateFieldGet(_c, _field) / 4); | ||
_classPrivateFieldSet(_d = A.getInstance(), _field, _classPrivateFieldGet(_d, _field) * 5); | ||
_classPrivateFieldSet(_e = A.getInstance(), _field, Math.pow(_classPrivateFieldGet(_e, _field), 6)); | ||
_classPrivateFieldSet(_f = A.getInstance(), _field, _classPrivateFieldGet(_f, _field) % 7); | ||
_classPrivateFieldSet(_g = A.getInstance(), _field, _classPrivateFieldGet(_g, _field) << 8); | ||
_classPrivateFieldSet(_h = A.getInstance(), _field, _classPrivateFieldGet(_h, _field) >> 9); | ||
_classPrivateFieldSet(_j = A.getInstance(), _field, _classPrivateFieldGet(_j, _field) >>> 10); | ||
_classPrivateFieldSet(_k = A.getInstance(), _field, _classPrivateFieldGet(_k, _field) & 11); | ||
_classPrivateFieldSet(_l = A.getInstance(), _field, _classPrivateFieldGet(_l, _field) | 12); | ||
_classPrivateFieldSet(_m = A.getInstance(), _field, _classPrivateFieldGet(_m, _field) ^ 13); | ||
} | ||
A.getInstance = function () { | ||
return new A(); | ||
}; | ||
return A; | ||
}()); | ||
_field = new WeakMap(); |
Oops, something went wrong.
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.
@joeywatts I think we can merge this soon. Would help if you could:
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.
✔️