回退 'Pull Request !3534 : add Observed/ObservedV2 type check for property of struct.'

This commit is contained in:
openharmony_ci 2024-06-24 01:39:02 +00:00 committed by Gitee
parent 93b015e1c1
commit bd6c897188
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 1 additions and 202 deletions

View File

@ -475,83 +475,6 @@ function visitAllNode(node: ts.Node, sourceFileNode: ts.SourceFile, allComponent
isSendableClass = false;
}
const v1ComponentDecorators: string[] = [
'State', 'Prop', 'Link', 'Provide', 'Consume',
'StorageLink', 'StorageProp', 'LocalStorageLink', 'LocalStorageProp'
];
const v2ComponentDecorators: string[] = [
'Local', 'Param', 'Event', 'Provider', 'Consumer'
];
function validatePropertyInStruct(structContext: boolean, decoratorNode: ts.Identifier,
decoratorName: string, sourceFileNode: ts.SourceFile, log: LogInfo[]): void {
if (structContext) {
const isV1Decorator: boolean = v1ComponentDecorators.includes(decoratorName);
const isV2Decorator: boolean = v2ComponentDecorators.includes(decoratorName);
if (!isV1Decorator && !isV2Decorator) {
return;
}
const classResult: ClassDecoratorResult = new ClassDecoratorResult();
const propertyNode: ts.PropertyDeclaration = getPropertyNodeByDecorator(decoratorNode);
if (propertyNode && propertyNode.type && globalProgram.checker) {
validatePropertyType(propertyNode.type, classResult);
}
let message: string;
if (isV1Decorator && classResult.hasObservedV2) {
message = `The type of the @${decoratorName} property can not be a class decorated with @ObservedV2.`;
addLog(LogType.ERROR, message, decoratorNode.getStart(), log, sourceFileNode);
return;
}
if (isV2Decorator && classResult.hasObserved) {
message = `The type of the @${decoratorName} property can not be a class decorated with @Observed.`;
addLog(LogType.ERROR, message, decoratorNode.getStart(), log, sourceFileNode);
return;
}
}
}
function getPropertyNodeByDecorator(decoratorNode: ts.Identifier): ts.PropertyDeclaration {
if (ts.isDecorator(decoratorNode.parent) && ts.isPropertyDeclaration(decoratorNode.parent.parent)) {
return decoratorNode.parent.parent;
}
if (ts.isCallExpression(decoratorNode.parent) && ts.isDecorator(decoratorNode.parent.parent) &&
ts.isPropertyDeclaration(decoratorNode.parent.parent.parent)) {
return decoratorNode.parent.parent.parent;
}
return undefined;
}
function validatePropertyType(node: ts.TypeNode, classResult: ClassDecoratorResult): void {
if (ts.isUnionTypeNode(node) && node.types && node.types.length) {
node.types.forEach((item: ts.TypeNode) => {
validatePropertyType(item, classResult);
});
}
if (ts.isTypeReferenceNode(node)) {
const typeNode: ts.Type = globalProgram.checker.getTypeAtLocation(node);
parsePropertyType(typeNode, classResult);
}
}
function parsePropertyType(type: ts.Type, classResult: ClassDecoratorResult): void {
// @ts-ignore
if (type && type.types && type.types.length) {
// @ts-ignore
type.types.forEach((item: ts.Type) => {
parsePropertyType(item, classResult);
});
}
if (type && type.symbol && type.symbol.valueDeclaration &&
ts.isClassDeclaration(type.symbol.valueDeclaration)) {
const result: ClassDecoratorResult = getClassDecoratorResult(type.symbol.valueDeclaration);
if (result.hasObserved) {
classResult.hasObserved = result.hasObserved;
}
if (result.hasObservedV2) {
classResult.hasObservedV2 = result.hasObservedV2;
}
}
}
function checkDecoratorCount(node: ts.Node, sourceFileNode: ts.SourceFile, log: LogInfo[]): void {
if (ts.isPropertyDeclaration(node) || ts.isGetAccessor(node) || ts.isMethodDeclaration(node)) {
const decorators: readonly ts.Decorator[] = ts.getAllDecorators(node);
@ -665,7 +588,6 @@ function checkDecorator(sourceFileNode: ts.SourceFile, node: ts.Node,
validateMethodDecorator(sourceFileNode, node, log, structContext, decoratorName);
validateClassDecorator(sourceFileNode, node, log, classContext, decoratorName, isObservedClass,
isObservedV1Class, isSendableClass);
validatePropertyInStruct(structContext, node, decoratorName, sourceFileNode, log);
return;
}
if (ts.isDecorator(node)) {

View File

@ -693,6 +693,5 @@
],
"v1ToV2Component.ts": [],
"v1ToV2ComponentValidate": [],
"v2ToV1ComponentValidate": [],
"property_observe_validate": []
"v2ToV1ComponentValidate": []
}

View File

@ -1,122 +0,0 @@
/*
* 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 Property_Observe_Validate {
build() {
}
}
@ObservedV2
class ObservedV2_A {
name: string = ""
constructor(name: string) {
this.name = name
}
}
@Observed
class Observed_B {
}
@ObservedV2
class ObservedV2_C {
name: string = ""
constructor(name: string) {
this.name = name
}
}
@Observed
class Observed_F {
value: boolean
}
namespace TestNameSpace {
@ObservedV2
export class ObservedV2_D {
}
@Observed
export class Observed_E {
value: number
}
}
type v2AliasType = ObservedV2_A
type v2AliasType1 = ObservedV2_A | Observed_B
type v2AliasType2 = ObservedV2_A | ObservedV2_C
@Component
struct TestV1Parent {
// build ok
regular_value: ObservedV2_A = new ObservedV2_A("hello")
@State state_value6: Observed_B = new Observed_B()
@State state_value7: Observed_B | Observed_F | TestNameSpace.Observed_E = new Observed_F()
@State state_value: ObservedV2_A = new ObservedV2_A("hello")
@State state_value1: TestNameSpace.ObservedV2_D = new TestNameSpace.ObservedV2_D()
@State state_value2: v2AliasType = new ObservedV2_A("hello")
@State state_value3: v2AliasType1 = new ObservedV2_A("hello")
@State state_value5: v2AliasType2 = new ObservedV2_C("hello")
@Prop prop_value: ObservedV2_A = new ObservedV2_A("hello")
@Link link_value: ObservedV2_A
@Provide provide_value: ObservedV2_A = new ObservedV2_A("hello")
@Consume consume_value: ObservedV2_A
@StorageLink("a") storage_link_value: ObservedV2_A = new ObservedV2_A("hello")
@StorageProp("b") storage_prop_value: ObservedV2_A = new ObservedV2_A("hello")
@LocalStorageLink("c") local_storage_link_value: ObservedV2_A = new ObservedV2_A("hello")
@LocalStorageProp("c") local_storage_prop_value: ObservedV2_A = new ObservedV2_A("hello")
build() {
}
}
type v1AliasType = Observed_B
type v1AliasType1 = ObservedV2_A | Observed_B
type v1AliasType2 = Observed_B | Observed_F
@ComponentV2
struct TestV2Parent {
// build ok
regular_value: Observed_B = new Observed_B();
@Local local_value7: ObservedV2_A = new ObservedV2_A("");
@Local local_value8: ObservedV2_A | ObservedV2_C | TestNameSpace.ObservedV2_D = new ObservedV2_A("");
@Local local_value9: TestNameSpace.ObservedV2_D = new TestNameSpace.ObservedV2_D()
@Local local_value: Observed_B = new Observed_B()
@Local local_value1: v1AliasType = new Observed_B()
@Local local_value2: v1AliasType1 = new Observed_B()
@Local local_value3: v1AliasType2 = new Observed_F()
@Local local_value5: ObservedV2_A | Observed_B = new Observed_B()
@Local local_value6: TestNameSpace.Observed_E = new TestNameSpace.Observed_E()
@Param @Require param_value1: Observed_B = new Observed_B()
@Once @Param param_value2: Observed_B = new Observed_B()
@Param param_value3: Observed_B = new Observed_B()
@Event event_value: Observed_B = new Observed_B()
@Provider() provide_value: Observed_B = new Observed_B()
@Consumer() consumer_value: Observed_B = new Observed_B()
build() {
}
}
`