diff --git a/compiler/src/pre_define.ts b/compiler/src/pre_define.ts index 54ce5cc..a1698ab 100644 --- a/compiler/src/pre_define.ts +++ b/compiler/src/pre_define.ts @@ -265,4 +265,7 @@ export const SUCCESS: number = 0; export const FAIL: number = 1; export const TS2ABC: string = 'ts2abc'; -export const ES2ABC: string = 'es2abc'; \ No newline at end of file +export const ES2ABC: string = 'es2abc'; + +export const GENERATE_ID = 'generateId'; +export const _GENERATE_ID = '__generate__Id'; diff --git a/compiler/src/process_component_build.ts b/compiler/src/process_component_build.ts index 75ecef9..931b56a 100644 --- a/compiler/src/process_component_build.ts +++ b/compiler/src/process_component_build.ts @@ -254,7 +254,7 @@ export function processComponentChild(node: ts.Block | ts.SourceFile, newStateme break; case ComponentType.customBuilderMethod: parent = undefined; - if (INNER_CUSTOM_BUILDER_METHOD.has(name)) { + if (CUSTOM_BUILDER_METHOD.has(name)) { newStatements.push(addInnerBuilderParameter(item)); } else { newStatements.push(item); diff --git a/compiler/src/process_component_class.ts b/compiler/src/process_component_class.ts index 69b327f..6cf0021 100644 --- a/compiler/src/process_component_class.ts +++ b/compiler/src/process_component_class.ts @@ -324,10 +324,8 @@ function processComponentMethod(node: ts.MethodDeclaration, parentComponentName: node.type, processComponentBlock(node.body, false, log, true)); } else if (hasDecorator(node, COMPONENT_BUILDER_DECORATOR, customBuilder)) { CUSTOM_BUILDER_METHOD.add(name); - INNER_CUSTOM_BUILDER_METHOD.add(name) - node.parameters.push(ts.factory.createParameterDeclaration(undefined, undefined, undefined, - ts.factory.createIdentifier(COMPONENT_CONSTRUCTOR_PARENT), undefined, undefined, - ts.factory.createIdentifier(COMPONENT_IF_UNDEFINED))); + INNER_CUSTOM_BUILDER_METHOD.add(name); + node.parameters.push(createParentParameter()); const builderNode: ts.MethodDeclaration = ts.factory.updateMethodDeclaration(node, customBuilder, node.modifiers, node.asteriskToken, node.name, node.questionToken, node.typeParameters, node.parameters, node.type, processComponentBlock(node.body, false, log, false, true)); @@ -351,6 +349,12 @@ function processComponentMethod(node: ts.MethodDeclaration, parentComponentName: return updateItem; } +export function createParentParameter(): ts.ParameterDeclaration { + return ts.factory.createParameterDeclaration(undefined, undefined, undefined, + ts.factory.createIdentifier(COMPONENT_CONSTRUCTOR_PARENT), undefined, undefined, + ts.factory.createIdentifier(COMPONENT_IF_UNDEFINED)); +} + function processBuildMember(node: ts.MethodDeclaration, context: ts.TransformationContext, log: LogInfo[]): ts.MethodDeclaration { return ts.visitNode(node, visitBuild); diff --git a/compiler/src/process_component_member.ts b/compiler/src/process_component_member.ts index 95f1f6b..010712f 100644 --- a/compiler/src/process_component_member.ts +++ b/compiler/src/process_component_member.ts @@ -14,6 +14,7 @@ */ import ts from 'typescript'; +const path = require('path'); import { INNER_COMPONENT_MEMBER_DECORATORS, @@ -53,7 +54,9 @@ import { COMPONENT_BUILDERPARAM_DECORATOR, COMPONENT_LOCAL_STORAGE_LINK_DECORATOR, COMPONENT_LOCAL_STORAGE_PROP_DECORATOR, - COMPONENT_CONSTRUCTOR_PARENT + COMPONENT_CONSTRUCTOR_PARENT, + EXTNAME_ETS, + _GENERATE_ID } from './pre_define'; import { forbiddenUseStateType, @@ -76,6 +79,7 @@ import { createReference, isProperty } from './process_component_class'; +import { transformLog } from './process_ui_syntax'; import { globalProgram, projectConfig } from '../main'; export type ControllerType = { @@ -594,13 +598,17 @@ function addCustomComponentId(node: ts.NewExpression, componentName: string, if (!argumentsArray) { argumentsArray = [ts.factory.createObjectLiteralExpression([], true)]; } - argumentsArray.unshift(ts.factory.createStringLiteral((++componentInfo.id).toString()), - isInnerBuilder ? ts.factory.createConditionalExpression( - ts.factory.createIdentifier(COMPONENT_CONSTRUCTOR_PARENT), - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createIdentifier(COMPONENT_CONSTRUCTOR_PARENT), - ts.factory.createToken(ts.SyntaxKind.ColonToken), ts.factory.createThis() - ) : ts.factory.createThis()); + ++componentInfo.id; + argumentsArray.unshift(isInnerBuilder ? ts.factory.createBinaryExpression( + ts.factory.createStringLiteral(path.basename(transformLog.sourceFile.fileName, EXTNAME_ETS) + '_'), + ts.factory.createToken(ts.SyntaxKind.PlusToken), ts.factory.createIdentifier(_GENERATE_ID)) : + ts.factory.createStringLiteral(componentInfo.id.toString()), + isInnerBuilder ? ts.factory.createConditionalExpression( + ts.factory.createIdentifier(COMPONENT_CONSTRUCTOR_PARENT), + ts.factory.createToken(ts.SyntaxKind.QuestionToken), + ts.factory.createIdentifier(COMPONENT_CONSTRUCTOR_PARENT), + ts.factory.createToken(ts.SyntaxKind.ColonToken), ts.factory.createThis() + ) : ts.factory.createThis()); node = ts.factory.updateNewExpression(node, node.expression, node.typeArguments, argumentsArray); } else if (argumentsArray) { @@ -704,7 +712,7 @@ export function isSimpleType(typeNode: ts.TypeNode, program: ts.Program, log?: L referenceType = true; } if (basicType && referenceType && log) { - validateVariableType(typeNode, log); + validateVariableType(typeNode, log); return false; } } diff --git a/compiler/src/process_custom_component.ts b/compiler/src/process_custom_component.ts index f1e5187..6c69be9 100644 --- a/compiler/src/process_custom_component.ts +++ b/compiler/src/process_custom_component.ts @@ -33,7 +33,8 @@ import { CUSTOM_COMPONENT_NEEDS_UPDATE_FUNCTION, CUSTOM_COMPONENT_MARK_STATIC_FUNCTION, COMPONENT_COMMON, - COMPONENT_CONSTRUCTOR_PARENT + COMPONENT_CONSTRUCTOR_PARENT, + GENERATE_ID } from './pre_define'; import { propertyCollection, @@ -367,26 +368,29 @@ function createFindChildById(id: string, name: string, isInnerBuilder: boolean = ts.factory.createConditionalExpression( ts.factory.createParenthesizedExpression( ts.factory.createBinaryExpression( - ts.factory.createThis(), + createConditionParent(isInnerBuilder), ts.factory.createToken(ts.SyntaxKind.AmpersandAmpersandToken), ts.factory.createPropertyAccessExpression( - ts.factory.createThis(), + createConditionParent(isInnerBuilder), ts.factory.createIdentifier(CUSTOM_COMPONENT_FUNCTION_FIND_CHILD_BY_ID) ))), ts.factory.createToken(ts.SyntaxKind.QuestionToken), ts.factory.createAsExpression(ts.factory.createCallExpression( - ts.factory.createPropertyAccessExpression(isInnerBuilder ? - ts.factory.createParenthesizedExpression(ts.factory.createConditionalExpression( - ts.factory.createIdentifier(COMPONENT_CONSTRUCTOR_PARENT), - ts.factory.createToken(ts.SyntaxKind.QuestionToken), - ts.factory.createIdentifier(COMPONENT_CONSTRUCTOR_PARENT), - ts.factory.createToken(ts.SyntaxKind.ColonToken), ts.factory.createThis() - )) : ts.factory.createThis(), ts.factory.createIdentifier( - `${CUSTOM_COMPONENT_FUNCTION_FIND_CHILD_BY_ID}`)), undefined, [ts.factory.createStringLiteral(id)]), + ts.factory.createPropertyAccessExpression(createConditionParent(isInnerBuilder), + ts.factory.createIdentifier(`${CUSTOM_COMPONENT_FUNCTION_FIND_CHILD_BY_ID}`)), undefined, + [isInnerBuilder ? ts.factory.createCallExpression(ts.factory.createIdentifier(GENERATE_ID), + undefined, []) : ts.factory.createStringLiteral(id)]), ts.factory.createTypeReferenceNode(ts.factory.createIdentifier(name))), ts.factory.createToken(ts.SyntaxKind.ColonToken), ts.factory.createIdentifier('undefined')))], ts.NodeFlags.Let)); } +function createConditionParent(isInnerBuilder: boolean): ts.ParenthesizedExpression | ts.ThisExpression { + return isInnerBuilder ? ts.factory.createParenthesizedExpression(ts.factory.createConditionalExpression( + ts.factory.createIdentifier(COMPONENT_CONSTRUCTOR_PARENT), ts.factory.createToken(ts.SyntaxKind.QuestionToken), + ts.factory.createIdentifier(COMPONENT_CONSTRUCTOR_PARENT), ts.factory.createToken(ts.SyntaxKind.ColonToken), + ts.factory.createThis())) : ts.factory.createThis(); +} + function createCustomComponentIfStatement(id: string, node: ts.ExpressionStatement, newObjectLiteralExpression: ts.ObjectLiteralExpression, parentName: string): ts.IfStatement { const viewName: string = `${CUSTOM_COMPONENT_EARLIER_CREATE_CHILD}${id}`; diff --git a/compiler/src/process_ui_syntax.ts b/compiler/src/process_ui_syntax.ts index f89cbcf..4e6c034 100644 --- a/compiler/src/process_ui_syntax.ts +++ b/compiler/src/process_ui_syntax.ts @@ -17,7 +17,10 @@ import ts from 'typescript'; import path from 'path'; import { componentCollection } from './validate_ui_syntax'; -import { processComponentClass } from './process_component_class'; +import { + processComponentClass, + createParentParameter +} from './process_component_class'; import processImport from './process_import'; import { PAGE_ENTRY_FUNCTION_NAME, @@ -48,7 +51,10 @@ import { CUSTOM_DIALOG_CONTROLLER_BUILDER, ESMODULE, ARK, - COMPONENT_COMMON + COMPONENT_COMMON, + EXTNAME_ETS, + GENERATE_ID, + _GENERATE_ID } from './pre_define'; import { componentInfo, @@ -107,6 +113,7 @@ export function processUISyntax(program: ts.Program, ut = false): Function { }); GLOBAL_STYLE_FUNCTION.clear(); const statements: ts.Statement[] = Array.from(node.statements); + generateId(statements, node); INTERFACE_NODE_SET.forEach(item => { statements.unshift(item); }); @@ -139,9 +146,10 @@ export function processUISyntax(program: ts.Program, ut = false): Function { } else if (hasDecorator(node, COMPONENT_BUILDER_DECORATOR) && node.name && node.body && ts.isBlock(node.body)) { CUSTOM_BUILDER_METHOD.add(node.name.getText()); + node.parameters.push(createParentParameter()); node = ts.factory.updateFunctionDeclaration(node, undefined, node.modifiers, node.asteriskToken, node.name, node.typeParameters, node.parameters, node.type, - processComponentBlock(node.body, false, transformLog.errors, false, false, node.name.getText())); + processComponentBlock(node.body, false, transformLog.errors, false, true, node.name.getText())); } else if (hasDecorator(node, COMPONENT_STYLES_DECORATOR)) { if (node.parameters.length === 0) { node = undefined; @@ -173,6 +181,41 @@ export function processUISyntax(program: ts.Program, ut = false): Function { }; } +function generateId(statements: ts.Statement[], node: ts.SourceFile): void { + statements.unshift( + ts.factory.createVariableStatement( + undefined, + ts.factory.createVariableDeclarationList( + [ts.factory.createVariableDeclaration( + ts.factory.createIdentifier(_GENERATE_ID), + undefined, + ts.factory.createKeywordTypeNode(ts.SyntaxKind.NumberKeyword), + ts.factory.createNumericLiteral("0") + )], + ts.NodeFlags.Let + ) + ), + ts.factory.createFunctionDeclaration( + undefined, + undefined, + undefined, + ts.factory.createIdentifier(GENERATE_ID), + undefined, + [], + ts.factory.createKeywordTypeNode(ts.SyntaxKind.StringKeyword), + ts.factory.createBlock( + [ts.factory.createReturnStatement(ts.factory.createBinaryExpression( + ts.factory.createStringLiteral(path.basename(node.fileName, EXTNAME_ETS)+'_'), + ts.factory.createToken(ts.SyntaxKind.PlusToken),ts.factory.createPrefixUnaryExpression( + ts.SyntaxKind.PlusPlusToken, + ts.factory.createIdentifier(_GENERATE_ID) + )))], + true + ) + ) + ) +} + function isCustomDialogController(node: ts.Expression) { const tempParent: ts.Node = node.parent; // @ts-ignore @@ -331,7 +374,7 @@ function getResourceDataNode(node: ts.CallExpression): ts.Node { function createResourceParam(resourceValue: number, resourceType: number, argsArr: ts.Expression[]): ts.ObjectLiteralExpression { - const propertyArray: Array[ts.PropertyAssignment] = [ + const propertyArray: Array = [ ts.factory.createPropertyAssignment( ts.factory.createStringLiteral(RESOURCE_NAME_ID), ts.factory.createNumericLiteral(resourceValue)