aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/components/SourceViewer/helpers/indexing.ts
blob: 14af7c8850629574ea2c641a28a2ab94c55222ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * SonarQube
 * Copyright (C) 2009-2023 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
import { flatten } from 'lodash';
import { Duplication, Issue, LinearIssueLocation, SourceLine } from '../../../types/types';
import { splitByTokens } from './highlight';
import { getLinearLocations } from './issueLocations';

export function issuesByLine(issues: Issue[]) {
  const index: { [line: number]: Issue[] } = {};
  issues.forEach((issue) => {
    const line = issue.textRange ? issue.textRange.endLine : 0;
    if (!(line in index)) {
      index[line] = [];
    }
    index[line].push(issue);
  });
  return index;
}

export function issuesByComponentAndLine(issues: Issue[] = []): {
  [component: string]: { [line: number]: Issue[] };
} {
  return issues.reduce((mapping: { [component: string]: { [line: number]: Issue[] } }, issue) => {
    mapping[issue.component] = mapping[issue.component] || {};
    const line = issue.textRange ? issue.textRange.endLine : 0;
    mapping[issue.component][line] = mapping[issue.component][line] || [];
    mapping[issue.component][line].push(issue);
    return mapping;
  }, {});
}

export function locationsByLine(issues: Pick<Issue, 'textRange'>[]) {
  const index: { [line: number]: LinearIssueLocation[] } = {};
  issues.forEach((issue) => {
    getLinearLocations(issue.textRange).forEach((location) => {
      if (!(location.line in index)) {
        index[location.line] = [];
      }
      index[location.line].push(location);
    });
  });
  return index;
}

export function duplicationsByLine(duplications: Duplication[] | undefined) {
  if (duplications == null) {
    return {};
  }

  const duplicationsByLine: { [line: number]: number[] } = {};

  duplications.forEach(({ blocks }, duplicationIndex) => {
    blocks.forEach((block) => {
      // eslint-disable-next-line no-underscore-dangle
      if (block._ref === '1') {
        for (let line = block.from; line < block.from + block.size; line++) {
          if (!(line in duplicationsByLine)) {
            duplicationsByLine[line] = [];
          }
          duplicationsByLine[line].push(duplicationIndex);
        }
      }
    });
  });

  return duplicationsByLine;
}

export function symbolsByLine(sources: SourceLine[]) {
  const index: { [line: number]: string[] } = {};
  sources.forEach((line) => {
    const container = document.createElement('div');
    container.innerHTML = line.code || '';
    const tokens = splitByTokens(container.childNodes);
    const symbols = flatten(
      tokens.map((token) => {
        const keys = token.className.match(/sym-\d+/g);
        return keys != null ? keys : [];
      })
    );
    index[line.line] = symbols.filter((key) => key);
  });
  return index;
}