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 16import { Comment } from '../../../typedef/parser/Comment'; 17import { ErrorTagFormat, ErrorMessage } from '../../../typedef/checker/result_type'; 18import { CommonFunctions } from '../../../utils/checkUtils'; 19 20export class TagRepeatCheck { 21 /** 22 * jsdoc tag repeat check 23 * @param { Comment.JsDocInfo } apiJsdoc 24 * @returns { ErrorTagFormat[] } 25 */ 26 static tagRepeatCheck(apiJsdoc: Comment.JsDocInfo): ErrorTagFormat[] { 27 const tagRepeatCheckResult: ErrorTagFormat[] = []; 28 const multipleTags: string[] = ['throws', 'param']; 29 const tagNameArr: string[] = []; 30 apiJsdoc.tags?.forEach((tag) => { 31 tagNameArr.push(tag.tag); 32 }); 33 if (tagNameArr.includes('deprecated')) { 34 return tagRepeatCheckResult; 35 } 36 const duplicateArr: string[] = tagNameArr.filter((item) => { 37 return tagNameArr.indexOf(item) !== tagNameArr.lastIndexOf(item); 38 }); 39 const duplicateSet: Set<string> = new Set(duplicateArr); 40 duplicateSet.forEach((duplicateTag) => { 41 if (!multipleTags.includes(duplicateTag)) { 42 const tagRepeatCheckFormat: ErrorTagFormat = { 43 state: false, 44 errorInfo: CommonFunctions.createErrorInfo(ErrorMessage.ERROR_REPEATLABEL, [duplicateTag]), 45 }; 46 tagRepeatCheckResult.push(tagRepeatCheckFormat); 47 } 48 }); 49 return tagRepeatCheckResult; 50 } 51} 52