diff --git a/compiler/src/process_component_class.ts b/compiler/src/process_component_class.ts index afec119090663210c7f96b7a37ba15e40bf7b7c5..f02a8ee2fac1b07ac6c1b22717756193010296fa 100644 --- a/compiler/src/process_component_class.ts +++ b/compiler/src/process_component_class.ts @@ -34,8 +34,6 @@ import { COMPONENT_TRANSITION_FUNCTION, COMPONENT_CREATE_FUNCTION, GEOMETRY_VIEW, - BUILDER_ATTR_NAME, - BUILDER_ATTR_BIND, COMPONENT_STYLES_DECORATOR, STYLES, CUSTOM_COMPONENT_EARLIER_CREATE_CHILD @@ -233,64 +231,10 @@ function processBuildMember(node: ts.MethodDeclaration, context: ts.Transformati if (isCustomComponentNode(node) || isCustomBuilderNode(node)) { return node; } - if ((ts.isIdentifier(node) || ts.isPropertyAccessExpression(node)) && - validateBuilderFunctionNode(node)) { - return getParsedBuilderAttrArgument(node); - } return ts.visitEachChild(node, visitBuildSecond, context); } } -function validateBuilderFunctionNode(node: ts.PropertyAccessExpression | ts.Identifier): boolean { - if (((ts.isPropertyAccessExpression(node) && node.expression && node.name && - node.expression.kind === ts.SyntaxKind.ThisKeyword && ts.isIdentifier(node.name) && - CUSTOM_BUILDER_METHOD.has(node.name.escapedText.toString())) || - ts.isIdentifier(node) && CUSTOM_BUILDER_METHOD.has(node.escapedText.toString())) && - !((ts.isPropertyAccessExpression(node) && validateBuilderParam(node)) || - (ts.isIdentifier(node) && node.parent && ts.isPropertyAccessExpression(node.parent) && - validateBuilderParam(node.parent)))) { - return true; - } else { - return false; - } -} - -function validateBuilderParam(node: ts.PropertyAccessExpression): boolean { - if (node.parent && ts.isCallExpression(node.parent) && node.parent.expression === node) { - return true; - } else { - return false; - } -} - -function getParsedBuilderAttrArgument(node: ts.PropertyAccessExpression | ts.Identifier): - ts.ObjectLiteralExpression { - let newObjectNode: ts.ObjectLiteralExpression = null; - if (ts.isPropertyAccessExpression(node)) { - newObjectNode = ts.factory.createObjectLiteralExpression([ - ts.factory.createPropertyAssignment( - ts.factory.createIdentifier(BUILDER_ATTR_NAME), - ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression( - node, - ts.factory.createIdentifier(BUILDER_ATTR_BIND) - ), - undefined, - [ts.factory.createThis()] - ) - ) - ]); - } else if (ts.isIdentifier(node)) { - newObjectNode = ts.factory.createObjectLiteralExpression([ - ts.factory.createPropertyAssignment( - ts.factory.createIdentifier(BUILDER_ATTR_NAME), - node - ) - ]) - } - return newObjectNode; -} - function isCustomComponentNode(node:ts.NewExpression | ts.ExpressionStatement): boolean { if ((ts.isNewExpression(node) && ts.isIdentifier(node.expression) && node.expression.escapedText && componentCollection.customComponents.has(node.expression.escapedText.toString())) || diff --git a/compiler/src/process_ui_syntax.ts b/compiler/src/process_ui_syntax.ts index 481074b65ce6e725b48b58f949790ef9cd165221..74d5354da02a0e4320d550a99d5705b068beb49d 100644 --- a/compiler/src/process_ui_syntax.ts +++ b/compiler/src/process_ui_syntax.ts @@ -33,7 +33,9 @@ import { RESOURCE_NAME_ID, RESOURCE_NAME_TYPE, RESOURCE_NAME_PARAMS, - RESOURCE_RAWFILE + RESOURCE_RAWFILE, + BUILDER_ATTR_NAME, + BUILDER_ATTR_BIND } from './pre_define'; import { componentInfo, @@ -125,6 +127,15 @@ export function processUISyntax(program: ts.Program, ut = false): Function { } else if (isWorker(node)) { node = processWorker(node as ts.NewExpression); } + if (ts.isCallExpression(node) && node.expression && (ts.isIdentifier(node.expression) || + ts.isPropertyAccessExpression(node.expression)) && validateBuilderFunctionNode(node.expression) && + node.parent && !ts.isMethodDeclaration(node.parent)) { + return getParsedBuilderAttrArgumentWithParams(node); + } + if ((ts.isIdentifier(node) || ts.isPropertyAccessExpression(node)) && validateBuilderFunctionNode(node) && + !validateBuilderParentNode(node) && node.parent && !ts.isMethodDeclaration(node.parent)) { + return getParsedBuilderAttrArgument(node); + } return ts.visitEachChild(node, processAllNodes, context); } function processResourceNode(node: ts.Node): ts.Node { @@ -150,6 +161,82 @@ export function processUISyntax(program: ts.Program, ut = false): Function { }; } +function validateBuilderFunctionNode(node: ts.PropertyAccessExpression | ts.Identifier): boolean { + if ((ts.isPropertyAccessExpression(node) && node.expression && node.name && + node.expression.kind === ts.SyntaxKind.ThisKeyword && ts.isIdentifier(node.name) && + CUSTOM_BUILDER_METHOD.has(node.name.escapedText.toString())) || + ts.isIdentifier(node) && CUSTOM_BUILDER_METHOD.has(node.escapedText.toString())) { + return true; + } else { + return false; + } +} + +function validateBuilderParentNode(node: ts.PropertyAccessExpression | ts.Identifier): boolean { + if ((ts.isPropertyAccessExpression(node) && validateBuilderParam(node)) || + (ts.isIdentifier(node) && node.parent && ts.isPropertyAccessExpression(node.parent) && + validateBuilderParam(node.parent))) { + return true; + } + return false; +} + +function validateBuilderParam(node: ts.PropertyAccessExpression): boolean { + if (node.parent && ts.isCallExpression(node.parent) && node.parent.expression === node) { + return true; + } else { + return false; + } +} + +function getParsedBuilderAttrArgument(node: ts.PropertyAccessExpression | ts.Identifier): + ts.ObjectLiteralExpression { + let newObjectNode: ts.ObjectLiteralExpression = null; + if (ts.isPropertyAccessExpression(node)) { + newObjectNode = ts.factory.createObjectLiteralExpression([ + ts.factory.createPropertyAssignment( + ts.factory.createIdentifier(BUILDER_ATTR_NAME), + ts.factory.createCallExpression( + ts.factory.createPropertyAccessExpression( + node, + ts.factory.createIdentifier(BUILDER_ATTR_BIND) + ), + undefined, + [ts.factory.createThis()] + ) + ) + ]); + } else if (ts.isIdentifier(node)) { + newObjectNode = ts.factory.createObjectLiteralExpression([ + ts.factory.createPropertyAssignment( + ts.factory.createIdentifier(BUILDER_ATTR_NAME), + node + ) + ]) + } + return newObjectNode; +} + +function getParsedBuilderAttrArgumentWithParams(node: ts.CallExpression): + ts.ObjectLiteralExpression { + return ts.factory.createObjectLiteralExpression([ + ts.factory.createPropertyAssignment( + ts.factory.createIdentifier(BUILDER_ATTR_NAME), + ts.factory.createArrowFunction( + undefined, + undefined, + [], + undefined, + ts.factory.createToken(ts.SyntaxKind.EqualsGreaterThanToken), + ts.factory.createBlock( + [ts.factory.createExpressionStatement(node)], + true + ) + ) + ) + ]); +} + function collectComponents(node: ts.SourceFile): void { // @ts-ignore if (node.identifiers && node.identifiers.size) {