Skip to content

Commit

Permalink
Temporarily disable 'context.access'
Browse files Browse the repository at this point in the history
  • Loading branch information
rbuckton committed Jan 18, 2023
1 parent 02e93fd commit 673073c
Show file tree
Hide file tree
Showing 101 changed files with 630 additions and 609 deletions.
205 changes: 152 additions & 53 deletions src/compiler/factory/emitHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
isComputedPropertyName,
isIdentifier,
memoize,
ObjectLiteralElementLike,
PrivateIdentifier,
ScriptTarget,
setEmitFlags,
Expand Down Expand Up @@ -251,65 +250,165 @@ export function createEmitHelperFactory(context: TransformationContext): EmitHel
]);
}

function createESDecorateClassElementAccessGetMethod(elementName: ESDecorateName) {
const accessor = elementName.computed ?
factory.createElementAccessExpression(factory.createThis(), elementName.name) :
factory.createPropertyAccessExpression(factory.createThis(), elementName.name);

return factory.createMethodDeclaration(
/*modifiers*/ undefined,
/*asteriskToken*/ undefined,
"get",
/*questionToken*/ undefined,
/*typeParameters*/ undefined,
[],
/*type*/ undefined,
factory.createBlock([factory.createReturnStatement(accessor)])
);
}

function createESDecorateClassElementAccessSetMethod(elementName: ESDecorateName) {
const accessor = elementName.computed ?
factory.createElementAccessExpression(factory.createThis(), elementName.name) :
factory.createPropertyAccessExpression(factory.createThis(), elementName.name);

return factory.createMethodDeclaration(
/*modifiers*/ undefined,
/*asteriskToken*/ undefined,
"set",
/*questionToken*/ undefined,
/*typeParameters*/ undefined,
[factory.createParameterDeclaration(
/*modifiers*/ undefined,
/*dotDotDotToken*/ undefined,
factory.createIdentifier("value")
)],
/*type*/ undefined,
factory.createBlock([
factory.createExpressionStatement(
factory.createAssignment(
accessor,
factory.createIdentifier("value")
)
)
])
);
}

