fix static member for @ComponentV2.

Signed-off-by: lihong <lihong67@huawei.com>
Change-Id: I3505ff3d387306d8ac86228496b781527c24a1f0
This commit is contained in:
lihong 2024-07-15 19:59:25 +08:00
parent b910492eed
commit c61dcf8c08
2 changed files with 170 additions and 10 deletions

View File

@ -69,6 +69,7 @@ export class StructInfo {
builderParamDecoratorSet: Set<string> = new Set();
regularSet: Set<string> = new Set();
propertiesMap: Map<string, ts.Expression> = new Map();
staticPropertySet: Set<string> = new Set();
}
const structMapInEts: Map<string, StructInfo> = new Map();
@ -144,11 +145,15 @@ function traverseStructInfo(structInfo: StructInfo,
const needInitFromParams: string[] = [...structInfo.builderParamDecoratorSet,
...structInfo.eventDecoratorSet];
for (const property of structInfo.propertiesMap) {
setPropertyStatement(structInfo, addStatementsInConstructor, property[0], property[1],
needInitFromParams);
if (!structInfo.staticPropertySet.has(property[0])) {
setPropertyStatement(structInfo, addStatementsInConstructor, property[0], property[1],
needInitFromParams);
}
}
for (const param of structInfo.paramDecoratorMap) {
paramStatementsInStateVarsMethod.push(updateParamNode(param[0]));
if (!structInfo.staticPropertySet.has(param[0])) {
paramStatementsInStateVarsMethod.push(updateParamNode(param[0]));
}
}
}
@ -191,27 +196,31 @@ function processComponentProperty(member: ts.PropertyDeclaration, structInfo: St
log: LogInfo[]): ts.PropertyDeclaration {
const propName: string = member.name.getText();
const decorators: readonly ts.Decorator[] = ts.getAllDecorators(member);
let initializer: ts.Expression;
if (structInfo.staticPropertySet.has(propName)) {
initializer = member.initializer;
}
if (structInfo.paramDecoratorMap.has(propName)) {
return processParamProperty(member, decorators);
return processParamProperty(member, decorators, initializer);
}
if (structInfo.builderParamDecoratorSet.has(propName)) {
return processBuilderParamProperty(member, log, decorators);
return processBuilderParamProperty(member, log, decorators, initializer);
}
return ts.factory.updatePropertyDeclaration(member,
ts.concatenateDecoratorsAndModifiers(decorators, ts.getModifiers(member)),
member.name, member.questionToken, member.type, undefined);
member.name, member.questionToken, member.type, initializer);
}
function processParamProperty(member: ts.PropertyDeclaration,
decorators: readonly ts.Decorator[]): ts.PropertyDeclaration {
decorators: readonly ts.Decorator[], initializer: ts.Expression): ts.PropertyDeclaration {
const newDecorators: readonly ts.Decorator[] = removeDecorator(decorators, constantDefine.REQUIRE);
return ts.factory.updatePropertyDeclaration(member,
ts.concatenateDecoratorsAndModifiers(newDecorators, ts.getModifiers(member)),
member.name, member.questionToken, member.type, undefined);
member.name, member.questionToken, member.type, initializer);
}
function processBuilderParamProperty(member: ts.PropertyDeclaration, log: LogInfo[],
decorators: readonly ts.Decorator[]): ts.PropertyDeclaration {
decorators: readonly ts.Decorator[], initializer: ts.Expression): ts.PropertyDeclaration {
if (judgeBuilderParamAssignedByBuilder(member)) {
log.push({
type: LogType.ERROR,
@ -222,7 +231,7 @@ function processBuilderParamProperty(member: ts.PropertyDeclaration, log: LogInf
const newDecorators: readonly ts.Decorator[] = removeDecorator(decorators, constantDefine.BUILDER_PARAM);
return ts.factory.updatePropertyDeclaration(member,
ts.concatenateDecoratorsAndModifiers(newDecorators, ts.getModifiers(member)),
member.name, member.questionToken, member.type, undefined);
member.name, member.questionToken, member.type, initializer);
}
function setInitValue(propName: string, initializer: ts.Expression,
@ -257,12 +266,26 @@ function parseComponentProperty(node: ts.StructDeclaration, structInfo: StructIn
node.members.forEach((member: ts.ClassElement) => {
if (ts.isPropertyDeclaration(member)) {
const decorators: readonly ts.Decorator[] = ts.getAllDecorators(member);
const modifiers: readonly ts.Modifier[] = ts.getModifiers(member);
structInfo.propertiesMap.set(member.name.getText(), member.initializer);
parsePropertyDecorator(member, decorators, structInfo, log, sourceFileNode);
parsePropertyModifiers(member.name.getText(), structInfo, modifiers);
}
});
}
function parsePropertyModifiers(propName: string, structInfo: StructInfo,
modifiers: readonly ts.Modifier[]): void {
if (modifiers && modifiers.length) {
const isStatic: boolean = modifiers.some((item: ts.Modifier) => {
return item.kind === ts.SyntaxKind.StaticKeyword;
});
if (isStatic) {
structInfo.staticPropertySet.add(propName);
}
}
}
class PropertyDecorator {
hasParam: boolean = false;
hasRequire: boolean = false;

View File

@ -0,0 +1,137 @@
/*
* 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
@ComponentV2
struct HomeComponent {
build() {
Column() {
}
}
}
@Builder
function testBuilder() {
Text("testBuilder")
}
@ComponentV2
struct ChildComponent {
@Local static local_value: string = "hello"
@Param static param_value: string = "hello"
@Event static event_value: Function = () => {}
@Provider() static provider_value: number = 0
@Consumer("a") static consumer_value: boolean = true
@BuilderParam static builder_value: Function = testBuilder
@Monitor("local_value")
static testMonitor() {}
@Computed
static get fullName() {
return ChildComponent.param_value
}
build() {
Column() {}
}
}
`
exports.expectResult =
`"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
if (!("finalizeConstruction" in ViewPU.prototype)) {
Reflect.set(ViewPU.prototype, "finalizeConstruction", () => { });
}
class HomeComponent extends ViewV2 {
constructor(parent, params, __localStorage, elmtId = -1, paramsLambda, extraInfo) {
super(parent, elmtId, extraInfo);
this.finalizeConstruction();
}
initialRender() {
this.observeComponentCreation2((elmtId, isInitialRender) => {
Column.create();
}, Column);
Column.pop();
}
rerender() {
this.updateDirtyElements();
}
static getEntryName() {
return "HomeComponent";
}
}
function testBuilder(parent = null) {
(parent ? parent : this).observeComponentCreation2((elmtId, isInitialRender) => {
Text.create("testBuilder");
}, Text);
Text.pop();
}
class ChildComponent extends ViewV2 {
constructor(parent, params, __localStorage, elmtId = -1, paramsLambda, extraInfo) {
super(parent, elmtId, extraInfo);
this.finalizeConstruction();
}
static testMonitor() { }
static get fullName() {
return ChildComponent.param_value;
}
initialRender() {
this.observeComponentCreation2((elmtId, isInitialRender) => {
Column.create();
}, Column);
Column.pop();
}
rerender() {
this.updateDirtyElements();
}
}
ChildComponent.local_value = "hello";
ChildComponent.param_value = "hello";
ChildComponent.event_value = () => { };
ChildComponent.provider_value = 0;
ChildComponent.consumer_value = true;
ChildComponent.builder_value = testBuilder;
__decorate([
Local
], ChildComponent, "local_value", void 0);
__decorate([
Param
], ChildComponent, "param_value", void 0);
__decorate([
Event
], ChildComponent, "event_value", void 0);
__decorate([
Provider()
], ChildComponent, "provider_value", void 0);
__decorate([
Consumer("a")
], ChildComponent, "consumer_value", void 0);
__decorate([
Monitor("local_value")
], ChildComponent, "testMonitor", null);
__decorate([
Computed
], ChildComponent, "fullName", null);
ViewStackProcessor.StartGetAccessRecordingFor(ViewStackProcessor.AllocateNewElmetIdForNextComponent());
loadDocument(new HomeComponent(undefined, {}));
ViewStackProcessor.StopGetAccessRecording();
`