-
Notifications
You must be signed in to change notification settings - Fork 889
Sort failures by failure's line and character #3345
Conversation
Thanks for your interest in palantir/tslint, @eggggger! Before we can accept your pull request, you need to sign our contributor license agreement - just visit https://cla.palantir.com/ and follow the instructions. Once you sign, I'll automatically update this pull request. |
7351684
to
f16d4d3
Compare
f16d4d3
to
094f807
Compare
src/linter.ts
Outdated
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 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.
src/linter.ts
Outdated
@@ -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); |
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
and code-frame
.
src/linter.ts
Outdated
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 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
src/linter.ts
Outdated
}); | ||
|
||
return sortedFailures; | ||
} |
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.
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();
}
@ajafff Thank you for your suggestion,and is it better to move sorting to |
src/formatters/codeFrameFormatter.ts
Outdated
@@ -97,4 +98,8 @@ export class Formatter extends AbstractFormatter { | |||
|
|||
return `${outputLines.join("\n")}\n`; | |||
} | |||
|
|||
public sortFailures(failures: RuleFailure[]): RuleFailure[] { |
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.
these methods don't need to be public
src/formatters/codeFrameFormatter.ts
Outdated
@@ -97,4 +98,8 @@ export class Formatter extends AbstractFormatter { | |||
|
|||
return `${outputLines.join("\n")}\n`; | |||
} | |||
|
|||
public sortFailures(failures: RuleFailure[]): RuleFailure[] { | |||
return failures.slice().sort(RuleFailure.compare); |
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.
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.
@eggggger You're almost done. CI fails due to linting errors:
you can also run linting locally with |
Thanks @eggggger |
PR checklist
Overview of change:
Is there anything you'd like reviewers to focus on?
CHANGELOG.md entry:
[enhancement]
Sort failures by line and character for formatters