Skip to content

Commit

Permalink
New: function-paren-newline rule (fixes #6074)
Browse files Browse the repository at this point in the history
  • Loading branch information
not-an-aardvark committed Feb 19, 2017
1 parent daf6f26 commit d550869
Show file tree
Hide file tree
Showing 35 changed files with 1,121 additions and 137 deletions.
11 changes: 5 additions & 6 deletions Makefile.js
Expand Up @@ -863,7 +863,8 @@ target.checkLicenses = function() {

if (impermissible.length) {
impermissible.forEach(dependency => {
console.error("%s license for %s is impermissible.",
console.error(
"%s license for %s is impermissible.",
dependency.licenses,
dependency.name
);
Expand Down Expand Up @@ -1081,11 +1082,9 @@ target.perf = function() {
() => {

// Count test target files.
const count = glob.sync(
process.platform === "win32"
? PERF_MULTIFILES_TARGETS.slice(2).replace("\\", "/")
: PERF_MULTIFILES_TARGETS
).length;
const count = glob.sync(process.platform === "win32"
? PERF_MULTIFILES_TARGETS.slice(2).replace("\\", "/")
: PERF_MULTIFILES_TARGETS).length;

runPerformanceTest(
`Multi Files (${count} files):`,
Expand Down
1 change: 1 addition & 0 deletions conf/eslint.json
Expand Up @@ -165,6 +165,7 @@
"func-names": "off",
"func-name-matching": "off",
"func-style": "off",
"function-paren-newline": "off",
"generator-star-spacing": "off",
"global-require": "off",
"guard-for-in": "off",
Expand Down
225 changes: 225 additions & 0 deletions docs/rules/function-paren-newline.md
@@ -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.

Many styleguides require or disallow newlines inside of function parentheses.

## Rule Details

This rule enforces consistent line breaks inside parentheses of function parameters or arguments.

### Options

This rule has a single option, which can either be a string or an object.

* `"always"` requires line breaks inside all function parentheses.
* `"never"` disallows line breaks inside all function parentheses.
* `"multiline"` (default) requires linebreaks inside function parentheses if any of the parameters/arguments have a line break between them. Otherwise, it disallows linebreaks.
* `{ "minItems": value }` requires linebreaks inside function parentheses if the number of parameters/arguments is at least `value`. Otherwise, it disallows linebreaks.

Example configurations:

```json
{
"rules": {
"function-paren-newline": ["error", "never"]
}
}
```

```json
{
"rules": {
"function-paren-newline": ["error", { "minItems": 3 }]
}
}
```

Examples of **incorrect** code for this rule with the `"always"` option:

```js
/* eslint function-paren-newline: ["error", "always"] */

function foo(bar, baz) {}

var foo = function(bar, baz) {};

var foo = (bar, baz) => {};

foo(bar, baz);
```

Examples of **correct** code for this rule with the `"always"` option:

```js
/* eslint function-paren-newline: ["error", "always"] */

function foo(
bar,
baz
) {}

var foo = function(
bar, baz
) {};

var foo = (
bar,
baz
) => {};

foo(
bar,
baz
);
```

Examples of **incorrect** code for this rule with the `"never"` option:

```js
/* eslint function-paren-newline: ["error", "never"] */

function foo(
bar,
baz
) {}

var foo = function(
bar, baz
) {};

var foo = (
bar,
baz
) => {};

foo(
bar,
baz
);
```

Examples of **correct** code for this rule with the `"never"` option:

```js
/* eslint function-paren-newline: ["error", "never"] */

function foo(bar, baz) {}

function foo(bar,
baz) {}

var foo = function(bar, baz) {};

var foo = (bar, baz) => {};

foo(bar, baz);

foo(bar,
baz);
```

Examples of **incorrect** code for this rule with the default `"multiline"` option:

```js
/* eslint function-paren-newline: ["error", "multiline"] */

function foo(bar,
baz
) {}

var foo = function(
bar, baz
) {};

var foo = (
bar,
baz) => {};

foo(bar,
baz);

foo(
function() {
return baz;
}
);
```

Examples of **correct** code for this rule with the default `"multiline"` option:

```js
/* eslint function-paren-newline: ["error", "multiline"] */

function foo(bar, baz) {}

var foo = function(
bar,
baz
) {};

var foo = (bar, baz) => {};

foo(bar, baz, qux);

foo(
bar,
baz,
qux
);

foo(function() {
return baz;
});
```

Examples of **incorrect** code for this rule with the `{ "minItems": 3 }` option:

```js
/* eslint function-paren-newline: ["error", { "minItems": 3 }] */

function foo(
bar,
baz
) {}

function foo(bar, baz, qux) {}

var foo = function(
bar, baz
) {};

var foo = (bar,
baz) => {};

foo(bar,
baz);
```

Examples of **correct** code for this rule with the `{ "minItems": 3 }` option:

```js
/* eslint function-paren-newline: ["error", { "minItems": 3 }] */

function foo(bar, baz) {}

var foo = function(
bar,
baz,
qux
) {};

var foo = (
bar, baz, qux
) => {};

foo(bar, baz);

foo(
bar, baz, qux
);
```

## When Not To Use It

If don't want to enforce consistent linebreaks inside function parentheses, do not turn on this rule.
12 changes: 5 additions & 7 deletions lib/cli-engine.js
Expand Up @@ -530,13 +530,11 @@ CLIEngine.getErrorResults = function(results) {
const filteredMessages = result.messages.filter(isErrorMessage);

if (filteredMessages.length > 0) {
filtered.push(
Object.assign(result, {
messages: filteredMessages,
errorCount: filteredMessages.length,
warningCount: 0
})
);
filtered.push(Object.assign(result, {
messages: filteredMessages,
errorCount: filteredMessages.length,
warningCount: 0
}));
}
});

Expand Down
12 changes: 8 additions & 4 deletions lib/code-path-analysis/code-path-analyzer.js
Expand Up @@ -154,7 +154,8 @@ function forwardCurrentToHead(analyzer, node) {
analyzer.emitter.emit(
"onCodePathSegmentEnd",
currentSegment,
node);
node
);
}
}
}
Expand All @@ -175,7 +176,8 @@ function forwardCurrentToHead(analyzer, node) {
analyzer.emitter.emit(
"onCodePathSegmentStart",
headSegment,
node);
node
);
}
}
}
Expand All @@ -202,7 +204,8 @@ function leaveFromCurrentSegment(analyzer, node) {
analyzer.emitter.emit(
"onCodePathSegmentEnd",
currentSegment,
node);
node
);
}
}

Expand Down Expand Up @@ -369,7 +372,8 @@ function processCodePathToEnter(analyzer, node) {
case "SwitchStatement":
state.pushSwitchContext(
node.cases.some(isCaseNode),
astUtils.getLabel(node));
astUtils.getLabel(node)
);
break;

case "TryStatement":
Expand Down
3 changes: 2 additions & 1 deletion lib/code-path-analysis/code-path-segment.js
Expand Up @@ -164,7 +164,8 @@ class CodePathSegment {
return new CodePathSegment(
id,
flattenUnusedSegments(allPrevSegments),
allPrevSegments.some(isReachable));
allPrevSegments.some(isReachable)
);
}

/**
Expand Down

0 comments on commit d550869

Please sign in to comment.