- 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
- 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 |
|---|---|---|
| @@ -24,6 +24,11 @@ namespace ts { | ||
| * Stores if the identifier is static or not | ||
| */ | ||
| isStatic: boolean; | ||
| /** | ||
| * Stores if the identifier declaration is valid or not. Reserved names (e.g. #constructor) | ||
| * or duplicate identifiers are considered invalid. | ||
| */ | ||
| isValid: boolean; | ||
| } | ||
| interface PrivateIdentifierAccessorInfo extends PrivateIdentifierInfoBase { | ||
| kind: PrivateIdentifierKind.Accessor; | ||
| @@ -260,6 +265,13 @@ namespace ts { | ||
| return visitEachChild(node, classElementVisitor, context); | ||
| } | ||
| // leave invalid code untransformed | ||
| const info = accessPrivateIdentifier(node.name); | ||
| Debug.assert(info, "Undeclared private name for property declaration."); | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Can a user write source text that would fail this assertion? Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I don't think so. That would indicate a bug in | ||
| if (!info.isValid) { | ||
| return node; | ||
| } | ||
| const functionName = getHoistedFunctionName(node); | ||
| if (functionName) { | ||
| getPendingExpressions().push( | ||
| @@ -303,17 +315,27 @@ namespace ts { | ||
| function visitPropertyDeclaration(node: PropertyDeclaration) { | ||
| Debug.assert(!some(node.decorators)); | ||
| if (isPrivateIdentifier(node.name)) { | ||
| if (!shouldTransformPrivateElements) { | ||
| // Initializer is elided as the field is initialized in transformConstructor. | ||
| return factory.updatePropertyDeclaration( | ||
| node, | ||
| /*decorators*/ undefined, | ||
| visitNodes(node.modifiers, visitor, isModifier), | ||
| node.name, | ||
| /*questionOrExclamationToken*/ undefined, | ||
| /*type*/ undefined, | ||
| /*initializer*/ undefined | ||
| ); | ||
| } | ||
| // leave invalid code untransformed | ||
| const info = accessPrivateIdentifier(node.name); | ||
| Debug.assert(info, "Undeclared private name for property declaration."); | ||
| if (!info.isValid) { | ||
| return node; | ||
| } | ||
| } | ||
| // Create a temporary variable to store a computed property name (if necessary). | ||
| // If it's not inlineable, then we emit an expression after the class which assigns | ||
| @@ -1162,55 +1184,62 @@ namespace ts { | ||
| const env = getPrivateIdentifierEnvironment(); | ||
| const { weakSetName, classConstructor } = env; | ||
| const assignmentExpressions: Expression[] = []; | ||
| const privateName = node.name.escapedText; | ||
| const previousInfo = env.identifiers.get(privateName); | ||
| const isValid = !isReservedPrivateName(node.name) && previousInfo === undefined; | ||
| if (hasStaticModifier(node)) { | ||
| Debug.assert(classConstructor, "weakSetName should be set in private identifier environment"); | ||
| if (isPropertyDeclaration(node)) { | ||
| const variableName = createHoistedVariableForPrivateName(text, node); | ||
| env.identifiers.set(privateName, { | ||
| kind: PrivateIdentifierKind.Field, | ||
| variableName, | ||
| brandCheckIdentifier: classConstructor, | ||
| isStatic: true, | ||
| isValid, | ||
| }); | ||
| } | ||
| else if (isMethodDeclaration(node)) { | ||
| const functionName = createHoistedVariableForPrivateName(text, node); | ||
| env.identifiers.set(privateName, { | ||
| kind: PrivateIdentifierKind.Method, | ||
| methodName: functionName, | ||
| brandCheckIdentifier: classConstructor, | ||
| isStatic: true, | ||
| isValid, | ||
| }); | ||
| } | ||
| else if (isGetAccessorDeclaration(node)) { | ||
| const getterName = createHoistedVariableForPrivateName(text + "_get", node); | ||
| if (previousInfo?.kind === PrivateIdentifierKind.Accessor && previousInfo.isStatic && !previousInfo.getterName) { | ||
| previousInfo.getterName = getterName; | ||
| } | ||
| else { | ||
| env.identifiers.set(privateName, { | ||
| kind: PrivateIdentifierKind.Accessor, | ||
| getterName, | ||
| setterName: undefined, | ||
| brandCheckIdentifier: classConstructor, | ||
| isStatic: true, | ||
| isValid, | ||
| }); | ||
| } | ||
| } | ||
| else if (isSetAccessorDeclaration(node)) { | ||
| const setterName = createHoistedVariableForPrivateName(text + "_set", node); | ||
| if (previousInfo?.kind === PrivateIdentifierKind.Accessor && previousInfo.isStatic && !previousInfo.setterName) { | ||
| previousInfo.setterName = setterName; | ||
| } | ||
| else { | ||
| env.identifiers.set(privateName, { | ||
| kind: PrivateIdentifierKind.Accessor, | ||
| getterName: undefined, | ||
| setterName, | ||
| brandCheckIdentifier: classConstructor, | ||
| isStatic: true, | ||
| isValid, | ||
| }); | ||
| } | ||
| } | ||
| @@ -1220,11 +1249,12 @@ namespace ts { | ||
| } | ||
| else if (isPropertyDeclaration(node)) { | ||
| const weakMapName = createHoistedVariableForPrivateName(text, node); | ||
| env.identifiers.set(privateName, { | ||
| kind: PrivateIdentifierKind.Field, | ||
| brandCheckIdentifier: weakMapName, | ||
| isStatic: false, | ||
| variableName: undefined, | ||
| isValid, | ||
| }); | ||
| assignmentExpressions.push(factory.createAssignment( | ||
| @@ -1239,46 +1269,48 @@ namespace ts { | ||
| else if (isMethodDeclaration(node)) { | ||
| Debug.assert(weakSetName, "weakSetName should be set in private identifier environment"); | ||
| env.identifiers.set(privateName, { | ||
| kind: PrivateIdentifierKind.Method, | ||
| methodName: createHoistedVariableForPrivateName(text, node), | ||
| brandCheckIdentifier: weakSetName, | ||
| isStatic: false, | ||
| isValid, | ||
| }); | ||
| } | ||
| else if (isAccessor(node)) { | ||
| Debug.assert(weakSetName, "weakSetName should be set in private identifier environment"); | ||
| if (isGetAccessor(node)) { | ||
| const getterName = createHoistedVariableForPrivateName(text + "_get", node); | ||
| if (previousInfo?.kind === PrivateIdentifierKind.Accessor && !previousInfo.isStatic && !previousInfo.getterName) { | ||
| previousInfo.getterName = getterName; | ||
| } | ||
| else { | ||
| env.identifiers.set(privateName, { | ||
| kind: PrivateIdentifierKind.Accessor, | ||
| getterName, | ||
| setterName: undefined, | ||
| brandCheckIdentifier: weakSetName, | ||
| isStatic: false, | ||
| isValid, | ||
| }); | ||
| } | ||
| } | ||
| else { | ||
| const setterName = createHoistedVariableForPrivateName(text + "_set", node); | ||
| if (previousInfo?.kind === PrivateIdentifierKind.Accessor && !previousInfo.isStatic && !previousInfo.setterName) { | ||
| previousInfo.setterName = setterName; | ||
| } | ||
| else { | ||
| env.identifiers.set(privateName, { | ||
| kind: PrivateIdentifierKind.Accessor, | ||
| getterName: undefined, | ||
| setterName, | ||
| brandCheckIdentifier: weakSetName, | ||
| isStatic: false, | ||
| isValid, | ||
| }); | ||
| } | ||
| } | ||
| @@ -1475,4 +1507,8 @@ namespace ts { | ||
| [receiver] | ||
| ); | ||
| } | ||
| function isReservedPrivateName(node: PrivateIdentifier) { | ||
| return node.escapedText === "#constructor"; | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading.Please reload this page.