!818 多级struct传递@Builder this指向错误

Merge pull request !818 from houhaoyu/master6
This commit is contained in:
openharmony_ci
2022-07-30 08:28:44 +00:00
committed by Gitee
8 changed files with 112 additions and 64 deletions
+2
View File
@@ -82,6 +82,8 @@ export const STYLES_ATTRIBUTE: Set<string> = new Set();
export const INTERFACE_NODE_SET: Set<ts.InterfaceDeclaration> = new Set();
export const INNER_CUSTOM_BUILDER_METHOD: Set<string> = new Set();
export const JS_BIND_COMPONENTS: Set<string> = new Set([
'ForEach', 'LazyForEach', ...GESTURE_TYPE_NAMES, 'Gesture',
'PanGestureOption', 'CustomDialogController', 'Storage', 'Scroller', 'SwiperController',
+48 -26
View File
@@ -76,6 +76,7 @@ import {
GLOBAL_STYLE_FUNCTION,
COMMON_ATTRS,
CUSTOM_BUILDER_PROPERTIES,
INNER_CUSTOM_BUILDER_METHOD,
ID_ATTRS
} from './component_map';
import {
@@ -112,9 +113,10 @@ export function processComponentBuild(node: ts.MethodDeclaration,
}
export function processComponentBlock(node: ts.Block, isLazy: boolean, log: LogInfo[],
isTransition: boolean = false): ts.Block {
isTransition: boolean = false, isInnerBuilder: boolean = false): ts.Block {
const newStatements: ts.Statement[] = [];
processComponentChild(node, newStatements, log);
processComponentChild(node, newStatements, log,
{isAcceleratePreview: false, line: 0, column: 0, fileName: ''}, isInnerBuilder);
if (isLazy) {
newStatements.unshift(createRenderingInProgress(true));
}
@@ -210,8 +212,9 @@ function validateEtsComponentNode(node: ts.CallExpression | ts.EtsComponentExpre
let sourceNode: ts.SourceFile;
export function processComponentChild(node: ts.Block | ts.SourceFile, newStatements: ts.Statement[], log: LogInfo[],
supplement: supplementType = {isAcceleratePreview: false, line: 0, column: 0, fileName: ''}): void {
export function processComponentChild(node: ts.Block | ts.SourceFile, newStatements: ts.Statement[],
log: LogInfo[], supplement: supplementType = {isAcceleratePreview: false, line: 0, column: 0, fileName: ''},
isInnerBuilder: boolean = false): void {
if (supplement.isAcceleratePreview) {
newsupplement = supplement;
const compilerOptions = ts.readConfigFile(
@@ -239,19 +242,25 @@ export function processComponentChild(node: ts.Block | ts.SourceFile, newStateme
item = expressionResult;
}
}
processCustomComponent(item as ts.ExpressionStatement, newStatements, log, name);
processCustomComponent(item as ts.ExpressionStatement, newStatements, log, name, isInnerBuilder);
}
break;
case ComponentType.forEachComponent:
processForEachComponent(item, newStatements, log);
processForEachComponent(item, newStatements, log, isInnerBuilder);
break;
case ComponentType.customBuilderMethod:
if (INNER_CUSTOM_BUILDER_METHOD.has(name)) {
newStatements.push(addInnerBuilderParameter(item));
} else {
newStatements.push(item);
}
break;
case ComponentType.builderParamMethod:
newStatements.push(item);
newStatements.push(addInnerBuilderParameter(item));
break;
}
} else if (ts.isIfStatement(item)) {
processIfStatement(item, newStatements, log);
processIfStatement(item, newStatements, log, isInnerBuilder);
} else if (!ts.isBlock(item)) {
log.push({
type: LogType.ERROR,
@@ -269,6 +278,16 @@ export function processComponentChild(node: ts.Block | ts.SourceFile, newStateme
};
}
function addInnerBuilderParameter(node: ts.ExpressionStatement): ts.ExpressionStatement {
if (node.expression && ts.isCallExpression(node.expression) && node.expression.arguments) {
node.expression.arguments.push(ts.factory.createThis());
return ts.factory.createExpressionStatement(ts.factory.updateCallExpression(node.expression,
node.expression.expression, node.expression.typeArguments, node.expression.arguments));
} else {
return node;
}
}
function processExpressionStatementChange(node: ts.ExpressionStatement, nextNode: ts.Block,
log: LogInfo[]): ts.ExpressionStatement {
let name: string;
@@ -409,7 +428,7 @@ function getRealNodePos(node: ts.Node): number {
}
function processForEachComponent(node: ts.ExpressionStatement, newStatements: ts.Statement[],
log: LogInfo[]): void {
log: LogInfo[], isInnerBuilder: boolean = false): void {
const popNode: ts.ExpressionStatement = ts.factory.createExpressionStatement(createFunction(
// @ts-ignore
node.expression.expression as ts.Identifier,
@@ -427,7 +446,7 @@ function processForEachComponent(node: ts.ExpressionStatement, newStatements: ts
ts.factory.createIdentifier(FOREACH_GET_RAW_OBJECT)), undefined, [argumentsArray[0]]);
}
argumentsArray.splice(0, 1, arrayObserveredObject);
const newArrowNode: ts.ArrowFunction = processForEachBlock(node.expression, log);
const newArrowNode: ts.ArrowFunction = processForEachBlock(node.expression, log, isInnerBuilder);
if (newArrowNode) {
argumentsArray.splice(1, 1, newArrowNode);
}
@@ -445,7 +464,8 @@ function addForEachId(node: ts.ExpressionStatement): ts.ExpressionStatement {
...forEachComponent.arguments]));
}
function processForEachBlock(node: ts.CallExpression, log: LogInfo[]): ts.ArrowFunction {
function processForEachBlock(node: ts.CallExpression, log: LogInfo[],
isInnerBuilder: boolean = false): ts.ArrowFunction {
if (node.arguments.length > 1 && ts.isArrowFunction(node.arguments[1])) {
const isLazy: boolean = node.expression.getText() === COMPONENT_LAZYFOREACH;
const arrowNode: ts.ArrowFunction = node.arguments[1] as ts.ArrowFunction;
@@ -467,7 +487,8 @@ function processForEachBlock(node: ts.CallExpression, log: LogInfo[]): ts.ArrowF
} else {
return ts.factory.updateArrowFunction(
arrowNode, arrowNode.modifiers, arrowNode.typeParameters, arrowNode.parameters,
arrowNode.type, arrowNode.equalsGreaterThanToken, processComponentBlock(body, isLazy, log));
arrowNode.type, arrowNode.equalsGreaterThanToken,
processComponentBlock(body, isLazy, log, false, isInnerBuilder));
}
}
return null;
@@ -485,14 +506,15 @@ function createRenderingInProgress(isTrue: boolean): ts.ExpressionStatement {
}
function processIfStatement(node: ts.IfStatement, newStatements: ts.Statement[],
log: LogInfo[]): void {
log: LogInfo[], isInnerBuilder: boolean = false): void {
const ifCreate: ts.ExpressionStatement = createIfCreate();
const newIfNode: ts.IfStatement = processInnerIfStatement(node, 0, log);
const newIfNode: ts.IfStatement = processInnerIfStatement(node, 0, log, isInnerBuilder);
const ifPop: ts.ExpressionStatement = createIfPop();
newStatements.push(ifCreate, newIfNode, ifPop);
}
function processInnerIfStatement(node: ts.IfStatement, id: number, log: LogInfo[]): ts.IfStatement {
function processInnerIfStatement(node: ts.IfStatement, id: number, log: LogInfo[],
isInnerBuilder: boolean = false): ts.IfStatement {
if (ts.isIdentifier(node.expression) && node.expression.originalKeywordKind === undefined &&
!node.expression.escapedText) {
log.push({
@@ -503,15 +525,15 @@ function processInnerIfStatement(node: ts.IfStatement, id: number, log: LogInfo[
node = ts.factory.updateIfStatement(node, ts.factory.createIdentifier(COMPONENT_IF_UNDEFINED),
node.thenStatement, node.elseStatement);
}
const newThenStatement: ts.Statement = processThenStatement(node.thenStatement, id, log);
const newElseStatement: ts.Statement = processElseStatement(node.elseStatement, id, log);
const newThenStatement: ts.Statement = processThenStatement(node.thenStatement, id, log, isInnerBuilder);
const newElseStatement: ts.Statement = processElseStatement(node.elseStatement, id, log, isInnerBuilder);
const newIfNode: ts.IfStatement = ts.factory.updateIfStatement(
node, node.expression, newThenStatement, newElseStatement);
return newIfNode;
}
function processThenStatement(thenStatement: ts.Statement, id: number,
log: LogInfo[]): ts.Statement {
log: LogInfo[], isInnerBuilder: boolean = false): ts.Statement {
if (ts.isExpressionStatement(thenStatement) && ts.isIdentifier(thenStatement.expression) &&
thenStatement.expression.originalKeywordKind === undefined &&
!thenStatement.expression.escapedText) {
@@ -523,36 +545,36 @@ function processThenStatement(thenStatement: ts.Statement, id: number,
}
if (thenStatement) {
if (ts.isBlock(thenStatement)) {
thenStatement = processIfBlock(thenStatement, id, log);
thenStatement = processIfBlock(thenStatement, id, log, isInnerBuilder);
} else if (ts.isIfStatement(thenStatement)) {
thenStatement = processInnerIfStatement(thenStatement, 0, log);
thenStatement = processInnerIfStatement(thenStatement, 0, log, isInnerBuilder);
thenStatement = ts.factory.createBlock(
[createIfCreate(), createIfBranchId(id), thenStatement, createIfPop()], true);
} else {
thenStatement = ts.factory.createBlock([thenStatement], true);
thenStatement = processIfBlock(thenStatement as ts.Block, id, log);
thenStatement = processIfBlock(thenStatement as ts.Block, id, log, isInnerBuilder);
}
}
return thenStatement;
}
function processElseStatement(elseStatement: ts.Statement, id: number,
log: LogInfo[]): ts.Statement {
log: LogInfo[], isInnerBuilder: boolean = false): ts.Statement {
if (elseStatement) {
if (ts.isBlock(elseStatement)) {
elseStatement = processIfBlock(elseStatement, id + 1, log);
} else if (ts.isIfStatement(elseStatement)) {
elseStatement = processInnerIfStatement(elseStatement, id + 1, log);
elseStatement = processInnerIfStatement(elseStatement, id + 1, log, isInnerBuilder);
} else {
elseStatement = ts.factory.createBlock([elseStatement], true);
elseStatement = processIfBlock(elseStatement as ts.Block, id + 1, log);
elseStatement = processIfBlock(elseStatement as ts.Block, id + 1, log, isInnerBuilder);
}
}
return elseStatement;
}
function processIfBlock(block: ts.Block, id: number, log: LogInfo[]): ts.Block {
return addIfBranchId(id, processComponentBlock(block, false, log));
function processIfBlock(block: ts.Block, id: number, log: LogInfo[], isInnerBuilder: boolean = false): ts.Block {
return addIfBranchId(id, processComponentBlock(block, false, log, false, isInnerBuilder));
}
function addIfBranchId(id: number, container: ts.Block): ts.Block {
+12 -5
View File
@@ -55,14 +55,17 @@ import {
COMPONENT_SET_AND_LINK,
COMPONENT_SET_AND_PROP,
COMPONENT_CONSTRUCTOR_UNDEFINED,
CUSTOM_COMPONENT
CUSTOM_COMPONENT,
COMPONENT_CONSTRUCTOR_PARENT,
COMPONENT_IF_UNDEFINED
} from './pre_define';
import {
BUILDIN_STYLE_NAMES,
CUSTOM_BUILDER_METHOD,
INNER_STYLE_FUNCTION,
INTERFACE_NODE_SET,
STYLES_ATTRIBUTE
STYLES_ATTRIBUTE,
INNER_CUSTOM_BUILDER_METHOD
} from './component_map';
import {
componentCollection,
@@ -300,9 +303,13 @@ function processComponentMethod(node: ts.MethodDeclaration, parentComponentName:
node.type, processComponentBlock(node.body, false, log, true));
} else if (hasDecorator(node, COMPONENT_BUILDER_DECORATOR)) {
CUSTOM_BUILDER_METHOD.add(name);
const builderNode: ts.MethodDeclaration = ts.factory.updateMethodDeclaration(node, undefined, node.modifiers,
node.asteriskToken, node.name, node.questionToken, node.typeParameters, node.parameters,
node.type, processComponentBlock(node.body, false, log));
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)));
const builderNode: ts.MethodDeclaration = ts.factory.updateMethodDeclaration(node, undefined,
node.modifiers, node.asteriskToken, node.name, node.questionToken, node.typeParameters,
node.parameters, node.type, processComponentBlock(node.body, false, log, false, true));
updateItem = processBuildMember(builderNode, context, log);
} else if (hasDecorator(node, COMPONENT_STYLES_DECORATOR)) {
if (node.parameters && node.parameters.length === 0) {
+13 -6
View File
@@ -52,7 +52,8 @@ import {
COMPONENT_CREATE_FUNCTION,
COMPONENT_BUILDERPARAM_DECORATOR,
COMPONENT_LOCAL_STORAGE_LINK_DECORATOR,
COMPONENT_LOCAL_STORAGE_PROP_DECORATOR
COMPONENT_LOCAL_STORAGE_PROP_DECORATOR,
COMPONENT_CONSTRUCTOR_PARENT
} from './pre_define';
import {
forbiddenUseStateType,
@@ -572,14 +573,15 @@ export function createViewCreate(node: ts.NewExpression | ts.Identifier): ts.Cal
ts.factory.createIdentifier(COMPONENT_CREATE_FUNCTION), ts.factory.createNodeArray([node]));
}
export function createCustomComponentNewExpression(node: ts.CallExpression, name: string)
: ts.NewExpression {
export function createCustomComponentNewExpression(node: ts.CallExpression, name: string,
isInnerBuilder: boolean = false): ts.NewExpression {
const newNode: ts.NewExpression = ts.factory.createNewExpression(node.expression,
node.typeArguments, node.arguments.length ? node.arguments : []);
return addCustomComponentId(newNode, name);
return addCustomComponentId(newNode, name, isInnerBuilder);
}
function addCustomComponentId(node: ts.NewExpression, componentName: string): ts.NewExpression {
function addCustomComponentId(node: ts.NewExpression, componentName: string,
isInnerBuilder: boolean = false): ts.NewExpression {
for (const item of componentCollection.customComponents) {
componentInfo.componentNames.add(item);
}
@@ -593,7 +595,12 @@ function addCustomComponentId(node: ts.NewExpression, componentName: string): ts
argumentsArray = [ts.factory.createObjectLiteralExpression([], true)];
}
argumentsArray.unshift(ts.factory.createStringLiteral((++componentInfo.id).toString()),
ts.factory.createThis());
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) {
+21 -11
View File
@@ -32,7 +32,8 @@ import {
COMPONENT_CONSTRUCTOR_UNDEFINED,
CUSTOM_COMPONENT_NEEDS_UPDATE_FUNCTION,
CUSTOM_COMPONENT_MARK_STATIC_FUNCTION,
COMPONENT_COMMON
COMPONENT_COMMON,
COMPONENT_CONSTRUCTOR_PARENT
} from './pre_define';
import {
propertyCollection,
@@ -75,14 +76,14 @@ const decoractorMap: Map<string, Map<string, Set<string>>> = new Map(
[COMPONENT_OBJECT_LINK_DECORATOR, objectLinkCollection]]);
export function processCustomComponent(node: ts.ExpressionStatement, newStatements: ts.Statement[],
log: LogInfo[], name: string): void {
log: LogInfo[], name: string, isInnerBuilder: boolean = false): void {
const componentNode: ts.CallExpression = getCustomComponentNode(node);
if (componentNode) {
const hasChainCall: boolean = componentNode.parent &&
ts.isPropertyAccessExpression(componentNode.parent);
let ischangeNode: boolean = false;
let customComponentNewExpression: ts.NewExpression = createCustomComponentNewExpression(
componentNode, name);
componentNode, name, isInnerBuilder);
let argumentsArray: ts.PropertyAssignment[];
if (isHasChild(componentNode)) {
// @ts-ignore
@@ -100,7 +101,7 @@ export function processCustomComponent(node: ts.ExpressionStatement, newStatemen
ts.factory.createNewExpression(componentNode.expression, componentNode.typeArguments,
[ts.factory.createObjectLiteralExpression(argumentsArray, true)]));
customComponentNewExpression = createCustomComponentNewExpression(
newNode.expression as ts.CallExpression, name);
newNode.expression as ts.CallExpression, name, isInnerBuilder);
}
}
if (hasChainCall) {
@@ -109,7 +110,8 @@ export function processCustomComponent(node: ts.ExpressionStatement, newStatemen
ts.factory.createIdentifier(COMPONENT_CREATE_FUNCTION), null)));
bindComponentAttr(node, ts.factory.createIdentifier(COMPONENT_COMMON), newStatements, log);
}
addCustomComponent(node, newStatements, customComponentNewExpression, log, name, componentNode);
addCustomComponent(node, newStatements, customComponentNewExpression, log, name, componentNode,
isInnerBuilder);
if (hasChainCall) {
newStatements.push(ts.factory.createExpressionStatement(
createFunction(ts.factory.createIdentifier(COMPONENT_COMMON),
@@ -140,18 +142,20 @@ function changeNodeFromCallToArrow(node: ts.CallExpression): ts.ArrowFunction {
}
function addCustomComponent(node: ts.ExpressionStatement, newStatements: ts.Statement[],
newNode: ts.NewExpression, log: LogInfo[], name: string, componentNode: ts.CallExpression): void {
newNode: ts.NewExpression, log: LogInfo[], name: string, componentNode: ts.CallExpression,
isInnerBuilder: boolean = false): void {
if (ts.isNewExpression(newNode)) {
const propertyArray: ts.ObjectLiteralElementLike[] = [];
validateCustomComponentPrams(componentNode, name, propertyArray, log);
addCustomComponentStatements(node, newStatements, newNode, name, propertyArray);
addCustomComponentStatements(node, newStatements, newNode, name, propertyArray, isInnerBuilder);
}
}
function addCustomComponentStatements(node: ts.ExpressionStatement, newStatements: ts.Statement[],
newNode: ts.NewExpression, name: string, props: ts.ObjectLiteralElementLike[]): void {
newNode: ts.NewExpression, name: string, props: ts.ObjectLiteralElementLike[],
isInnerBuilder: boolean = false): void {
const id: string = componentInfo.id.toString();
newStatements.push(createFindChildById(id, name), createCustomComponentIfStatement(id,
newStatements.push(createFindChildById(id, name, isInnerBuilder), createCustomComponentIfStatement(id,
ts.factory.updateExpressionStatement(node, createViewCreate(newNode)),
ts.factory.createObjectLiteralExpression(props, true), name));
}
@@ -355,12 +359,18 @@ function getPropertyDecoratorKind(propertyName: string, customComponentName: str
}
}
function createFindChildById(id: string, name: string): ts.VariableStatement {
function createFindChildById(id: string, name: string, isInnerBuilder: boolean = false): ts.VariableStatement {
return ts.factory.createVariableStatement(undefined, ts.factory.createVariableDeclarationList(
[ts.factory.createVariableDeclaration(ts.factory.createIdentifier(
`${CUSTOM_COMPONENT_EARLIER_CREATE_CHILD}${id}`), undefined, ts.factory.createTypeReferenceNode(
ts.factory.createIdentifier(name)), ts.factory.createAsExpression(ts.factory.createCallExpression(
ts.factory.createPropertyAccessExpression(ts.factory.createThis(), ts.factory.createIdentifier(
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.createTypeReferenceNode(ts.factory.createIdentifier(name))))], ts.NodeFlags.Let));
}
@@ -185,12 +185,12 @@ class MyComponent extends View {
set hideBar(newValue) {
this.__hideBar.set(newValue);
}
textBuilder() {
textBuilder(parent = undefined) {
Text.create("文本");
Text.fontSize(30);
Text.pop();
}
NavigationTitlePara(label) {
NavigationTitlePara(label, parent = undefined) {
Column.create();
Text.create(label);
Text.width(80);
@@ -198,7 +198,7 @@ class MyComponent extends View {
Text.pop();
Column.pop();
}
MenuBuilder() {
MenuBuilder(parent = undefined) {
Flex.create({ direction: FlexDirection.Column, justifyContent: FlexAlign.Center, alignItems: ItemAlign.Center });
Flex.width(100);
Text.create('Test menu item 1');
@@ -82,10 +82,10 @@ class TestPage extends View {
set value(newValue) {
this.__value.set(newValue);
}
TitleCompView() {
let earlierCreatedChild_2 = this.findChildById("2");
TitleCompView(parent = undefined) {
let earlierCreatedChild_2 = (parent ? parent : this).findChildById("2");
if (earlierCreatedChild_2 == undefined) {
View.create(new TitleComp("2", this, { title: this.__value }));
View.create(new TitleComp("2", parent ? parent : this, { title: this.__value }));
}
else {
earlierCreatedChild_2.updateWithValueParams({});
@@ -94,7 +94,7 @@ class TestPage extends View {
}
render() {
Flex.create();
this.TitleCompView();
this.TitleCompView(this);
Flex.pop();
}
}
@@ -121,8 +121,8 @@ class CustomContainer extends View {
Column.create();
Text.create(this.header);
Text.pop();
this.content();
this.callContent();
this.content(this);
this.callContent(this);
Text.create(this.footer);
Text.pop();
Column.pop();
@@ -147,7 +147,7 @@ class CustomContainer2 extends View {
Column.create();
Text.create(this.header);
Text.pop();
this.content();
this.content(this);
Column.pop();
}
}
@@ -172,14 +172,14 @@ class CustomContainerUser extends View {
set text(newValue) {
this.__text.set(newValue);
}
specificParam() {
specificParam(parent = undefined) {
Column.create();
Text.create("content");
Text.fontSize(50);
Text.pop();
Column.pop();
}
callSpecificParam(label1, label2) {
callSpecificParam(label1, label2, parent = undefined) {
Column.create();
Text.create(label1);
Text.fontSize(50);
@@ -200,7 +200,7 @@ class CustomContainerUser extends View {
Column.onClick(() => {
this.text = "changeHeader";
});
specificParam("111", "22");
specificParam("111", "22", this);
Column.pop();
}
}));
@@ -213,7 +213,7 @@ class CustomContainerUser extends View {
Column.onClick(() => {
this.text = "changeHeader";
});
specificParam("111", "22");
specificParam("111", "22", this);
Column.pop();
}
});
@@ -249,7 +249,7 @@ class CustomContainerUser extends View {
Column.onClick(() => {
this.text = "changeHeader";
});
this.callSpecificParam("111", '222');
this.callSpecificParam("111", '222', this);
Column.pop();
}
}));
@@ -262,7 +262,7 @@ class CustomContainerUser extends View {
Column.onClick(() => {
this.text = "changeHeader";
});
this.callSpecificParam("111", '222');
this.callSpecificParam("111", '222', this);
Column.pop();
}
});