From 9853d839d62252c77e917a89ed4cd942a59a4435 Mon Sep 17 00:00:00 2001 From: puyajun Date: Sat, 19 Mar 2022 15:02:09 +0800 Subject: [PATCH] puyajun@huawei.com dix check component Signed-off-by: puyajun --- compiler/src/validate_ui_syntax.ts | 97 +++++++++++++----------------- 1 file changed, 41 insertions(+), 56 deletions(-) diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 6a326f3e7..0a199d73b 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -318,7 +318,7 @@ function checkAllNode(node: ts.Node, allComponentNames: Set, sourceFileN } function checkNoChildComponent(node: ts.Node, sourceFileNode: ts.SourceFile, log: LogInfo[]): void { - if (ts.isExpressionStatement(node) && ts.isCallExpression(node.expression) && + if (ts.isExpressionStatement(node) && ts.isEtsComponentExpression(node.expression) && ts.isIdentifier(node.expression.expression) && hasChild(node)) { const componentName: string = node.expression.expression.escapedText.toString(); const pos: number = node.expression.expression.getStart(); @@ -328,30 +328,24 @@ function checkNoChildComponent(node: ts.Node, sourceFileNode: ts.SourceFile, log } function hasChild(node: ts.ExpressionStatement): boolean { - const callExpression: ts.CallExpression = node.expression as ts.CallExpression; - const nodeName: ts.Identifier = callExpression.expression as ts.Identifier; - if (AUTOMIC_COMPONENT.has(nodeName.escapedText.toString()) && getNextNode(node)) { + const etsComponentExpression: ts.EtsComponentExpression = node.expression as ts.EtsComponentExpression; + const nodeName: ts.Identifier = etsComponentExpression.expression as ts.Identifier; + if (AUTOMIC_COMPONENT.has(nodeName.escapedText.toString()) && getNextNode(etsComponentExpression)) { return true; } return false; } -function getNextNode(node: ts.Node): ts.Block { - if (node.parent && ts.isBlock(node.parent) && node.parent.statements) { - const statementsArray: ts.Node[] = Array.from(node.parent.statements); - for (let i = 0; i < statementsArray.length - 1; i++) { - const curNode: ts.Node = statementsArray[i]; - const nextNode: ts.Node = statementsArray[i + 1]; - if (node === curNode && ts.isBlock(nextNode)) { - return nextNode; - } - } +function getNextNode(node: ts.EtsComponentExpression): ts.Block { + if (node.body && ts.isBlock(node.body)) { + const statementsArray: ts.Block = node.body; + return statementsArray } } function checkOneChildComponent(node: ts.Node, allComponentNames: Set, sourceFileNode: ts.SourceFile, log: LogInfo[]): void { - if (ts.isExpressionStatement(node) && ts.isCallExpression(node.expression) && + if (ts.isExpressionStatement(node) && ts.isEtsComponentExpression(node.expression) && ts.isIdentifier(node.expression.expression) && hasNonSingleChild(node, allComponentNames)) { const componentName: string = node.expression.expression.escapedText.toString(); const pos: number = node.expression.expression.getStart(); @@ -362,22 +356,22 @@ function checkOneChildComponent(node: ts.Node, allComponentNames: Set, } function hasNonSingleChild(node: ts.ExpressionStatement, allComponentNames: Set): boolean { - const callExpression: ts.CallExpression = node.expression as ts.CallExpression; - const nodeName: ts.Identifier = callExpression.expression as ts.Identifier; - const nextBlockNode: ts.Block = getNextNode(node); + const etsComponentExpression: ts.EtsComponentExpression = node.expression as ts.EtsComponentExpression; + const nodeName: ts.Identifier = etsComponentExpression.expression as ts.Identifier; + const BlockNode: ts.Block = getNextNode(etsComponentExpression); if (SINGLE_CHILD_COMPONENT.has(nodeName.escapedText.toString())) { - if (!nextBlockNode) { + if (!BlockNode) { return false; } - if (nextBlockNode && nextBlockNode.statements) { - const length: number = nextBlockNode.statements.length; + if (BlockNode && BlockNode.statements) { + const length: number = BlockNode.statements.length; if (!length) { return false; } if (length > 3) { return true; } - const childCount: number = getBlockChildrenCount(nextBlockNode, allComponentNames); + const childCount: number = getBlockChildrenCount(BlockNode, allComponentNames); if (childCount > 1) { return true; } @@ -391,28 +385,19 @@ function getBlockChildrenCount(blockNode: ts.Block, allComponentNames: Set 1) { break; @@ -421,7 +406,7 @@ function getBlockChildrenCount(blockNode: ts.Block, allComponentNames: Set): boolean { +function isComponent(node: ts.EtsComponentExpression, allComponentNames: Set): boolean { if (ts.isIdentifier(node.expression) && allComponentNames.has(node.expression.escapedText.toString())) { return true; @@ -429,7 +414,7 @@ function isComponent(node: ts.CallExpression, allComponentNames: Set): b return false; } -function isForEachComponent(node: ts.CallExpression): boolean { +function isForEachComponent(node: ts.EtsComponentExpression): boolean { if (ts.isIdentifier(node.expression)) { const componentName: string = node.expression.escapedText.toString(); return componentName === COMPONENT_FOREACH || componentName === COMPONENT_LAZYFOREACH; @@ -452,10 +437,10 @@ function getStatementCount(node: ts.Node, allComponentNames: Set): numbe maxCount = getBlockChildrenCount(node, allComponentNames); } else if (ts.isIfStatement(node)) { maxCount = getIfChildrenCount(node, allComponentNames); - } else if (ts.isExpressionStatement(node) && ts.isCallExpression(node.expression) && + } else if (ts.isExpressionStatement(node) && ts.isEtsComponentExpression(node.expression) && isForEachComponent(node.expression)) { maxCount = 2; - } else if (ts.isExpressionStatement(node) && ts.isCallExpression(node.expression) && + } else if (ts.isExpressionStatement(node) && ts.isEtsComponentExpression(node.expression) && !isForEachComponent(node.expression) && isComponent(node.expression, allComponentNames)) { maxCount = 1; } @@ -478,10 +463,10 @@ function checkSpecificChildComponent(node: ts.Node, allComponentNames: Set): boolean { - const callExpression: ts.CallExpression = node.expression as ts.CallExpression; - const nodeName: ts.Identifier = callExpression.expression as ts.Identifier; + const etsComponentExpression: ts.EtsComponentExpression = node.expression as ts.EtsComponentExpression; + const nodeName: ts.Identifier = etsComponentExpression.expression as ts.Identifier; const nodeNameString: string = nodeName.escapedText.toString(); - const blockNode: ts.Block = getNextNode(node); + const blockNode: ts.Block = getNextNode(etsComponentExpression); let isNonspecific: boolean = false; if (SPECIFIC_CHILD_COMPONENT.has(nodeNameString) && blockNode) { const specificChildSet: Set = SPECIFIC_CHILD_COMPONENT.get(nodeNameString); @@ -502,7 +487,7 @@ function isNonspecificChildBlock(blockNode: ts.Block, specificChildSet: Set, +function isNonspecificChildForEach(node: ts.EtsComponentExpression, specificChildSet: Set, allComponentNames: Set): boolean { - if (ts.isCallExpression(node) && node.arguments && + if (ts.isEtsComponentExpression(node) && node.arguments && node.arguments.length > 1 && ts.isArrowFunction(node.arguments[1])) { const arrowFunction: ts.ArrowFunction = node.arguments[1] as ts.ArrowFunction; - const body: ts.Block | ts.CallExpression | ts.IfStatement = - arrowFunction.body as ts.Block | ts.CallExpression | ts.IfStatement; + const body: ts.Block | ts.EtsComponentExpression | ts.IfStatement = + arrowFunction.body as ts.Block | ts.EtsComponentExpression | ts.IfStatement; if (!body) { return false; } @@ -558,7 +543,7 @@ function isNonspecificChildForEach(node: ts.CallExpression, specificChildSet: Se isNonspecificChildForEach(body, specificChildSet, allComponentNames)) { return true; } - if (ts.isCallExpression(body) && !isForEachComponent(body) && + if (ts.isEtsComponentExpression(body) && !isForEachComponent(body) && isComponent(body, allComponentNames) && isNonspecificChildNonForEach(body, specificChildSet)) { return true; @@ -567,7 +552,7 @@ function isNonspecificChildForEach(node: ts.CallExpression, specificChildSet: Se return false; } -function isNonspecificChildNonForEach(node: ts.CallExpression, +function isNonspecificChildNonForEach(node: ts.EtsComponentExpression, specificChildSet: Set): boolean { if (ts.isIdentifier(node.expression) && !specificChildSet.has(node.expression.escapedText.toString())) { @@ -587,12 +572,12 @@ function isNonspecificChildIfStatement(node: ts.Node, specificChildSet: Set