diff --git a/arkui-plugins/ui-plugins/interop/initstatevar.ts b/arkui-plugins/ui-plugins/interop/initstatevar.ts index 1f43c179e015d9c039e977070a2f1c661c4c057b..22bf008e568fb01bf586846786bcb5f85000b1c1 100644 --- a/arkui-plugins/ui-plugins/interop/initstatevar.ts +++ b/arkui-plugins/ui-plugins/interop/initstatevar.ts @@ -23,7 +23,8 @@ import { hasDecorator } from '../property-translators/utils'; import { DecoratorNames } from '../../common/predefines'; -export function initialArgs(args: arkts.ObjectExpression, varMap: Map, updateProp: arkts.Property[]): arkts.Statement[] { +export function initialArgs(args: arkts.ObjectExpression, varMap: Map, + updateProp: arkts.Property[], node: arkts.CallExpression, isV2: boolean): arkts.Statement[] { const result: arkts.Statement[] = []; const proxySet = new Set(); @@ -40,8 +41,9 @@ export function initialArgs(args: arkts.ObjectExpression, varMap: Map valueType = getPropertyType( annotations ) ); + isComponentV2forParent = valueProperty.parent.annotations.some( + annotation => annotation.expr instanceof arkts.Identifier && annotation.expr.name === 'ComponentV2'); + } else { + //依赖于懿后续补充parent信息 + } + if ( keyProperty !== undefined ) { + keyProperty.annotations.some( ( annotations ) => ketType = getPropertyType( annotations ) ); + } + if ( isV2 ) { + if ( !isComponentV2forParent ) { + let errorMessage = `The '${valueType}' property '${valueName}' cannot be assigned to the ${ketType} property ${keyName} when interop`; + const diagnosticKind = arkts.DiagnosticKind.create( errorMessage, arkts.PluginDiagnosticType.ES2PANDA_PLUGIN_ERROR ); + arkts.Diagnostic.logDiagnostic(diagnosticKind, arkts.getStartPosition(node)); + } + } else { + if ( isComponentV2forParent ) { + const annotationsKey = keyProperty.annotations; + if (!isV2 && valueProperty !== undefined && annotationsKey.length !== 0) { + if ( ( hasDecorator( valueProperty, DecoratorNames.LOCAL ) || hasDecorator( valueProperty, DecoratorNames.PROVIDER ) || + hasDecorator( valueProperty, DecoratorNames.CONSUMER ) || hasDecorator( valueProperty, DecoratorNames.PARAM ) || + hasDecorator( valueProperty, DecoratorNames.EVENT ))) { + let errorMessage = `The '${valueType}' property '${valueName}' cannot be assigned to the ${ketType} property ${keyName} when interop`; + const diagnosticKind = arkts.DiagnosticKind.create( errorMessage, arkts.PluginDiagnosticType.ES2PANDA_PLUGIN_ERROR ); + arkts.Diagnostic.logDiagnostic(diagnosticKind, arkts.getStartPosition(node)); + } + const annotations = valueProperty.annotations; + if (annotations.length === 0) { + let errorMessage = `The '${valueType}' property '${valueName}' cannot be assigned to the ${ketType} property ${keyName} when interop`; + const diagnosticKind = arkts.DiagnosticKind.create( errorMessage, arkts.PluginDiagnosticType.ES2PANDA_PLUGIN_ERROR ); + arkts.Diagnostic.logDiagnostic(diagnosticKind, arkts.getStartPosition(node)); + } + } + } + } + } +} + +function getPropertyType(anno: arkts.AnnotationUsage) { + let propertyType = 'regular'; + if (!!anno.expr && arkts.isIdentifier(anno.expr)) { + propertyType = anno.expr.name; + } + if ( propertyType !== 'regular' && propertyType !== '' ) { + return '@' + propertyType; + } else { + return 'regular'; + } +} + export function createVariableLet(varName: string, expression: arkts.AstNode): arkts.VariableDeclaration { return arkts.factory.createVariableDeclaration( arkts.Es2pandaModifierFlags.MODIFIER_FLAGS_NONE, diff --git a/arkui-plugins/ui-plugins/interop/interop.ts b/arkui-plugins/ui-plugins/interop/interop.ts index 5544444107e055b8b6186686a3fb034159bef282..131a55a06a1457309cfd48002e220c9d7a3202a8 100644 --- a/arkui-plugins/ui-plugins/interop/interop.ts +++ b/arkui-plugins/ui-plugins/interop/interop.ts @@ -174,7 +174,7 @@ function createComponent(className: string): arkts.Statement[] { function createWrapperBlock(context: InteropContext, varMap: Map, - updateProp: arkts.Property[]): arkts.BlockStatement { + updateProp: arkts.Property[], node: arkts.CallExpression, isV2: boolean): arkts.BlockStatement { const className: string = context.className; const path: string = context.path; const args: arkts.ObjectExpression | undefined = context.arguments; @@ -186,7 +186,7 @@ function createWrapperBlock(context: InteropContext, varMap: Map, - updateProp: arkts.Property[]): arkts.ArrowFunctionExpression { - const block = createWrapperBlock(context, varMap, updateProp); + updateProp: arkts.Property[], node: arkts.CallExpression, isV2: boolean): arkts.ArrowFunctionExpression { + const block = createWrapperBlock(context, varMap, updateProp, node, isV2); return arkts.factory.createArrowFunction( arkts.factory.createScriptFunction( block, @@ -432,10 +432,15 @@ export function generateArkUICompatible(node: arkts.CallExpression): arkts.CallE arguments: options, content: content, }; - + const decl = arkts.getDecl(classInterop); + if (!(decl instanceof arkts.ClassDefinition)) { + throw Error("can't find legacy class declaration"); + } const varMap: Map = generateVarMap(context, classInterop); const updateProp: arkts.Property[] = []; - const initializer = createInitializer(context, varMap, updateProp); + const isComponentV2 = decl.annotations.some( + annotation => annotation.expr instanceof arkts.Identifier && annotation.expr.name === 'ComponentV2'); + const initializer = createInitializer(context, varMap, updateProp, node, isComponentV2); const updater = createUpdater(updateProp); const result = arkts.factory.updateCallExpression( node,