Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
Transpile private accessors
Signed-off-by: Kubilay Kahveci <kahvecikubilay@gmail.com>
  • Loading branch information
@mkubilayk@dragomirtitian
mkubilayk authored anddragomirtitian committedFeb 3, 2021
commitdee87bb6a0e8ea0181de4f44f0dc08ab2f309029
41 changes: 41 additions & 0 deletionssrc/compiler/factory/emitHelpers.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -36,6 +36,8 @@ namespace ts {
createClassPrivateFieldSetHelper(receiver: Expression, privateField: Identifier, value: Expression): Expression;
createClassPrivateMethodGetHelper(receiver: Expression, instances: Identifier, fn: Identifier): Expression;
createClassPrivateReadonlyHelper(receiver: Expression, value: Expression): Expression;
createClassPrivateAccessorGetHelper(receiver: Expression, instances: Identifier, fn: Identifier): Expression;
createClassPrivateAccessorSetHelper(receiver: Expression, instances: Identifier, fn: Identifier, value: Expression): Expression;
}

export function createEmitHelperFactory(context: TransformationContext): EmitHelperFactory {
Expand DownExpand Up@@ -76,6 +78,8 @@ namespace ts {
createClassPrivateFieldSetHelper,
createClassPrivateMethodGetHelper,
createClassPrivateReadonlyHelper,
createClassPrivateAccessorGetHelper,
createClassPrivateAccessorSetHelper,
};

/**
Expand DownExpand Up@@ -392,6 +396,15 @@ namespace ts {
return factory.createCallExpression(getUnscopedHelperName("__classPrivateReadonly"), /*typeArguments*/ undefined, [receiver, value]);
}

function createClassPrivateAccessorGetHelper(receiver: Expression, instances: Identifier, fn: Identifier) {
context.requestEmitHelper(classPrivateAccessorGetHelper);
return factory.createCallExpression(getUnscopedHelperName("__classPrivateAccessorGet"), /*typeArguments*/ undefined, [receiver, instances, fn]);
}

function createClassPrivateAccessorSetHelper(receiver: Expression, instances: Identifier, fn: Identifier, value: Expression) {
context.requestEmitHelper(classPrivateAccessorSetHelper);
return factory.createCallExpression(getUnscopedHelperName("__classPrivateAccessorSet"), /*typeArguments*/ undefined, [receiver, instances, fn, value]);
}
}

