Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
Sort failures by failure's line and character
Browse files Browse the repository at this point in the history
  • Loading branch information
eggggger committed Oct 19, 2017
1 parent 7719167 commit 094f807
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/linter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
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;
}));
});

return sortedFailures;
}

private getAllFailures(sourceFile: ts.SourceFile, enabledRules: IRule[]): RuleFailure[] {
const failures = flatMap(enabledRules, (rule) => this.applyRule(rule, sourceFile));
return removeDisabledFailures(sourceFile, failures);
Expand Down

0 comments on commit 094f807

Please sign in to comment.