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

Implemented fixer for member-ordering and added corresponding tests. #3935

Merged
merged 3 commits into from
Jun 26, 2018
Merged

Implemented fixer for member-ordering and added corresponding tests. #3935

merged 3 commits into from
Jun 26, 2018

Conversation

NaridaL
Copy link
Contributor

@NaridaL NaridaL commented Jun 1, 2018

PR checklist

Overview of change:

New fixer. See checkMembers JSDoc for implementation details.

Is there anything you'd like reviewers to focus on?

test/rules/member-ordering/alphabetize/ is failing, but the diff isn't showing anything. The .fix doesn't exactly mirror the error message in some cases, as many configurations have multiple correct orders; that shouldn't be an issue.

I'm currently using node.getFullStart() and node getEnd() to find the node source, however this doesn't match the typescript trivia ownership rules (see failing test test/rules/member-ordering/fix-trivia). I've seen ts.getLeadingCommentRanges, but that starts at the beginning of the comment and whitespace before the leading comments would get lost. It seems like the best way would be to split at the first line break after a node, but I haven't seen any corresponding utility functions. Anyone have any advice on the matter or a reference to another rule which does something similar?

CHANGELOG.md entry: [new-fixer] member-ordering

@palantirtech
Copy link
Member

Thanks for your interest in palantir/tslint, @NaridaL! 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.

@NaridaL
Copy link
Contributor Author

NaridaL commented Jun 1, 2018

image

@NaridaL
Copy link
Contributor Author

NaridaL commented Jun 10, 2018

Anyone available to review this PR?

@listepo-alterpost
Copy link
Contributor

@ajafff ?

Copy link

@giladgray giladgray left a comment

Choose a reason for hiding this comment

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

a (mostly) green build is a good indication that your code is ready for review.

const sortedMembers = members.slice().sort((a, b) => {
// first, sort by member rank
// const ai = info(a);
// const bi = info(b);

Choose a reason for hiding this comment

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

no commented code

/**
* Array.prototype.findIndex, but the last index.
*/
function arrayFindLastIndex<T>(

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, that doesn't support a custom predicate.

}

/**
* Applies a Replacement to a part of the text which starts at offset.

Choose a reason for hiding this comment

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

this comment does not match the impl. it returns a string??

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's just a slightly modified version of Replacement.apply https://github.com/palantir/tslint/blob/master/src/language/rule/rule.ts#L204, which also returns a string.

@@ -250,7 +270,8 @@ class MemberOrderingWalker extends Lint.AbstractWalker<Options> {
: "at the beginning of the class/interface";
const errorLine1 = `Declaration of ${nodeType} not allowed after declaration of ${prevNodeType}. ` +
`Instead, this should come ${locationHint}.`;
this.addFailureAtNode(member, errorLine1);
this.addFailureAtNode(member, errorLine1, []);

Choose a reason for hiding this comment

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

why the addition of empty array? seems unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The empty array is needed so the Replacement can later be pushed (L241). The field itself is readonly. I copied this tidbit from the ordered-imports rule. https://github.com/palantir/tslint/blob/master/src/rules/orderedImportsRule.ts#L265

Choose a reason for hiding this comment

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

a code comment would clarify this for the future.

@NaridaL
Copy link
Contributor Author

NaridaL commented Jun 14, 2018

@giladgray Thanks for the review, do you have anything to add re: the two points I mentioned in the first post?

@NaridaL
Copy link
Contributor Author

NaridaL commented Jun 15, 2018

test/rules/member-ordering/alphabetize/ had a switched empty line and non-empty line, which wasn't showing up in the diff.

I went ahead and implemented a function to figure out the next newline after a node.

This should be mergeable now.

@giladgray
Copy link

@NaridaL merge latest master to fix failing test.

Copy link

@giladgray giladgray left a comment

Choose a reason for hiding this comment

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

almost there.

@@ -250,7 +270,8 @@ class MemberOrderingWalker extends Lint.AbstractWalker<Options> {
: "at the beginning of the class/interface";
const errorLine1 = `Declaration of ${nodeType} not allowed after declaration of ${prevNodeType}. ` +
`Instead, this should come ${locationHint}.`;
this.addFailureAtNode(member, errorLine1);
this.addFailureAtNode(member, errorLine1, []);

Choose a reason for hiding this comment

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

a code comment would clarify this for the future.

@@ -272,6 +294,54 @@ class MemberOrderingWalker extends Lint.AbstractWalker<Options> {
prevRank = rank;
}
}
if (failureExists) {
// const info = (x: Node)=>[x.name && nameString(x.name), this.memberRank(x), members.indexOf(x)]

Choose a reason for hiding this comment

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

remove commented code

// if yes, remove it from the list so that we do not return overlapping Replacements
const fixIndex = arrayFindLastIndex(
this.fixes,
([, r]) => r.start >= start && r.start + r.length <= end,

Choose a reason for hiding this comment

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

🙊 didn't know you could destructure an array and skip indices!!

@NaridaL
Copy link
Contributor Author

NaridaL commented Jun 18, 2018

I rebased and fixed the comments.

Copy link

@giladgray giladgray left a comment

Choose a reason for hiding this comment

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

@NaridaL this is awesome stuff! great job on tackling such a tricky feature.

@NaridaL
Copy link
Contributor Author

NaridaL commented Jun 18, 2018

@giladgray Are you sure this fixes #3965 ?

@giladgray
Copy link

giladgray commented Jun 26, 2018

@NaridaL you wrote this... are you sure that it fixes it?

edit: ah gotcha, changed language in description.

@NaridaL
Copy link
Contributor Author

NaridaL commented Jun 26, 2018

This doesn't actually do anything for #3965 . #3984 does, however.

@giladgray giladgray merged commit f82ef3c into palantir:master Jun 26, 2018
@NaridaL NaridaL deleted the member-ordering-fixer branch June 26, 2018 21:06
pablobirukov pushed a commit to evolution-gaming/tslint that referenced this pull request Jul 4, 2018
…alantir#3935)

* Implemented fixer for member-ordering and added corresponding tests.

* Improved node boundary calculation so trivia remains attached to the correct nodes.

* minor comment fixes.
@Justinidlerz
Copy link

Justinidlerz commented Jul 13, 2018

Wow, I'm look forward to the member-ordering feature long time, Could publish an new npm beta version? I want to use it now.

@roman-petrov
Copy link

@Justinidlerz same for our team. Member ordering is hard to fix manually in a big project. @NaridaL, thank you for great feature! looking forward to see next release soon

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.

Feature Request: correct member-ordering issues with the --fix flag
6 participants