This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
Sort failures by failure's line and character #3345
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,19 +131,40 @@ class Linter { | |
throw new Error(`formatter '${formatterName}' not found`); | ||
} | ||
|
||
const output = formatter.format(this.failures, this.fixes); | ||
// Sort failures by failure's line and character. | ||
const failures = this.sortFailures(this.failures); | ||
const fixes = this.sortFailures(this.fixes); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the fixes are actually never really used by a formatter, so they don't need to be sorted. |
||
const output = formatter.format(failures, fixes); | ||
|
||
const errorCount = this.failures.filter((failure) => failure.getRuleSeverity() === "error").length; | ||
return { | ||
errorCount, | ||
failures: this.failures, | ||
fixes: this.fixes, | ||
failures, | ||
fixes, | ||
format: formatterName, | ||
output, | ||
warningCount: this.failures.length - errorCount, | ||
}; | ||
} | ||
|
||
public sortFailures(failures: RuleFailure[]): RuleFailure[] { | ||
let sortedFailures: RuleFailure[] = []; | ||
|
||
const fileFailuresMap = createMultiMap(failures, (failure) => [failure.getFileName(), failure]); | ||
fileFailuresMap.forEach((fileFailures) => { | ||
sortedFailures = sortedFailures.concat(fileFailures.sort((failureA, failureB) => { | ||
const failureALineAndCharacter = failureA.getStartPosition().getLineAndCharacter(); | ||
const failureBLineAndCharacter = failureB.getStartPosition().getLineAndCharacter(); | ||
if (failureALineAndCharacter.line === failureBLineAndCharacter.line) { | ||
return failureALineAndCharacter.character - failureBLineAndCharacter.character; | ||
} | ||
return failureALineAndCharacter.line - failureBLineAndCharacter.line; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of comparing line and character, you can simply compare the position |
||
})); | ||
}); | ||
|
||
return sortedFailures; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As described above, I'd like to move this to the formatters. To make the compare logic reusable, consider adding it as static method to public static compare(a: RuleFailure, b: RuleFailure): number {
if (a.fileName !== b.fileName) {
return a.fileName < b.fileName ? -1 : 1;
}
return a.startPosition.getPosition() - b.startPosition.getPosition();
} |
||
|
||
private getAllFailures(sourceFile: ts.SourceFile, enabledRules: IRule[]): RuleFailure[] { | ||
const failures = flatMap(enabledRules, (rule) => this.applyRule(rule, sourceFile)); | ||
return removeDisabledFailures(sourceFile, failures); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd suggest to move the sorting of failures to each formatter that needs it. Formatters for machine consumption shouldn't rely on any order.
That means there are 4 formatters affected:
prose
,verbose
,stylish
andcode-frame
.