1/* 2 * Copyright (c) 2021-2022 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 */ 15const { 16 requireTypescriptModule, tagsArrayOfOrder, commentNodeWhiteList, parseJsDoc, ErrorType, ErrorLevel, FileType, 17 inheritArr, ErrorValueInfo, createErrorInfo, isWhiteListFile, 18} = require('../utils'); 19const { addAPICheckErrorLogs } = require('../compile_info'); 20const rules = require('../../code_style_rule.json'); 21const whiteLists = require('../../config/jsdocCheckWhiteList.json'); 22const ts = requireTypescriptModule(); 23 24/** 25 * 判断标签是否为官方标签 26 */ 27function isOfficialTag(tagName) { 28 return tagsArrayOfOrder.indexOf(tagName) === -1; 29} 30 31/** 32 * 判断标签排列是否为升序 33 */ 34function isAscendingOrder(tags) { 35 let checkResult = true; 36 for (let tagIndex = 0; tagIndex < tags.length; tagIndex++) { 37 if (tagIndex + 1 < tags.length) { 38 // 获取前后两个tag下标 39 const firstIndex = tagsArrayOfOrder.indexOf(tags[tagIndex].tag); 40 const secondIndex = tagsArrayOfOrder.indexOf(tags[tagIndex + 1].tag); 41 // 判断标签是否为官方标签 42 const firstTag = isOfficialTag(tags[tagIndex].tag); 43 // 非自定义标签在前或数组降序时报错 44 if ((firstTag && secondIndex > -1) || (firstIndex > secondIndex && secondIndex > -1)) { 45 checkResult = false; 46 break; 47 } 48 } 49 }; 50 return checkResult; 51} 52 53// check jsdoc order 54function checkApiOrder(comments) { 55 const checkOrderRusult = []; 56 comments.forEach(docInfo => { 57 if (isAscendingOrder(docInfo.tags)) { 58 checkOrderRusult.push({ 59 checkResult: true, 60 errorInfo: '', 61 }); 62 } else { 63 const errorInfo = ErrorValueInfo.ERROR_ORDER; 64 checkOrderRusult.push({ 65 checkResult: false, 66 errorInfo: errorInfo, 67 }); 68 } 69 }); 70 return checkOrderRusult; 71} 72exports.checkApiOrder = checkApiOrder; 73 74function checkAPITagName(tag, node, sourcefile, fileName, JSDocIndec) { 75 const APITagNameResult = { 76 checkResult: true, 77 errorInfo: '', 78 }; 79 const tagName = tag.tag; 80 const docTags = [...rules.decorators.customDoc, ...rules.decorators.jsDoc]; 81 const decoratorRuleSet = new Set(docTags); 82 if (!decoratorRuleSet.has(tagName) && commentNodeWhiteList.includes(node.kind)) { 83 APITagNameResult.checkResult = false; 84 APITagNameResult.errorInfo = createErrorInfo(ErrorValueInfo.ERROR_LABELNAME, [tagName]); 85 addAPICheckErrorLogs(node, sourcefile, fileName, ErrorType.WRONG_SCENE, APITagNameResult.errorInfo, 86 FileType.JSDOC, ErrorLevel.MIDDLE); 87 } 88 return APITagNameResult; 89} 90exports.checkAPITagName = checkAPITagName; 91 92function checkParentInheritTag(node, inheritTag, inheritResult, JSocIndex) { 93 const parentTagArr = []; 94 if (ts.isSourceFile(node.parent)) { 95 return inheritResult; 96 } 97 if (!ts.isModuleBlock(node.parent)) { 98 const comments = parseJsDoc(node.parent); 99 if (comments.length > 0 && Array.isArray(comments[comments.length - 1].tags)) { 100 comments[comments.length - 1].tags.forEach(tag => { 101 parentTagArr.push(tag.tag); 102 }); 103 if (parentTagArr.includes(inheritTag)) { 104 inheritResult.checkResult = false; 105 inheritResult.errorInfo += createErrorInfo(ErrorValueInfo.ERROR_INFO_INHERIT, [inheritTag]); 106 } else { 107 checkParentInheritTag(node.parent, inheritTag, inheritResult, JSocIndex); 108 } 109 } 110 } else if (ts.isModuleBlock(node.parent)) { 111 checkParentInheritTag(node.parent, inheritTag, inheritResult, JSocIndex); 112 } 113 114 return inheritResult; 115} 116 117function checkInheritTag(comment, node, sourcefile, fileName, JSocIndex) { 118 const inheritResult = { 119 checkResult: true, 120 errorInfo: '', 121 }; 122 const tagArr = []; 123 if (commentNodeWhiteList.includes(node.kind)) { 124 comment.tags.forEach(tag => { 125 tagArr.push(tag.tag); 126 }); 127 inheritArr.forEach((inheritTag, index) => { 128 if (!tagArr.includes(inheritTag)) { 129 checkParentInheritTag(node, inheritArr[index], inheritResult, JSocIndex); 130 } 131 }); 132 if (!inheritResult.checkResult) { 133 addAPICheckErrorLogs(node, sourcefile, fileName, ErrorType.WRONG_SCENE, inheritResult.errorInfo, FileType.API, 134 ErrorLevel.MIDDLE); 135 } 136 } 137 return inheritResult; 138} 139exports.checkInheritTag = checkInheritTag; 140