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 */ 15 16const rules = require('../code_style_rule.json'); 17const { addAPICheckErrorLogs } = require('./compile_info'); 18const { getAPINote, ErrorType, ErrorLevel, FileType } = require('./utils'); 19 20// duplicate removal 21const API_ERROR_DECORATOR_POS = new Set([]); 22 23function checkAPIDecorators(node, sourcefile, fileName) { 24 const apiNote = getAPINote(node); 25 if (API_ERROR_DECORATOR_POS.has(node.pos)) { 26 return; 27 } 28 29 const regex = /\*\s*\@[A-Za-z0-9]+\b/g; 30 const matchResult = apiNote.match(regex); 31 let hasCodeStyleError = false; 32 let errorInfo = ''; 33 if (matchResult) { 34 matchResult.forEach(decorator => { 35 const docTags = [...rules.decorators.customDoc, ...rules.decorators.jsDoc]; 36 const decoratorRuleSet = new Set(docTags); 37 const apiDecorator = decorator.replace(/^\*\s*\@/, ''); 38 if (!decoratorRuleSet.has(apiDecorator)) { 39 hasCodeStyleError = true; 40 if (errorInfo !== '') { 41 errorInfo += `,${apiDecorator}`; 42 } else { 43 errorInfo += apiDecorator; 44 } 45 } 46 }); 47 48 if (hasCodeStyleError) { 49 API_ERROR_DECORATOR_POS.add(node.pos); 50 errorInfo += '.'; 51 addAPICheckErrorLogs(node, sourcefile, fileName, ErrorType.UNKNOW_DECORATOR, errorInfo, FileType.JSDOC, 52 ErrorLevel.MIDDLE); 53 } 54 } 55} 56 57exports.checkAPIDecorators = checkAPIDecorators; 58