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

Conversation

eggggger
Copy link
Contributor

PR checklist

  • Addresses an existing issue: #3335
  • New feature, bugfix, or enhancement
    • Includes tests
  • Documentation update

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

@palantirtech
Copy link
Member

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.

@eggggger eggggger changed the title Sort failures by failure\'s line and character Sort failures by failure's line and character Oct 19, 2017
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);
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.

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);
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.

src/linter.ts Outdated
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

src/linter.ts Outdated
});

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

@eggggger
Copy link
Contributor Author

eggggger commented Oct 19, 2017

@ajafff Thank you for your suggestion,and is it better to move sorting to AbstractFormatter?

@@ -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

@@ -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);
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.

@ajafff
Copy link
Contributor

ajafff commented Oct 19, 2017

@eggggger You're almost done. CI fails due to linting errors:

/home/ubuntu/tslint/src/formatters/stylishFormatter.ts
ERROR: 42:47  semicolon        Missing semicolon

/home/ubuntu/tslint/src/language/rule/rule.ts
ERROR: 261:5  member-ordering  Declaration of public static method not allowed after declaration of public instance method. Instead, this should come after private instance fields.

you can also run linting locally with yarn lint

@ajafff ajafff merged commit f1e0de2 into palantir:master Oct 20, 2017
@ajafff
Copy link
Contributor

ajafff commented Oct 20, 2017

Thanks @eggggger

HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants