From 3927426b5799b72ed96a9b24bccb0465ab67aa08 Mon Sep 17 00:00:00 2001 From: houhaoyu Date: Fri, 22 Apr 2022 21:50:31 +0800 Subject: [PATCH] houhaoyu@huawei.com fix call extend attr in extend attr Signed-off-by: houhaoyu Change-Id: Ib615b0d0d3b4afae44798759f69eba68e9d146ce --- compiler/src/ets_checker.ts | 20 +++++++++++--------- compiler/src/process_import.ts | 5 +++-- compiler/src/validate_ui_syntax.ts | 17 ++++++++++++++--- 3 files changed, 28 insertions(+), 14 deletions(-) diff --git a/compiler/src/ets_checker.ts b/compiler/src/ets_checker.ts index 02166a2..8139850 100644 --- a/compiler/src/ets_checker.ts +++ b/compiler/src/ets_checker.ts @@ -20,7 +20,8 @@ import * as ts from 'typescript'; import { projectConfig } from '../main'; import { processSystemApi, - preprocessExtend + preprocessExtend, + preprocessNewExtend } from './validate_ui_syntax'; import { INNER_COMPONENT_MEMBER_DECORATORS, @@ -283,23 +284,23 @@ function loopNodeFindDoubleDollar(node: ts.Node, parentComponentName: string): v while (node) { if (ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression)) { const argument: ts.NodeArray = node.arguments; - const propertyName: ts.Identifier | ts.PrivateIdentifier= node.expression.name; + const propertyName: ts.Identifier | ts.PrivateIdentifier = node.expression.name; if (isCanAddDoubleDollar(propertyName.getText(), parentComponentName)) { - argument.forEach((item: ts.Node)=> { + argument.forEach((item: ts.Node) => { doubleDollarCollection(item); }); - } + } } else if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && node.arguments && node.arguments.length) { node.arguments.forEach((item: ts.Node) => { if (ts.isObjectLiteralExpression(item) && item.properties && item.properties.length) { - item.properties.forEach((param: ts.Node)=>{ + item.properties.forEach((param: ts.Node) => { if (isObjectPram(param, parentComponentName)) { doubleDollarCollection(param.initializer); } - }) + }); } - }) + }); } node = node.expression; } @@ -316,12 +317,12 @@ function doubleDollarCollection(item: ts.Node): void { function isObjectPram(param: ts.Node, parentComponentName:string): boolean { return ts.isPropertyAssignment(param) && param.name && ts.isIdentifier(param.name) && - param.initializer && PROPERTIES_ADD_DOUBLE_DOLLAR.has(parentComponentName) && + param.initializer && PROPERTIES_ADD_DOUBLE_DOLLAR.has(parentComponentName) && PROPERTIES_ADD_DOUBLE_DOLLAR.get(parentComponentName).has(param.name.getText()); } function isCanAddDoubleDollar(propertyName: string, parentComponentName: string): boolean { - return PROPERTIES_ADD_DOUBLE_DOLLAR.has(parentComponentName) && + return PROPERTIES_ADD_DOUBLE_DOLLAR.has(parentComponentName) && PROPERTIES_ADD_DOUBLE_DOLLAR.get(parentComponentName).has(propertyName) || propertyName === BIND_POPUP; } @@ -344,6 +345,7 @@ function processDraw(source: string): string { function processContent(source: string): string { source = processSystemApi(source); source = preprocessExtend(source, extendCollection); + source = preprocessNewExtend(source, extendCollection); source = processDraw(source); return source; } diff --git a/compiler/src/process_import.ts b/compiler/src/process_import.ts index 8d6c713..7a88429 100644 --- a/compiler/src/process_import.ts +++ b/compiler/src/process_import.ts @@ -33,6 +33,7 @@ import { linkCollection, componentCollection, preprocessExtend, + preprocessNewExtend, processSystemApi, propCollection, isObservedClass, @@ -86,11 +87,11 @@ export default function processImport(node: ts.ImportDeclaration | ts.ImportEqua fileResolvePath = getFileResolvePath(fileResolvePath, pagesDir, filePath, projectConfig.projectPath); } if (fs.existsSync(fileResolvePath) && fs.statSync(fileResolvePath).isFile()) { - const content: string = preprocessExtend(processSystemApi( + const content: string = preprocessNewExtend(preprocessExtend(processSystemApi( fs.readFileSync(fileResolvePath, { encoding: 'utf-8' }).replace( new RegExp('\\b' + STRUCT + '\\b.+\\{', 'g'), item => { return item.replace(new RegExp('\\b' + STRUCT + '\\b', 'g'), `${CLASS} `); - }))); + })))); const sourceFile: ts.SourceFile = ts.createSourceFile(filePath, content, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS); visitAllNode(sourceFile, defaultName, asName, path.dirname(fileResolvePath), log, new Set(), new Set()); diff --git a/compiler/src/validate_ui_syntax.ts b/compiler/src/validate_ui_syntax.ts index 355a915..19a134e 100644 --- a/compiler/src/validate_ui_syntax.ts +++ b/compiler/src/validate_ui_syntax.ts @@ -371,7 +371,7 @@ function hasChild(node: ts.ExpressionStatement): boolean { function getNextNode(node: ts.EtsComponentExpression): ts.Block { if (node.body && ts.isBlock(node.body)) { const statementsArray: ts.Block = node.body; - return statementsArray + return statementsArray; } } @@ -731,9 +731,9 @@ function collectionStates(node: ts.Decorator, decorator: string, name: string, case COMPONENT_LOCAL_STORAGE_PROP_DECORATOR: collectionlocalStorageParam(node, name, localStorageProp); break; - } } - +} + function collectionlocalStorageParam(node: ts.Decorator, name: string, localStorage: Map>): void { const localStorageParam: Set = new Set(); @@ -753,6 +753,7 @@ export function sourceReplace(source: string, sourcePath: string): ReplaceResult let content: string = source; const log: LogInfo[] = []; content = preprocessExtend(content); + content = preprocessNewExtend(content); // process @system. content = processSystemApi(content, false, sourcePath); @@ -774,6 +775,16 @@ export function preprocessExtend(content: string, extendCollection?: Set }); } +export function preprocessNewExtend(content: string, extendCollection?: Set): string { + const REG_EXTEND: RegExp = /@Extend\s*\([^\)]+\)\s*function\s+([^\(\s]+)\s*\(/gm; + return content.replace(REG_EXTEND, (item, item1) => { + if (extendCollection) { + extendCollection.add(item1); + } + return item; + }); +} + export function processSystemApi(content: string, isProcessAllowList: boolean = false, sourcePath: string = null, isSystemModule: boolean = false): string { let REG_SYSTEM: RegExp;