Permalink
Browse files

Update: Support classes in `padded-blocks` (fixes #5092)

  • Loading branch information...
alberto committed Feb 26, 2016
1 parent 6f47151 commit 4f82b2b945d4864f519b4045e47814e5d4a62f2a
Showing with 141 additions and 13 deletions.
  1. +57 −10 docs/rules/padded-blocks.md
  2. +18 −2 lib/rules/padded-blocks.js
  3. +66 −1 tests/lib/rules/padded-blocks.js
@@ -18,9 +18,9 @@ padded blocks or never do it.
This rule enforces consistent padding within blocks.
This rule takes one argument, which can be an string or an object. If it is `"always"` (the default) then block statements must start **and** end with a blank line. If `"never"`, then block statements should neither start nor end with a blank line. By default, this rule ignores padding in switch statements.
This rule takes one argument, which can be an string or an object. If it is `"always"` (the default) then block statements must start **and** end with a blank line. If `"never"`, then block statements should neither start nor end with a blank line. By default, this rule ignores padding in switch statements and classes.
If you want to enforce padding for switch statements, a configuration object can be passed as the rule argument to configure the cases separately ( e.g. `{ "blocks": "always", "switches": "always" }` ).
If you want to enforce padding within switches and classes, a configuration object can be passed as the rule argument to configure the cases separately ( e.g. `{ "blocks": "always", "switches": "always", "classes": "always" }` ).
The following patterns are considered problems when set to `"always"`:
@@ -126,20 +126,20 @@ if (a)
}
```
The following patterns are considered problems when configured `{ switches: "always" }`:
The following patterns are considered problems when configured `{ "switches": "always" }`:
```js
/*eslint padded-blocks: [2, { switches: "always" }]*/
/*eslint padded-blocks: [2, { "switches": "always" }]*/
switch (a) {
case 0: foo();
}
```
The following patterns are not considered problems when configured `{ switches: "always" }`:
The following patterns are not considered problems when configured `{ "switches": "always" }`:
```js
/*eslint padded-blocks: [2, { switches: "always" }]*/
/*eslint padded-blocks: [2, { "switches": "always" }]*/
switch (a) {
@@ -152,10 +152,10 @@ if (a) {
}
```
The following patterns are considered problems when configured `{ switches: "never" }`:
The following patterns are considered problems when configured `{ "switches": "never" }`:
```js
/*eslint padded-blocks: [2, { switches: "never" }]*/
/*eslint padded-blocks: [2, { "switches": "never" }]*/
switch (a) {
@@ -164,10 +164,10 @@ switch (a) {
}
```
The following patterns are not considered problems when configured `{ switches: "never" }`:
The following patterns are not considered problems when configured `{ "switches": "never" }`:
```js
/*eslint padded-blocks: [2, { switches: "never" }]*/
/*eslint padded-blocks: [2, { "switches": "never" }]*/
switch (a) {
case 0: foo();
@@ -180,6 +180,53 @@ if (a) {
}
```
The following patterns are considered problems when configured `{ "classes": "always" }`:
```js
/*eslint padded-blocks: [2, { "classes": "always" }]*/
class A {
constructor(){
}
}
```
The following patterns are not considered problems when configured `{ "classes": "always" }`:
```js
/*eslint padded-blocks: [2, { "classes": "always" }]*/
class A {
constructor(){
}
}
```
The following patterns are considered problems when configured `{ "classes": "never" }`:
```js
/*eslint padded-blocks: [2, { "classes": "never" }]*/
class A {
constructor(){
}
}
```
The following patterns are not considered problems when configured `{ "classes": "never" }`:
```js
/*eslint padded-blocks: [2, { "classes": "never" }]*/
class A {
constructor(){
}
}
```
## When Not To Use It
View
@@ -16,13 +16,16 @@ module.exports = function(context) {
if (typeof config === "string") {
options.blocks = config === "always";
} else if (typeof config === "object") {
} else {
if (config.hasOwnProperty("blocks")) {
options.blocks = config.blocks === "always";
}
if (config.hasOwnProperty("switches")) {
options.switches = config.switches === "always";
}
if (config.hasOwnProperty("classes")) {
options.classes = config.classes === "always";
}
}
var ALWAYS_MESSAGE = "Block must be padded by blank lines.",
@@ -102,7 +105,8 @@ module.exports = function(context) {
return options.blocks;
case "SwitchStatement":
return options.switches;
case "ClassBody":
return options.classes;
/* istanbul ignore next */
default:
throw new Error("unreachable");
@@ -173,6 +177,15 @@ module.exports = function(context) {
};
}
if (options.hasOwnProperty("classes")) {
rule.ClassBody = function(node) {
if (node.body.length === 0) {
return;
}
checkPadding(node);
};
}
return rule;
};
@@ -190,6 +203,9 @@ module.exports.schema = [
},
"switches": {
"enum": ["always", "never"]
},
"classes": {
"enum": ["always", "never"]
}
},
"additionalProperties": false,
@@ -47,6 +47,15 @@ ruleTester.run("padded-blocks", rule, {
{code: "switch (a) {\n\ncase 0: foo();\ncase 1: bar();\n\n}", options: [{"switches": "always"}]},
{code: "switch (a) {\n\n//comment\ncase 0: foo();//comment\n\n}", options: [{"switches": "always"}]},
{code: "switch (a) {//coment\n\ncase 0: foo();\ncase 1: bar();\n\n/* comment */}", options: [{"switches": "always"}]},
// Ignore classes by default
{code: "class A{\nfoo(){}\n}", parserOptions: { ecmaVersion: 6 }},
{code: "class A{\n\nfoo(){}\n\n}", parserOptions: { ecmaVersion: 6 }},
{code: "class A{}", parserOptions: { ecmaVersion: 6 }, options: [{"classes": "always"}]},
{code: "class A{\n\n}", parserOptions: { ecmaVersion: 6 }, options: [{"classes": "always"}]},
{code: "class A{\n\nfoo(){}\n\n}", parserOptions: { ecmaVersion: 6 }, options: [{"classes": "always"}]},
{code: "{\na();\n}", options: ["never"]},
{code: "{\na();}", options: ["never"]},
{code: "{a();\n}", options: ["never"]},
@@ -62,7 +71,8 @@ ruleTester.run("padded-blocks", rule, {
{code: "{\n\n// comment\nif (\n// comment\n a) {}\n\n }", options: ["always"] },
{code: "{\n// comment\nif (\n// comment\n a) {}\n }", options: ["never"] },
{code: "{\n// comment\nif (\n// comment\n a) {}\n }", options: [{"blocks": "never"}] },
{code: "switch (a) {\ncase 0: foo();\n}", options: [{"switches": "never"}]}
{code: "switch (a) {\ncase 0: foo();\n}", options: [{"switches": "never"}]},
{code: "class A{\nfoo(){}\n}", parserOptions: { ecmaVersion: 6 }, options: [{"classes": "never"}]}
],
invalid: [
{
@@ -198,6 +208,23 @@ ruleTester.run("padded-blocks", rule, {
}
]
},
{
code: "class A {\nconstructor(){}\n}",
parserOptions: { ecmaVersion: 6 },
options: [{"classes": "always"}],
errors: [
{
message: ALWAYS_MESSAGE,
line: 1,
column: 9
},
{
message: ALWAYS_MESSAGE,
line: 3,
column: 1
}
]
},
{
code: "{a();}",
errors: [
@@ -323,6 +350,44 @@ ruleTester.run("padded-blocks", rule, {
column: 3
}
]
},
{
code: "class A {\n\nconstructor(){\n\nfoo();\n\n}\n\n}",
parserOptions: { ecmaVersion: 6 },
options: [{"classes": "never"}],
errors: [
{
message: NEVER_MESSAGE,
line: 1
},
{
message: NEVER_MESSAGE,
line: 9
}
]
},
{
code: "class A {\n\nconstructor(){\n\nfoo();\n\n}\n\n}",
parserOptions: { ecmaVersion: 6 },
options: [{"blocks": "never", "classes": "never"}],
errors: [
{
message: NEVER_MESSAGE,
line: 1
},
{
message: NEVER_MESSAGE,
line: 3
},
{
message: NEVER_MESSAGE,
line: 7
},
{
message: NEVER_MESSAGE,
line: 9
}
]
}
]
});

0 comments on commit 4f82b2b

Please sign in to comment.