Skip to content

Commit

Permalink
fix: Remove duplicate messages
Browse files Browse the repository at this point in the history
Some findings are reported as usage of deprecated API but also as usage
of global variables. This change removes the duplicate messages and
keeps the one about the deprecated API as it provides more information.
  • Loading branch information
matz3 committed Mar 13, 2024
1 parent 4eb2f0b commit 62d709a
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 45 deletions.
51 changes: 42 additions & 9 deletions src/detectors/Reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,52 @@ export default class Reporter implements BaseReporter {
let errorCount = 0;
let warningCount = 0;
let fatalErrorCount = 0;
for (const {severity, fatal} of this.#messages) {
if (severity === LintMessageSeverity.Error) {
errorCount++;
if (fatal) {
fatalErrorCount++;
}
} else {
warningCount++;
const lineColumnMessagesMap = new Map<string, LintMessage[]>();
const messages: LintMessage[] = [];
for (const message of this.#messages) {
// Group messages by line/column so that we can deduplicate them
if (!message.line || !message.column) {
// If there is no line or column, we cannot group/deduplicate
messages.push(message);
continue;
}
const lineColumnKey = `${message.line}:${message.column}`;
let lineColumnMessages = lineColumnMessagesMap.get(lineColumnKey);
if (!lineColumnMessages) {
lineColumnMessages = [];
lineColumnMessagesMap.set(lineColumnKey, lineColumnMessages);
}
lineColumnMessages.push(message);
}

// Add deduplicated messages to the result
for (const lineColumnMessages of lineColumnMessagesMap.values()) {
// If there are multiple messages for the same line/column,
// and at least one of them is NOT a "no-globals-js" message,
// we can deduplicate the "no-globals-js" messages.
const deduplicateGlobalMessages = lineColumnMessages.length > 1 &&
lineColumnMessages.some((message) => message.ruleId !== "ui5-linter-no-globals-js");

lineColumnMessages.forEach((message) => {
if (deduplicateGlobalMessages && message.ruleId === "ui5-linter-no-globals-js") {
// Skip global messages if there are other messages for the same line/column
return;
}
if (message.severity === LintMessageSeverity.Error) {
errorCount++;
if (message.fatal) {
fatalErrorCount++;
}
} else {
warningCount++;
}
messages.push(message);
});
}

return {
filePath: this.#getFileName(),
messages: this.#messages,
messages,
coverageInfo: this.#coverageInfo,
errorCount,
warningCount,
Expand Down
10 changes: 1 addition & 9 deletions test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ Generated by [AVA](https://avajs.dev).
[
{
coverageInfo: [],
errorCount: 2,
errorCount: 1,
fatalErrorCount: 0,
filePath: 'sap.ui.jsview.js',
messages: [
Expand All @@ -809,14 +809,6 @@ Generated by [AVA](https://avajs.dev).
ruleId: 'ui5-linter-no-deprecated-api',
severity: 2,
},
{
column: 1,
fatal: undefined,
line: 1,
message: 'Access of global variable \'sap\' (sap.ui.jsview)',
ruleId: 'ui5-linter-no-globals-js',
severity: 2,
},
],
warningCount: 0,
},
Expand Down
Binary file modified test/lib/linter/rules/snapshots/NoDeprecatedApi.ts.snap
Binary file not shown.
30 changes: 3 additions & 27 deletions test/lib/linter/snapshots/linter.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ Generated by [AVA](https://avajs.dev).
},
{
coverageInfo: [],
errorCount: 3,
errorCount: 2,
fatalErrorCount: 0,
filePath: 'webapp/test/integration/opaTests.qunit.js',
messages: [
Expand All @@ -577,14 +577,6 @@ Generated by [AVA](https://avajs.dev).
ruleId: 'ui5-linter-no-deprecated-api',
severity: 2,
},
{
column: 1,
fatal: undefined,
line: 4,
message: 'Access of global variable \'sap\' (sap.ui.getCore)',
ruleId: 'ui5-linter-no-globals-js',
severity: 2,
},
],
warningCount: 0,
},
Expand Down Expand Up @@ -678,7 +670,7 @@ Generated by [AVA](https://avajs.dev).
},
{
coverageInfo: [],
errorCount: 3,
errorCount: 2,
fatalErrorCount: 0,
filePath: 'webapp/test/unit/unitTests.qunit.js',
messages: [
Expand All @@ -700,14 +692,6 @@ Generated by [AVA](https://avajs.dev).
ruleId: 'ui5-linter-no-deprecated-api',
severity: 2,
},
{
column: 1,
fatal: undefined,
line: 6,
message: 'Access of global variable \'sap\' (sap.ui.getCore)',
ruleId: 'ui5-linter-no-globals-js',
severity: 2,
},
],
warningCount: 0,
},
Expand Down Expand Up @@ -923,7 +907,7 @@ Generated by [AVA](https://avajs.dev).
},
{
coverageInfo: [],
errorCount: 3,
errorCount: 2,
fatalErrorCount: 0,
filePath: 'src/main/js/library.js',
messages: [
Expand All @@ -945,14 +929,6 @@ Generated by [AVA](https://avajs.dev).
ruleId: 'ui5-linter-no-deprecated-api',
severity: 2,
},
{
column: 2,
fatal: undefined,
line: 15,
message: 'Access of global variable \'sap\' (sap.ui.getCore)',
ruleId: 'ui5-linter-no-globals-js',
severity: 2,
},
],
warningCount: 0,
},
Expand Down
Binary file modified test/lib/linter/snapshots/linter.ts.snap
Binary file not shown.

0 comments on commit 62d709a

Please sign in to comment.