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 result = require("../check_result.json"); 17const rules = require("../code_style_rule.json"); 18const { getAPINote, error_type } = require('./utils'); 19const { addAPICheckErrorLogs } = require('./compile_info'); 20 21function checkPermission(node, sourcefile, fileName) { 22 const permissionTagsPart = [...rules.decorators["permission"]]; 23 const permissionTags = [] 24 permissionTagsPart.forEach((item) => { 25 const tag = 'ohos.permission.' + item 26 permissionTags.push(tag) 27 }) 28 29 const permissionRuleSet = new Set(permissionTags); 30 const apiNote = getAPINote(node); 31 const apiNoteArr = apiNote.split('*'); 32 let hasPermissionError = false; 33 let errorInfo = ""; 34 apiNoteArr.forEach(note => { 35 if (note.match(new RegExp('@permission'))) { 36 const permissionNote = note.replace('@permission', '').trim(); 37 if (/( or | and )/g.test(permissionNote)) { 38 const permissionArr = permissionNote.split(/( or | and )/) 39 permissionArr.forEach(item => { 40 if (!/( or | and )/g.test(item)) { 41 if (!permissionRuleSet.has(item)) { 42 hasPermissionError = true; 43 if (errorInfo !== "") { 44 errorInfo += `,${item}`; 45 } else { 46 errorInfo += item; 47 } 48 } 49 } 50 }) 51 } else { 52 if (!permissionRuleSet.has(permissionNote) && !/N\/A/.test(permissionNote)) { 53 hasPermissionError = true; 54 if (errorInfo !== "") { 55 errorInfo += `,${permissionNote}`; 56 } else { 57 errorInfo += permissionNote; 58 } 59 } 60 } 61 62 } 63 }); 64 65 if (hasPermissionError) { 66 addAPICheckErrorLogs(node, sourcefile, fileName, error_type.UNKNOW_PERMISSION, errorInfo); 67 } 68} 69exports.checkPermission = checkPermission;