From d5f560fd8c92225cf2a0d1afb7b56b05c507a41a Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Sat, 15 Jan 2022 14:41:02 +0800 Subject: [PATCH] houhaoyu@huawei.com Signed-off-by: houhaoyu Change-Id: I7aee1710b59ef1efcddef8f68246b91477063732 --- compiler/src/component_map.ts | 2 +- compiler/src/pre_define.ts | 3 + compiler/src/process_component_class.ts | 13 ++-- compiler/src/process_component_constructor.ts | 70 ++++++++++--------- compiler/src/process_ui_syntax.ts | 3 - 5 files changed, 49 insertions(+), 42 deletions(-) diff --git a/compiler/src/component_map.ts b/compiler/src/component_map.ts index 0250947eb..1a5ec2a97 100644 --- a/compiler/src/component_map.ts +++ b/compiler/src/component_map.ts @@ -77,7 +77,7 @@ export interface ExtendParamterInterfance { export const EXTEND_ATTRIBUTE: Map> = new Map(); export const STYLES_ATTRIBUTE: Set = new Set(); -export const INTERFACE_NODE_SET: Set = new Set(); +export const INTERFACE_NODE_SET: Set = new Set(); export const JS_BIND_COMPONENTS: Set = new Set([ ...GESTURE_TYPE_NAMES, 'Gesture', diff --git a/compiler/src/pre_define.ts b/compiler/src/pre_define.ts index a0a1f741c..e6f325bb3 100644 --- a/compiler/src/pre_define.ts +++ b/compiler/src/pre_define.ts @@ -191,3 +191,6 @@ export const $$_VALUE: string = 'value'; export const $$_CHANGE_EVENT: string = 'changeEvent'; export const $$_THIS: string = '$$this'; export const $$_NEW_VALUE: string = 'newValue'; + +export const INTERFACE_NAME_SUFFIX:string = '_Params'; +export const OBSERVED_PROPERTY_ABSTRACT:string = 'ObservedPropertyAbstract'; diff --git a/compiler/src/process_component_class.ts b/compiler/src/process_component_class.ts index 8d7b4d42d..d5a0a0b05 100644 --- a/compiler/src/process_component_class.ts +++ b/compiler/src/process_component_class.ts @@ -50,7 +50,9 @@ import { BUILDER_ATTR_NAME, BUILDER_ATTR_BIND, COMPONENT_STYLES_DECORATOR, - STYLES + STYLES, + INTERFACE_NAME_SUFFIX, + OBSERVED_PROPERTY_ABSTRACT } from './pre_define'; import { BUILDIN_STYLE_NAMES, @@ -122,7 +124,7 @@ function processMembers(members: ts.NodeArray, parentComponentN const checkController: ControllerType = { hasController: !componentCollection.customDialogs.has(parentComponentName.getText()) }; let interfaceNode = ts.factory.createInterfaceDeclaration(undefined, undefined, - parentComponentName.getText()+'_Params', undefined, undefined, []) + parentComponentName.getText() + INTERFACE_NAME_SUFFIX, undefined, undefined, []) members.forEach((item: ts.ClassElement) => { let updateItem: ts.ClassElement; if (ts.isPropertyDeclaration(item)) { @@ -202,7 +204,7 @@ function addPropertyMember(item: ts.ClassElement, newMembers: ts.ClassElement[], break; case COMPONENT_STORAGE_PROP_DECORATOR: case COMPONENT_STORAGE_LINK_DECORATOR: - newType = ts.factory.createTypeReferenceNode('ObservedPropertyAbstract', [type]); + newType = ts.factory.createTypeReferenceNode(OBSERVED_PROPERTY_ABSTRACT, [type]); break; } updatePropertyItem = createPropertyDeclaration(propertyItem, newType, false); @@ -532,8 +534,9 @@ function createParamsInitBlock(express: string, statements: ts.Statement[], express === COMPONENT_CONSTRUCTOR_DELETE_PARAMS ? undefined : ts.factory.createIdentifier(CREATE_CONSTRUCTOR_PARAMS), undefined, express === COMPONENT_CONSTRUCTOR_DELETE_PARAMS ? undefined : - ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(parentComponentName.getText() + '_Params') - , undefined), undefined)], undefined, ts.factory.createBlock(statements, true)); + ts.factory.createTypeReferenceNode( + ts.factory.createIdentifier(parentComponentName.getText() + INTERFACE_NAME_SUFFIX), undefined), + undefined)], undefined, ts.factory.createBlock(statements, true)); return methodDeclaration; } diff --git a/compiler/src/process_component_constructor.ts b/compiler/src/process_component_constructor.ts index 750923bc6..79e247232 100644 --- a/compiler/src/process_component_constructor.ts +++ b/compiler/src/process_component_constructor.ts @@ -21,7 +21,8 @@ import { COMPONENT_CONSTRUCTOR_PARAMS, COMPONENT_CONSTRUCTOR_UPDATE_PARAMS, COMPONENT_WATCH_FUNCTION, - BASE_COMPONENT_NAME + BASE_COMPONENT_NAME, + INTERFACE_NAME_SUFFIX } from './pre_define'; export function getInitConstructor(members: ts.NodeArray): ts.ConstructorDeclaration { @@ -57,45 +58,48 @@ export function updateConstructor(ctorNode: ts.ConstructorDeclaration, } } if (ctorNode) { - const tsPara: ts.ParameterDeclaration[] | ts.NodeArray = + let ctorPara: ts.ParameterDeclaration[] | ts.NodeArray = modifyPara || ctorNode.parameters; - const newTSPara: ts.ParameterDeclaration[] = []; if (isAdd) { - tsPara.forEach((item) => { - let parameter: ts.ParameterDeclaration = item; - switch (item.getText()) { - case COMPONENT_CONSTRUCTOR_ID + '?': - parameter = ts.factory.updateParameterDeclaration(item, item.decorators, item.modifiers, - item.dotDotDotToken, item.name, item.questionToken, - ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), item.initializer); - break; - case COMPONENT_CONSTRUCTOR_PARENT + '?': - parameter = ts.factory.createParameterDeclaration(item.decorators, item.modifiers, - item.dotDotDotToken, item.name, item.questionToken, - ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(BASE_COMPONENT_NAME), undefined), - item.initializer); - break; - case COMPONENT_CONSTRUCTOR_PARAMS + '?': - parameter = ts.factory.updateParameterDeclaration(item, item.decorators, item.modifiers, - item.dotDotDotToken, item.name, item.questionToken, - ts.factory.createTypeReferenceNode(ts.factory.createIdentifier - (parentComponentName.getText() + '_Params'), undefined), item.initializer); - break; - } - newTSPara.push(parameter); - }) - ctorNode = ts.factory.updateConstructorDeclaration(ctorNode, ctorNode.decorators, - ctorNode.modifiers, newTSPara, - ts.factory.createBlock(modifyBody || ctorNode.body.statements, true)); - } else { - ctorNode = ts.factory.updateConstructorDeclaration(ctorNode, ctorNode.decorators, - ctorNode.modifiers, modifyPara || ctorNode.parameters, - ts.factory.createBlock(modifyBody || ctorNode.body.statements, true)); + ctorPara = addParamsType(ctorNode, modifyPara, parentComponentName); } + ctorNode = ts.factory.updateConstructorDeclaration(ctorNode, ctorNode.decorators, + ctorNode.modifiers, ctorPara, ts.factory.createBlock(modifyBody || ctorNode.body.statements, true)); } return ctorNode; } +function addParamsType(ctorNode: ts.ConstructorDeclaration, modifyPara: ts.ParameterDeclaration[], + parentComponentName: ts.Identifier): ts.ParameterDeclaration[] { + const tsPara: ts.ParameterDeclaration[] | ts.NodeArray = + modifyPara || ctorNode.parameters; + const newTSPara: ts.ParameterDeclaration[] = []; + tsPara.forEach((item) => { + let parameter: ts.ParameterDeclaration = item; + switch (item.getText()) { + case COMPONENT_CONSTRUCTOR_ID + '?': + parameter = ts.factory.updateParameterDeclaration(item, item.decorators, item.modifiers, + item.dotDotDotToken, item.name, item.questionToken, + ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), item.initializer); + break; + case COMPONENT_CONSTRUCTOR_PARENT + '?': + parameter = ts.factory.createParameterDeclaration(item.decorators, item.modifiers, + item.dotDotDotToken, item.name, item.questionToken, + ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(BASE_COMPONENT_NAME), undefined), + item.initializer); + break; + case COMPONENT_CONSTRUCTOR_PARAMS + '?': + parameter = ts.factory.updateParameterDeclaration(item, item.decorators, item.modifiers, + item.dotDotDotToken, item.name, item.questionToken, + ts.factory.createTypeReferenceNode(ts.factory.createIdentifier + (parentComponentName.getText() + INTERFACE_NAME_SUFFIX), undefined), item.initializer); + break; + } + newTSPara.push(parameter); + }) + return newTSPara; +} + export function addConstructor(ctorNode: any, watchMap: Map, parentComponentName: ts.Identifier): ts.ConstructorDeclaration { const watchStatements: ts.ExpressionStatement[] = []; diff --git a/compiler/src/process_ui_syntax.ts b/compiler/src/process_ui_syntax.ts index ed4d651ab..85890a21f 100644 --- a/compiler/src/process_ui_syntax.ts +++ b/compiler/src/process_ui_syntax.ts @@ -88,9 +88,6 @@ export function processUISyntax(program: ts.Program, ut = false): Function { }); node = ts.factory.updateSourceFile(node, statements); INTERFACE_NODE_SET.clear(); - const printer = ts.createPrinter({newLine: ts.NewLineKind.LineFeed}) - const result = printer.printNode(ts.EmitHint.Unspecified, node, node) - console.log(result) return node; } else { return node; -- Gitee