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

object-literal-sort-keys: Allow grouping of object properties via additional blank lines #3191

Merged
merged 1 commit into from Sep 12, 2017

Conversation

Merott
Copy link
Contributor

@Merott Merott commented Aug 31, 2017

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.

@palantirtech
Copy link
Member

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)));
Copy link
Contributor

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,
};
Copy link
Contributor

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

@Merott Merott force-pushed the obj-literal-sort-grouping branch 3 times, most recently from ae100cb to 03061d3 Compare September 1, 2017 16:33
@Merott
Copy link
Contributor Author

Merott commented Sep 1, 2017

Thank you @ajafff - I've pushed up an update based on your feedback.

@adidahiya
Copy link
Contributor

This rule change makes sense to me. This would work similar to ordered-imports which allows import groups separated by newlines. In fact we might just want to make this the default behavior and not keep it behind a rule option, to be consistent with import statement linting behavior. Thoughts @ajafff @Merott?

@Merott
Copy link
Contributor Author

Merott commented Sep 2, 2017

It would make me personally happy to have this behaviour by default, similar toordered-imports as you mentioned. However, given that this behaviour could be considered a breaking change if it were to be applied by default, I thought about introducing it as an option instead.

I should also mention that ordered-imports doesn't support grouping with new lines within each import statement. Thet'd be closer in terms of syntax to what my proposed rule option here does, and is something that I'd also like to have.

@ajafff
Copy link
Contributor

ajafff commented Sep 2, 2017

Making this the default behavior is not a breaking change. Code that passes now will also pass after this change.
I think this makes sense to be the default.

@Merott
Copy link
Contributor Author

Merott commented Sep 2, 2017

@ajafff OK - I will take out the option and update the PR accordingly.

@Merott Merott changed the title object-literal-sort-keys: Add "allow-newline-grouping" option object-literal-sort-keys: Allow grouping of object properties via additional blank lines Sep 2, 2017
@Merott
Copy link
Contributor Author

Merott commented Sep 2, 2017

Ready for a re-review 👍🏻.

Copy link
Contributor

@ajafff ajafff left a 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)) {
Copy link
Contributor

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@ajafff ajafff merged commit 6c10500 into palantir:master Sep 12, 2017
@ajafff
Copy link
Contributor

ajafff commented Sep 12, 2017

Thanks @Merott

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

4 participants