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 */ 15import { ErrorMessage, ErrorTagFormat } from '../../../typedef/checker/result_type'; 16import { Comment } from '../../../typedef/parser/Comment'; 17import { tagsArrayOfOrder, CommonFunctions } from '../../../utils/checkUtils'; 18import { ApiInfo } from '../../../typedef/parser/ApiInfoDefination'; 19 20export class OrderCheck { 21 /** 22 * Check if the tags order is correct. 23 * @param { Comment.JsDocInfo } apiJsdoc -api jsdoc all infomation 24 * @returns { boolean } 25 */ 26 static orderCheck(singleApi: ApiInfo, apiJsdoc: Comment.JsDocInfo): ErrorTagFormat { 27 const orderCheckResult: ErrorTagFormat = { 28 state: true, 29 errorInfo: '', 30 }; 31 const tagsOrder: Comment.CommentTag[] | undefined = apiJsdoc.tags; 32 if (tagsOrder === undefined) { 33 return orderCheckResult; 34 } 35 const tagsNameOrder: string[] = []; 36 tagsOrder.forEach((tagsOrder: Comment.CommentTag) => { tagsNameOrder.push(tagsOrder.tag) }); 37 38 if (tagsNameOrder.includes('deprecated')) { 39 return orderCheckResult; 40 } 41 42 for (let tagIndex = 0; tagIndex < tagsOrder.length; tagIndex++) { 43 if (tagIndex + 1 < tagsOrder.length) { 44 // 获取前后两个tag下标 45 const firstIndex = tagsArrayOfOrder.indexOf(tagsOrder[tagIndex].tag); 46 const secondIndex = tagsArrayOfOrder.indexOf(tagsOrder[tagIndex + 1].tag); 47 // 判断标签是否为官方标签 48 const firstTag = CommonFunctions.isOfficialTag(tagsOrder[tagIndex].tag); 49 // 非自定义标签在前或数组降序时报错 50 if (tagsOrder[tagIndex].tag !== 'form' && tagsOrder[tagIndex + 1].tag !== 'form' && 51 ((firstTag && secondIndex > -1) || (firstIndex > secondIndex && secondIndex > -1))) { 52 orderCheckResult.state = false; 53 orderCheckResult.errorInfo = CommonFunctions.createErrorInfo(ErrorMessage.ERROR_ORDER, [tagsOrder[tagIndex].tag]); 54 break; 55 } else if (tagsOrder[tagIndex].tag === 'form') { 56 orderCheckResult.state = OrderCheck.formOrderCheck(tagsOrder, tagIndex, firstIndex, secondIndex); 57 orderCheckResult.errorInfo = CommonFunctions.createErrorInfo(ErrorMessage.ERROR_ORDER, [tagsOrder[tagIndex].tag]); 58 } 59 } 60 } 61 return orderCheckResult; 62 } 63 64 /** 65 * 对form标签的顺序做兼容处理 66 */ 67 static formOrderCheck(tags: Comment.CommentTag[], tagIndex: number, firstIndex: number, secondIndex: number): boolean { 68 const frontFirstIndex = tagIndex - 1 > -1 ? tagsArrayOfOrder.indexOf(tags[tagIndex - 1].tag) : 0; 69 const formNeighborArr = [frontFirstIndex, firstIndex]; 70 const newTagIndex = tagsArrayOfOrder.lastIndexOf(tags[tagIndex].tag); 71 const newFormNeighborArr = [frontFirstIndex, newTagIndex]; 72 if (secondIndex > -1) { 73 formNeighborArr.push(secondIndex); 74 newFormNeighborArr.push(secondIndex); 75 } 76 if (!CommonFunctions.isAscending(formNeighborArr) && !CommonFunctions.isAscending(newFormNeighborArr)) { 77 return false; 78 } 79 return true; 80 } 81 82} 83