From 2e6cda94f17e409e2b50746a761aacc77ba84d7c Mon Sep 17 00:00:00 2001 From: fanjiaojiao0729 Date: Fri, 8 Dec 2023 17:14:39 +0800 Subject: [PATCH] =?UTF-8?q?new=20api=20check=20bug=20fix:function=20object?= =?UTF-8?q?=E7=89=B9=E6=AE=8A=E5=9C=BA=E6=99=AF=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: fanjiaojiao0729 --- .../coreImpl/checker/src/tag_value_check.ts | 30 ++++++++++++++++--- .../src/coreImpl/parser/NodeProcessor.ts | 4 ++- .../src/typedef/parser/ApiInfoDefination.ts | 20 ++++++++++++- .../dts_parser/src/utils/checkUtils.ts | 10 +++++++ 4 files changed, 58 insertions(+), 6 deletions(-) diff --git a/build-tools/dts_parser/src/coreImpl/checker/src/tag_value_check.ts b/build-tools/dts_parser/src/coreImpl/checker/src/tag_value_check.ts index 513c18f799..fdde4d2999 100644 --- a/build-tools/dts_parser/src/coreImpl/checker/src/tag_value_check.ts +++ b/build-tools/dts_parser/src/coreImpl/checker/src/tag_value_check.ts @@ -21,6 +21,7 @@ import { MethodInfo, PropertyInfo, ParamInfo } from '../../../typedef/parser/Api import { PunctuationMark } from '../../../utils/Constant'; import { SystemCapability } from '../config/syscapConfigFile.json'; import { module } from '../config/permissionConfigFile.json'; +import ts from 'typescript'; export class TagValueCheck { /** @@ -193,7 +194,14 @@ export class TagValueCheck { errorInfo: '', }; const returnsTagValue: string = tag.type; - const returnsApiValue: string[] = (singleApi as MethodInfo).getReturnValue(); + + let returnsApiValue: string[] = []; + const spacealCase: string[] = CommonFunctions.judgeSpecialCase((singleApi as MethodInfo).returnValueType); + if (spacealCase.length > 0) { + returnsApiValue = spacealCase; + } else { + returnsApiValue = (singleApi as MethodInfo).getReturnValue(); + } if (returnsApiValue.length === 0) { returnsValueCheckResult.state = false; returnsValueCheckResult.errorInfo = CommonFunctions.createErrorInfo(ErrorMessage.ERROR_USE, ['returns']); @@ -247,7 +255,14 @@ export class TagValueCheck { return typeValueCheckResult; } let typeTagValue: string = tag.type.replace(/\s/g, ''); - let typeApiValue: string[] = (singleApi as PropertyInfo).type; + let typeApiValue: string[] = []; + const spacealCase: string[] = CommonFunctions.judgeSpecialCase((singleApi as PropertyInfo).typeKind); + if (spacealCase.length > 0) { + typeApiValue = spacealCase; + } else { + typeApiValue = (singleApi as PropertyInfo).type; + } + let typeApiUnionValue: string = typeApiValue.join('|'); const isOptional: boolean = !(singleApi as PropertyInfo).getIsRequired(); if (isOptional && typeApiValue.length === 1) { @@ -255,7 +270,7 @@ export class TagValueCheck { } else if (isOptional && typeApiValue.length > 1) { typeApiUnionValue = '?(' + typeApiUnionValue + ')'; } - if (typeTagValue !== typeApiUnionValue) { + if (typeTagValue.replace(/\s/g, '') !== typeApiUnionValue.replace(/\s/g, '')) { typeValueCheckResult.state = false; typeValueCheckResult.errorInfo = ErrorMessage.ERROR_INFO_VALUE_TYPE; } @@ -393,7 +408,14 @@ export class TagValueCheck { const paramTagName: string = tag.name; const paramApiInfos: ParamInfo[] = (singleApi as MethodInfo).getParams(); const paramApiName: string = paramApiInfos[paramIndex]?.getApiName(); - const paramApiType: string[] = paramApiInfos[paramIndex]?.getType(); + let paramApiType: string[] = []; + const spacealCase: string[] = paramApiInfos[paramIndex] ? + CommonFunctions.judgeSpecialCase(paramApiInfos[paramIndex].paramType) : []; + if (spacealCase.length > 0) { + paramApiType = spacealCase; + } else { + paramApiType = paramApiInfos[paramIndex]?.getType(); + } if (paramTagName !== paramApiName) { paramValueCheckResult.state = false; diff --git a/build-tools/dts_parser/src/coreImpl/parser/NodeProcessor.ts b/build-tools/dts_parser/src/coreImpl/parser/NodeProcessor.ts index 48b1e5b347..4eff77e9f3 100644 --- a/build-tools/dts_parser/src/coreImpl/parser/NodeProcessor.ts +++ b/build-tools/dts_parser/src/coreImpl/parser/NodeProcessor.ts @@ -293,7 +293,7 @@ export class NodeProcessorHelper { //export * from 'test'; exportDeclareInfo.setApiName( StringConstant.EXPORT + - (exportDeclarationNode.moduleSpecifier ? exportDeclarationNode.moduleSpecifier.getText() : '') + (exportDeclarationNode.moduleSpecifier ? exportDeclarationNode.moduleSpecifier.getText() : '') ); } else if (ts.isNamespaceExport(exportClause)) { //export * as myTest from 'test'; @@ -524,6 +524,7 @@ export class NodeProcessorHelper { ModifierHelper.processModifiers(propertyNode.modifiers, propertyInfo); propertyInfo.setIsRequired(!propertyNode.questionToken ? true : false); propertyInfo.addType(NodeProcessorHelper.processDataType(propertyNode.type)); + propertyInfo.setTypeKind(propertyNode.type ? propertyNode.type.kind : -1); return propertyInfo; } @@ -569,6 +570,7 @@ export class NodeProcessorHelper { if (methodNode.type && ts.SyntaxKind.VoidKeyword !== methodNode.type.kind) { const returnValues: string[] = NodeProcessorHelper.processDataType(methodNode.type); methodInfo.setReturnValue(returnValues); + methodInfo.setReturnValueType(methodNode.type.kind); } for (let i = 0; i < methodNode.parameters.length; i++) { const param: ts.ParameterDeclaration = methodNode.parameters[i]; diff --git a/build-tools/dts_parser/src/typedef/parser/ApiInfoDefination.ts b/build-tools/dts_parser/src/typedef/parser/ApiInfoDefination.ts index 859fd3a757..d9b86911c4 100644 --- a/build-tools/dts_parser/src/typedef/parser/ApiInfoDefination.ts +++ b/build-tools/dts_parser/src/typedef/parser/ApiInfoDefination.ts @@ -213,7 +213,7 @@ export class BasicApiInfo { } } -export class ExportDefaultInfo extends BasicApiInfo {} +export class ExportDefaultInfo extends BasicApiInfo { } export class ReferenceInfo extends BasicApiInfo { pathName: string = ''; @@ -421,6 +421,7 @@ export class PropertyInfo extends ApiInfo { isReadOnly: boolean = false; // 属性是否为只读 isRequired: boolean = false; // 属性是否为必选 isStatic: boolean = false; // 属性是否为静态 + typeKind: ts.SyntaxKind = -1; //type类型的kind值 addType(type: string[]): void { this.type.push(...type); @@ -453,6 +454,14 @@ export class PropertyInfo extends ApiInfo { getIsStatic(): boolean { return this.isStatic; } + + setTypeKind(typeKind: ts.SyntaxKind): void { + this.typeKind = typeKind; + } + + getTypeKind(): ts.SyntaxKind { + return this.typeKind; + } } export class ConstantInfo extends ApiInfo { @@ -510,6 +519,7 @@ export class MethodInfo extends ApiInfo { returnValue: string[] = []; // 方法的返回值类型 isStatic: boolean = false; // 方法是否是静态 sync: string = ''; //同步函数标志 + returnValueType: ts.SyntaxKind = -1; setCallForm(callForm: string): void { this.callForm = callForm; @@ -535,6 +545,14 @@ export class MethodInfo extends ApiInfo { return this.returnValue; } + setReturnValueType(returnValueType: ts.SyntaxKind): void { + this.returnValueType = returnValueType; + } + + getReturnValueType(): ts.SyntaxKind { + return this.returnValueType; + } + setIsStatic(isStatic: boolean): void { this.isStatic = isStatic; } diff --git a/build-tools/dts_parser/src/utils/checkUtils.ts b/build-tools/dts_parser/src/utils/checkUtils.ts index f6d0e48c7f..29647a7aa6 100644 --- a/build-tools/dts_parser/src/utils/checkUtils.ts +++ b/build-tools/dts_parser/src/utils/checkUtils.ts @@ -204,6 +204,16 @@ export class CommonFunctions { } return checkApiVersion; } + + static judgeSpecialCase(type: ts.SyntaxKind): string[] { + let specialCaseType: string[] = []; + if (type === ts.SyntaxKind.TypeLiteral) { + specialCaseType = ['object']; + } else if (type === ts.SyntaxKind.FunctionType) { + specialCaseType = ['function']; + } + return specialCaseType; + } } /** -- Gitee