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: iife statement type for padding-line-between-statements (fixes #10853) #10916

Merged
merged 1 commit into from Oct 12, 2018
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
11 changes: 6 additions & 5 deletions docs/rules/padding-line-between-statements.md
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions lib/rules/padding-line-between-statements.js
Expand Up @@ -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 &&
Expand Down
74 changes: 74 additions & 0 deletions tests/lib/rules/padding-line-between-statements.js
Expand Up @@ -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
//----------------------------------------------------------------------
Expand Down Expand Up @@ -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
//----------------------------------------------------------------------
Expand Down Expand Up @@ -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;",
Expand Down