-
Notifications
You must be signed in to change notification settings - Fork 889
Implemented fixer for member-ordering and added corresponding tests. #3935
Conversation
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. |
Anyone available to review this PR? |
@ajafff ? |
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.
a (mostly) green build is a good indication that your code is ready for review.
src/rules/memberOrderingRule.ts
Outdated
const sortedMembers = members.slice().sort((a, b) => { | ||
// first, sort by member rank | ||
// const ai = info(a); | ||
// const bi = info(b); |
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.
no commented code
/** | ||
* Array.prototype.findIndex, but the last index. | ||
*/ | ||
function arrayFindLastIndex<T>( |
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.
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.
Yeah, that doesn't support a custom predicate.
} | ||
|
||
/** | ||
* Applies a Replacement to a part of the text which starts at offset. |
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.
this comment does not match the impl. it returns a string??
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.
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.
src/rules/memberOrderingRule.ts
Outdated
@@ -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, []); |
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.
why the addition of empty array? seems unnecessary
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 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
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.
a code comment would clarify this for the future.
@giladgray Thanks for the review, do you have anything to add re: the two points I mentioned in the first post? |
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. |
@NaridaL merge latest master to fix failing test. |
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.
almost there.
src/rules/memberOrderingRule.ts
Outdated
@@ -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, []); |
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.
a code comment would clarify this for the future.
src/rules/memberOrderingRule.ts
Outdated
@@ -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)] |
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.
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, |
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.
🙊 didn't know you could destructure an array and skip indices!!
I rebased and fixed the comments. |
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.
@NaridaL this is awesome stuff! great job on tackling such a tricky feature.
@giladgray Are you sure this fixes #3965 ? |
@NaridaL edit: ah gotcha, changed language in description. |
…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.
Wow, I'm look forward to the |
@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 |
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