13af6ab5fSopenharmony_ci/*
23af6ab5fSopenharmony_ci * Copyright (c) 2022-2024 Huawei Device Co., Ltd.
33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License.
53af6ab5fSopenharmony_ci * You may obtain a copy of the License at
63af6ab5fSopenharmony_ci *
73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
83af6ab5fSopenharmony_ci *
93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and
133af6ab5fSopenharmony_ci * limitations under the License.
143af6ab5fSopenharmony_ci */
153af6ab5fSopenharmony_ci
163af6ab5fSopenharmony_ciimport * as fs from 'fs';
173af6ab5fSopenharmony_ci
183af6ab5fSopenharmony_ciconst ARK_TS_ISSUES_PREFIX = 'ArkTS';
193af6ab5fSopenharmony_ciconst ARK_TS_ISSUES_ERROR_CATEGORY = 'ArkTS Migration Errors';
203af6ab5fSopenharmony_ci
213af6ab5fSopenharmony_ciinterface DefectInfo {
223af6ab5fSopenharmony_ci  // warn: 1; error: 2;
233af6ab5fSopenharmony_ci  severity: number;
243af6ab5fSopenharmony_ci  description: string;
253af6ab5fSopenharmony_ci  mergeKey: string;
263af6ab5fSopenharmony_ci  reportLine: number;
273af6ab5fSopenharmony_ci  reportColumn: number;
283af6ab5fSopenharmony_ci  ruleId: string | null;
293af6ab5fSopenharmony_ci  ruleDocPath: string | null;
303af6ab5fSopenharmony_ci  category: string | null;
313af6ab5fSopenharmony_ci  fixable: boolean;
323af6ab5fSopenharmony_ci  fixKey: string;
333af6ab5fSopenharmony_ci}
343af6ab5fSopenharmony_ci
353af6ab5fSopenharmony_ciinterface FileDefectInfo {
363af6ab5fSopenharmony_ci  defects: DefectInfo[];
373af6ab5fSopenharmony_ci  output: string | undefined;
383af6ab5fSopenharmony_ci  filePath: string;
393af6ab5fSopenharmony_ci}
403af6ab5fSopenharmony_ci
413af6ab5fSopenharmony_citype ReportJson = FileDefectInfo[];
423af6ab5fSopenharmony_ci
433af6ab5fSopenharmony_ciinterface ArkTsIssueType {
443af6ab5fSopenharmony_ci  description: string;
453af6ab5fSopenharmony_ci  type: string;
463af6ab5fSopenharmony_ci  count: number;
473af6ab5fSopenharmony_ci}
483af6ab5fSopenharmony_ci
493af6ab5fSopenharmony_ciinterface Statistics {
503af6ab5fSopenharmony_ci  totalErrors: number;
513af6ab5fSopenharmony_ci  totalWarnings: number;
523af6ab5fSopenharmony_ci  linesWithErrors: number;
533af6ab5fSopenharmony_ci  linesWithWarnings: number;
543af6ab5fSopenharmony_ci  issues: Map<number, ArkTsIssueType>;
553af6ab5fSopenharmony_ci}
563af6ab5fSopenharmony_ci
573af6ab5fSopenharmony_cifunction isError(defectInfo: DefectInfo): boolean {
583af6ab5fSopenharmony_ci  return defectInfo.category === ARK_TS_ISSUES_ERROR_CATEGORY;
593af6ab5fSopenharmony_ci}
603af6ab5fSopenharmony_ci
613af6ab5fSopenharmony_cifunction fillIssueInfo(statistics: Statistics, defectInfo: DefectInfo): void {
623af6ab5fSopenharmony_ci  const recipeNo = parseInt(
633af6ab5fSopenharmony_ci    defectInfo.ruleDocPath.substring('docs/recipe'.length, defectInfo.ruleDocPath.length - '.md'.length)
643af6ab5fSopenharmony_ci  );
653af6ab5fSopenharmony_ci  const issueInfo = statistics.issues.get(recipeNo);
663af6ab5fSopenharmony_ci  if (!issueInfo) {
673af6ab5fSopenharmony_ci    statistics.issues.set(recipeNo, {
683af6ab5fSopenharmony_ci      description: defectInfo.description,
693af6ab5fSopenharmony_ci      type: isError(defectInfo) ? 'error' : 'warn',
703af6ab5fSopenharmony_ci      count: 1
713af6ab5fSopenharmony_ci    });
723af6ab5fSopenharmony_ci  } else {
733af6ab5fSopenharmony_ci    issueInfo.count += 1;
743af6ab5fSopenharmony_ci  }
753af6ab5fSopenharmony_ci}
763af6ab5fSopenharmony_ci
773af6ab5fSopenharmony_cifunction parse(reportJson: ReportJson): Statistics {
783af6ab5fSopenharmony_ci  const statistics: Statistics = {
793af6ab5fSopenharmony_ci    totalErrors: 0,
803af6ab5fSopenharmony_ci    totalWarnings: 0,
813af6ab5fSopenharmony_ci    linesWithErrors: 0,
823af6ab5fSopenharmony_ci    linesWithWarnings: 0,
833af6ab5fSopenharmony_ci    issues: new Map()
843af6ab5fSopenharmony_ci  };
853af6ab5fSopenharmony_ci
863af6ab5fSopenharmony_ci  for (const fileInfo of reportJson) {
873af6ab5fSopenharmony_ci    const linesWithErrors: Set<number> = new Set();
883af6ab5fSopenharmony_ci    const linesWithWarnings: Set<number> = new Set();
893af6ab5fSopenharmony_ci
903af6ab5fSopenharmony_ci    for (const defectInfo of fileInfo.defects) {
913af6ab5fSopenharmony_ci      // count only issues from ArkTS Linter
923af6ab5fSopenharmony_ci      if (!defectInfo.category.startsWith(ARK_TS_ISSUES_PREFIX)) {
933af6ab5fSopenharmony_ci        continue;
943af6ab5fSopenharmony_ci      }
953af6ab5fSopenharmony_ci
963af6ab5fSopenharmony_ci      fillIssueInfo(statistics, defectInfo);
973af6ab5fSopenharmony_ci
983af6ab5fSopenharmony_ci      if (isError(defectInfo)) {
993af6ab5fSopenharmony_ci        statistics.totalErrors += 1;
1003af6ab5fSopenharmony_ci        linesWithErrors.add(defectInfo.reportLine);
1013af6ab5fSopenharmony_ci      } else {
1023af6ab5fSopenharmony_ci        statistics.totalWarnings += 1;
1033af6ab5fSopenharmony_ci        linesWithWarnings.add(defectInfo.reportLine);
1043af6ab5fSopenharmony_ci      }
1053af6ab5fSopenharmony_ci    }
1063af6ab5fSopenharmony_ci    statistics.linesWithErrors += linesWithErrors.size;
1073af6ab5fSopenharmony_ci    statistics.linesWithWarnings += linesWithWarnings.size;
1083af6ab5fSopenharmony_ci  }
1093af6ab5fSopenharmony_ci
1103af6ab5fSopenharmony_ci  return statistics;
1113af6ab5fSopenharmony_ci}
1123af6ab5fSopenharmony_ci
1133af6ab5fSopenharmony_cifunction read(filePath: string): ReportJson {
1143af6ab5fSopenharmony_ci  return JSON.parse(fs.readFileSync(filePath, { encoding: 'utf8', flag: 'r' }));
1153af6ab5fSopenharmony_ci}
1163af6ab5fSopenharmony_ci
1173af6ab5fSopenharmony_cifunction main(): void {
1183af6ab5fSopenharmony_ci  if (process.argv.length < 3) {
1193af6ab5fSopenharmony_ci    console.error('Path to input json was not provided, exiting');
1203af6ab5fSopenharmony_ci    process.exit(1);
1213af6ab5fSopenharmony_ci  }
1223af6ab5fSopenharmony_ci  console.log(parse(read(process.argv[2])));
1233af6ab5fSopenharmony_ci}
1243af6ab5fSopenharmony_ci
1253af6ab5fSopenharmony_ci/*
1263af6ab5fSopenharmony_ci * file is stored in project's directory under the following path:
1273af6ab5fSopenharmony_ci * <PROJECT_ROOT_DIR>/.idea/code-linter/eslintAgent/output.json
1283af6ab5fSopenharmony_ci */
1293af6ab5fSopenharmony_cimain();
130