function createESDecorateClassElementAccessObject(name: ESDecorateName, access: ESDecorateClassElementAccess) {
const properties: ObjectLiteralElementLike[] = [];
if (access.get) properties.push(createESDecorateClassElementAccessGetMethod(name));
if (access.set) properties.push(createESDecorateClassElementAccessSetMethod(name));
return factory.createObjectLiteralExpression(properties);
}
// Per https://github.com/tc39/proposal-decorators/issues/494, we may need to change the emit for the `access` object
// so that it does not need to be used via `.call`. The following two sections represent the options presented in
// tc39/proposal-decorators#494
//
// === Current approach (`access.get.call(obj)`, `access.set.call(obj, value)`) ===
//
// function createESDecorateClassElementAccessGetMethod(elementName: ESDecorateName) {
// const accessor = elementName.computed ?
// factory.createElementAccessExpression(factory.createThis(), elementName.name) :
// factory.createPropertyAccessExpression(factory.createThis(), elementName.name);
//
// return factory.createMethodDeclaration(
// /*modifiers*/ undefined,
// /*asteriskToken*/ undefined,
// "get",
// /*questionToken*/ undefined,
// /*typeParameters*/ undefined,
// [],
// /*type*/ undefined,
// factory.createBlock([factory.createReturnStatement(accessor)])
// );
// }
//
// function createESDecorateClassElementAccessSetMethod(elementName: ESDecorateName) {
// const accessor = elementName.computed ?
// factory.createElementAccessExpression(factory.createThis(), elementName.name) :
// factory.createPropertyAccessExpression(factory.createThis(), elementName.name);
//
// return factory.createMethodDeclaration(
// /*modifiers*/ undefined,
// /*asteriskToken*/ undefined,
// "set",
// /*questionToken*/ undefined,
// /*typeParameters*/ undefined,
// [factory.createParameterDeclaration(
// /*modifiers*/ undefined,
// /*dotDotDotToken*/ undefined,
// factory.createIdentifier("value")
// )],
// /*type*/ undefined,
// factory.createBlock([
// factory.createExpressionStatement(
// factory.createAssignment(
// accessor,
// factory.createIdentifier("value")
// )
// )
// ])
// );
// }
//
// function createESDecorateClassElementAccessObject(name: ESDecorateName, access: ESDecorateClassElementAccess) {
// const properties: ObjectLiteralElementLike[] = [];
// if (access.get) properties.push(createESDecorateClassElementAccessGetMethod(name));
// if (access.set) properties.push(createESDecorateClassElementAccessSetMethod(name));
// return factory.createObjectLiteralExpression(properties);
// }
//
// === Suggested approach (`access.get(obj)`, `access.set(obj, value)`, `access.has(obj)`) ===
//
// function createESDecorateClassElementAccessGetMethod(elementName: ESDecorateName) {
// const accessor = elementName.computed ?
// factory.createElementAccessExpression(factory.createIdentifier("obj"), elementName.name) :
// factory.createPropertyAccessExpression(factory.createIdentifier("obj"), elementName.name);
//
// return factory.createMethodDeclaration(
// /*modifiers*/ undefined,
// /*asteriskToken*/ undefined,
// "get",
// /*questionToken*/ undefined,
// /*typeParameters*/ undefined,
// [factory.createParameterDeclaration(
// /*modifiers*/ undefined,
// /*dotDotDotToken*/ undefined,
// factory.createIdentifier("obj")
// )],
// /*type*/ undefined,
// factory.createBlock([factory.createReturnStatement(accessor)])
// );
// }
//
// function createESDecorateClassElementAccessSetMethod(elementName: ESDecorateName) {
// const accessor = elementName.computed ?
// factory.createElementAccessExpression(factory.createIdentifier("obj"), elementName.name) :
// factory.createPropertyAccessExpression(factory.createIdentifier("obj"), elementName.name);
//
// return factory.createMethodDeclaration(
// /*modifiers*/ undefined,
// /*asteriskToken*/ undefined,
// "set",
// /*questionToken*/ undefined,
// /*typeParameters*/ undefined,
// [factory.createParameterDeclaration(
// /*modifiers*/ undefined,
// /*dotDotDotToken*/ undefined,
// factory.createIdentifier("obj")
// ),
// factory.createParameterDeclaration(
// /*modifiers*/ undefined,
// /*dotDotDotToken*/ undefined,
// factory.createIdentifier("value")
// )],
// /*type*/ undefined,
// factory.createBlock([
// factory.createExpressionStatement(
// factory.createAssignment(
// accessor,
// factory.createIdentifier("value")
// )
// )
// ])
// );
// }
//
// function createESDecorateClassElementAccessHasMethod(elementName: ESDecorateName) {
// const propertyName =
// elementName.computed ? elementName.name :
// isIdentifier(elementName.name) ? factory.createStringLiteralFromNode(elementName.name) :
// elementName.name;
//
// return factory.createMethodDeclaration(
// /*modifiers*/ undefined,
// /*asteriskToken*/ undefined,
// "has",
// /*questionToken*/ undefined,
// /*typeParameters*/ undefined,
// [factory.createParameterDeclaration(
// /*modifiers*/ undefined,
// /*dotDotDotToken*/ undefined,
// factory.createIdentifier("obj")
// )],
// /*type*/ undefined,
// factory.createBlock([factory.createReturnStatement(
// factory.createBinaryExpression(
// propertyName,
// SyntaxKind.InKeyword,
// factory.createIdentifier("obj")
// )
// )])
// );
// }
//
// function createESDecorateClassElementAccessObject(name: ESDecorateName, access: ESDecorateClassElementAccess) {
// const properties: ObjectLiteralElementLike[] = [];
// if (access.get) properties.push(createESDecorateClassElementAccessGetMethod(name));
// if (access.set) properties.push(createESDecorateClassElementAccessSetMethod(name));
// property.push(createESDecorateClassElementAccessHasMethod(name));
// return factory.createObjectLiteralExpression(properties);
// }

function createESDecorateClassElementContextObject(contextIn: ESDecorateClassElementContext) {
return factory.createObjectLiteralExpression([
factory.createPropertyAssignment(factory.createIdentifier("kind"), factory.createStringLiteral(contextIn.kind)),
factory.createPropertyAssignment(factory.createIdentifier("name"), contextIn.name.computed ? contextIn.name.name : factory.createStringLiteralFromNode(contextIn.name.name)),
factory.createPropertyAssignment(factory.createIdentifier("static"), contextIn.static ? factory.createTrue() : factory.createFalse()),
factory.createPropertyAssignment(factory.createIdentifier("private"), contextIn.private ? factory.createTrue() : factory.createFalse()),
factory.createPropertyAssignment(factory.createIdentifier("access"), createESDecorateClassElementAccessObject(contextIn.name, contextIn.access))

// Disabled, pending resolution of https://github.com/tc39/proposal-decorators/issues/494
// factory.createPropertyAssignment(factory.createIdentifier("access"), createESDecorateClassElementAccessObject(contextIn.name, contextIn.access))
]);
}

Expand Down
6 changes: 2 additions & 4 deletions src/compiler/transformers/classFields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1052,16 +1052,14 @@ export function transformClassFields(context: TransformationContext): (x: Source
return undefined;
}

factory.updatePropertyDeclaration(
return factory.updatePropertyDeclaration(
node,
visitNodes(node.modifiers, modifierVisitor, isModifier),
visitNode(node.name, propertyNameVisitor, isPropertyName),
/*questionOrExclamationToken*/ undefined,
/*type*/ undefined,
visitNode(node.initializer, visitor, isExpression)
)

return visitEachChild(node, classElementVisitor, context);
);
}

function transformFieldInitializer(node: PropertyDeclaration) {
Expand Down
Loading

0 comments on commit 673073c

Please sign in to comment.