question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule Proposal: newline-in-function-parens

See original GitHub issue

EDIT: I updated options in https://github.com/eslint/eslint/issues/6074#issuecomment-217124523


From validateAlignedFunctionParameters.

This will require/disallow line breaks after open parentheses of function parameters and before close parentheses of function parameters.

{
    "newline-in-function-parens": ["error", "always" or "never" or "multiline" or {"minItems": 2}]
}
  • "always" (default) - Requires line breaks always.
  • "never" - Disallows line breaks always.
  • "multiline" - Requires line breaks when the content is multiline. Otherwise, disallows line breaks.
  • {"minItems": <integer>} - Requires line breaks when the number of items is more than the specific integer. Otherwise, disallows line breaks.

Valid:

/*eslint newline-in-function-parens: ["error", "always"]*/

function foo(
) {}
function foo(
    a
) {}
function foo(
    a, b
) {}
function foo(
    a,
    b
) {}
function foo(
    a = function() {
        dosomething();
    }
) {}
/*eslint newline-in-function-parens: ["error", "never"]*/

function foo() {}
function foo(a) {}
function foo(a, b) {}
function foo(a,
    b) {}
function foo(a = function() {
    dosomething();
}) {}
/*eslint newline-in-function-parens: ["error", "multiline"]*/

function foo() {}
function foo(a) {}
function foo(a, b) {}
function foo(
    a,
    b
) {}
function foo(
    a = function() {
        dosomething();
    }
) {}
/*eslint newline-in-function-parens: ["error", {"minItems": 2}]*/

function foo() {}
function foo(a) {}
function foo(
    a, b
) {}
function foo(
    a,
    b
) {}
function foo(a = function() {
    dosomething();
}) {}

Invalid:

/*eslint newline-in-function-parens: ["error", "always"]*/

function foo() {}
function foo(a) {}
function foo(a, b) {}
function foo(a,
    b) {}
function foo(a = function() {
    dosomething();
}) {}
/*eslint newline-in-function-parens: ["error", "never"]*/

function foo(
) {}
function foo(
    a
) {}
function foo(
    a, b
) {}
function foo(
    a,
    b
) {}
function foo(
    a = function() {
        dosomething();
    }
) {}
/*eslint newline-in-function-parens: ["error", "multiline"]*/

function foo(
) {}
function foo(
    a
) {}
function foo(
    a, b
) {}
function foo(a,
    b) {}
function foo(a = function() {
    dosomething();
}) {}
/*eslint newline-in-function-parens: ["error", {"minItems": 2}]*/

function foo(
) {}
function foo(
    a
) {}
function foo(a, b) {}
function foo(a,
    b) {}
function foo(
    a = function() {
        dosomething();
    }
) {}

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:13
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

3reactions
jesstelfordcommented, Nov 22, 2016

Does this also cover newlines between arguments? If not, could we also add:

  • between (default is “any”) - Setting for the line break between arguments.

Valid

/*eslint newline-in-function-parens: ["error", {"between": "multiline"}]*/

function foo(
) {}
function foo(
    a
) {}
function foo(a) {}
function foo(
    a,
    b
) {}
function foo(
    a = function() {
        dosomething();
    }
) {}

Invalid

/*eslint newline-in-function-parens: ["error", {"between": "multiline"}]*/

function foo(
    a, b
) {}

My use-case: We enforce max-len to 100, and for longer lines we can meet that rule by doing:

someReallyCoolFunction(this.aLongPropertyName.subPropertyNamesToo,
  this.anotherPropertyName.whySoManySubPropertyNames);

Which we would like to enforce being written as:

someReallyCoolFunction(
  this.aLongPropertyName.subPropertyNamesToo,
  this.anotherPropertyName.whySoManySubPropertyNames
);

Ie; When there is a newline anywhere, everything should be newline. Otherwise, nothing should be newline.

3reactions
mysticateacommented, May 5, 2016

Original JSCS rule seems to apply this rule to FunctionDeclaration, FunctionExpression, and ArrowFunctionExpression.

I think, we can add overrides option to apply for each kind. (also, original rule can configure each paren of open/close)

{
    "newline-in-function-parens": ["error", "always" or "never" or "multiline" or {"minItems": 2} or "any"]
    // or
    "newline-in-function-parens": ["error", {
        "open": "always" or "never" or "multiline" or {"minItems": 2} or "any",
        "close": "always" or "never" or "multiline" or {"minItems": 2} or "any",
        "overrides": {
            "declaration": null,
            "expression": null,
            "arrow": null
        }
    }]
}
  • “always” (default) - Requires line breaks always.

  • “never” - Disallows line breaks always.

  • “multiline” - Requires line breaks when the content is multiline. Otherwise, disallows line breaks.

  • {“minItems”: <integer>} - Requires line breaks when the number of items is more than the specific integer. Otherwise, disallows line breaks.

  • "any" - Does not warn.

  • open (default is "always") - Setting for the line break after the open parenthesis.

  • close (default is "always") - Setting for the line break before the close parenthesis.

  • overrides - We can override setting for each kind of functions. For example:

    {
        "newline-in-function-parens": ["error", {
            "open": "multiline",
            "close": "multiline",
            "overrides": {
                "arrow": "never" // or {"open": "never", "close": "never"}
            }
        }]
    }
    
Read more comments on GitHub >

github_iconTop Results From Across the Web

function-paren-newline - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
function-parentheses-newline-inside - Stylelint
Require a newline or disallow whitespace on the inside of the parentheses of functions. The fix option can automatically fix all of the...
Read more >
2. Lexical analysis — Python 3.11.1 documentation
A Python program is read by a parser. Input to the parser is a stream of tokens, generated by the lexical analyzer. This...
Read more >
Optional Braces - Scala 3 - EPFL
In a brace-delimited region, no statement is allowed to start to the left of the first statement after the opening brace that starts...
Read more >
UAX #14: Unicode Line Breaking Algorithm
Newline Functions are defined in the Unicode Standard as providing additional mandatory breaks. They are not individual characters, but are encoded as sequences ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found