-
Notifications
You must be signed in to change notification settings - Fork 885
object-literal-sort-keys: Allow grouping of object properties via additional blank lines #3191
Conversation
Thanks for your interest in palantir/tslint, @Merott! 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. |
@@ -184,6 +193,10 @@ function walk(ctx: Lint.WalkContext<Options>, checker?: ts.TypeChecker): void { | |||
} | |||
} | |||
|
|||
function hasNewLineBefore(sourceFile: ts.SourceFile, element: ts.ObjectLiteralElement) { | |||
return /\r?\n\r?\n/.test(sourceFile.text.slice(element.getFullStart(), element.getStart(sourceFile))); |
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 will also match for blank lines inside comments, for example:
let foo = {
foo: 1,
/* I need some space to express myself
and here the comment continues */
bar: 2,
};
You can have a look at newline-before-return
to see how I would check for a blank line before, between or after comments (but not inside).
https://github.com/palantir/tslint/blob/master/src/rules/newlineBeforeReturnRule.ts#L62-L81
|
||
c: 3, | ||
e: 5, | ||
}; |
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.
please add one of these test cases to an existing test to check if the new behavior is not enabled without the option
ae100cb
to
03061d3
Compare
Thank you @ajafff - I've pushed up an update based on your feedback. |
This rule change makes sense to me. This would work similar to |
It would make me personally happy to have this behaviour by default, similar to I should also mention that |
Making this the default behavior is not a breaking change. Code that passes now will also pass after this change. |
@ajafff OK - I will take out the option and update the PR accordingly. |
03061d3
to
742a7a3
Compare
Ready for a re-review 👍🏻. |
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.
Code looks good with one suggestion regarding performance
@@ -137,6 +142,10 @@ function walk(ctx: Lint.WalkContext<Options>, checker?: ts.TypeChecker): void { | |||
break; | |||
case ts.SyntaxKind.ShorthandPropertyAssignment: | |||
case ts.SyntaxKind.PropertyAssignment: | |||
if (hasBlankLineBefore(ctx.sourceFile, property)) { |
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 can be pretty expensive when checked for each property. I'd prefer to only check this right before adding a failure
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.
Done.
742a7a3
to
8430d73
Compare
Thanks @Merott |
PR checklist
Overview of change:
Changes the behaviour of
object-literal-sort-keys
when using alphabetical ordering, and allows object keys to be grouped together separated with blank lines.CHANGELOG.md entry:
[enhancement]
object-literal-sort-keys
: allow grouping of object properties via additional blank lines when using alphabetical ordering.