1/* 2 * Copyright (c) 2025 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 * as fs from 'fs'; 17import path from 'path'; 18import { IssueReport } from '../../model/Defects'; 19import { FileUtils } from '../../Index'; 20import Logger, { LOG_MODULE_TYPE } from 'arkanalyzer/lib/utils/logger'; 21 22const logger = Logger.getLogger(LOG_MODULE_TYPE.HOMECHECK, 'Disable'); 23 24export const DisableText = { 25 FILE_DISABLE_TEXT: '\/* homecheck-disable *\/', 26 NEXT_LINE_DISABLE_TEXT: '\/\/ homecheck-disable-next-line ', 27}; 28 29export async function filterDisableIssue(lineList: string[], issues: IssueReport[], filePath: string): Promise<IssueReport[]> { 30 let filtedIssues: IssueReport[] = []; 31 try { 32 for (const issue of issues) { 33 // @migration/arkui-data-observation规则的自动修复是在定义处,存在跨文件场景 34 const actualFilePath = path.normalize(issue.defect.mergeKey.split('%')[0]); 35 if (path.normalize(actualFilePath) !== path.normalize(filePath)) { 36 if (!fs.existsSync(actualFilePath)) { 37 continue; 38 } 39 lineList = await FileUtils.readLinesFromFile(actualFilePath); 40 } 41 // 有些特殊规则允许返回行列号为0 42 if (issue.defect.reportLine < 0 || issue.defect.reportLine - 1 > lineList.length) { 43 continue; 44 } 45 const text = lineList[issue.defect.reportLine - 2]; 46 if (!isDisableIssue(text, issue.defect.ruleId)) { 47 filtedIssues.push(issue); 48 } 49 } 50 } catch (e) { 51 logger.error(e); 52 } 53 return filtedIssues; 54} 55 56function isDisableIssue(lineText: string, ruleId: string): boolean { 57 if (!lineText || lineText.length === 0) { 58 return false; 59 } 60 61 if (lineText.includes(DisableText.NEXT_LINE_DISABLE_TEXT) && lineText.includes(ruleId)) { 62 return true; 63 } 64 return false; 65} 66