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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
* SonarQube
* Copyright (C) 2009-2024 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 * as React from 'react';
import { getSources } from '../../../api/components';
import { locationsByLine } from '../../../components/SourceViewer/helpers/indexing';
import { getBranchLikeQuery } from '../../../sonar-aligned/helpers/branch-like';
import { BranchLike } from '../../../types/branch-like';
import { Hotspot } from '../../../types/security-hotspots';
import { Component, ExpandDirection, FlowLocation, SourceLine } from '../../../types/types';
import { constructSourceViewerFile, getLocations } from '../utils';
import HotspotSnippetContainerRenderer from './HotspotSnippetContainerRenderer';
interface Props {
branchLike?: BranchLike;
component: Component;
hotspot: Hotspot;
onLocationSelect: (index: number) => void;
selectedHotspotLocation?: number;
}
interface State {
highlightedSymbols: string[];
lastLine?: number;
loading: boolean;
sourceLines: SourceLine[];
secondaryLocations: FlowLocation[];
}
const BUFFER_LINES = 10;
const EXPAND_BY_LINES = 50;
export default class HotspotSnippetContainer extends React.Component<Props, State> {
mounted = false;
state: State = {
highlightedSymbols: [],
loading: true,
sourceLines: [],
secondaryLocations: [],
};
async componentDidMount() {
this.mounted = true;
await this.initializeSecondaryLocations();
this.fetchSources();
}
async componentDidUpdate(prevProps: Props) {
if (prevProps.hotspot.key !== this.props.hotspot.key) {
await this.initializeSecondaryLocations();
this.fetchSources();
}
}
componentWillUnmount() {
this.mounted = false;
}
checkLastLine(lines: SourceLine[], target: number): number | undefined {
if (lines.length < 1) {
return undefined;
}
const lastLineReceived = lines[lines.length - 1].line;
if (lastLineReceived < target) {
return lastLineReceived;
}
return undefined;
}
async fetchSources() {
const {
branchLike,
hotspot: { component, textRange },
} = this.props;
const { secondaryLocations } = this.state;
if (!textRange) {
// Hotspot not associated to any loc
this.setState({ loading: false, lastLine: undefined, sourceLines: [] });
return;
}
// Search for the min startLine within primary and secondary locations
const from = Math.max(
1,
Math.min(
...[textRange, ...secondaryLocations.map((l) => l.textRange)].map(
(t) => t.startLine - BUFFER_LINES,
),
),
);
// Search for the max endLine within primary and secondary locations
const to = Math.max(
...[textRange, ...secondaryLocations.map((l) => l.textRange)].map(
// Add 1 to check for end-of-file
(t) => t.endLine + BUFFER_LINES + 1,
),
);
this.setState({ loading: true });
let sourceLines = await getSources({
key: component.key,
from,
to,
...getBranchLikeQuery(branchLike),
}).catch(() => [] as SourceLine[]);
if (this.mounted) {
const lastLine = this.checkLastLine(sourceLines, to);
// remove extra sourceline if we didn't reach the end:
sourceLines = lastLine ? sourceLines : sourceLines.slice(0, -1);
this.setState({ lastLine, loading: false, sourceLines });
}
}
initializeSecondaryLocations() {
const { hotspot } = this.props;
return new Promise((resolve) => {
this.setState(
{
secondaryLocations: getLocations(hotspot.flows, undefined).map((location, index) => ({
...location,
index,
text: location.msg,
})),
},
() => resolve(undefined),
);
});
}
handleExpansion = (direction: ExpandDirection) => {
const { branchLike, hotspot } = this.props;
const { sourceLines } = this.state;
const range =
direction === 'up'
? {
from: Math.max(1, sourceLines[0].line - EXPAND_BY_LINES),
to: sourceLines[0].line - 1,
}
: {
from: sourceLines[sourceLines.length - 1].line + 1,
// Add 1 to check for end-of-file:
to: sourceLines[sourceLines.length - 1].line + EXPAND_BY_LINES + 1,
};
return getSources({
key: hotspot.component.key,
...range,
...getBranchLikeQuery(branchLike),
}).then((additionalLines) => {
const { lastLine: previousLastLine } = this.state;
const lastLine =
direction === 'down' ? this.checkLastLine(additionalLines, range.to) : previousLastLine;
let concatSourceLines;
if (direction === 'up') {
concatSourceLines = additionalLines.concat(sourceLines);
} else {
// remove extra sourceline if we didn't reach the end:
concatSourceLines = sourceLines.concat(
lastLine ? additionalLines : additionalLines.slice(0, -1),
);
}
this.setState({
lastLine,
sourceLines: concatSourceLines,
});
});
};
handleSymbolClick = (highlightedSymbols: string[]) => {
this.setState({ highlightedSymbols });
};
render() {
const { hotspot, selectedHotspotLocation } = this.props;
const { highlightedSymbols, lastLine, loading, sourceLines, secondaryLocations } = this.state;
const locations = locationsByLine([hotspot]);
const sourceViewerFile = constructSourceViewerFile(hotspot, lastLine);
return (
<HotspotSnippetContainerRenderer
highlightedSymbols={highlightedSymbols}
hotspot={hotspot}
loading={loading}
locations={locations}
onExpandBlock={this.handleExpansion}
onSymbolClick={this.handleSymbolClick}
onLocationSelect={this.props.onLocationSelect}
sourceLines={sourceLines}
sourceViewerFile={sourceViewerFile}
secondaryLocations={secondaryLocations}
selectedHotspotLocation={selectedHotspotLocation}
/>
);
}
}
|