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

New: function-paren-newline rule (fixes #6074) #8102

Merged
merged 4 commits into from Sep 1, 2017

Conversation

not-an-aardvark
Copy link
Member

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

[x] New rule (see #6074)

What changes did you make? (Give an overview)

This implements the function-paren-newline rule as described in #6074, and enables it on the ESLint codebase for dogfooding. The rule applies to all function parameters, as well as CallExpression/NewExpression arguments.

I decided to use multiline as the default, instead of always as described in #6074, because always seems like a very unusual style.

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

  • I'm unsure of the correct behavior for the multiline option. There are two possible behaviors that I can think of, which are subtly different:

    1. Require newlines if there are linebreaks anywhere within the parameters/arguments.
    2. Require newlines if there are any linebreaks between two parameters/arguments.

    If option 1 is used, the following code is reported, because the arguments span multiple lines. I think this is very undesirable.

    array.map(function(x) {
      return x + 2;
    });

    If option 2 is used, the rule reports code like this, which is slightly undesirable:

    return Boolean(
      foo && bar &&
      baz && qux
    );

    Currently, option 2 is implemented, but I'm concerned that it makes code less readable in some cases.

  • The rule can produce some very long lines. For example, the following code gets reported:

    foo(
      bar("baz", qux(
        new Foo()
      ))
    );
    
    // gets fixed to
    
    foo(bar("baz", qux(new Foo())))

    I'm concerned that this can make the code less readable.

  • Due to Modify rules for handling indentation of closing parentheses #7522, the autofixer for this rule can be difficult to use. For example:

    function foo() {
        foo(
            bar,
            baz);
    }
    
    // gets fixed to:
    
    function foo() {
        foo(
            bar,
            baz
    );
    }

    Since the indent rule doesn't report or fix the indentation of the closing ), it usually ends up in the wrong place. The indent rewrite fixes this issue, so maybe we should wait until 4.0.0 before releasing this rule.

@not-an-aardvark not-an-aardvark added accepted There is consensus among the team that this change meets the criteria for inclusion do not merge This pull request should not be merged yet feature This change adds a new feature to ESLint rule Relates to ESLint's core rules labels Feb 19, 2017
@not-an-aardvark not-an-aardvark added this to the JSCS Compatibility milestone Feb 19, 2017
@mention-bot
Copy link

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

@eslintbot
Copy link

LGTM

@platinumazure
Copy link
Member

Do we need an autofix in the first release of this rule? We could put a fixer in for 4.0 but release a usable rule in a minor release, no?

@not-an-aardvark not-an-aardvark removed the do not merge This pull request should not be merged yet label Apr 7, 2017
@kaicataldo
Copy link
Member

Looks like we have some merge conflicts here. No need to rush this though since it doesn't have to be released in a major version.

@not-an-aardvark not-an-aardvark added do not merge This pull request should not be merged yet needs bikeshedding Minor details about this change need to be discussed labels May 5, 2017
ilyavolodin
ilyavolodin previously approved these changes May 5, 2017
@@ -0,0 +1,225 @@
# enforce consistent line breaks inside function parentheses (function-paren-newline)

(fixable) The `--fix` option on the [command line](../user-guide/command-line-interface#fix) automatically fixes problems reported by this rule.
Copy link
Member

Choose a reason for hiding this comment

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

We don't need this anymore.

@not-an-aardvark
Copy link
Member Author

Does anyone have any thoughts on how to address the usability issues described in the comment at the top?

@ilyavolodin
Copy link
Member

I think the only way to fix this usability issue is to introduce yet another option. While option 2 is reasonable, I personally prefer option 1. I also see an issue of requiring closing brace to be on it's own line (I personally prefer it to be on the same line as last argument).

@ebramanti
Copy link

What is the status on this?

@not-an-aardvark
Copy link
Member Author

I've been thinking about this more. What if we added a selector option to the rule to allow greater configurability? Would that solve all the use cases we need? For example, maybe people could use that to enforce parens for nested functions.

@jainanshul
Copy link

Is there an update on this issue?

@platinumazure
Copy link
Member

Could we maybe add a consistent option?

  • Wouldn't enforce having or not having newlines inside function parens, in general, but
  • A newline is required after an opening function paren iff a newline is present before a closing function paren

I think that would be a sensible default, as well.

Also, does this apply to function calls, function declarations, or both? If both, is it worth creating rules for (or supporting options in this rule for) different behaviors for function calls vs function declarations?

@not-an-aardvark
Copy link
Member Author

Could we maybe add a consistent option?

I agree, adding a consistent option sounds like a good idea.

I think we should make consistent a string option like always and never, rather than adding it to the object with minItems. We added a similar thing to the object for object-curly-newline, and ended up with a confusing schema where one option affects the behavior of another.

Also, does this apply to function calls, function declarations, or both?

It applies to all "function parens" (i.e. the parentheses around the arguments in CallExpressions and NewExpressions, and the parentheses around the parameters in FunctionDeclarations, FunctionExpressions, and ArrowFunctionExpressions).

@platinumazure
Copy link
Member

I think we should make consistent a string option like always and never, rather than adding it to the object with minItems.

👍, I had that in mind as well.

It applies to all "function parens" (i.e. the parentheses around the arguments in CallExpressions and NewExpressions, and the parentheses around the parameters in FunctionDeclarations, FunctionExpressions, and ArrowFunctionExpressions).

I'm okay with this for now, but does it not seem likely that we'll get a request to support different behavior? (But that can certainly be added later as a non-breaking change.)

@eslintbot
Copy link

LGTM

@not-an-aardvark not-an-aardvark removed do not merge This pull request should not be merged yet needs bikeshedding Minor details about this change need to be discussed labels Aug 20, 2017
@eslintbot
Copy link

LGTM

@not-an-aardvark not-an-aardvark dismissed ilyavolodin’s stale review August 24, 2017 01:16

Dismissing this review because a lot of time has passed and the proposal has changed a bit.

@not-an-aardvark not-an-aardvark requested a review from a team August 24, 2017 01:22
@eslintbot
Copy link

LGTM

@not-an-aardvark
Copy link
Member Author

@eslint/eslint-team This should be ready to merge after review. Sorry about the long bikeshedding delay.

@eslintbot
Copy link

LGTM

@eslintbot
Copy link

LGTM

@ilyavolodin ilyavolodin merged commit 0e09973 into master Sep 1, 2017
@ilyavolodin ilyavolodin deleted the function-paren-newline branch September 1, 2017 22:08
@jainanshul
Copy link

When do you guys plan to release a new eslint version with this feature?

@not-an-aardvark
Copy link
Member Author

An hour ago

@jainanshul
Copy link

jainanshul commented Sep 4, 2017

multiline rule doesn't seem to like if we break an only function parameter in a new line.

function test(a: string) {
  console.log(a);
}

test(
  'hello',
);

In some cases the only argument is a really long argument and it seems nicer, being able to break in on the next line. Is there a way to allow 1 function parameter to be on a new line as part of `multiline` rule?

@jstephenson
Copy link

Is there a proposed solution for @jainanshul's scenario above?

@not-an-aardvark
Copy link
Member Author

No. Currently, the rule is working as designed, but if you'd like to propose an enhancement to this rule, please create a new issue.

@eslint-deprecated eslint-deprecated bot locked and limited conversation to collaborators Mar 1, 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 Mar 1, 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 feature This change adds a new feature to ESLint rule Relates to ESLint's core rules
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants