- Notifications
You must be signed in to change notification settings - Fork13.2k
ES private class elements#42458
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from1 commit
e3a3fb84ab27a26977b9fbc8c07e2760ed804af85df048d72f8cfb814aa4c3b0ac12717da5bd9e133f60aa5dd8aa3889a78952caadee87bbaec7e9bf3bbf0fa1b59cd6b74498d51f6cdc1757704301af53a056b295df2d1590124e219ea8325da8caa2228beff4f737704727c73925dc67fd38d45c2a41356b91db3914645ebb3d67006c09b87661331a8c009219e995a27a32964fc27d2ab6fc23f6c67374e6f362b4f2fb2a692d363041e64757e068a7f807e7b0b130c7f69c924058e65116f4748c8709a5aae497e47f294cc48027bdb37bbc944dd526e7f6ecfab6c3f661ef69c6c2a646edd8136f7fbd749cf96eafe2de8a437490a36c2732581addbb3c461b92a61fd5f89f746aff24636a60e429f5816ab3b62d3c7d8a2b9d0d205593a7ca36da2cdd30805b78d77a8e8078ae0File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -11,13 +11,17 @@ namespace ts { | ||
| const enum PrivateIdentifierPlacement { | ||
| InstanceField, | ||
| InstanceMethod, | ||
| InstanceGetterOnly, | ||
| InstanceSetterOnly, | ||
| InstanceGetterAndSetter | ||
| } | ||
| type PrivateIdentifierInfo = | ||
| | PrivateIdentifierInstanceField | ||
| | PrivateIdentifierInstanceMethod | ||
| | PrivateIdentifierInstanceGetterOnly | ||
| | PrivateIdentifierInstanceSetterOnly | ||
| | PrivateIdentifierInstanceGetterAndSetter; | ||
| interface PrivateIdentifierInstanceField { | ||
| placement: PrivateIdentifierPlacement.InstanceField; | ||
| @@ -29,10 +33,20 @@ namespace ts { | ||
| functionName: Identifier; | ||
| } | ||
| interface PrivateIdentifierInstanceGetterOnly { | ||
| placement: PrivateIdentifierPlacement.InstanceGetterOnly; | ||
| getterName: Identifier; | ||
| } | ||
| interface PrivateIdentifierInstanceSetterOnly { | ||
| placement: PrivateIdentifierPlacement.InstanceSetterOnly; | ||
| setterName: Identifier; | ||
| } | ||
| interface PrivateIdentifierInstanceGetterAndSetter { | ||
| placement: PrivateIdentifierPlacement.InstanceGetterAndSetter; | ||
| getterName: Identifier; | ||
| setterName: Identifier; | ||
| } | ||
| interface PrivateIdentifierEnvironment { | ||
| @@ -229,46 +243,46 @@ namespace ts { | ||
| } | ||
| const functionName = getHoistedFunctionName(node); | ||
| if (functionName) { | ||
| getPendingExpressions().push( | ||
| factory.createAssignment( | ||
| functionName, | ||
| factory.createFunctionExpression( | ||
| transformedMethod.modifiers, | ||
| transformedMethod.asteriskToken, | ||
| functionName, | ||
| transformedMethod.typeParameters, | ||
| transformedMethod.parameters, | ||
| transformedMethod.type, | ||
| transformedMethod.body | ||
| ) | ||
| ) | ||
| ); | ||
| } | ||
| // remove method declaration from class | ||
| return undefined; | ||
| } | ||
| function getHoistedFunctionName(node: MethodDeclaration | AccessorDeclaration) { | ||
| Debug.assert(isPrivateIdentifier(node.name)); | ||
| constinfo = accessPrivateIdentifier(node.name); | ||
| Debug.assert(info, "Undeclared private name for property declaration."); | ||
| if (info.placement === PrivateIdentifierPlacement.InstanceMethod) { | ||
| returninfo.functionName; | ||
| } | ||
| if (isGetAccessor(node) && | ||
| (info.placement === PrivateIdentifierPlacement.InstanceGetterOnly || | ||
| info.placement === PrivateIdentifierPlacement.InstanceGetterAndSetter)) { | ||
| returninfo.getterName; | ||
| } | ||
| if (isSetAccessor(node) && | ||
| (info.placement === PrivateIdentifierPlacement.InstanceSetterOnly || | ||
| info.placement === PrivateIdentifierPlacement.InstanceGetterAndSetter)) { | ||
| return info.setterName; | ||
| } | ||
| } | ||
| function visitPropertyDeclaration(node: PropertyDeclaration) { | ||
| @@ -314,13 +328,17 @@ namespace ts { | ||
| getPrivateIdentifierEnvironment().weakSetName, | ||
| info.functionName | ||
| ); | ||
| case PrivateIdentifierPlacement.InstanceGetterOnly: | ||
| case PrivateIdentifierPlacement.InstanceGetterAndSetter: | ||
| return context.getEmitHelperFactory().createClassPrivateAccessorGetHelper( | ||
| receiver, | ||
| getPrivateIdentifierEnvironment().weakSetName, | ||
| info.getterName | ||
| ); | ||
| case PrivateIdentifierPlacement.InstanceSetterOnly: | ||
| return context.getEmitHelperFactory().createClassPrivateWriteonlyHelper( | ||
| receiver | ||
| ); | ||
| default: return Debug.fail("Unexpected private identifier placement"); | ||
| } | ||
| } | ||
| @@ -523,15 +541,17 @@ namespace ts { | ||
| right | ||
| ); | ||
| case PrivateIdentifierPlacement.InstanceMethod: | ||
| case PrivateIdentifierPlacement.InstanceGetterOnly: | ||
| return context.getEmitHelperFactory().createClassPrivateReadonlyHelper( | ||
| receiver, | ||
| right | ||
| ); | ||
| case PrivateIdentifierPlacement.InstanceSetterOnly: | ||
| case PrivateIdentifierPlacement.InstanceGetterAndSetter: | ||
| return context.getEmitHelperFactory().createClassPrivateAccessorSetHelper( | ||
| receiver, | ||
| getPrivateIdentifierEnvironment().weakSetName, | ||
| info.setterName, | ||
| right | ||
| ); | ||
| default: return Debug.fail("Unexpected private identifier placement"); | ||
| @@ -912,7 +932,6 @@ namespace ts { | ||
| privateIdentifierInfo.weakMapName | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| else { | ||
| @@ -1103,16 +1122,42 @@ namespace ts { | ||
| } | ||
| else if (isAccessor(node)) { | ||
| const previousInfo = findPreviousAccessorInfo(node); | ||
| if (isGetAccessor(node)) { | ||
| const getterName = createHoistedVariableForPrivateName(text + "_get"); | ||
| if (previousInfo) { | ||
| Debug.assert(previousInfo.placement === PrivateIdentifierPlacement.InstanceSetterOnly); | ||
| info = { | ||
| placement: PrivateIdentifierPlacement.InstanceGetterAndSetter, | ||
| getterName, | ||
| setterName: previousInfo.setterName | ||
| }; | ||
| } | ||
| else { | ||
| info = { | ||
| placement: PrivateIdentifierPlacement.InstanceGetterOnly, | ||
| getterName | ||
| }; | ||
| } | ||
| } | ||
| else { | ||
| const setterName = createHoistedVariableForPrivateName(text + "_set"); | ||
| if (previousInfo) { | ||
| Debug.assert(previousInfo.placement === PrivateIdentifierPlacement.InstanceGetterOnly); | ||
| info = { | ||
| placement: PrivateIdentifierPlacement.InstanceGetterAndSetter, | ||
| setterName, | ||
| getterName: previousInfo.getterName | ||
| }; | ||
| } | ||
| else { | ||
| info = { | ||
| placement: PrivateIdentifierPlacement.InstanceSetterOnly, | ||
| setterName | ||
| }; | ||
| } | ||
| } | ||
| getPrivateIdentifierEnvironment().hasPrivateMethods = true; | ||
| @@ -1125,9 +1170,12 @@ namespace ts { | ||
| getPendingExpressions().push(...assignmentExpressions); | ||
| } | ||
| function findPreviousAccessorInfo( | ||
| node: PrivateIdentifierGetAccessorDeclaration | PrivateIdentifierSetAccessorDeclaration | ||
| ): PrivateIdentifierInstanceGetterOnly | PrivateIdentifierInstanceSetterOnly | undefined { | ||
| const info = getPrivateIdentifierEnvironment().identifiers.get(node.name.escapedText); | ||
| if (info?.placement === PrivateIdentifierPlacement.InstanceGetterOnly || | ||
| ||
| info?.placement === PrivateIdentifierPlacement.InstanceSetterOnly) { | ||
| return info; | ||
| } | ||
| } | ||