add validate v2 component assign v1 component for @Link value.

Signed-off-by: lihong <lihong67@huawei.com>
Change-Id: I1245011f6ca14910ec23c09ca388f5446e9e9f76
This commit is contained in:
lihong 2024-06-20 11:26:24 +08:00
parent c38ef22512
commit c91180dc2f
6 changed files with 122 additions and 24 deletions

View File

@ -351,8 +351,9 @@ class ChildAndParentComponentInfo {
processStructComponentV2.getOrCreateStructInfo(childName);
this.paramDecoratorMap = this.childStructInfo.paramDecoratorMap;
this.updatePropsDecoratorsV2 = [...this.childStructInfo.eventDecoratorSet, ...this.paramDecoratorMap.keys()];
this.parentStructInfo =
processStructComponentV2.getOrCreateStructInfo(componentCollection.currentClassName);
this.parentStructInfo = componentCollection.currentClassName ?
processStructComponentV2.getOrCreateStructInfo(componentCollection.currentClassName) :
new StructInfo();
this.forbiddenInitPropsV2 = [...this.childStructInfo.localDecoratorSet,
...this.childStructInfo.providerDecoratorSet, ...this.childStructInfo.consumerDecoratorSet,
...this.childStructInfo.regularSet];
@ -405,7 +406,7 @@ function validateChildProperty(item: ts.PropertyAssignment, itemName: string,
log.push({
type: LogType.ERROR,
message: `Property '${itemName}' in the custom component '${info.childName}'` +
` cannot initialize here (forbidden to specify).`,
` cannot be initialized here (forbidden to specify).`,
pos: item.getStart()
});
return;
@ -518,7 +519,7 @@ function isAllowedTypeForFunction(type: ts.Type): boolean {
}
function validateInitParam(childName: string, curChildProps: Set<string>,
node: ts.CallExpression, log: LogInfo[]): void {
node: ts.CallExpression, log: LogInfo[], parentStructInfo: StructInfo): void {
const childStructInfo: StructInfo = processStructComponentV2.getAliasStructInfo(node) ||
processStructComponentV2.getOrCreateStructInfo(childName);
const paramDecoratorMap: Map<string, ParamDecoratorInfo> = childStructInfo.paramDecoratorMap;
@ -538,6 +539,12 @@ function validateInitParam(childName: string, curChildProps: Set<string>,
});
}
});
} else if (parentStructInfo.isComponentV2 && childStructInfo.linkDecoratorsV1.length) {
log.push({
type: LogType.ERROR,
message: 'The @ComponentV2 struct must not contain any @Component with an @Link decorated variable',
pos: node.getStart()
});
}
}
@ -969,9 +976,12 @@ function validateCustomComponentPrams(node: ts.CallExpression, name: string,
}
});
}
const parentStructInfo: StructInfo = componentCollection.currentClassName ?
processStructComponentV2.getOrCreateStructInfo(componentCollection.currentClassName) :
new StructInfo();
validateInitDecorator(node, name, curChildProps, log);
validateMandatoryToInitViaParam(node, name, curChildProps, log);
validateInitParam(name, curChildProps, node, log);
validateMandatoryToInitViaParam(node, name, curChildProps, log, parentStructInfo);
validateInitParam(name, curChildProps, node, log, parentStructInfo);
}
function getCustomComponentNode(node: ts.ExpressionStatement): ts.CallExpression {
@ -1268,24 +1278,29 @@ function validateForbiddenToInitViaParam(node: ts.ObjectLiteralElementLike,
log.push({
type: LogType.ERROR,
message: `Property '${node.name.getText()}' in the custom component '${customComponentName}'` +
` cannot initialize here (forbidden to specify).`,
` cannot be initialized here (forbidden to specify).`,
pos: node.name.getStart()
});
}
}
function validateMandatoryToInitViaParam(node: ts.CallExpression, customComponentName: string,
curChildProps: Set<string>, log: LogInfo[]): void {
curChildProps: Set<string>, log: LogInfo[], parentStructInfo: StructInfo): void {
let mandatoryToInitViaParamSet: Set<string>;
if (projectConfig.compileMode === 'esmodule' && process.env.compileTool === 'rollup' && node.expression) {
mandatoryToInitViaParamSet = new Set([
...getCollectionSet(node.expression.getText(), storedFileInfo.overallLinkCollection),
...getCollectionSet(node.expression.getText(), storedFileInfo.overallObjectLinkCollection)]);
const overAll: string[] = [
...getCollectionSet(node.expression.getText(), storedFileInfo.overallObjectLinkCollection)];
if (!parentStructInfo.isComponentV2) {
overAll.unshift(...getCollectionSet(node.expression.getText(), storedFileInfo.overallLinkCollection));
}
mandatoryToInitViaParamSet = new Set(overAll);
customComponentName = node.expression.getText();
} else {
mandatoryToInitViaParamSet = new Set([
...getCollectionSet(customComponentName, linkCollection),
...getCollectionSet(customComponentName, objectLinkCollection)]);
const arr: string[] = [...getCollectionSet(customComponentName, objectLinkCollection)];
if (!parentStructInfo.isComponentV2) {
arr.unshift(...getCollectionSet(customComponentName, linkCollection));
}
mandatoryToInitViaParamSet = new Set(arr);
}
mandatoryToInitViaParamSet.forEach(item => {
if (item && !curChildProps.has(item)) {

View File

@ -885,16 +885,21 @@ function setComponentCollectionInfo(name: string, componentSet: IComponentSet, i
provideInitialization.set(name, componentSet.provideInit);
privateCollection.set(name, componentSet.privateCollection);
if (asComponentName) {
processStructComponentV2.getOrCreateStructInfo(asComponentName).updatePropsDecoratorsV1.push(
...componentSet.states, ...componentSet.props, ...componentSet.links,
const asComponentNameStructInfo: StructInfo =
processStructComponentV2.getOrCreateStructInfo(asComponentName);
asComponentNameStructInfo.updatePropsDecoratorsV1.push(
...componentSet.states, ...componentSet.props,
...componentSet.provides, ...componentSet.objectLinks
);
asComponentNameStructInfo.linkDecoratorsV1.push(...componentSet.links);
return;
}
processStructComponentV2.getOrCreateStructInfo(name).updatePropsDecoratorsV1.push(
...componentSet.states, ...componentSet.props, ...componentSet.links,
const nameStructInfo: StructInfo = processStructComponentV2.getOrCreateStructInfo(name);
nameStructInfo.updatePropsDecoratorsV1.push(
...componentSet.states, ...componentSet.props,
...componentSet.provides, ...componentSet.objectLinks
);
nameStructInfo.linkDecoratorsV1.push(...componentSet.links);
}
function parseComponentInImportNode(originNode: ts.StructDeclaration, name: string,

View File

@ -60,6 +60,7 @@ export class StructInfo {
isReusable: boolean = false;
structName: string = '';
updatePropsDecoratorsV1: string[] = [];
linkDecoratorsV1: string[] = [];
paramDecoratorMap: Map<string, ParamDecoratorInfo> = new Map();
eventDecoratorSet: Set<string> = new Set();
localDecoratorSet: Set<string> = new Set();

View File

@ -1276,9 +1276,10 @@ function collectComponentProps(node: ts.StructDeclaration, structInfo: StructInf
provideInitialization.set(componentName, componentSet.provideInit);
privateCollection.set(componentName, componentSet.privateCollection);
structInfo.updatePropsDecoratorsV1.push(
...componentSet.states, ...componentSet.props, ...componentSet.links,
...componentSet.states, ...componentSet.props,
...componentSet.provides, ...componentSet.objectLinks
);
structInfo.linkDecoratorsV1.push(...componentSet.links);
}
export function getComponentSet(node: ts.StructDeclaration, uiCheck: boolean = false): IComponentSet {

View File

@ -138,7 +138,7 @@
"type": "WARN"
},
"validateForbiddenToInitViaParam": {
"message": "Property 'message' in the custom component 'Child' cannot initialize here (forbidden to specify).",
"message": "Property 'message' in the custom component 'Child' cannot be initialized here (forbidden to specify).",
"type": "ERROR"
},
"validateMandatoryToInitViaParam": {
@ -589,19 +589,19 @@
"type": "ERROR"
},
{
"message": "Property 'regular_value' in the custom component 'testChild' cannot initialize here (forbidden to specify).",
"message": "Property 'regular_value' in the custom component 'testChild' cannot be initialized here (forbidden to specify).",
"type": "ERROR"
},
{
"message": "Property 'local_value' in the custom component 'testChild' cannot initialize here (forbidden to specify).",
"message": "Property 'local_value' in the custom component 'testChild' cannot be initialized here (forbidden to specify).",
"type": "ERROR"
},
{
"message": "Property 'provider_value' in the custom component 'testChild' cannot initialize here (forbidden to specify).",
"message": "Property 'provider_value' in the custom component 'testChild' cannot be initialized here (forbidden to specify).",
"type": "ERROR"
},
{
"message": "Property 'consumer_value' in the custom component 'testChild' cannot initialize here (forbidden to specify).",
"message": "Property 'consumer_value' in the custom component 'testChild' cannot be initialized here (forbidden to specify).",
"type": "ERROR"
}
],
@ -691,6 +691,32 @@
"type": "ERROR"
}
],
"v2ToV1Link": [
{
"message": "Property 'link_value' in the custom component 'V1' is missing (mandatory to specify).",
"type": "ERROR"
},
{
"message": "Property 'link_value' in the custom component 'V1' is missing (mandatory to specify).",
"type": "ERROR"
},
{
"message": "The @ComponentV2 struct must not contain any @Component with an @Link decorated variable",
"type": "ERROR"
},
{
"message": "The @ComponentV2 struct must not contain any @Component with an @Link decorated variable",
"type": "ERROR"
},
{
"message": "The @ComponentV2 struct must not contain any @Component with an @Link decorated variable",
"type": "ERROR"
},
{
"message": "The @ComponentV2 struct must not contain any @Component with an @Link decorated variable",
"type": "ERROR"
}
],
"v1ToV2Component.ts": [],
"v1ToV2ComponentValidate": [],
"v2ToV1ComponentValidate": [],

View File

@ -0,0 +1,50 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
exports.source = `
@Entry
@Component
struct TestParent {
@State state_value: string = "hello state_value"
build() {
Column() {
V1()
V1({})
}
}
}
@ComponentV2
struct V2 {
@Local local_value: string = "hello local_value"
build() {
Column() {
V1()
V1({})
V1({link_value: this.local_value})
V1({link_value: "hello"})
}
}
}
@Component
struct V1 {
@Link link_value: string
build() {
Text("hello V1")
}
}
`