-
Notifications
You must be signed in to change notification settings - Fork 23
/
report.js
223 lines (192 loc) · 6 KB
/
report.js
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
const path = require('path');
const eslintHelpers = require('./eslint');
const eslintCliEngine = require('eslint').CLIEngine;
const SourceMappingDecoder = require('remix-lib/src/sourceMappingDecoder');
const mythx2Severity = {
High: 2,
Medium: 1,
};
const decoder = new SourceMappingDecoder();
/**
* @returns ESLint formatter module
*/
const getFormatter = (name) => {
const custom = ['text'];
if (custom.includes(name)) {
name = path.join(__dirname, 'formatters/', name + '.js');
}
return eslintCliEngine.getFormatter(name);
};
/**
* Turn a srcmap entry (the thing between semicolons) into a line and
* column location.
* We make use of this.sourceMappingDecoder of this class to make
* the conversion.
*
* @param {string} srcEntry - a single entry of solc sourceMap
* @param {Array} lineBreakPositions - array returned by the function 'mapLineBreakPositions'
* @returns {line: number, column: number}
*/
const textSrcEntry2lineColumn = (srcEntry, lineBreakPositions) => {
const ary = srcEntry.split(':');
const sourceLocation = {
length: parseInt(ary[1], 10),
start: parseInt(ary[0], 10),
};
const loc = decoder.convertOffsetToLineColumn(sourceLocation, lineBreakPositions);
// FIXME: note we are lossy in that we don't return the end location
if (loc.start) {
// Adjust because routines starts lines at 0 rather than 1.
loc.start.line++;
}
if (loc.end) {
loc.end.line++;
}
return [loc.start, loc.end];
};
/**
* Convert a MythX issue into an ESLint-style issue.
* The eslint report format which we use, has these fields:
*
* - column,
* - endCol,
* - endLine,
* - fatal,
* - line,
* - message,
* - ruleId,
* - severity
*
* but a MythX JSON report has these fields:
*
* - description.head
* - description.tail,
* - locations
* - severity
* - swcId
* - swcTitle
*
* @param {object} issue - the MythX issue we want to convert
* @param {string} sourceCode - holds the contract code
* @param {object[]} locations - array of text-only MythX API issue locations
* @returns eslint -issue object
*/
const issue2EsLint = (issue, sourceCode, locations) => {
const swcLink = issue.swcID
? 'https://swcregistry.io/SWC-registry/docs/' + issue.swcID
: 'N/A';
const esIssue = {
mythxIssue: issue,
mythxTextLocations: locations,
sourceCode: sourceCode,
fatal: false,
ruleId: swcLink,
message: issue.description.head,
severity: mythx2Severity[issue.severity] || 1,
line: -1,
column: 0,
endLine: -1,
endCol: 0
};
let startLineCol, endLineCol;
const lineBreakPositions = decoder.getLinebreakPositions(sourceCode);
if (locations.length) {
[startLineCol, endLineCol] = textSrcEntry2lineColumn(
locations[0].sourceMap,
lineBreakPositions
);
}
if (startLineCol) {
esIssue.line = startLineCol.line;
esIssue.column = startLineCol.column;
esIssue.endLine = endLineCol.line;
esIssue.endCol = endLineCol.column;
}
return esIssue;
};
/**
* Gets the source index from the issue sourcemap
*
* @param {object} location - MythX API issue location object
* @returns {number}
*/
const getSourceIndex = location => {
const sourceMapRegex = /(\d+):(\d+):(\d+)/g;
const match = sourceMapRegex.exec(location.sourceMap);
// Ignore `-1` source index for compiler generated code
return match ? match[3] : 0;
};
/**
* Converts MythX analyze API output item to Eslint compatible object
* @param {object} report - issue item from the collection MythX analyze API output
* @param {object} data - Contains array of solidity contracts source code and the input filepath of contract
* @returns {object}
*/
const convertMythXReport2EsIssue = (report, data) => {
const { sources, functionHashes } = data;
const results = {};
/**
* Filters locations only for source files.
* Other location types are not supported to detect code.
*
* @param {object} location
*/
const textLocationFilterFn = location => (
(location.sourceType === 'solidity-file')
&&
(location.sourceFormat === 'text')
);
report.issues.forEach(issue => {
const locations = issue.locations.filter(textLocationFilterFn);
const location = locations.length ? locations[0] : undefined;
let sourceCode = '';
let sourcePath = '<unknown>';
if (location) {
const sourceList = location.sourceList || report.sourceList || [];
const sourceIndex = getSourceIndex(location);
const fileName = sourceList[sourceIndex];
if (fileName) {
sourcePath = fileName;
if (sources[fileName]) {
sourceCode = sources[fileName].content;
}
}
}
if (!results[sourcePath]) {
results[sourcePath] = {
errorCount: 0,
warningCount: 0,
fixableErrorCount: 0,
fixableWarningCount: 0,
filePath: sourcePath,
functionHashes,
sourceCode,
messages: [],
};
}
results[sourcePath].messages.push(
issue2EsLint(issue, sourceCode, locations)
);
});
for (const key in results) {
const result = results[key];
for (const { fatal, severity } of result.messages) {
if (eslintHelpers.isFatal(fatal, severity)) {
result.errorCount++;
} else {
result.warningCount++;
}
}
}
return Object.values(results);
};
const formatIssues = (data, issues) => {
const eslintIssues = issues
.map(report => convertMythXReport2EsIssue(report, data))
.reduce((acc, curr) => acc.concat(curr), []);
return eslintHelpers.getUniqueIssues(eslintIssues);
};
module.exports = {
formatIssues,
getFormatter
};