1/* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 17import { Comment } from '../../../typedef/parser/Comment'; 18import { ErrorMessage, ErrorTagFormat } from '../../../typedef/checker/result_type'; 19import { CommonFunctions } from '../../../utils/checkUtils'; 20 21export class CheckErrorCode { 22 23 static errorCodeList: number[] = [201, 202, 203, 301, 401, 501, 502, 801, 901]; 24 25 static isArrayNotEmpty(arr: any): boolean { 26 return Array.isArray(arr) && arr.length > 0; 27 } 28 29 /** 30 * 判断数组arr1中的每个数字是否在数组arr2中 31 * @param arr1 32 * @param arr2 33 */ 34 static hasNumberInArray(arr1: number[], arr2: number[]): boolean { 35 return arr1.every(num => arr2.includes(num)); 36 } 37 38 static checkErrorCode(apiJsdoc: Comment.JsDocInfo): ErrorTagFormat { 39 const checkResult: ErrorTagFormat = { 40 state: true, 41 errorInfo: '', 42 }; 43 44 const errorCodes = apiJsdoc.errorCodes.filter(number => number >= 100 && number < 1000); 45 ; 46 if (this.isArrayNotEmpty(errorCodes)) { 47 if (!this.hasNumberInArray(errorCodes, this.errorCodeList)) { 48 checkResult.state = false; 49 checkResult.errorInfo = ErrorMessage.ERROR_ERROR_CODE; 50 } 51 } 52 return checkResult; 53 } 54}