Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: Add "multiline" type to padding-line-between-statements (#8668)
* Update: Add multiline-expression statement type

* Chore: Add valid and invalid tests for multiline-expression

* Docs: Add description of multiline-expression

And minor tweaks to existing descriptions.

* Chore: Remove spurious newlines

* Chore: Add additional test cases
  • Loading branch information
Lobstrosity authored and platinumazure committed Feb 3, 2018
1 parent bb213dc commit 1da1ada
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 3 deletions.
7 changes: 4 additions & 3 deletions docs/rules/padding-line-between-statements.md
Expand Up @@ -22,8 +22,8 @@ function foo() {

This rule does nothing if no configuration.

A configuration is an object which has 3 properties; `blankLine`, `prev` and `next`. For example, `{ blankLine: "always", prev: "var", next: "return" }` is meaning "it requires one or more blank lines between a variable declaration and a `return` statement."
You can supply any number of configurations. If an statement pair matches multiple configurations, the last matched configuration will be used.
A configuration is an object which has 3 properties; `blankLine`, `prev` and `next`. For example, `{ blankLine: "always", prev: "var", next: "return" }` means "it requires one or more blank lines between a variable declaration and a `return` statement."
You can supply any number of configurations. If a statement pair matches multiple configurations, the last matched configuration will be used.

```json
{
Expand Down Expand Up @@ -66,7 +66,8 @@ You can supply any number of configurations. If an statement pair matches multip
- `"if"` is `if` statements.
- `"import"` is `import` declarations.
- `"let"` is `let` variable declarations.
- `"multiline-block-like"` is block like statements. This is the same as `block-like` type, but only the block is multiline.
- `"multiline-block-like"` is block like statements. This is the same as `block-like` type, but only if the block is multiline.
- `"multiline-expression"` is expression statements. This is the same as `expression` type, but only if the statement is multiline.
- `"return"` is `return` statements.
- `"switch"` is `switch` statements.
- `"throw"` is `throw` statements.
Expand Down
6 changes: 6 additions & 0 deletions lib/rules/padding-line-between-statements.js
Expand Up @@ -358,6 +358,12 @@ const StatementTypes = {
node.loc.start.line !== node.loc.end.line &&
isBlockLikeStatement(sourceCode, node)
},
"multiline-expression": {
test: (node, sourceCode) =>
node.loc.start.line !== node.loc.end.line &&
node.type === "ExpressionStatement" &&
!isDirectivePrologue(node, sourceCode)
},

block: newNodeTypeTester("BlockStatement"),
empty: newNodeTypeTester("EmptyStatement"),
Expand Down
58 changes: 58 additions & 0 deletions tests/lib/rules/padding-line-between-statements.js
Expand Up @@ -534,6 +534,35 @@ ruleTester.run("padding-line-between-statements", rule, {
]
},

//----------------------------------------------------------------------
// multiline-expression
//----------------------------------------------------------------------

{
code: "foo()\n\nfoo(\n\tx,\n\ty\n)",
options: [
{ blankLine: "always", prev: "*", next: "multiline-expression" }
]
},
{
code: "foo()\nfoo()",
options: [
{ blankLine: "always", prev: "*", next: "multiline-expression" }
]
},
{
code: "() => {\n\tsomeArray.forEach(x => doSomething(x));\n\treturn theThing;\n}",
options: [
{ blankLine: "always", prev: "multiline-expression", next: "return" }
]
},
{
code: "() => {\n\tsomeArray.forEach(\n\t\tx => doSomething(x)\n\t);\n\n\treturn theThing;\n}",
options: [
{ blankLine: "always", prev: "multiline-expression", next: "return" }
]
},

//----------------------------------------------------------------------
// break
//----------------------------------------------------------------------
Expand Down Expand Up @@ -2943,6 +2972,35 @@ ruleTester.run("padding-line-between-statements", rule, {
errors: [MESSAGE_ALWAYS]
},

//----------------------------------------------------------------------
// multiline-expression
//----------------------------------------------------------------------

{
code: "foo()\n\nfoo(\n\tx,\n\ty\n)",
output: "foo()\nfoo(\n\tx,\n\ty\n)",
options: [
{ blankLine: "never", prev: "*", next: "multiline-expression" }
],
errors: [MESSAGE_NEVER]
},
{
code: "foo()\nfoo(\n\tx,\n\ty\n)",
output: "foo()\n\nfoo(\n\tx,\n\ty\n)",
options: [
{ blankLine: "always", prev: "*", next: "multiline-expression" }
],
errors: [MESSAGE_ALWAYS]
},
{
code: "() => {\n\tsomeArray.forEach(\n\t\tx => doSomething(x)\n\t);\n\treturn theThing;\n}",
output: "() => {\n\tsomeArray.forEach(\n\t\tx => doSomething(x)\n\t);\n\n\treturn theThing;\n}",
options: [
{ blankLine: "always", prev: "multiline-expression", next: "return" }
],
errors: [MESSAGE_ALWAYS]
},

//----------------------------------------------------------------------
// break
//----------------------------------------------------------------------
Expand Down

0 comments on commit 1da1ada

Please sign in to comment.