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 "multiline" type to padding-line-between-statements #8668

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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