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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/formatters/codeFrameFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export class Formatter extends AbstractFormatter {
if (typeof failures[0] === "undefined") {
return "\n";
}
failures = this.sortFailures(failures);

const outputLines: string[] = [];

Expand Down Expand Up @@ -97,4 +98,8 @@ export class Formatter extends AbstractFormatter {

return `${outputLines.join("\n")}\n`;
}

public sortFailures(failures: RuleFailure[]): RuleFailure[] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these methods don't need to be public

return failures.slice().sort(RuleFailure.compare);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally, I would not write a method for this simple statement. If you want to keep the method, it makes sense to move it to AbstractFormatter as you already noted. Works for me either way.

}
}
5 changes: 5 additions & 0 deletions src/formatters/proseFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class Formatter extends AbstractFormatter {
if (failures.length === 0 && (fixes === undefined || fixes.length === 0)) {
return "\n";
}
failures = this.sortFailures(failures);

const fixLines: string[] = [];
if (fixes !== undefined) {
Expand Down Expand Up @@ -60,4 +61,8 @@ export class Formatter extends AbstractFormatter {

return `${fixLines.concat(errorLines).join("\n")}\n`;
}

public sortFailures(failures: RuleFailure[]): RuleFailure[] {
return failures.slice().sort(RuleFailure.compare);
}
}
5 changes: 5 additions & 0 deletions src/formatters/stylishFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class Formatter extends AbstractFormatter {
/* tslint:enable:object-literal-sort-keys */

public format(failures: RuleFailure[]): string {
failures = this.sortFailures(failures)
const outputLines = this.mapToMessages(failures);

// Removes initial blank line
Expand All @@ -49,6 +50,10 @@ export class Formatter extends AbstractFormatter {
return `${outputLines.join("\n")}\n`;
}

public sortFailures(failures: RuleFailure[]): RuleFailure[] {
return failures.slice().sort(RuleFailure.compare);
}

private mapToMessages(failures: RuleFailure[]): string[] {
if (failures.length === 0) {
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/formatters/verboseFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,14 @@ export class Formatter extends AbstractFormatter {
/* tslint:enable:object-literal-sort-keys */

public format(failures: RuleFailure[]): string {
failures = this.sortFailures(failures);
return `${this.mapToMessages(failures).join("\n")}\n`;
}

public sortFailures(failures: RuleFailure[]): RuleFailure[] {
return failures.slice().sort(RuleFailure.compare);
}

private mapToMessages(failures: RuleFailure[]): string[] {
return failures.map((failure: RuleFailure) => {
const fileName = failure.getFileName();
Expand Down
7 changes: 7 additions & 0 deletions src/language/rule/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,13 @@ export class RuleFailure {
this.ruleSeverity = "error";
}

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();
}

public getFileName() {
return this.fileName;
}
Expand Down