Skip to content
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 4 commits into from Oct 20, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/linter.ts
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);
Copy link
Contributor

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 and code-frame.

const fixes = this.sortFailures(this.fixes);
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
Copy link
Contributor

Choose a reason for hiding this comment

The 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 class RuleFailure in src/language/rule/rule.ts:

    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);
Expand Down