diff --git a/docs/rules/padding-line-between-statements.md b/docs/rules/padding-line-between-statements.md index c5625973453..390dada294e 100644 --- a/docs/rules/padding-line-between-statements.md +++ b/docs/rules/padding-line-between-statements.md @@ -20,9 +20,9 @@ function foo() { ## Rule Details -This rule does nothing if no configuration. +This rule does nothing if no configurations are provided. -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." +A configuration is an object which has 3 properties; `blankLine`, `prev` and `next`. For example, `{ blankLine: "always", prev: "var", next: "return" }` means "one or more blank lines are required 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 @@ -46,11 +46,11 @@ You can supply any number of configurations. If a statement pair matches multipl - `STATEMENT_TYPE` is one of the following, or an array of the following. - `"*"` is wildcard. This matches any statements. - `"block"` is lonely blocks. - - `"block-like"` is block like statements. This matches statements that the last token is the closing brace of blocks; e.g. `{ }`, `if (a) { }`, and `while (a) { }`. + - `"block-like"` is block like statements. This matches statements that the last token is the closing brace of blocks; e.g. `{ }`, `if (a) { }`, and `while (a) { }`. Also matches immediately invoked function expression statements. - `"break"` is `break` statements. - `"case"` is `case` labels. - - `"cjs-export"` is `export` statements of CommonJS; e.g. `module.exports = 0`, `module.exports.foo = 1`, and `exports.foo = 2`. This is the special cases of assignment. - - `"cjs-import"` is `import` statements of CommonJS; e.g. `const foo = require("foo")`. This is the special cases of variable declarations. + - `"cjs-export"` is `export` statements of CommonJS; e.g. `module.exports = 0`, `module.exports.foo = 1`, and `exports.foo = 2`. This is a special case of assignment. + - `"cjs-import"` is `import` statements of CommonJS; e.g. `const foo = require("foo")`. This is a special case of variable declarations. - `"class"` is `class` declarations. - `"const"` is `const` variable declarations. - `"continue"` is `continue` statements. @@ -64,6 +64,7 @@ You can supply any number of configurations. If a statement pair matches multipl - `"for"` is `for` loop families. This matches all statements that the first token is `for` keyword. - `"function"` is function declarations. - `"if"` is `if` statements. + - `"iife"` is immediately invoked function expression statements. This matches calls on a function expression, optionally prefixed with a unary operator. - `"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 if the block is multiline. diff --git a/lib/rules/padding-line-between-statements.js b/lib/rules/padding-line-between-statements.js index f571e1e2e33..4af85fef015 100644 --- a/lib/rules/padding-line-between-statements.js +++ b/lib/rules/padding-line-between-statements.js @@ -353,6 +353,9 @@ const StatementTypes = { node.type === "ExpressionStatement" && !isDirectivePrologue(node, sourceCode) }, + iife: { + test: isIIFEStatement + }, "multiline-block-like": { test: (node, sourceCode) => node.loc.start.line !== node.loc.end.line && diff --git a/tests/lib/rules/padding-line-between-statements.js b/tests/lib/rules/padding-line-between-statements.js index 3f72f8ff2d2..82147d389d4 100644 --- a/tests/lib/rules/padding-line-between-statements.js +++ b/tests/lib/rules/padding-line-between-statements.js @@ -852,6 +852,35 @@ ruleTester.run("padding-line-between-statements", rule, { ] }, + //---------------------------------------------------------------------- + // iife + //---------------------------------------------------------------------- + + { + code: "(function(){\n})()\n\nvar a = 2;", + options: [ + { blankLine: "always", prev: "iife", next: "*" } + ] + }, + { + code: "+(function(){\n})()\n\nvar a = 2;", + options: [ + { blankLine: "always", prev: "iife", next: "*" } + ] + }, + { + code: "(function(){\n})()\nvar a = 2;", + options: [ + { blankLine: "never", prev: "iife", next: "*" } + ] + }, + { + code: "+(function(){\n})()\nvar a = 2;", + options: [ + { blankLine: "never", prev: "iife", next: "*" } + ] + }, + //---------------------------------------------------------------------- // import //---------------------------------------------------------------------- @@ -3371,6 +3400,43 @@ ruleTester.run("padding-line-between-statements", rule, { errors: [MESSAGE_ALWAYS] }, + //---------------------------------------------------------------------- + // iife + //---------------------------------------------------------------------- + + { + code: "(function(){\n})()\n\nvar a = 2;", + output: "(function(){\n})()\nvar a = 2;", + options: [ + { blankLine: "never", prev: "iife", next: "*" } + ], + errors: [MESSAGE_NEVER] + }, + { + code: "+(function(){\n})()\n\nvar a = 2;", + output: "+(function(){\n})()\nvar a = 2;", + options: [ + { blankLine: "never", prev: "iife", next: "*" } + ], + errors: [MESSAGE_NEVER] + }, + { + code: "(function(){\n})()\nvar a = 2;", + output: "(function(){\n})()\n\nvar a = 2;", + options: [ + { blankLine: "always", prev: "iife", next: "*" } + ], + errors: [MESSAGE_ALWAYS] + }, + { + code: "+(function(){\n})()\nvar a = 2;", + output: "+(function(){\n})()\n\nvar a = 2;", + options: [ + { blankLine: "always", prev: "iife", next: "*" } + ], + errors: [MESSAGE_ALWAYS] + }, + //---------------------------------------------------------------------- // import //---------------------------------------------------------------------- @@ -4247,6 +4313,14 @@ ruleTester.run("padding-line-between-statements", rule, { ], errors: [MESSAGE_NEVER] }, + { + code: "+(function(){\n})()\n\nvar a = 2;", + output: "+(function(){\n})()\nvar a = 2;", + options: [ + { blankLine: "never", prev: "block-like", next: "*" } + ], + errors: [MESSAGE_NEVER] + }, { code: "var a = function() {};\n\nvar b = 2;", output: "var a = function() {};\nvar b = 2;",