From 1da1ada2ce21960a450e7e97762cfca0c7dd7233 Mon Sep 17 00:00:00 2001 From: Matthew Bennett Date: Fri, 2 Feb 2018 18:48:59 -0700 Subject: [PATCH] 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 --- docs/rules/padding-line-between-statements.md | 7 ++- lib/rules/padding-line-between-statements.js | 6 ++ .../rules/padding-line-between-statements.js | 58 +++++++++++++++++++ 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/docs/rules/padding-line-between-statements.md b/docs/rules/padding-line-between-statements.md index 06b0a217259..38b7fa63776 100644 --- a/docs/rules/padding-line-between-statements.md +++ b/docs/rules/padding-line-between-statements.md @@ -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 { @@ -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. diff --git a/lib/rules/padding-line-between-statements.js b/lib/rules/padding-line-between-statements.js index 9d1a4a3c7eb..e03cec8ae74 100644 --- a/lib/rules/padding-line-between-statements.js +++ b/lib/rules/padding-line-between-statements.js @@ -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"), diff --git a/tests/lib/rules/padding-line-between-statements.js b/tests/lib/rules/padding-line-between-statements.js index aa91a6b077b..597c4883500 100644 --- a/tests/lib/rules/padding-line-between-statements.js +++ b/tests/lib/rules/padding-line-between-statements.js @@ -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 //---------------------------------------------------------------------- @@ -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 //----------------------------------------------------------------------