From e9ae25c76c66509cb57c38c813daf3e0bdd01e8d Mon Sep 17 00:00:00 2001 From: lihong Date: Fri, 24 Jun 2022 11:42:41 +0800 Subject: [PATCH] lihong67@huawei.com add checkHeritageClauses. Signed-off-by: lihong Change-Id: If266a92b03c5401ed05201b1108a1db510617496 --- compiler/src/pre_define.ts | 1 + compiler/src/process_component_class.ts | 27 ++++++++++++++++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/compiler/src/pre_define.ts b/compiler/src/pre_define.ts index 1760126..e45393e 100644 --- a/compiler/src/pre_define.ts +++ b/compiler/src/pre_define.ts @@ -96,6 +96,7 @@ export const COMPONENT_BUILD_FUNCTION: string = 'build'; export const COMPONENT_RENDER_FUNCTION: string = 'render'; export const COMPONENT_TRANSITION_FUNCTION: string = 'pageTransition'; export const COMPONENT_TRANSITION_NAME: string = 'PageTransition'; +export const CUSTOM_COMPONENT: string = 'CustomComponent'; export const COMPONENT_BUTTON: string = 'Button'; export const COMPONENT_FOREACH: string = 'ForEach'; diff --git a/compiler/src/process_component_class.ts b/compiler/src/process_component_class.ts index fcea374..48f6ca5 100644 --- a/compiler/src/process_component_class.ts +++ b/compiler/src/process_component_class.ts @@ -54,7 +54,8 @@ import { COMPONENT_CONSTRUCTOR_LOCALSTORAGE, COMPONENT_SET_AND_LINK, COMPONENT_SET_AND_PROP, - COMPONENT_CONSTRUCTOR_UNDEFINED + COMPONENT_CONSTRUCTOR_UNDEFINED, + CUSTOM_COMPONENT } from './pre_define'; import { BUILDIN_STYLE_NAMES, @@ -97,7 +98,7 @@ export function processComponentClass(node: ts.StructDeclaration, context: ts.Tr const memberNode: ts.ClassElement[] = processMembers(node.members, node.name, context, log, program, checkPreview(node)); return ts.factory.createClassDeclaration(undefined, node.modifiers, node.name, - node.typeParameters, updateHeritageClauses(), memberNode); + node.typeParameters, updateHeritageClauses(node, log), memberNode); } function checkPreview(node: ts.ClassDeclaration) { @@ -381,7 +382,15 @@ function getGeometryReaderFunctionBlock(node: ts.ArrowFunction | ts.FunctionExpr return processComponentBlock(blockNode, false, log); } -function updateHeritageClauses(): ts.NodeArray { +function updateHeritageClauses(node: ts.StructDeclaration, log: LogInfo[]) + : ts.NodeArray { + if (node.heritageClauses && !checkHeritageClauses(node)) { + log.push({ + type: LogType.ERROR, + message: 'The struct component is not allowed to extends other class or implements other interface.', + pos: node.heritageClauses.pos + }); + } const result:ts.HeritageClause[] = []; const heritageClause:ts.HeritageClause = ts.factory.createHeritageClause( ts.SyntaxKind.ExtendsKeyword, @@ -391,6 +400,18 @@ function updateHeritageClauses(): ts.NodeArray { return ts.factory.createNodeArray(result); } +function checkHeritageClauses(node: ts.StructDeclaration): boolean { + if (node.heritageClauses.length === 1 && node.heritageClauses[0].types && + node.heritageClauses[0].types.length === 1) { + const expressionNode: ts.ExpressionWithTypeArguments = node.heritageClauses[0].types[0]; + if (expressionNode.expression && ts.isIdentifier(expressionNode.expression) && + expressionNode.expression.escapedText.toString() === CUSTOM_COMPONENT) { + return true; + } + } + return false; +} + export function isProperty(node: ts.Node): Boolean { if (judgmentParentType(node)) { if (node.parent.parent.expression && ts.isIdentifier(node.parent.parent.expression) &&