1/* 2 * Copyright (c) 2022-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 path from 'node:path'; 17import type { FileIssues, RuleFix } from 'homecheck'; 18import type { CommandLineOptions } from './CommandLineOptions'; 19import type { ProblemInfo } from './ProblemInfo'; 20import { FaultID } from './Problems'; 21import { shouldProcessFile } from './LinterRunner'; 22 23interface RuleConfigInfo { 24 ruleSet: string[]; 25} 26 27interface ProjectConfigInfo { 28 projectName: string | undefined; 29 projectPath: string | undefined; 30 logPath: string; 31 arkCheckPath: string; 32 ohosSdkPath: string; 33 hmsSdkPath: string; 34 reportDir: string; 35 languageTags: Map<string, number>; 36 fileOrFolderToCheck: string[]; 37} 38 39export function getHomeCheckConfigInfo(cmdOptions: CommandLineOptions): { 40 ruleConfigInfo: RuleConfigInfo; 41 projectConfigInfo: ProjectConfigInfo; 42} { 43 let inputFiles = cmdOptions.inputFiles; 44 let fliesTocheck: string[] = inputFiles; 45 if (cmdOptions.scanWholeProjectInHomecheck === true) { 46 fliesTocheck = []; 47 } 48 inputFiles = inputFiles.filter((input) => { 49 return shouldProcessFile(cmdOptions, input); 50 }); 51 const languageTags = new Map<string, number>(); 52 inputFiles.forEach((file) => { 53 languageTags.set(path.normalize(file), 2); 54 }); 55 const ruleConfigInfo = { 56 ruleSet: ['plugin:@migration/all'], 57 files: ['**/*.ets', '**/*.ts', '**/*.js'] 58 }; 59 const projectConfigInfo = { 60 projectName: cmdOptions.arktsWholeProjectPath, 61 projectPath: cmdOptions.arktsWholeProjectPath, 62 logPath: cmdOptions.outputFilePath ? path.join(cmdOptions.outputFilePath, 'HomeCheck.log') : './HomeCheck.log', 63 arkCheckPath: './node_modules/homecheck', 64 ohosSdkPath: cmdOptions.sdkDefaultApiPath ? cmdOptions.sdkDefaultApiPath : '', 65 hmsSdkPath: cmdOptions.sdkExternalApiPath ? cmdOptions.sdkExternalApiPath[0] : '', 66 reportDir: './', 67 languageTags: languageTags, 68 fileOrFolderToCheck: fliesTocheck, 69 logLevel: cmdOptions.verbose ? 'DEBUG' : 'INFO', 70 arkAnalyzerLogLevel: cmdOptions.verbose ? 'DEBUG' : 'ERROR' 71 }; 72 return { ruleConfigInfo, projectConfigInfo }; 73} 74 75export function transferIssues2ProblemInfo(fileIssuesArray: FileIssues[]): Map<string, ProblemInfo[]> { 76 const result = new Map<string, ProblemInfo[]>(); 77 fileIssuesArray.forEach((fileIssues) => { 78 fileIssues.issues.forEach((issueReport) => { 79 const defect = issueReport.defect; 80 const problemInfo: ProblemInfo = { 81 line: defect.reportLine, 82 column: defect.reportColumn, 83 endLine: defect.reportLine, 84 endColumn: defect.reportColumn, 85 start: 0, 86 end: 0, 87 type: '', 88 severity: defect.severity, 89 faultId: FaultID.LAST_ID, 90 problem: defect.problem, 91 suggest: '', 92 rule: defect.description, 93 ruleTag: -1, 94 autofixable: defect.fixable 95 }; 96 if (problemInfo.autofixable) { 97 const fix = issueReport.fix as RuleFix; 98 const replacementText = fix.text; 99 const start = fix.range[0]; 100 const end = fix.range[1]; 101 problemInfo.autofix = [{ replacementText, start, end }]; 102 problemInfo.autofixTitle = defect.ruleId; 103 } 104 const filePath = path.normalize(defect.mergeKey.split('%')[0]); 105 const problems = result.get(filePath) || []; 106 problems.push(problemInfo); 107 result.set(filePath, problems); 108 }); 109 }); 110 return result; 111} 112