Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update: add indent options for array and object literals (fixes #7473) #7681

Merged
merged 2 commits into from Dec 8, 2016

Conversation

not-an-aardvark
Copy link
Member

What is the purpose of this pull request? (put an "X" next to item)

[x] Changes an existing rule

See #7473

What changes did you make? (Give an overview)

This adds an ArrayExpression and ObjectExpression option to the indent rule, as described in #7473.

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

Nothing in particular.

@not-an-aardvark not-an-aardvark added accepted There is consensus among the team that this change meets the criteria for inclusion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules labels Nov 30, 2016
@eslintbot
Copy link

LGTM

@mention-bot
Copy link

@not-an-aardvark, thanks for your PR! By analyzing the history of the files in this pull request, we identified @vitorbal, @alecharmon and @gyandeeps to be potential reviewers.

} else if (parent.type === "ObjectExpression" || parent.type === "ArrayExpression") {
nodeIndent += options[parent.type] * indentSize;
} else if (parent.type === "CallExpression" || parent.type === "NewExpression") {
nodeIndent += (typeof options.CallExpression.arguments === "number" ? options.CallExpression.arguments : 1) * indentSize;
Copy link
Member

Choose a reason for hiding this comment

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

Incidentally, is typeof checks unnecessary for ObjectExpression differently to CallExpression?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, because CallExpression indentation is ignored by default, so the default value of options.CallExpression.arguments is null. However, ArrayExpression and ObjectExpression indentations are not ignored by default, so the default values of options.ArrayExpression and options.ObjectExpression are 1.

Copy link
Member

@mysticatea mysticatea Dec 7, 2016

Choose a reason for hiding this comment

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

I got it. Thank you for the explanation.
Actually, "first" * indentSize was in mind and I had forgotten that options.CallExpression.arguments can be null.

Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

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

Thank you for this PR!
But, there are cases that it clashes.

}

if (options[node.type] === "first") {
elementsIndent = elements[0].loc.start.column;
Copy link
Member

Choose a reason for hiding this comment

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

This seems to throw TypeError if there are empty objects/arrays.

@eslintbot
Copy link

LGTM

Copy link
Member

@mysticatea mysticatea left a comment

Choose a reason for hiding this comment

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

LGTM, thank you!

@larionov
Copy link

larionov commented Dec 7, 2016

I tried to make it work with with indentation like this but had no success. Do you think this is even possible?

export default angular.module('foo', [
        module1,
        module2,
        module3
    ])
    .directive('bar', barDirective);

I have a rule set like this, and tried different values of ArrayExpression and MemberExpression, no luck.

"indent": [2, 4, { "SwitchCase": 1, "MemberExpression": 1, "ArrayExpression": 1}]

@not-an-aardvark
Copy link
Member Author

@larionov The correct indentation for that case would be:

export default angular.module('foo', [
        module1,
        module2,
        module3
])
    .directive('bar', barDirective);

or alternatively you could put the .directive call on the same line:

export default angular.module('foo', [
        module1,
        module2,
        module3
]).directive('bar', barDirective);

Since the array after foo is opened on a line with an indentation of 0, it should also be closed on a line with an indentation of 0.

@larionov
Copy link

larionov commented Dec 7, 2016

@not-an-aardvark I use http://jsbeautifier.org/ plugin to format my code automatically and it formats this code like in my first example, so I thought that was somewhat generally accepted. But anyway I already formatted all the code like in your first example, not sure how will second one will go with huge lists of directives.

@mysticatea
Copy link
Member

Just linked to #4081 since it's similar topic.

@not-an-aardvark not-an-aardvark merged commit b921d1f into master Dec 8, 2016
@not-an-aardvark not-an-aardvark deleted the array-object-indent branch December 8, 2016 16:19
@NicholasBoll
Copy link

This PR seems to have broken ObjectExpression default. It used to be 1 and now it is "first". So either the documentation is wrong (no longer default 1) or the code is wrong. We had our CI suddenly fail with indentation errors with object expressions shortly after the 3.12.0 release.

Our configuration for indent is:

"indent" : [2, 2, {"SwitchCase": 1}],

@gyandeeps
Copy link
Member

@NicholasBoll Can you please open a new issue with what u r seeing. Thanks

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Feb 6, 2018
@eslint-deprecated eslint-deprecated bot added the archived due to age This issue has been archived; please open a new issue for any further discussion label Feb 6, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
accepted There is consensus among the team that this change meets the criteria for inclusion archived due to age This issue has been archived; please open a new issue for any further discussion enhancement This change enhances an existing feature of ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants