From ca5afa12966dd2a7607d0a8e2456fd877e93894c Mon Sep 17 00:00:00 2001 From: root Date: Sun, 6 Feb 2022 00:29:52 +0800 Subject: [PATCH 1/2] yangbo198@huawei.com Signed-off-by: root Change-Id: I40909a127a170028aafb7d1f023dba4121d48de0 --- compiler/src/component_map.ts | 3 + compiler/src/process_component_build.ts | 127 +++++++++++++++++++++++- compiler/src/process_component_class.ts | 56 ----------- compiler/src/validate_ui_syntax.ts | 9 +- 4 files changed, 135 insertions(+), 60 deletions(-) diff --git a/compiler/src/component_map.ts b/compiler/src/component_map.ts index 550a0886d..f15b95483 100644 --- a/compiler/src/component_map.ts +++ b/compiler/src/component_map.ts @@ -89,6 +89,9 @@ export const JS_BIND_COMPONENTS: Set = new Set([ export const NEEDPOP_COMPONENT: Set = new Set(['Blank', 'Search']); +export const CUSTOM_BUILDER_PROPERTIES: Set = new Set(['bindPopup', 'bindMenu', 'bindContextMenu', 'title', + 'menus', 'toolBar', 'tabBar']); + (function initComponent() { Object.keys(COMPONENT_MAP).forEach((componentName) => { INNER_COMPONENT_NAMES.add(componentName); diff --git a/compiler/src/process_component_build.ts b/compiler/src/process_component_build.ts index ea2b141bc..6255f6c41 100644 --- a/compiler/src/process_component_build.ts +++ b/compiler/src/process_component_build.ts @@ -52,7 +52,9 @@ import { $$_VALUE, $$_CHANGE_EVENT, $$_THIS, - $$_NEW_VALUE + $$_NEW_VALUE, + BUILDER_ATTR_NAME, + BUILDER_ATTR_BIND } from './pre_define'; import { INNER_COMPONENT_NAMES, @@ -66,7 +68,8 @@ import { NEEDPOP_COMPONENT, INNER_STYLE_FUNCTION, GLOBAL_STYLE_FUNCTION, - COMMON_ATTRS + COMMON_ATTRS, + CUSTOM_BUILDER_PROPERTIES } from './component_map'; import { componentCollection } from './validate_ui_syntax'; import { processCustomComponent } from './process_custom_component'; @@ -516,6 +519,16 @@ export function bindComponentAttr(node: ts.ExpressionStatement, identifierNode: const statements: ts.Statement[] = []; const lastStatement: AnimationInfo = { statement: null, kind: false }; while (temp && ts.isCallExpression(temp) && temp.expression) { + if (temp.expression && (validatePropertyAccessExpressionWithCustomBuilder(temp.expression) || + validateIdentifierWithCustomBuilder(temp.expression))) { + let propertyName: string = ''; + if (ts.isIdentifier(temp.expression)) { + propertyName = temp.expression.escapedText.toString(); + } else if (ts.isPropertyAccessExpression(temp.expression)) { + propertyName = temp.expression.name.escapedText.toString(); + } + temp = propertyName === 'bindPopup'? processBindPopupBuilder(temp) : processCustomBuilderProperty(temp); + } if (ts.isPropertyAccessExpression(temp.expression) && temp.expression.name && ts.isIdentifier(temp.expression.name)) { addComponentAttr(temp, temp.expression.name, lastStatement, statements, identifierNode, log, @@ -538,6 +551,116 @@ export function bindComponentAttr(node: ts.ExpressionStatement, identifierNode: } } +function processCustomBuilderProperty(node: ts.CallExpression): ts.CallExpression { + let newArguments: ts.Expression[] = []; + node.arguments.forEach((argument, index) => { + if (index === 0) { + newArguments.push(parseBuilderNode(argument)); + } else { + newArguments.push(argument); + } + }); + node = ts.factory.updateCallExpression(node, node.expression, node.typeArguments, newArguments); + return node; +} + +function parseBuilderNode(node: ts.Node): ts.ObjectLiteralExpression { + if (ts.isPropertyAccessExpression(node) && node.expression && + node.expression.kind === ts.SyntaxKind.ThisKeyword && node.name && ts.isIdentifier(node.name) && + CUSTOM_BUILDER_METHOD.has(node.name.escapedText.toString())) { + return processPropertyBuilder(node); + } else if (ts.isIdentifier(node) && CUSTOM_BUILDER_METHOD.has(node.escapedText.toString())) { + return processIdentifierBuilder(node); + } else if (ts.isCallExpression(node)) { + return getParsedBuilderAttrArgumentWithParams(node); + } +} + +function processBindPopupBuilder(node: ts.CallExpression): ts.CallExpression { + let newArguments: ts.Expression[] = []; + node.arguments.forEach((argument, index) => { + if (index === 1) { + // @ts-ignore + newArguments.push(processBindPopupBuilderProperty(argument)); + } else { + newArguments.push(argument); + } + }); + node = ts.factory.updateCallExpression(node, node.expression, node.typeArguments, newArguments); + return node; +} + +function processBindPopupBuilderProperty(node: ts.ObjectLiteralExpression): ts.ObjectLiteralExpression { + let newProperties: ts.PropertyAssignment[] = []; + node.properties.forEach((property: ts.PropertyAssignment, index) => { + if (index === 0) { + if (property.name && ts.isIdentifier(property.name) && property.name.escapedText.toString() === 'builder') { + newProperties.push(ts.factory.updatePropertyAssignment(property, property.name, + parseBuilderNode(property.initializer))) + } else { + newProperties.push(property); + } + } else { + newProperties.push(property); + } + }); + return ts.factory.updateObjectLiteralExpression(node, newProperties); +} + +function processPropertyBuilder(node: ts.PropertyAccessExpression): ts.ObjectLiteralExpression { + return 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()] + ) + ) + ]); +} + +function processIdentifierBuilder(node: ts.Identifier): ts.ObjectLiteralExpression { + return ts.factory.createObjectLiteralExpression([ + ts.factory.createPropertyAssignment( + ts.factory.createIdentifier(BUILDER_ATTR_NAME), + node + ) + ]); +} + +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 validatePropertyAccessExpressionWithCustomBuilder(node: ts.Node): boolean { + return ts.isPropertyAccessExpression(node) && node.name && + ts.isIdentifier(node.name) && CUSTOM_BUILDER_PROPERTIES.has(node.name.escapedText.toString()); +} + +function validateIdentifierWithCustomBuilder(node: ts.Node): boolean { + return ts.isIdentifier(node) && CUSTOM_BUILDER_PROPERTIES.has(node.escapedText.toString()); +} + function createArrowFunctionFor$$($$varExp: ts.Expression): ts.ArrowFunction { return ts.factory.createArrowFunction( undefined, undefined, diff --git a/compiler/src/process_component_class.ts b/compiler/src/process_component_class.ts index 0e759bfc4..83cbaab23 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/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 148178cb2..1ed90e5d2 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -44,7 +44,8 @@ import { COMPONENT_EXTEND_DECORATOR, COMPONENT_OBSERVED_DECORATOR, STYLES, - VALIDATE_MODULE + VALIDATE_MODULE, + COMPONENT_BUILDER_DECORATOR } from './pre_define'; import { INNER_COMPONENT_NAMES, @@ -54,7 +55,8 @@ import { BUILDIN_STYLE_NAMES, EXTEND_ATTRIBUTE, GLOBAL_STYLE_FUNCTION, - STYLES_ATTRIBUTE + STYLES_ATTRIBUTE, + CUSTOM_BUILDER_METHOD } from './component_map'; import { LogType, @@ -324,6 +326,9 @@ function visitAllNode(node: ts.Node, sourceFileNode: ts.SourceFile, allComponent if (ts.isClassDeclaration(node) && node.name && ts.isIdentifier(node.name)) { collectComponentProps(node); } + if (ts.isMethodDeclaration(node) && hasDecorator(node, COMPONENT_BUILDER_DECORATOR)) { + CUSTOM_BUILDER_METHOD.add(node.name.getText()); + } node.getChildren().forEach((item: ts.Node) => visitAllNode(item, sourceFileNode, allComponentNames, log)); } -- Gitee From ff937a334bd8d926a9bb6e8d6cc9d4f30f476eb8 Mon Sep 17 00:00:00 2001 From: root Date: Sun, 6 Feb 2022 00:29:52 +0800 Subject: [PATCH 2/2] yangbo198@huawei.com Signed-off-by: root Change-Id: I40909a127a170028aafb7d1f023dba4121d48de0 --- compiler/src/component_map.ts | 3 + compiler/src/process_component_build.ts | 129 +++++++++++++++++++++++- compiler/src/process_component_class.ts | 56 ---------- compiler/src/validate_ui_syntax.ts | 9 +- 4 files changed, 137 insertions(+), 60 deletions(-) diff --git a/compiler/src/component_map.ts b/compiler/src/component_map.ts index 550a0886d..f15b95483 100644 --- a/compiler/src/component_map.ts +++ b/compiler/src/component_map.ts @@ -89,6 +89,9 @@ export const JS_BIND_COMPONENTS: Set = new Set([ export const NEEDPOP_COMPONENT: Set = new Set(['Blank', 'Search']); +export const CUSTOM_BUILDER_PROPERTIES: Set = new Set(['bindPopup', 'bindMenu', 'bindContextMenu', 'title', + 'menus', 'toolBar', 'tabBar']); + (function initComponent() { Object.keys(COMPONENT_MAP).forEach((componentName) => { INNER_COMPONENT_NAMES.add(componentName); diff --git a/compiler/src/process_component_build.ts b/compiler/src/process_component_build.ts index ea2b141bc..c06857729 100644 --- a/compiler/src/process_component_build.ts +++ b/compiler/src/process_component_build.ts @@ -52,7 +52,10 @@ import { $$_VALUE, $$_CHANGE_EVENT, $$_THIS, - $$_NEW_VALUE + $$_NEW_VALUE, + BUILDER_ATTR_NAME, + BUILDER_ATTR_BIND, + CUSTOM_DIALOG_CONTROLLER_BUILDER } from './pre_define'; import { INNER_COMPONENT_NAMES, @@ -66,7 +69,8 @@ import { NEEDPOP_COMPONENT, INNER_STYLE_FUNCTION, GLOBAL_STYLE_FUNCTION, - COMMON_ATTRS + COMMON_ATTRS, + CUSTOM_BUILDER_PROPERTIES } from './component_map'; import { componentCollection } from './validate_ui_syntax'; import { processCustomComponent } from './process_custom_component'; @@ -516,6 +520,16 @@ export function bindComponentAttr(node: ts.ExpressionStatement, identifierNode: const statements: ts.Statement[] = []; const lastStatement: AnimationInfo = { statement: null, kind: false }; while (temp && ts.isCallExpression(temp) && temp.expression) { + if (temp.expression && (validatePropertyAccessExpressionWithCustomBuilder(temp.expression) || + validateIdentifierWithCustomBuilder(temp.expression))) { + let propertyName: string = ''; + if (ts.isIdentifier(temp.expression)) { + propertyName = temp.expression.escapedText.toString(); + } else if (ts.isPropertyAccessExpression(temp.expression)) { + propertyName = temp.expression.name.escapedText.toString(); + } + temp = propertyName === BIND_POPUP? processBindPopupBuilder(temp) : processCustomBuilderProperty(temp); + } if (ts.isPropertyAccessExpression(temp.expression) && temp.expression.name && ts.isIdentifier(temp.expression.name)) { addComponentAttr(temp, temp.expression.name, lastStatement, statements, identifierNode, log, @@ -538,6 +552,117 @@ export function bindComponentAttr(node: ts.ExpressionStatement, identifierNode: } } +function processCustomBuilderProperty(node: ts.CallExpression): ts.CallExpression { + let newArguments: ts.Expression[] = []; + node.arguments.forEach((argument, index) => { + if (index === 0) { + newArguments.push(parseBuilderNode(argument)); + } else { + newArguments.push(argument); + } + }); + node = ts.factory.updateCallExpression(node, node.expression, node.typeArguments, newArguments); + return node; +} + +function parseBuilderNode(node: ts.Node): ts.ObjectLiteralExpression { + if (ts.isPropertyAccessExpression(node) && node.expression && + node.expression.kind === ts.SyntaxKind.ThisKeyword && node.name && ts.isIdentifier(node.name) && + CUSTOM_BUILDER_METHOD.has(node.name.escapedText.toString())) { + return processPropertyBuilder(node); + } else if (ts.isIdentifier(node) && CUSTOM_BUILDER_METHOD.has(node.escapedText.toString())) { + return processIdentifierBuilder(node); + } else if (ts.isCallExpression(node)) { + return getParsedBuilderAttrArgumentWithParams(node); + } +} + +function processBindPopupBuilder(node: ts.CallExpression): ts.CallExpression { + let newArguments: ts.Expression[] = []; + node.arguments.forEach((argument, index) => { + if (index === 1) { + // @ts-ignore + newArguments.push(processBindPopupBuilderProperty(argument)); + } else { + newArguments.push(argument); + } + }); + node = ts.factory.updateCallExpression(node, node.expression, node.typeArguments, newArguments); + return node; +} + +function processBindPopupBuilderProperty(node: ts.ObjectLiteralExpression): ts.ObjectLiteralExpression { + let newProperties: ts.PropertyAssignment[] = []; + node.properties.forEach((property: ts.PropertyAssignment, index) => { + if (index === 0) { + if (property.name && ts.isIdentifier(property.name) && + property.name.escapedText.toString() === CUSTOM_DIALOG_CONTROLLER_BUILDER) { + newProperties.push(ts.factory.updatePropertyAssignment(property, property.name, + parseBuilderNode(property.initializer))) + } else { + newProperties.push(property); + } + } else { + newProperties.push(property); + } + }); + return ts.factory.updateObjectLiteralExpression(node, newProperties); +} + +function processPropertyBuilder(node: ts.PropertyAccessExpression): ts.ObjectLiteralExpression { + return 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()] + ) + ) + ]); +} + +function processIdentifierBuilder(node: ts.Identifier): ts.ObjectLiteralExpression { + return ts.factory.createObjectLiteralExpression([ + ts.factory.createPropertyAssignment( + ts.factory.createIdentifier(BUILDER_ATTR_NAME), + node + ) + ]); +} + +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 validatePropertyAccessExpressionWithCustomBuilder(node: ts.Node): boolean { + return ts.isPropertyAccessExpression(node) && node.name && + ts.isIdentifier(node.name) && CUSTOM_BUILDER_PROPERTIES.has(node.name.escapedText.toString()); +} + +function validateIdentifierWithCustomBuilder(node: ts.Node): boolean { + return ts.isIdentifier(node) && CUSTOM_BUILDER_PROPERTIES.has(node.escapedText.toString()); +} + function createArrowFunctionFor$$($$varExp: ts.Expression): ts.ArrowFunction { return ts.factory.createArrowFunction( undefined, undefined, diff --git a/compiler/src/process_component_class.ts b/compiler/src/process_component_class.ts index 0e759bfc4..83cbaab23 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/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 148178cb2..1ed90e5d2 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -44,7 +44,8 @@ import { COMPONENT_EXTEND_DECORATOR, COMPONENT_OBSERVED_DECORATOR, STYLES, - VALIDATE_MODULE + VALIDATE_MODULE, + COMPONENT_BUILDER_DECORATOR } from './pre_define'; import { INNER_COMPONENT_NAMES, @@ -54,7 +55,8 @@ import { BUILDIN_STYLE_NAMES, EXTEND_ATTRIBUTE, GLOBAL_STYLE_FUNCTION, - STYLES_ATTRIBUTE + STYLES_ATTRIBUTE, + CUSTOM_BUILDER_METHOD } from './component_map'; import { LogType, @@ -324,6 +326,9 @@ function visitAllNode(node: ts.Node, sourceFileNode: ts.SourceFile, allComponent if (ts.isClassDeclaration(node) && node.name && ts.isIdentifier(node.name)) { collectComponentProps(node); } + if (ts.isMethodDeclaration(node) && hasDecorator(node, COMPONENT_BUILDER_DECORATOR)) { + CUSTOM_BUILDER_METHOD.add(node.name.getText()); + } node.getChildren().forEach((item: ts.Node) => visitAllNode(item, sourceFileNode, allComponentNames, log)); } -- Gitee