From 8994175cd2f2b7d286aaed35075a1754a82df5af Mon Sep 17 00:00:00 2001 From: wangcaoyu Date: Wed, 10 Jul 2024 09:14:28 +0800 Subject: [PATCH] =?UTF-8?q?codeCheck=E9=97=AE=E9=A2=98=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wangcaoyu --- .../api_check_plugin/src/api_check_plugin.js | 2 +- build-tools/dts_parser/package.json | 2 +- build-tools/dts_parser/src/bin/config.ts | 63 +++++------ build-tools/dts_parser/src/bin/writer.ts | 4 +- .../coreImpl/checker/src/api_check_plugin.ts | 2 +- .../src/coreImpl/checker/src/check_chinese.ts | 6 +- .../coreImpl/checker/src/check_error_code.ts | 6 +- .../checker/src/event_method_check.ts | 6 +- .../coreImpl/checker/src/tag_inherit_check.ts | 2 +- .../checker/src/tag_legality_check.ts | 4 +- .../coreImpl/checker/src/tag_value_check.ts | 6 +- .../src/coreImpl/diff/DiffProcessor.ts | 104 ++++++++++++------ .../dts_parser/src/coreImpl/diff/diff.ts | 22 ++-- .../src/coreImpl/parser/NodeProcessor.ts | 18 +-- .../src/typedef/checker/result_type.ts | 4 +- .../src/typedef/diff/ApiInfoDiff.ts | 24 ++-- .../src/typedef/parser/ApiInfoDefination.ts | 16 +-- .../dts_parser/src/utils/FunctionUtils.ts | 4 +- .../dts_parser/src/utils/StringUtils.ts | 4 +- .../dts_parser/src/utils/checkUtils.ts | 3 +- 20 files changed, 165 insertions(+), 137 deletions(-) diff --git a/build-tools/api_check_plugin/src/api_check_plugin.js b/build-tools/api_check_plugin/src/api_check_plugin.js index 8f44a2875..3d22761b1 100644 --- a/build-tools/api_check_plugin/src/api_check_plugin.js +++ b/build-tools/api_check_plugin/src/api_check_plugin.js @@ -141,7 +141,7 @@ function reqGitApi(scanResult, prId, ApiCheckResult) { rules.administrators.forEach((administrator) => { administrators.add(administrator.user); }); - if (ApiCheckResult|| !prId || prId === 'NA') { + if (ApiCheckResult || !prId || prId === 'NA') { return scanResult; } const commentRequestPath = `https://gitee.com/api/v5/repos/openharmony/interface_sdk-js/pulls/${prId}/comments?page=1&per_page=100&direction=desc`; diff --git a/build-tools/dts_parser/package.json b/build-tools/dts_parser/package.json index 22e0aaa8a..3b8167b1a 100644 --- a/build-tools/dts_parser/package.json +++ b/build-tools/dts_parser/package.json @@ -8,7 +8,7 @@ "test": "echo \"Error: no test specified\" && exit 1", "testAll": "mocha --config test/mocha/.mocharc.jsonc", "pack": "cross-env NODE_ENV='production' webpack --mode=production", - "build": "npm run pack --bundle --stats-error-details", + "build": "npm run pack --bundle", "product": "ts-node src/main.ts -i test/debug/ut_nest_007.d.ts -o test/debug/output.json" }, "keywords": [], diff --git a/build-tools/dts_parser/src/bin/config.ts b/build-tools/dts_parser/src/bin/config.ts index c3327d596..4aa1157e0 100644 --- a/build-tools/dts_parser/src/bin/config.ts +++ b/build-tools/dts_parser/src/bin/config.ts @@ -37,7 +37,7 @@ import { ApiStatisticsHelper } from '../coreImpl/statistics/Statistics'; import { ApiStatisticsInfo, StatisticsInfoValueType } from '../typedef/statistics/ApiStatistics'; import { SyscapProcessorHelper } from '../coreImpl/diff/syscapFieldProcessor'; import { ApiCountInfo } from '../typedef/count/ApiCount'; -import { ApiCountHelper } from '../coreImpl/count/count' +import { ApiCountHelper } from '../coreImpl/count/count'; import { CommonFunctions } from '../utils/checkUtils'; import { FunctionUtils, KitData } from '../utils/FunctionUtils'; import { ApiType } from '../typedef/parser/ApiInfoDefination'; @@ -272,9 +272,9 @@ function collectApi(options: OptionObjType): ToolNameValueType { } else { allApis = Parser.parseFile(path.resolve(fileDir, '..'), fileDir); } - const statisticsInfosObject: StatisticsInfoValueType = ApiStatisticsHelper.getApiStatisticsInfos(allApis); - const fileContent: string = Parser.getParseResults(allApis); - let data: ApiStatisticsInfo[] | string [] = [fileContent]; + const statisticsInfosObject: StatisticsInfoValueType = ApiStatisticsHelper.getApiStatisticsInfos(allApis); + const fileContent: string = Parser.getParseResults(allApis); + let data: ApiStatisticsInfo[] | string[] = [fileContent]; if (options.format === 'excel') { const allApiStatisticsInfos: ApiStatisticsInfo[] | undefined = statisticsInfosObject.allApiStatisticsInfos; data = statisticsInfosObject.apiStatisticsInfos; @@ -374,7 +374,7 @@ function collectApiCallback(apiData: ApiStatisticsInfo[], workbook: ExcelJS.Work * @param apiData * @param workbook */ -function handleCollectData(apiData: ApiStatisticsInfo[], workbook: ExcelJS.Workbook){ +function handleCollectData(apiData: ApiStatisticsInfo[], workbook: ExcelJS.Workbook): void { const sheet: ExcelJS.Worksheet = workbook.addWorksheet(); const apiRelationsSet: Set = new Set(); const kitData: KitData = FunctionUtils.readKitFile(); @@ -449,7 +449,7 @@ function checkApi(): ToolNameValueType { if (fs.existsSync(filePathTxt)) { mdApiFiles = CommonFunctions.getMdFiles(filePathTxt); } - LocalEntry.checkEntryLocal(mdApiFiles, ['all'], './result.json', '', 'true'); + LocalEntry.checkEntryLocal(mdApiFiles, ['all'], './result.json', '', 'true'); return { data: [], }; @@ -579,9 +579,8 @@ function detectionApi(options: OptionObjType): ToolNameValueType { 'detection.json' )} -O ${path.resolve(options.output)}`; } else if (process.env.NODE_ENV === 'production') { - runningCommand = `${path.resolve(FileUtils.getBaseDirName(), './main.exe')} -N detection -L ${ - options.checkLabels - } -P ${path.resolve(path.dirname(options.output), 'detection.json')} -O ${path.resolve(options.output)}`; + runningCommand = `${path.resolve(FileUtils.getBaseDirName(), './main.exe')} -N detection -L ${options.checkLabels + } -P ${path.resolve(path.dirname(options.output), 'detection.json')} -O ${path.resolve(options.output)}`; } buffer = execSync(runningCommand, { timeout: 120000, @@ -648,7 +647,7 @@ function countApiCallback(data: ApiCountInfo[], workbook: ExcelJS.Workbook): voi countInfo.getsubSystem(), countInfo.getKitName(), countInfo.getFilePath(), - countInfo.getApiNumber() + countInfo.getApiNumber() ]; }); } @@ -688,7 +687,7 @@ function diffApiCallback( WriterHelper.MarkdownReporter.writeInMarkdown(data, dest); if (options?.all) { - addApiNumberSheet(relationsSet, workbook, data, kitData) + addApiNumberSheet(relationsSet, workbook, data, kitData); } @@ -702,7 +701,8 @@ function diffApiCallback( * @param data * @param kitData */ -function addApiNumberSheet(relationsSet: Set, workbook: ExcelJS.Workbook, data: BasicDiffInfo[], kitData: KitData){ +function addApiNumberSheet(relationsSet: Set, workbook: ExcelJS.Workbook, data: BasicDiffInfo[], + kitData: KitData): void { const numberSheet: ExcelJS.Worksheet = workbook.addWorksheet('api变更数量统计'); numberSheet.views = [{ xSplit: 2 }]; numberSheet.getRow(1).values = [ @@ -789,33 +789,22 @@ function handleData(data: BasicDiffInfo[], diffTypeNumberArr: DiffNumberInfo[]): * @param diffNumberInfo * @returns */ -function getCompatibleObject(diffNumberInfo: DiffNumberInfo) { +function getCompatibleObject(diffNumberInfo: DiffNumberInfo): string { const compatibleInfoSet: Set = new Set(diffNumberInfo.getAllCompatible()); let compatibleSign = 0; let incompatibleSign = 0; if (compatibleInfoSet.size === 2) { compatibleSign = 1; incompatibleSign = 1; - return `{ - "兼容性":${compatibleSign}, - "非兼容性":${incompatibleSign} - }`; - } - if (compatibleInfoSet.has(true)) { + } else if (compatibleInfoSet.has(true)) { compatibleSign = 1; - return `{ - "兼容性":${compatibleSign}, - "非兼容性":${incompatibleSign} - }`; - } - - if (compatibleInfoSet.has(false)) { + } else if (compatibleInfoSet.has(false)) { incompatibleSign = 1; - return `{ - "兼容性":${compatibleSign}, - "非兼容性":${incompatibleSign} - }`; } + return `{ + "兼容性":${compatibleSign}, + "非兼容性":${incompatibleSign} + }`; } /** @@ -824,14 +813,14 @@ function getCompatibleObject(diffNumberInfo: DiffNumberInfo) { * @param diffNumberInfo * @returns */ -function calculateChangeNumber(diffNumberInfo: DiffNumberInfo) { +function calculateChangeNumber(diffNumberInfo: DiffNumberInfo): string { const changeTypeSet: Set = new Set(diffNumberInfo.getAllChangeType()); - let newApiNumber: number = 0, - apiDeleteNumber = 0, - apiDeprecatedNumber = 0, - apiChangeNumber = 0, - apiConstrainedChange = 0, - apiPrototypeChange = 0; + let newApiNumber: number = 0; + let apiDeleteNumber: number = 0; + let apiDeprecatedNumber: number = 0; + let apiChangeNumber: number = 0; + let apiConstrainedChange: number = 0; + let apiPrototypeChange: number = 0; if (changeTypeSet.has('API修改(原型修改)')) { apiPrototypeChange++; } diff --git a/build-tools/dts_parser/src/bin/writer.ts b/build-tools/dts_parser/src/bin/writer.ts index 82f00378c..ea2b4144a 100644 --- a/build-tools/dts_parser/src/bin/writer.ts +++ b/build-tools/dts_parser/src/bin/writer.ts @@ -19,7 +19,7 @@ import fs from 'fs'; import { LogUtil } from '../utils/logUtil'; import { ToolNameExcelCallback, joinNewMessage, joinOldMessage, ToolReturnData } from './config'; import { BasicDiffInfo, diffTypeMap } from '../typedef/diff/ApiInfoDiff'; -import { OptionObjType } from './config' +import { OptionObjType } from './config'; import { SyscapProcessorHelper } from '../coreImpl/diff/syscapFieldProcessor'; export namespace WriterHelper { @@ -107,7 +107,7 @@ export namespace WriterHelper { } return data.getOldDtsName(); } - + /** * 使用文件名进行排序 * diff --git a/build-tools/dts_parser/src/coreImpl/checker/src/api_check_plugin.ts b/build-tools/dts_parser/src/coreImpl/checker/src/api_check_plugin.ts index 6f37cb799..df3d501f0 100644 --- a/build-tools/dts_parser/src/coreImpl/checker/src/api_check_plugin.ts +++ b/build-tools/dts_parser/src/coreImpl/checker/src/api_check_plugin.ts @@ -172,7 +172,7 @@ export class Check { const forbiddenWordsCheckResult: ErrorTagFormat = ForbiddenWordsCheck.forbiddenWordsCheck(singleApi as ClassInfo); const anonymousFunction: ErrorTagFormat = AnonymousFunctionCheck.checkAnonymousFunction(singleApi); - + if (!orderCheckResult.state) { const errorBaseInfo: ErrorBaseInfo = new ErrorBaseInfo(); errorBaseInfo diff --git a/build-tools/dts_parser/src/coreImpl/checker/src/check_chinese.ts b/build-tools/dts_parser/src/coreImpl/checker/src/check_chinese.ts index 08a02b428..10d87346d 100644 --- a/build-tools/dts_parser/src/coreImpl/checker/src/check_chinese.ts +++ b/build-tools/dts_parser/src/coreImpl/checker/src/check_chinese.ts @@ -13,9 +13,9 @@ * limitations under the License. */ -import {ErrorMessage, ErrorTagFormat,} from '../../../typedef/checker/result_type'; -import {CommonFunctions} from '../../../utils/checkUtils'; -import {Comment} from "../../../typedef/parser/Comment"; +import { ErrorMessage, ErrorTagFormat, } from '../../../typedef/checker/result_type'; +import { CommonFunctions } from '../../../utils/checkUtils'; +import { Comment } from "../../../typedef/parser/Comment"; export class ChineseCheck { diff --git a/build-tools/dts_parser/src/coreImpl/checker/src/check_error_code.ts b/build-tools/dts_parser/src/coreImpl/checker/src/check_error_code.ts index 44a40c072..7a2253233 100644 --- a/build-tools/dts_parser/src/coreImpl/checker/src/check_error_code.ts +++ b/build-tools/dts_parser/src/coreImpl/checker/src/check_error_code.ts @@ -14,9 +14,9 @@ */ -import {Comment} from "../../../typedef/parser/Comment"; -import {ErrorMessage, ErrorTagFormat} from "../../../typedef/checker/result_type"; -import {CommonFunctions} from "../../../utils/checkUtils"; +import { Comment } from "../../../typedef/parser/Comment"; +import { ErrorMessage, ErrorTagFormat } from "../../../typedef/checker/result_type"; +import { CommonFunctions } from "../../../utils/checkUtils"; export class CheckErrorCode { diff --git a/build-tools/dts_parser/src/coreImpl/checker/src/event_method_check.ts b/build-tools/dts_parser/src/coreImpl/checker/src/event_method_check.ts index 4f4b61a7e..35cc75302 100644 --- a/build-tools/dts_parser/src/coreImpl/checker/src/event_method_check.ts +++ b/build-tools/dts_parser/src/coreImpl/checker/src/event_method_check.ts @@ -52,8 +52,10 @@ export class EventMethodChecker { const onEventPublishVersionValue: string = onEvent.length > 0 ? onEvent[0].jsDocInfos[0].since : '-1'; const offEvent: MethodInfo[] = eventMethod.offEvents.length > 0 ? eventMethod.offEvents : []; const offEventPublishVersionValue: string = offEvent.length > 0 ? offEvent[0].jsDocInfos[0].since : '-1'; - const isLostOnEvent: boolean = eventMethod.onEvents.length === 0 && eventMethod.offEvents.length !== 0 && offEventPublishVersionValue === JSON.stringify(ApiCheckVersion); - const isLostOffEvent: boolean = eventMethod.onEvents.length !== 0 && eventMethod.offEvents.length === 0 && onEventPublishVersionValue === JSON.stringify(ApiCheckVersion); + const isLostOnEvent: boolean = eventMethod.onEvents.length === 0 && eventMethod.offEvents.length !== 0 && + offEventPublishVersionValue === JSON.stringify(ApiCheckVersion); + const isLostOffEvent: boolean = eventMethod.onEvents.length !== 0 && eventMethod.offEvents.length === 0 && + onEventPublishVersionValue === JSON.stringify(ApiCheckVersion); // check on&off event pair if (isLostOnEvent || isLostOffEvent) { diff --git a/build-tools/dts_parser/src/coreImpl/checker/src/tag_inherit_check.ts b/build-tools/dts_parser/src/coreImpl/checker/src/tag_inherit_check.ts index c70e210ef..2005f8442 100644 --- a/build-tools/dts_parser/src/coreImpl/checker/src/tag_inherit_check.ts +++ b/build-tools/dts_parser/src/coreImpl/checker/src/tag_inherit_check.ts @@ -55,7 +55,7 @@ export class TagInheritCheck { const trueCheckResult: ErrorTagFormat = { state: true, errorInfo: '', - } + }; if (parentApisJsdoc === undefined) { return true; } diff --git a/build-tools/dts_parser/src/coreImpl/checker/src/tag_legality_check.ts b/build-tools/dts_parser/src/coreImpl/checker/src/tag_legality_check.ts index 4dd3cfd49..57c1ab5cb 100644 --- a/build-tools/dts_parser/src/coreImpl/checker/src/tag_legality_check.ts +++ b/build-tools/dts_parser/src/coreImpl/checker/src/tag_legality_check.ts @@ -79,7 +79,7 @@ export class LegalityCheck { return apiLegalityCheckResult; } const tagsTag: string[] = []; - apiTags.forEach((apiTag: Comment.CommentTag) => { tagsTag.push(apiTag.tag) }); + apiTags.forEach((apiTag: Comment.CommentTag) => { tagsTag.push(apiTag.tag); }); if (tagsTag.includes('deprecated')) { return apiLegalityCheckResult; } @@ -223,7 +223,7 @@ export class LegalityCheck { // check repeat throws const orderedThrowsCode: string[] = apiThrowsCode.sort(); for (var i = 0; i < orderedThrowsCode.length; i++) { - if (orderedThrowsCode[i] == orderedThrowsCode[i + 1]) { + if (orderedThrowsCode[i] === orderedThrowsCode[i + 1]) { apiRepeatThrows.state = false; apiRepeatThrows.errorInfo = CommonFunctions.createErrorInfo(ErrorMessage.ERROR_REPEATLABEL, ['throws']); } 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 3070869e5..33d926acc 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 @@ -38,7 +38,7 @@ export class TagValueCheck { return tagValueError; } const tagsTag: string[] = []; - tagsName.forEach((tagName: Comment.CommentTag) => { tagsTag.push(tagName.tag) }); + tagsName.forEach((tagName: Comment.CommentTag) => { tagsTag.push(tagName.tag); }); const isDeprecated: boolean = tagsTag.includes('deprecated'); tagsName.forEach((tag) => { let errorTagInfo: ErrorTagFormat = { @@ -182,7 +182,7 @@ export class TagValueCheck { let returnsApiValue: string[] = []; const legalApiArr: string[] = [ApiType.METHOD, ApiType.TYPE_ALIAS]; if (!legalApiArr.includes(singleApi.getApiType())) { - return returnsValueCheckResult + return returnsValueCheckResult; } const spacealCase: string[] = CommonFunctions.judgeSpecialCase((singleApi as MethodInfo).returnValueType); if (singleApi.getApiType() === ApiType.TYPE_ALIAS) { @@ -230,7 +230,7 @@ export class TagValueCheck { const genericArr: GenericInfo[] = singleApi.getGenericInfo(); if (genericArr.length > 0) { let genericInfo = genericArr.map((generic) => { - return generic.getGenericContent() + return generic.getGenericContent(); }).join(','); apiValue = apiValue + '<' + genericInfo + '>'; } diff --git a/build-tools/dts_parser/src/coreImpl/diff/DiffProcessor.ts b/build-tools/dts_parser/src/coreImpl/diff/DiffProcessor.ts index 7216ff367..cdb0b34a5 100644 --- a/build-tools/dts_parser/src/coreImpl/diff/DiffProcessor.ts +++ b/build-tools/dts_parser/src/coreImpl/diff/DiffProcessor.ts @@ -385,9 +385,9 @@ export namespace DiffProcessorHelper { if (deprecatedVersionOfNew === '-1') { return diffTypeInfo.setDiffType(ApiDiffType.DEPRECATED_HAVE_TO_NA); } - + } - + if (deprecatedVersionOfNew === '-1') { return diffTypeInfo.setDiffType(ApiDiffType.DEPRECATED_HAVE_TO_NA); } @@ -672,7 +672,9 @@ export namespace DiffProcessorHelper { const newReturnType: string[] = newApiInfo.getReturnType()?.split('|'); const olaMethodTypeStr = oldReturnType.toString().replace(/\r|\n|\s+|'|"/g, ''); const newMethodTypeStr = newReturnType.toString().replace(/\r|\n|\s+|'|"/g, ''); - if (olaMethodTypeStr === newMethodTypeStr) return; + if (olaMethodTypeStr === newMethodTypeStr) { + return undefined; + } diffTypeInfo.setOldMessage(olaMethodTypeStr).setNewMessage(newMethodTypeStr); if (checkParentContainChild(newReturnType, oldReturnType)) { return diffTypeInfo.setDiffType(ApiDiffType.TYPE_ALIAS_FUNCTION_RETURN_TYPE_ADD); @@ -786,15 +788,17 @@ export namespace DiffProcessorHelper { newMethodParams: ParamInfo[], diffTypeInfos: DiffTypeInfo[], diffMethodType: DiffMethodType - ) { + ): void { const oldParamLen: number = oldMethodParams.length; const newParamLen: number = newMethodParams.length; // 1.新旧版本参数一个不存在即不符合,直接返回 - if (!oldParamLen || !newParamLen) return; + if (!oldParamLen || !newParamLen) { + return; + } // 2. 判断新旧版本参数名称相同的参数的个数和新旧版本的参数是否相同,相同即为新增或者减少参数 // 2.1 循环得到所有的参数名称 - const oldParamNames: string[] = [], - newParamNames: string[] = []; + const oldParamNames: string[] = []; + const newParamNames: string[] = []; for (let i: number = 0; i < Math.max(oldParamLen, newParamLen); i++) { const newCur: ParamInfo = newMethodParams[i]; const oldCur: ParamInfo = oldMethodParams[i]; @@ -808,7 +812,9 @@ export namespace DiffProcessorHelper { // 2.3 得到参数相同的个数 const sameParamsLength: number = oldParamLen - oldDiffParams.length; // 2.4 判断新旧版本参数名称相同的参数的个数和新旧版本的参数是否相同,相同即为新增或者减少参数 - if (sameParamsLength === oldParamLen || sameParamsLength === newParamLen) return; + if (sameParamsLength === oldParamLen || sameParamsLength === newParamLen) { + return; + } let oldDiffInfos: ParamInfo[] = oldMethodParams; let newDiffInfos: ParamInfo[] = newMethodParams; @@ -841,17 +847,23 @@ export namespace DiffProcessorHelper { newMethodParams: ParamInfo[], diffTypeInfos: DiffTypeInfo[], diffMethodType: DiffMethodType - ) { + ): void { const oldParamLen: number = oldMethodParams.length; const newParamLen: number = newMethodParams.length; // 1.如果旧版本的参数长度不大于1,或者两者长度不一致,直接返回 - if (oldParamLen <= 1 || oldParamLen !== newParamLen) return; + if (oldParamLen <= 1 || oldParamLen !== newParamLen) { + return; + } // 2.判断两个版本的相同位置的参数名称是否一致,相同直接返回 const isSamePosition: boolean = checkIsSameOfSamePosition(newMethodParams, oldMethodParams); - if (isSamePosition) return; + if (isSamePosition) { + return; + } // 3.如果旧版本的参数不完全包含新版本的参数或者两个版本的参数是否完全一致,一个不符合直接返回 const isContain: boolean = checkIsContain(oldMethodParams, newMethodParams); - if (!isContain) return; + if (!isContain) { + return; + } // 4.上述情况都不符合,处理返回信息 const oldNamesStr: string = stitchMethodParameters(oldMethodParams); const newNamesStr: string = stitchMethodParameters(newMethodParams); @@ -872,14 +884,18 @@ export namespace DiffProcessorHelper { newMethodParams: ParamInfo[], diffTypeInfos: DiffTypeInfo[], diffMethodType: DiffMethodType - ) { + ): void { const oldParamLen: number = oldMethodParams.length; const newParamLen: number = newMethodParams.length; // 1.如果新版本参数为空或者旧版本参数长度大于或者等于新版本参数长度,直接返回 - if (newParamLen === 0 || oldParamLen >= newParamLen) return; + if (newParamLen === 0 || oldParamLen >= newParamLen) { + return; + } // 2.新版本参数需要完全包含旧版本,如果不包含,直接返回 const isContain: boolean = checkIsContain(newMethodParams, oldMethodParams); - if (!isContain) return; + if (!isContain) { + return; + } // 3.是否存在新增的可选参数 const oldParamNames: string[] = oldMethodParams.map((oldParam: ParamInfo) => oldParam.getApiName()); const addParams: ParamInfo[] = newMethodParams.filter((newParam: ParamInfo) => { @@ -887,7 +903,9 @@ export namespace DiffProcessorHelper { return !oldParamNames.includes(curParamName) && !newParam.getIsRequired(); }); // 4.新版本新增的参数是否存在参数是可选类型,不存在直接返回 - if (!addParams.length) return; + if (!addParams.length) { + return; + } // 5.存在新增的参数是可选参数,处理返回信息 const addParamNamesStr: string = stitchMethodParameters(addParams); const diffTypeInfo: DiffTypeInfo = new DiffTypeInfo(); @@ -907,14 +925,18 @@ export namespace DiffProcessorHelper { newMethodParams: ParamInfo[], diffTypeInfos: DiffTypeInfo[], diffMethodType: DiffMethodType - ) { + ): void { const oldParamLen: number = oldMethodParams.length; const newParamLen: number = newMethodParams.length; // 1.如果新版本参数为空或者旧版本参数长度大于或者等于新版本参数长度,直接返回 - if (newParamLen === 0 || oldParamLen >= newParamLen) return; + if (newParamLen === 0 || oldParamLen >= newParamLen) { + return; + } // 2.新版本参数需要完全包含旧版本,如果不包含,直接返回 const isContain: boolean = checkIsContain(newMethodParams, oldMethodParams); - if (!isContain) return; + if (!isContain) { + return; + } // 3.是否存在新增的必选参数 const oldParamNames: string[] = oldMethodParams.map((oldParam: ParamInfo) => oldParam.getApiName()); const addParams: ParamInfo[] = newMethodParams.filter((newParam: ParamInfo) => { @@ -922,7 +944,9 @@ export namespace DiffProcessorHelper { return !oldParamNames.includes(curParamName) && newParam.getIsRequired(); }); // 4.新版本新增的参数是否存在参数是必选类型,不存在直接返回 - if (!addParams.length) return; + if (!addParams.length) { + return; + } // 5.存在新增的参数是可选参数,处理返回信息 const addParamNamesStr: string = stitchMethodParameters(addParams); const diffTypeInfo: DiffTypeInfo = new DiffTypeInfo(); @@ -942,14 +966,18 @@ export namespace DiffProcessorHelper { newMethodParams: ParamInfo[], diffTypeInfos: DiffTypeInfo[], diffMethodType: DiffMethodType - ) { + ): void { const oldParamLen: number = oldMethodParams.length; const newParamLen: number = newMethodParams.length; // 1.旧版本参数为空或者新版本参数长度大于或者等于旧版本参数长度,直接返回 - if (oldParamLen === 0 || newParamLen >= oldParamLen) return; + if (oldParamLen === 0 || newParamLen >= oldParamLen) { + return; + } // 2.如果旧版本的参数不包含新版本的参数,直接返回 const isContain: boolean = checkIsContain(oldMethodParams, newMethodParams); - if (newParamLen > 0 && !isContain) return; + if (newParamLen > 0 && !isContain) { + return; + } // 3.参数减少,处理返回信息 const newParamNames: string[] = newMethodParams.map((newParam: ParamInfo) => newParam.getApiName()); const reduceParams: ParamInfo[] = oldMethodParams.filter( @@ -972,17 +1000,21 @@ export namespace DiffProcessorHelper { newMethodParams: ParamInfo[], diffTypeInfos: DiffTypeInfo[], diffMethodType: DiffMethodType - ) { + ): void { // 1.新旧版本的参数长度应大于0 const oldParamLen: number = oldMethodParams.length; const newParamLen: number = newMethodParams.length; - if (!oldParamLen || !newParamLen) return; + if (!oldParamLen || !newParamLen) { + return; + } // 2.找到参数名称一致和参数类型一致的参数进行比较,不存在直接返回 const sameParamInfos: ParamInfo[] = oldMethodParams.filter((oldParam: ParamInfo) => { const oldParamName: string = oldParam.getApiName(); return newMethodParams.find((newParam: ParamInfo) => newParam.getApiName() === oldParamName); }); - if (!sameParamInfos.length) return; + if (!sameParamInfos.length) { + return; + } // 3.比较参数名和类型一致是否发生了可选/必选的变化,参数类型不需要计较 sameParamInfos.forEach((sameInfo: ParamInfo, idx: number) => { const curOldParamName: string = sameInfo.getApiName(); @@ -1015,17 +1047,21 @@ export namespace DiffProcessorHelper { newMethodParams: ParamInfo[], diffTypeInfos: DiffTypeInfo[], diffMethodType: DiffMethodType & DiffTypeChangeType - ) { + ): void { //1.判断新旧版本参数长度大于0 const oldParamLen: number = oldMethodParams.length; const newParamLen: number = newMethodParams.length; - if (!oldParamLen || !newParamLen) return; + if (!oldParamLen || !newParamLen) { + return; + } const newParamName: string[] = newMethodParams.map((newParam: ParamInfo) => newParam.getApiName()); // 2.需要新旧版本存在参数名称一致的,不存在直接返回 const sameParamInfo: ParamInfo[] = oldMethodParams.filter((oldParam: ParamInfo) => newParamName.includes(oldParam.getApiName()) ); - if (!sameParamInfo.length) return; + if (!sameParamInfo.length) { + return; + } // 3.寻找参数名称相同的情况下的参数类型变化的 sameParamInfo.forEach((curSame: ParamInfo, idx: number) => { const oldParamTypes: string[] = curSame.getType(); @@ -1353,9 +1389,13 @@ export namespace DiffProcessorHelper { const olaTypeAliasTypeStr: string = olaTypeAliasType.toString(); const newTypeAliasTypeStr: string = newTypeAliasType.toString(); // 1.两者定义相同,没有变化 - if (olaTypeAliasTypeStr === newTypeAliasTypeStr) return; + if (olaTypeAliasTypeStr === newTypeAliasTypeStr) { + return undefined; + } // 自定义函数类型 - if (oldApiInfo.getTypeIsFunction()) return; + if (oldApiInfo.getTypeIsFunction()) { + return undefined; + } // 2.两者定义不同 const diffMethodType: DiffTypeChangeType = { PARAM_TYPE_CHANGE: ApiDiffType.TYPE_ALIAS_CHANGE, @@ -1468,7 +1508,7 @@ export namespace DiffProcessorHelper { if (StringUtils.hasSubstring(oldParamTypeStr, newParamTypeStr)) { return diffTypes.PARAM_TYPE_REDUCE; } - return diffTypes.PARAM_TYPE_CHANGE + return diffTypes.PARAM_TYPE_CHANGE; } } diff --git a/build-tools/dts_parser/src/coreImpl/diff/diff.ts b/build-tools/dts_parser/src/coreImpl/diff/diff.ts index 9d3f71e68..1a911e811 100644 --- a/build-tools/dts_parser/src/coreImpl/diff/diff.ts +++ b/build-tools/dts_parser/src/coreImpl/diff/diff.ts @@ -186,7 +186,7 @@ export class DiffHelper { } else { DiffHelper.diffSameNumberFunction(oldApiInfos, newApiInfos, diffInfos, isCheck); } - + } /** @@ -203,8 +203,8 @@ export class DiffHelper { isAllSheet: boolean, isCheck?: boolean ): void { - const isAllDeprecated: boolean = DiffHelper.judgeIsAllDeprecated(newApiInfos); - if (oldApiInfos.length === newApiInfos.length) { + const isAllDeprecated: boolean = DiffHelper.judgeIsAllDeprecated(newApiInfos); + if (oldApiInfos.length === newApiInfos.length) { const apiNumber: number = oldApiInfos.length; for (let i = 0; i < apiNumber; i++) { DiffProcessorHelper.JsDocDiffHelper.diffJsDocInfo(oldApiInfos[i], newApiInfos[i], diffInfos, isAllDeprecated, isAllSheet); @@ -280,7 +280,7 @@ export class DiffHelper { static judgeIsAllDeprecated(apiInfos: ApiInfo[]): boolean { let isAllDeprecated: boolean = true; - apiInfos.forEach((apiInfo: ApiInfo)=>{ + apiInfos.forEach((apiInfo: ApiInfo) => { const deprecatedVersion = apiInfo.getLastJsDocInfo()?.getDeprecatedVersion(); if (deprecatedVersion === '-1' || !deprecatedVersion) { isAllDeprecated = false; @@ -289,9 +289,9 @@ export class DiffHelper { return isAllDeprecated; } - static handleDeprecatedVersion(apiInfos: ApiInfo[]) { + static handleDeprecatedVersion(apiInfos: ApiInfo[]): void { let isAllDeprecated: boolean = true; - apiInfos.forEach((apiInfo: ApiInfo)=>{ + apiInfos.forEach((apiInfo: ApiInfo) => { const deprecatedVersion = apiInfo.getLastJsDocInfo()?.getDeprecatedVersion(); if (deprecatedVersion === '-1' || !deprecatedVersion) { isAllDeprecated = false; @@ -300,9 +300,9 @@ export class DiffHelper { if (isAllDeprecated) { return; } - apiInfos.forEach((apiInfo: ApiInfo)=>{ + apiInfos.forEach((apiInfo: ApiInfo) => { apiInfo.getLastJsDocInfo()?.setDeprecatedVersion('-1'); - }) + }); } @@ -403,15 +403,15 @@ export class DiffHelper { const jsdocTextArr: string[] = apiInfo.getJsDocText().split('*/'); const clonedJsdocTextArr: string[] = jsdocTextArr; if (clonedJsdocTextArr[1] && StringUtils.hasSubstring(clonedJsdocTextArr[1], CommentHelper.fileJsDoc)) { - jsdocTextArr.splice(1,1); + jsdocTextArr.splice(1, 1); } if (clonedJsdocTextArr[0] && StringUtils.hasSubstring(clonedJsdocTextArr[0], CommentHelper.fileJsDoc)) { - jsdocTextArr.splice(0,1); + jsdocTextArr.splice(0, 1); } if (clonedJsdocTextArr[0] && StringUtils.hasSubstring(clonedJsdocTextArr[0], CommentHelper.licenseKeyword)) { - jsdocTextArr.splice(0,1); + jsdocTextArr.splice(0, 1); } return jsdocTextArr.join('*/'); diff --git a/build-tools/dts_parser/src/coreImpl/parser/NodeProcessor.ts b/build-tools/dts_parser/src/coreImpl/parser/NodeProcessor.ts index 12413aadf..802a9c661 100644 --- a/build-tools/dts_parser/src/coreImpl/parser/NodeProcessor.ts +++ b/build-tools/dts_parser/src/coreImpl/parser/NodeProcessor.ts @@ -283,10 +283,10 @@ export class NodeProcessorHelper { if (ts.isStructDeclaration(node)) { return ts.visitNodes(node.members, (node) => { if (ts.isConstructorDeclaration(node)) { - return + return undefined; } - return node - }) + return node; + }); } if (ts.isTypeAliasDeclaration(node) && ts.isTypeLiteralNode(node.type)) { return node.type.members; @@ -738,11 +738,11 @@ export class NodeProcessorHelper { const fileSymbolMap: Map | undefined = NodeProcessorHelper.symbolOfTypeReferenceMap.get(filePath); if (!fileSymbolMap) { - return; + return undefined; } - const typeSymbol: ts.Symbol | undefined = fileSymbolMap.get(tsNode.getFullText().trim()) + const typeSymbol: ts.Symbol | undefined = fileSymbolMap.get(tsNode.getFullText().trim()); if (!typeSymbol) { - return; + return undefined; } return typeSymbol; } @@ -776,7 +776,7 @@ export class NodeProcessorHelper { if (parameter.type) { NodeProcessorHelper.processFunctionTypeNode(parameter.type, methodInfo, paramInfo, isParam); } - }) + }); NodeProcessorHelper.processFunctionTypeNode(typeNode.type, methodInfo, paramInfo, isParam); } if (!ts.isTypeReferenceNode(typeNode)) { @@ -804,7 +804,7 @@ export class NodeProcessorHelper { const typeArguments: ts.NodeArray | undefined = typeNode.typeArguments; typeArguments?.forEach((typeArgument: ts.TypeNode) => { NodeProcessorHelper.processFunctionTypeNode(typeArgument, methodInfo, paramInfo, isParam); - }) + }); try { const tsProgram: ts.Program = parserParam.getTsProgram(); const filePath: string = parserParam.getFilePath(); @@ -816,7 +816,7 @@ export class NodeProcessorHelper { } NodeProcessorHelper.setSymbolOfTypeReferenceMap(filePath, tsNode, symbol); } - }) + }); tsProgram.emit(); const currentTypeSymbol: ts.Symbol | undefined = NodeProcessorHelper.getSymbolOfTypeReferenceMap(filePath, typeNode); diff --git a/build-tools/dts_parser/src/typedef/checker/result_type.ts b/build-tools/dts_parser/src/typedef/checker/result_type.ts index e09023f70..5e3dc974a 100644 --- a/build-tools/dts_parser/src/typedef/checker/result_type.ts +++ b/build-tools/dts_parser/src/typedef/checker/result_type.ts @@ -143,10 +143,10 @@ export enum ErrorMessage { ERROR_SMALL_HUMP_NAME = 'This name [$$] should be named by small hump.', ERROR_SMALL_HUMP_NAME_FILE = 'This API file should be named by small hump.', ERROR_LARGE_HUMP_NAME_FILE = 'This API file should be named by large hump.', - ERROR_ANONYMOUS_FUNCTION ='Anonymous functions or anonymous object that are not allowed are used in this api.', + ERROR_ANONYMOUS_FUNCTION = 'Anonymous functions or anonymous object that are not allowed are used in this api.', ERROR_NO_JSDOC = 'Jsdoc needs to be added to the current API.', ERROR_NO_JSDOC_TAG = 'add tags to the Jsdoc.', - ERROR_HAS_CHINESE= 'Jsdoc has chinese.', + ERROR_HAS_CHINESE = 'Jsdoc has chinese.', ERROR_ERROR_CODE = 'The generic error code does not contain the current error code.', ERROR_CHANGES_JSDOC_LEVEL = 'Forbid changes: Cannot change from public API to system API.', ERROR_CHANGES_JSDOC_PERMISSION_RANGE = 'Forbid changes: Cannot reduce or permission or increase and permission.', diff --git a/build-tools/dts_parser/src/typedef/diff/ApiInfoDiff.ts b/build-tools/dts_parser/src/typedef/diff/ApiInfoDiff.ts index 54b63279c..7e8c9dcc6 100644 --- a/build-tools/dts_parser/src/typedef/diff/ApiInfoDiff.ts +++ b/build-tools/dts_parser/src/typedef/diff/ApiInfoDiff.ts @@ -14,7 +14,7 @@ */ import ts from 'typescript'; -import { ApiInfo, MethodInfo, ApiType } from '../parser/ApiInfoDefination'; +import { ApiInfo, MethodInfo, ApiType } from '../parser/ApiInfoDefination'; import { Comment } from '../parser/Comment'; import { NumberConstant } from '../../utils/Constant'; export class BasicDiffInfo { @@ -60,7 +60,7 @@ export class BasicDiffInfo { newSyscapField: string = ''; //kit信息 oldKitInfo: string = ''; - newKitInfo:string = ''; + newKitInfo: string = ''; //是否为系统API isSystemapi: boolean = false; @@ -370,7 +370,6 @@ export class DiffNumberInfo { isApi: boolean = true; apiRelation: string = ''; isSystemapi: boolean = false; - setApiName(apiName: string): DiffNumberInfo { this.apiName = apiName; @@ -414,7 +413,7 @@ export class DiffNumberInfo { return this.apiType; } - setAllDiffType(diffType: string): DiffNumberInfo{ + setAllDiffType(diffType: string): DiffNumberInfo { this.allDiffType.push(diffType); return this; } @@ -447,9 +446,9 @@ export class DiffNumberInfo { return this.newDiffMessage; } - setAllChangeType(changeType: string | undefined): DiffNumberInfo{ + setAllChangeType(changeType: string | undefined): DiffNumberInfo { if (!changeType) { - return this + return this; } this.allChangeType.push(changeType); return this; @@ -459,7 +458,7 @@ export class DiffNumberInfo { return this.allChangeType; } - setAllCompatible(isCompatible: boolean): DiffNumberInfo{ + setAllCompatible(isCompatible: boolean): DiffNumberInfo { this.allCompatible.push(isCompatible); return this; } @@ -476,7 +475,7 @@ export class DiffNumberInfo { return this.diffTypeNumber; } - setIsApi(isApi: boolean) { + setIsApi(isApi: boolean): DiffNumberInfo { this.isApi = isApi; return this; } @@ -485,12 +484,12 @@ export class DiffNumberInfo { return this.isApi; } - setApiRelation(apiRelation: string) { + setApiRelation(apiRelation: string): DiffNumberInfo { this.apiRelation = apiRelation; return this; } - getApiRelation(): string{ + getApiRelation(): string { return this.apiRelation; } @@ -505,7 +504,8 @@ export class DiffNumberInfo { } export interface JsDocDiffProcessor { - (oldJsDocInfo: Comment.JsDocInfo | undefined, newJsDocInfo: Comment.JsDocInfo | undefined, isAllDeprecated?: boolean, isAllSheet?: boolean): DiffTypeInfo | undefined; + (oldJsDocInfo: Comment.JsDocInfo | undefined, newJsDocInfo: Comment.JsDocInfo | undefined, isAllDeprecated?: boolean, + isAllSheet?: boolean): DiffTypeInfo | undefined; } export interface ApiSceneDiffProcessor { @@ -962,4 +962,4 @@ export const isNotApiSet: Set = new Set([ ApiType.NAMESPACE, ApiType.ENUM, ApiType.SOURCE_FILE, -]) +]); diff --git a/build-tools/dts_parser/src/typedef/parser/ApiInfoDefination.ts b/build-tools/dts_parser/src/typedef/parser/ApiInfoDefination.ts index f58486a89..78c58208e 100644 --- a/build-tools/dts_parser/src/typedef/parser/ApiInfoDefination.ts +++ b/build-tools/dts_parser/src/typedef/parser/ApiInfoDefination.ts @@ -72,10 +72,10 @@ export class BasicApiInfo { constructor(apiType: string = '', node: ts.Node, parentApi: BasicApiInfo | undefined) { this.node = node; this.setParentApi(parentApi); - this.setParentApiType(parentApi?.getApiType()) + this.setParentApiType(parentApi?.getApiType()); if (parentApi) { this.setFilePath(parentApi.getFilePath()); - this.setFileAbsolutePath(parentApi.getFileAbsolutePath()) + this.setFileAbsolutePath(parentApi.getFileAbsolutePath()); this.setIsStruct(parentApi.getIsStruct()); } this.setApiType(apiType); @@ -147,7 +147,7 @@ export class BasicApiInfo { getParentApi(): BasicApiInfo | undefined { return this.parentApi; } - + setParentApiType(parentApiType: string | undefined): void { this.parentApiType = parentApiType; } @@ -490,7 +490,7 @@ export class PropertyInfo extends ApiInfo { typeKind: ts.SyntaxKind = ts.SyntaxKind.Unknown; //type类型的kind值 typeLocations: TypeLocationInfo[] = []; // 参数、返回值的JsDoc信息 objLocations: TypeLocationInfo[] = []; // 匿名类型的JsDoc信息 - + constructor(apiType: string = '', node: ts.Node, parentApi: BasicApiInfo | undefined) { super(apiType, node, parentApi); let propertyNode: PropertyNode = node as PropertyNode; @@ -942,8 +942,8 @@ export class ParserParam { const value: ts.ResolvedModule = { resolvedFileName: '', isExternalLibraryImport: false - } - const alias: { [key: string]: string } = { + }; + const alias: { [key: string]: string; } = { "^(@ohos\\.inner\\.)(.*)$": "../../../base/ets/api/", "^(@ohos\\.)(.*)$": "../../../base/ets/api/", }; @@ -968,7 +968,7 @@ export class ParserParam { break; } } - const resolvedFileName: string | undefined = ts.resolveModuleName(moduleName, containingFile, compilerOptions, compilerHost).resolvedModule?.resolvedFileName + const resolvedFileName: string | undefined = ts.resolveModuleName(moduleName, containingFile, compilerOptions, compilerHost).resolvedModule?.resolvedFileName; if (resolvedFileName) { value.resolvedFileName = resolvedFileName; value.isExternalLibraryImport = true; @@ -986,7 +986,7 @@ export class ParserParam { } } -export type ExportImportValue = { key: string; value: string }; +export type ExportImportValue = { key: string; value: string; }; export interface NodeProcessorInterface { (node: ts.Node, parentApiInfo: BasicApiInfo): BasicApiInfo; } diff --git a/build-tools/dts_parser/src/utils/FunctionUtils.ts b/build-tools/dts_parser/src/utils/FunctionUtils.ts index d279238e6..6939d88df 100644 --- a/build-tools/dts_parser/src/utils/FunctionUtils.ts +++ b/build-tools/dts_parser/src/utils/FunctionUtils.ts @@ -27,8 +27,8 @@ export class FunctionUtils { */ static getPackageName(fileFilePath: string): string { const packageName = - fileFilePath.indexOf('component\\ets\\') >= 0 || fileFilePath.indexOf('component/ets/') >= 0 ? - 'ArkUI' : + fileFilePath.indexOf('component\\ets\\') >= 0 || fileFilePath.indexOf('component/ets/') >= 0 ? + 'ArkUI' : path.basename(fileFilePath).replace(/@|.d.ts$/g, ''); return packageName; } diff --git a/build-tools/dts_parser/src/utils/StringUtils.ts b/build-tools/dts_parser/src/utils/StringUtils.ts index 383c39944..72c43340d 100644 --- a/build-tools/dts_parser/src/utils/StringUtils.ts +++ b/build-tools/dts_parser/src/utils/StringUtils.ts @@ -20,10 +20,8 @@ export class StringUtils { try { flag = str.search(sub) !== -1; } catch (error) { - console.log('str=',str) - console.log('sub=',sub) LogUtil.e('StringUtils.hasSubstring', error); - } + } return flag; } diff --git a/build-tools/dts_parser/src/utils/checkUtils.ts b/build-tools/dts_parser/src/utils/checkUtils.ts index 93b397af7..a340ba090 100644 --- a/build-tools/dts_parser/src/utils/checkUtils.ts +++ b/build-tools/dts_parser/src/utils/checkUtils.ts @@ -102,7 +102,6 @@ export class GenerateFile { ); } - /** * 将错误信息输出为excel文件 * @param { ApiResultInfo[] } apiCheckArr @@ -262,7 +261,7 @@ export class CommonFunctions { static getErrorInfo(singleApi: BasicApiInfo | undefined, apiJsdoc: Comment.JsDocInfo | undefined, filePath: string, errorBaseInfo: ErrorBaseInfo): ApiCheckInfo { let apiInfo: ApiCheckInfo = new ApiCheckInfo(); if (singleApi === undefined) { - return apiInfo + return apiInfo; } const sinceVersion: number = apiJsdoc === undefined ? -1 : toNumber(apiJsdoc.since); apiInfo