Skip to content
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

ES private class elements #42458

Merged
merged 87 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from 86 commits
Commits
Show all changes
87 commits
Select commit Hold shift + click to select a range
e3a3fb8
Added support for private identifier methods.
dragomirtitian Jan 15, 2021
4ab27a2
Added tests for private methods.
dragomirtitian Jan 15, 2021
6977b9f
Added check to only not allow private name method signatures in anyth…
dragomirtitian Jan 18, 2021
bc8c07e
Added private accessors tests
dragomirtitian Jan 19, 2021
2760ed8
Transform private methods
mkubilayk Jan 25, 2021
04af85d
Rename shouldTransformPrivateFields
mkubilayk Jan 25, 2021
f048d72
Accept baseline
mkubilayk Jan 25, 2021
f8cfb81
Use a single WeakSet for brand-check
mkubilayk Jan 26, 2021
4aa4c3b
Accept baseline
mkubilayk Jan 26, 2021
0ac1271
Add a test for using private methods in static field initializers
mkubilayk Jan 26, 2021
7da5bd9
Add breaking checker test
mkubilayk Jan 26, 2021
e133f60
Add to instances once per-instance
mkubilayk Jan 26, 2021
aa5dd8a
Accept baseline
mkubilayk Jan 26, 2021
a3889a7
fix: evaluate receiver and rhs expressions before throwing on readonl…
mkubilayk Jan 26, 2021
8952caa
Add a test for evaluating rhs before readonly assignment
mkubilayk Jan 26, 2021
dee87bb
Transpile private accessors
mkubilayk Jan 27, 2021
aec7e9b
Accept baseline
mkubilayk Jan 27, 2021
f3bbf0f
fix: handle readonly/writeonly accessors
mkubilayk Jan 28, 2021
a1b59cd
accept baseline
mkubilayk Jan 28, 2021
6b74498
add a test for private setter without a getter
mkubilayk Jan 28, 2021
d51f6cd
fix: getAllUnscopedEmitHelpers
mkubilayk Jan 28, 2021
c175770
fix: better handling of duplicate names
mkubilayk Jan 29, 2021
4301af5
Fixed wrong error message for private methods in class expressions.
dragomirtitian Feb 1, 2021
3a056b2
change error message
mkubilayk Feb 1, 2021
95df2d1
add a test for async private methods with a higher target
mkubilayk Feb 1, 2021
590124e
fix: setter assignment returns rhs value
mkubilayk Feb 3, 2021
219ea83
add a test for setter assignment return value
mkubilayk Feb 3, 2021
25da8ca
fix: handle duplicate accessors
mkubilayk Feb 3, 2021
a2228be
add tests for duplicate accessors
mkubilayk Feb 3, 2021
ff4f737
docs: add missing parameter docs
mkubilayk Feb 3, 2021
704727c
Fixed failing test.
dragomirtitian Feb 3, 2021
73925dc
baseline-accept: ordering changes
mkubilayk Feb 17, 2021
67fd38d
fix: attach weakSetName to property declaration
mkubilayk Feb 17, 2021
45c2a41
add a test for nested private methods
mkubilayk Feb 17, 2021
356b91d
add a test with any
mkubilayk Feb 17, 2021
b391464
Merge pull request #51 from bloomberg/fix/nested-private-methods
dragomirtitian Feb 18, 2021
5ebb3d6
Added support for static private fields accessors and methods.
dragomirtitian Feb 16, 2021
7006c09
Added error message for private identifiers used with static decorato…
dragomirtitian Feb 16, 2021
b876613
Fixed emit static bug that used private names outside of classes for …
dragomirtitian Feb 18, 2021
31a8c00
Added tests for private static fields methods and accessors.
dragomirtitian Feb 18, 2021
9219e99
Merge remote-tracking branch 'remotes/origin/master' into es-private-…
dragomirtitian Feb 19, 2021
5a27a32
Merge remote-tracking branch 'remotes/bloomberg/es-private-methods-an…
dragomirtitian Feb 19, 2021
964fc27
Fixed error messages and tests after merge.
dragomirtitian Feb 19, 2021
d2ab6fc
Merge branch 'es-private-methods-and-accessors' into es-private-stati…
dragomirtitian Feb 19, 2021
23f6c67
Accept new baseline.
dragomirtitian Feb 19, 2021
374e6f3
Improved duplicate identifier checks for static private class elements.
dragomirtitian Feb 19, 2021
62b4f2f
Added error when using initializers with private static fields when u…
dragomirtitian Feb 23, 2021
b2a692d
Fixed code review issues.
dragomirtitian Feb 24, 2021
363041e
Removed semantically wrong emit on `useDefineForClassFields:true` wit…
dragomirtitian Feb 24, 2021
64757e0
Changed emit for uninitialized private static fields.
dragomirtitian Feb 25, 2021
68a7f80
Added runtime error in helper if a static private field is accessed b…
dragomirtitian Feb 25, 2021
7e7b0b1
Fixed code review comments for private identifier static class elements.
dragomirtitian Feb 25, 2021
30c7f69
add debug.assertNever for unknown node type (#53)
mkubilayk Mar 1, 2021
c924058
Merge remote-tracking branch 'upstream/master' into es-private-method…
mkubilayk Mar 1, 2021
e65116f
Merge remote-tracking branch 'upstream/master' into es-private-method…
mkubilayk Mar 2, 2021
4748c87
Fixed code review issues.
dragomirtitian Mar 3, 2021
09a5aae
Merge branch 'es-private-methods-and-accessors' into es-private-stati…
dragomirtitian Mar 3, 2021
497e47f
Fixed code review issues for private class elements.
dragomirtitian Mar 3, 2021
294cc48
Fixes class shadowing when checking access to a private static class …
dragomirtitian Mar 8, 2021
027bdb3
Merge remote-tracking branch 'remotes/origin/master' into es-private-…
dragomirtitian Mar 8, 2021
7bbc944
Merge remote-tracking branch 'upstream/master'
mkubilayk Mar 17, 2021
dd526e7
fix private methods/accessors in class expr inside a loop
mkubilayk Mar 17, 2021
f6ecfab
collapse switch case
mkubilayk Mar 17, 2021
6c3f661
fix class name
mkubilayk Mar 17, 2021
ef69c6c
simplify getPrivateMethodsAndAccessors
mkubilayk Mar 17, 2021
2a646ed
remove findPreviousAccessorInfo
mkubilayk Mar 17, 2021
d8136f7
lazily create weakSetName identifier
mkubilayk Mar 17, 2021
fbd749c
Merge remote-tracking branch 'remotes/bloomberg/es-private-static-fie…
dragomirtitian Mar 17, 2021
f96eafe
Merge remote-tracking branch 'remotes/bloomberg/es-private-static-fie…
dragomirtitian Mar 17, 2021
2de8a43
Merge branch 'es-private-methods-and-accessors' of https://github.com…
dragomirtitian Mar 18, 2021
7490a36
do not allocate a node if not needed in visitMehodDeclaration (#55)
mkubilayk Mar 18, 2021
c273258
Removed all the emit helpers for private identifier methods accessors…
dragomirtitian Mar 17, 2021
1addbb3
Simplified emit for private identifier class elements.
dragomirtitian Mar 18, 2021
c461b92
do not clone the receiver (#57)
mkubilayk Mar 18, 2021
a61fd5f
leave bad code in for #constructor and duplicate private names (#58)
mkubilayk Mar 19, 2021
89f746a
Added check for WeakSet collision.
dragomirtitian Mar 19, 2021
ff24636
Added error for using a set only accessor.
dragomirtitian Mar 22, 2021
a60e429
Merge pull request #61 from bloomberg/es-private-methods-and-accessor…
dragomirtitian Mar 22, 2021
f5816ab
update keyof tests and ?? (#62)
mkubilayk Mar 23, 2021
3b62d3c
fix emit helpers comments
mkubilayk Mar 23, 2021
7d8a2b9
produce an error if private field helpers are not up to date
mkubilayk Mar 23, 2021
d0d2055
add tests
mkubilayk Mar 23, 2021
93a7ca3
fix setter-only compound assignment
mkubilayk Mar 23, 2021
6da2cdd
Merge pull request #63 from bloomberg/helper-parameter-count
mkubilayk Mar 23, 2021
30805b7
fix tests
mkubilayk Mar 23, 2021
8d77a8e
fix duplicated trailing comments (#64)
mkubilayk Mar 24, 2021
8078ae0
clear receiver pos and setTextRange on helper calls
mkubilayk Mar 24, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
219 changes: 143 additions & 76 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

32 changes: 24 additions & 8 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -3284,6 +3284,26 @@
"category": "Error",
"code": 2802
},
"Cannot assign to private method '{0}'. Private methods are not writable.": {
"category": "Error",
"code": 2803
},
"Duplicate identifier '{0}'. Static and instance elements cannot share the same private name.": {
"category": "Error",
"code": 2804
},
"Static fields with private names can't have initializers when the '--useDefineForClassFields' flag is not specified with a '--target' of 'esnext'. Consider adding the '--useDefineForClassFields' flag.": {
"category": "Error",
"code": 2805
},
"Private accessor was defined without a getter.": {
"category": "Error",
"code": 2806
},
"This syntax requires an imported helper named '{1}' with {2} parameters, which is not compatible with the one in '{0}'. Consider upgrading your version of '{0}'.": {
"category": "Error",
"code": 2807
},

"Import declaration '{0}' is using private name '{1}'.": {
"category": "Error",
Expand Down Expand Up @@ -6334,14 +6354,6 @@
"category": "Error",
"code": 18019
},
"A method cannot be named with a private identifier.": {
"category": "Error",
"code": 18022
},
"An accessor cannot be named with a private identifier.": {
"category": "Error",
"code": 18023
},
"An enum member cannot be named with a private identifier.": {
"category": "Error",
"code": 18024
Expand Down Expand Up @@ -6385,5 +6397,9 @@
"Invalid value for 'jsxFragmentFactory'. '{0}' is not a valid identifier or qualified-name.": {
"category": "Error",
"code": 18035
},
"Class decorators can't be used with static private identifier. Consider removing the experimental decorator.": {
"category": "Error",
"code": 18036
}
}
150 changes: 130 additions & 20 deletions src/compiler/factory/emitHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ namespace ts {
createImportDefaultHelper(expression: Expression): Expression;
createExportStarHelper(moduleExpression: Expression, exportsExpression?: Expression): Expression;
// Class Fields Helpers
createClassPrivateFieldGetHelper(receiver: Expression, privateField: Identifier): Expression;
createClassPrivateFieldSetHelper(receiver: Expression, privateField: Identifier, value: Expression): Expression;
createClassPrivateFieldGetHelper(receiver: Expression, state: Identifier, kind: PrivateIdentifierKind, f: Identifier | undefined): Expression;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may need a test in checker that verifies the parameter count of __classPrivateFieldGet and __classPrivateFieldSet using the .d.ts we ship with tslib to verify you're using an up-to-date version of tslib.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see checkExternalEmitHelpers is used to check the existence of helpers. Were you thinking an extension to that, which would validate the parameter count for ExternalEmitHelpers.ClassPrivateFieldGet | ExternalEmitHelpers.ClassPrivateFieldSet?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this in 7d8a2b9.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[...] Were you thinking an extension to that, which would validate the parameter count for ExternalEmitHelpers.ClassPrivateFieldGet | ExternalEmitHelpers.ClassPrivateFieldSet?

That's precisely what I was describing, yes.

createClassPrivateFieldSetHelper(receiver: Expression, state: Identifier, value: Expression, kind: PrivateIdentifierKind, f: Identifier | undefined): Expression;
}

export function createEmitHelperFactory(context: TransformationContext): EmitHelperFactory {
Expand Down Expand Up @@ -368,15 +368,30 @@ namespace ts {

// Class Fields Helpers

function createClassPrivateFieldGetHelper(receiver: Expression, privateField: Identifier) {
function createClassPrivateFieldGetHelper(receiver: Expression, state: Identifier, kind: PrivateIdentifierKind, f: Identifier | undefined) {
context.requestEmitHelper(classPrivateFieldGetHelper);
return factory.createCallExpression(getUnscopedHelperName("__classPrivateFieldGet"), /*typeArguments*/ undefined, [receiver, privateField]);
let args;
if (!f) {
args = [receiver, state, factory.createStringLiteral(kind)];
}
else {
args = [receiver, state, factory.createStringLiteral(kind), f];
}
return factory.createCallExpression(getUnscopedHelperName("__classPrivateFieldGet"), /*typeArguments*/ undefined, args);
}

function createClassPrivateFieldSetHelper(receiver: Expression, privateField: Identifier, value: Expression) {
function createClassPrivateFieldSetHelper(receiver: Expression, state: Identifier, value: Expression, kind: PrivateIdentifierKind, f: Identifier | undefined) {
context.requestEmitHelper(classPrivateFieldSetHelper);
return factory.createCallExpression(getUnscopedHelperName("__classPrivateFieldSet"), /*typeArguments*/ undefined, [receiver, privateField, value]);
let args;
if (!f) {
args = [receiver, state, value, factory.createStringLiteral(kind)];
}
else {
args = [receiver, state, value, factory.createStringLiteral(kind), f];
}
return factory.createCallExpression(getUnscopedHelperName("__classPrivateFieldSet"), /*typeArguments*/ undefined, args);
}

}

/* @internal */
Expand Down Expand Up @@ -803,7 +818,6 @@ namespace ts {
};`
};

// emit output for the __export helper function
export const exportStarHelper: UnscopedEmitHelper = {
name: "typescript:export-star",
importName: "__exportStar",
Expand All @@ -816,31 +830,127 @@ namespace ts {
};`
};

// Class fields helpers
/**
* Parameters:
* @param receiver — The object from which the private member will be read.
* @param state — One of the following:
* - A WeakMap used to read a private instance field.
* - A WeakSet used as an instance brand for private instance methods and accessors.
* - A function value that should be the undecorated class constructor used to brand check private static fields, methods, and accessors.
* @param kind — (optional pre TS 4.3, required for TS 4.3+) One of the following values:
* - undefined — Indicates a private instance field (pre TS 4.3).
* - "f" — Indicates a private field (instance or static).
* - "m" — Indicates a private method (instance or static).
* - "a" — Indicates a private accessor (instance or static).
* @param f — (optional pre TS 4.3) Depends on the arguments for state and kind:
* - If kind is "m", this should be the function corresponding to the static or instance method.
* - If kind is "a", this should be the function corresponding to the getter method, or undefined if the getter was not defined.
* - If kind is "f" and state is a function, this should be an object holding the value of a static field, or undefined if the static field declaration has not yet been evaluated.
* Usage:
* This helper will only ever be used by the compiler in the following ways:
*
* Reading from a private instance field (pre TS 4.3):
* __classPrivateFieldGet(<any>, <WeakMap>)
*
* Reading from a private instance field (TS 4.3+):
* __classPrivateFieldGet(<any>, <WeakMap>, "f")
*
* Reading from a private instance get accessor (when defined, TS 4.3+):
* __classPrivateFieldGet(<any>, <WeakSet>, "a", <function>)
*
* Reading from a private instance get accessor (when not defined, TS 4.3+):
* __classPrivateFieldGet(<any>, <WeakSet>, "a", void 0)
* NOTE: This always results in a runtime error.
*
* Reading from a private instance method (TS 4.3+):
* __classPrivateFieldGet(<any>, <WeakSet>, "m", <function>)
*
* Reading from a private static field (TS 4.3+):
* __classPrivateFieldGet(<any>, <constructor>, "f", <{ value: any }>)
*
* Reading from a private static get accessor (when defined, TS 4.3+):
* __classPrivateFieldGet(<any>, <constructor>, "a", <function>)
*
* Reading from a private static get accessor (when not defined, TS 4.3+):
* __classPrivateFieldGet(<any>, <constructor>, "a", void 0)
* NOTE: This always results in a runtime error.
*
* Reading from a private static method (TS 4.3+):
* __classPrivateFieldGet(<any>, <constructor>, "m", <function>)
*/
export const classPrivateFieldGetHelper: UnscopedEmitHelper = {
name: "typescript:classPrivateFieldGet",
importName: "__classPrivateFieldGet",
scoped: false,
text: `
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, privateMap) {
if (!privateMap.has(receiver)) {
throw new TypeError("attempted to get private field on non-instance");
}
return privateMap.get(receiver);
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
};`
};

/**
* Parameters:
* @param receiver — The object on which the private member will be set.
* @param state — One of the following:
* - A WeakMap used to store a private instance field.
* - A WeakSet used as an instance brand for private instance methods and accessors.
* - A function value that should be the undecorated class constructor used to brand check private static fields, methods, and accessors.
* @param value — The value to set.
* @param kind — (optional pre TS 4.3, required for TS 4.3+) One of the following values:
* - undefined — Indicates a private instance field (pre TS 4.3).
* - "f" — Indicates a private field (instance or static).
* - "m" — Indicates a private method (instance or static).
* - "a" — Indicates a private accessor (instance or static).
* @param f — (optional pre TS 4.3) Depends on the arguments for state and kind:
* - If kind is "m", this should be the function corresponding to the static or instance method.
* - If kind is "a", this should be the function corresponding to the setter method, or undefined if the setter was not defined.
* - If kind is "f" and state is a function, this should be an object holding the value of a static field, or undefined if the static field declaration has not yet been evaluated.
* Usage:
* This helper will only ever be used by the compiler in the following ways:
*
* Writing to a private instance field (pre TS 4.3):
* __classPrivateFieldSet(<any>, <WeakMap>, <any>)
*
* Writing to a private instance field (TS 4.3+):
* __classPrivateFieldSet(<any>, <WeakMap>, <any>, "f")
*
* Writing to a private instance set accessor (when defined, TS 4.3+):
* __classPrivateFieldSet(<any>, <WeakSet>, <any>, "a", <function>)
*
* Writing to a private instance set accessor (when not defined, TS 4.3+):
* __classPrivateFieldSet(<any>, <WeakSet>, <any>, "a", void 0)
* NOTE: This always results in a runtime error.
*
* Writing to a private instance method (TS 4.3+):
* __classPrivateFieldSet(<any>, <WeakSet>, <any>, "m", <function>)
* NOTE: This always results in a runtime error.
*
* Writing to a private static field (TS 4.3+):
* __classPrivateFieldSet(<any>, <constructor>, <any>, "f", <{ value: any }>)
*
* Writing to a private static set accessor (when defined, TS 4.3+):
* __classPrivateFieldSet(<any>, <constructor>, <any>, "a", <function>)
*
* Writing to a private static set accessor (when not defined, TS 4.3+):
* __classPrivateFieldSet(<any>, <constructor>, <any>, "a", void 0)
* NOTE: This always results in a runtime error.
*
* Writing to a private static method (TS 4.3+):
* __classPrivateFieldSet(<any>, <constructor>, <any>, "m", <function>)
* NOTE: This always results in a runtime error.
*/
export const classPrivateFieldSetHelper: UnscopedEmitHelper = {
name: "typescript:classPrivateFieldSet",
importName: "__classPrivateFieldSet",
scoped: false,
text: `
var __classPrivateFieldSet = (this && this.__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 __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
if (kind === "m") throw new TypeError("Private method is not writable");
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
};`
};

Expand Down Expand Up @@ -897,4 +1007,4 @@ namespace ts {
&& (getEmitFlags(firstSegment.expression) & EmitFlags.HelperName)
&& firstSegment.expression.escapedText === helperName;
}
}
}
Loading