/* @internal */
Expand DownExpand Up@@ -882,6 +895,32 @@ namespace ts {
};`
};

export const classPrivateAccessorGetHelper: UnscopedEmitHelper = {
name: "typescript:classPrivateAccessorGet",
importName: "__classPrivateAccessorGet",
scoped: false,
text: `
var __classPrivateAccessorGet = (this && this.__classPrivateAccessorGet) || function (receiver, instances, fn) {
if (!instances.has(receiver)) {
throw new TypeError("attempted to get private accessor on non-instance");
}
return fn.call(receiver);
};`
};

export const classPrivateAccessorSetHelper: UnscopedEmitHelper = {
name: "typescript:classPrivateAccessorSet",
importName: "__classPrivateAccessorSet",
scoped: false,
text: `
var __classPrivateAccessorSet = (this && this.__classPrivateAccessorSet) || function (receiver, instances, fn, value) {
if (!instances.has(receiver)) {
throw new TypeError("attempted to set private accessor on non-instance");
}
return fn.call(receiver, value);
};`
};

let allUnscopedEmitHelpers: ReadonlyESMap<string, UnscopedEmitHelper> | undefined;

export function getAllUnscopedEmitHelpers() {
Expand DownExpand Up@@ -909,6 +948,8 @@ namespace ts {
classPrivateFieldSetHelper,
classPrivateMethodGetHelper,
classPrivateReadonlyHelper,
classPrivateAccessorGetHelper,
classPrivateAccessorSetHelper,
createBindingHelper,
setModuleDefaultHelper
], helper => helper.name));
Expand Down
80 changes: 69 additions & 11 deletionssrc/compiler/transformers/classFields.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -11,11 +11,13 @@ namespace ts {
const enum PrivateIdentifierPlacement {
InstanceField,
InstanceMethod,
InstanceAccessor
}

type PrivateIdentifierInfo =
| PrivateIdentifierInstanceField
| PrivateIdentifierInstanceMethod;
| PrivateIdentifierInstanceMethod
| PrivateIdentifierInstanceAccessor;

interface PrivateIdentifierInstanceField {
placement: PrivateIdentifierPlacement.InstanceField;
Expand All@@ -27,6 +29,12 @@ namespace ts {
functionName: Identifier;
}

interface PrivateIdentifierInstanceAccessor {
placement: PrivateIdentifierPlacement.InstanceAccessor;
getterName?: Identifier;
setterName?: Identifier;
}

interface PrivateIdentifierEnvironment {
/**
* Used for prefixing generated variable names.
Expand DownExpand Up@@ -166,8 +174,7 @@ namespace ts {

case SyntaxKind.GetAccessor:
case SyntaxKind.SetAccessor:
// Visit the name of the member (if it's a computed property name).
return visitEachChild(node, classElementVisitor, context);
return visitMethodDeclaration(node as AccessorDeclaration);

case SyntaxKind.MethodDeclaration:
return visitMethodDeclaration(node as MethodDeclaration);
Expand DownExpand Up@@ -213,7 +220,7 @@ namespace ts {
return node;
}

function visitMethodDeclaration(node: MethodDeclaration) {
function visitMethodDeclaration(node: MethodDeclaration | AccessorDeclaration) {
Debug.assert(!some(node.decorators));
const transformedMethod = visitEachChild(node, classElementVisitor, context);
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd rather the call tovisitEachChild happens inside theif clause below, and that we invoke the visitor for each element in theif (functionName) case below. It saves us from allocating a node that we will just toss away in the private method/accessor case.

mkubilayk reacted with thumbs up emoji

Expand DownExpand Up@@ -241,7 +248,7 @@ namespace ts {
return undefined;
}

function getHoistedFunctionName(node: MethodDeclaration) {
function getHoistedFunctionName(node: MethodDeclaration | AccessorDeclaration) {
Debug.assert(isPrivateIdentifier(node.name));
const privateIdentifierInfo = accessPrivateIdentifier(node.name);
Debug.assert(privateIdentifierInfo, "Undeclared private name for property declaration.");
Expand All@@ -250,6 +257,17 @@ namespace ts {
return privateIdentifierInfo.functionName;
}

if (privateIdentifierInfo.placement === PrivateIdentifierPlacement.InstanceAccessor) {
if (isGetAccessor(node)) {
Debug.assert(privateIdentifierInfo.getterName);
return privateIdentifierInfo.getterName;
}
if (isSetAccessor(node)) {
Debug.assert(privateIdentifierInfo.setterName);
return privateIdentifierInfo.setterName;
}
}

Debug.fail("Unexpected private identifier placement");
}

Expand DownExpand Up@@ -296,6 +314,13 @@ namespace ts {
getPrivateIdentifierEnvironment().weakSetName,
info.functionName
);
case PrivateIdentifierPlacement.InstanceAccessor: {
return context.getEmitHelperFactory().createClassPrivateAccessorGetHelper(
receiver,
getPrivateIdentifierEnvironment().weakSetName,
info.getterName! // TODO: TypeError is missing
);
}
default: return Debug.fail("Unexpected private identifier placement");
}
}
Expand DownExpand Up@@ -502,6 +527,13 @@ namespace ts {
receiver,
right
);
case PrivateIdentifierPlacement.InstanceAccessor:
return context.getEmitHelperFactory().createClassPrivateAccessorSetHelper(
receiver,
getPrivateIdentifierEnvironment().weakSetName,
info.setterName!, // TODO: TypeError if missing
right
);
default: return Debug.fail("Unexpected private identifier placement");
}
}
Expand DownExpand Up@@ -732,7 +764,10 @@ namespace ts {
properties = filter(properties, property => !!property.initializer || isPrivateIdentifier(property.name));
}

const privateMethods = filter(getMethods(node, /*isStatic*/ false), method => isPrivateIdentifier(method.name));
const privateMethods = filter(
[...getMethods(node, /*isStatic*/ false), ...getAccessors(node, /*isStatic*/ false)],
method => isPrivateIdentifier(method.name)
);
const needsConstructorBody = some(properties) || some(privateMethods);

// Only generate synthetic constructor when there are property initializers to move.
Expand DownExpand Up@@ -928,7 +963,7 @@ namespace ts {
* @param methods An array of method declarations.
* @param receiver The receiver on which each method should be assigned.
*/
function addMethodStatements(statements: Statement[], methods: readonly (MethodDeclaration)[], receiver: LeftHandSideExpression) {
function addMethodStatements(statements: Statement[], methods: readonly (MethodDeclaration | AccessorDeclaration)[], receiver: LeftHandSideExpression) {
if (!shouldTransformPrivateElements || !some(methods)) {
return;
}
Expand DownExpand Up@@ -1039,18 +1074,18 @@ namespace ts {

function addPrivateIdentifierToEnvironment(node: PrivateClassElementDeclaration) {
const text = getTextOfPropertyName(node.name) as string;
const uniqueVariableName = createHoistedVariableForPrivateName(text);
let info: PrivateIdentifierInfo;
const assignmentExpressions: Expression[] = [];

if (isPropertyDeclaration(node)) {
const weakMapName = createHoistedVariableForPrivateName(text);
info = {
placement: PrivateIdentifierPlacement.InstanceField,
weakMapName: uniqueVariableName
weakMapName,
};

assignmentExpressions.push(factory.createAssignment(
uniqueVariableName,
weakMapName,
factory.createNewExpression(
factory.createIdentifier("WeakMap"),
/*typeArguments*/ undefined,
Expand All@@ -1061,11 +1096,27 @@ namespace ts {
else if (isMethodDeclaration(node)) {
info = {
placement: PrivateIdentifierPlacement.InstanceMethod,
functionName:uniqueVariableName
functionName:createHoistedVariableForPrivateName(text)
};

getPrivateIdentifierEnvironment().hasPrivateMethods = true;
}
else if (isAccessor(node)) {
const previousInfo = findPreviousAccessorInfo(node);
info = {
...previousInfo,
placement: PrivateIdentifierPlacement.InstanceAccessor,
};

if (isGetAccessor(node)) {
info.getterName = createHoistedVariableForPrivateName(text + "_get");
}
else {
info.setterName = createHoistedVariableForPrivateName(text + "_set");;
}

getPrivateIdentifierEnvironment().hasPrivateMethods = true;
}
else {
return;
}
Expand All@@ -1074,6 +1125,13 @@ namespace ts {
getPendingExpressions().push(...assignmentExpressions);
}

function findPreviousAccessorInfo(node: PrivateIdentifierGetAccessorDeclaration | PrivateIdentifierSetAccessorDeclaration): PrivateIdentifierInstanceAccessor | undefined {
const info = getPrivateIdentifierEnvironment().identifiers.get(node.name.escapedText);
if (info && info.placement === PrivateIdentifierPlacement.InstanceAccessor) {
return info;
}
}

function createHoistedVariableForClass(name: string): Identifier {
const { className } = getPrivateIdentifierEnvironment();
const prefix = className ? `_${className}` : "";
Expand Down
21 changes: 20 additions & 1 deletionsrc/compiler/transformers/utilities.ts
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -347,12 +347,22 @@ namespace ts {
* Gets all the static or all the instance method declarations of a class.
*
* @param node The class node.
* @param isStatic A value indicating whether to getproperties from the static or instance side of the class.
* @param isStatic A value indicating whether to getmethods from the static or instance side of the class.
*/
export function getMethods(node: ClassExpression | ClassDeclaration, isStatic: boolean): readonly MethodDeclaration[] {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this used anywhere else outside of the classFields transform? The one place it seems to be used would be better suited to have the filter moved into it instead. The same comment applies togetAccessors, below.

mkubilayk reacted with thumbs up emoji
return filter(node.members, m => isStaticMethodDeclaration(m, isStatic)) as MethodDeclaration[];
}

/**
* Gets all the static or all the instance accessor declarations of a class.
*
* @param node The class node.
* @param isStatic A value indicating whether to get accessors from the static or instance side of the class.
*/
export function getAccessors(node: ClassExpression | ClassDeclaration, isStatic: boolean): readonly AccessorDeclaration[] {
return filter(node.members, m => isStaticAccessorDeclaration(m, isStatic)) as AccessorDeclaration[];
}

/**
* Is a class element either a static or an instance property declaration with an initializer?
*
Expand DownExpand Up@@ -386,4 +396,13 @@ namespace ts {
return isMethodDeclaration(member) && hasStaticModifier(member) === isStatic;
}

/**
* Gets a value indicating whether a class element is either a static or an instance accessor declaration.
*
* @param member The class element node.
* @param isStatic A value indicating whether the member should be a static or instance member.
*/
export function isStaticAccessorDeclaration(member: ClassElement, isStatic: boolean): member is AccessorDeclaration {
return isGetOrSetAccessorDeclaration(member) && hasStaticModifier(member) === isStatic;
}
}

[8]ページ先頭

©2009-2025 Movatter.jp