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

New: add allowSingleLineBlocks opt. to padded-blocks rule (fixes #7145) #11243

Merged
merged 2 commits into from Feb 15, 2019
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
45 changes: 44 additions & 1 deletion docs/rules/padded-blocks.md
Expand Up @@ -20,7 +20,10 @@ This rule enforces consistent empty line padding within blocks.

## Options

This rule has one option, which can be a string option or an object option.
This rule has two options, the first one can be a string option or an object option.
The second one is an object option, it can allow exceptions.

### First option

String option:

Expand All @@ -33,6 +36,10 @@ Object option:
* `"classes"` require or disallow padding within classes
* `"switches"` require or disallow padding within `switch` statements

### Second option

* `"allowSingleLineBlocks": true` allows single-line blocks

### always

Examples of **incorrect** code for this rule with the default `"always"` option:
Expand Down Expand Up @@ -346,6 +353,42 @@ if (a) {
}
```

### always + allowSingleLineBlocks

Examples of **incorrect** code for this rule with the `"always", {"allowSingleLineBlocks": true}` options:

```js
/*eslint padded-blocks: ["error", "always", { allowSingleLineBlocks: true }]*/

if (a) {
b();
}

if (a) {

b();
}

if (a) {
b();

}
```

Examples of **correct** code for this rule with the `"always", {"allowSingleLineBlocks": true}` options:

```js
/*eslint padded-blocks: ["error", "always", { allowSingleLineBlocks: true }]*/

if (a) { b(); }

if (a) {

b();

}
```

## When Not To Use It

You can turn this rule off if you are not concerned with the consistency of padding within blocks.
Expand Down
41 changes: 32 additions & 9 deletions lib/rules/padded-blocks.js
Expand Up @@ -5,6 +5,12 @@

"use strict";

//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------

const astUtils = require("../util/ast-utils");

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
Expand Down Expand Up @@ -45,32 +51,45 @@ module.exports = {
minProperties: 1
}
]
},
{
type: "object",
properties: {
allowSingleLineBlocks: {
type: "boolean"
}
}
}
]
},

create(context) {
const options = {};
const config = context.options[0] || "always";
const typeOptions = context.options[0] || "always";
const exceptOptions = context.options[1] || {};

if (typeof config === "string") {
const shouldHavePadding = config === "always";
if (typeof typeOptions === "string") {
const shouldHavePadding = typeOptions === "always";

options.blocks = shouldHavePadding;
options.switches = shouldHavePadding;
options.classes = shouldHavePadding;
} else {
if (Object.prototype.hasOwnProperty.call(config, "blocks")) {
options.blocks = config.blocks === "always";
if (Object.prototype.hasOwnProperty.call(typeOptions, "blocks")) {
options.blocks = typeOptions.blocks === "always";
}
if (Object.prototype.hasOwnProperty.call(config, "switches")) {
options.switches = config.switches === "always";
if (Object.prototype.hasOwnProperty.call(typeOptions, "switches")) {
options.switches = typeOptions.switches === "always";
}
if (Object.prototype.hasOwnProperty.call(config, "classes")) {
options.classes = config.classes === "always";
if (Object.prototype.hasOwnProperty.call(typeOptions, "classes")) {
options.classes = typeOptions.classes === "always";
}
}

if (Object.prototype.hasOwnProperty.call(exceptOptions, "allowSingleLineBlocks")) {
options.allowSingleLineBlocks = exceptOptions.allowSingleLineBlocks === true;
}

const ALWAYS_MESSAGE = "Block must be padded by blank lines.",
NEVER_MESSAGE = "Block must not be padded by blank lines.";

Expand Down Expand Up @@ -177,6 +196,10 @@ module.exports = {
blockHasTopPadding = isPaddingBetweenTokens(tokenBeforeFirst, firstBlockToken),
blockHasBottomPadding = isPaddingBetweenTokens(lastBlockToken, tokenAfterLast);

if (options.allowSingleLineBlocks && astUtils.isTokenOnSameLine(tokenBeforeFirst, tokenAfterLast)) {
return;
}

if (requirePaddingFor(node)) {
if (!blockHasTopPadding) {
context.report({
Expand Down
2 changes: 2 additions & 0 deletions tests/lib/rules/padded-blocks.js
Expand Up @@ -54,6 +54,8 @@ ruleTester.run("padded-blocks", rule, {
{ code: "{\na();}", options: ["never"] },
{ code: "{a();\n}", options: ["never"] },
{ code: "{a();}", options: ["never"] },
{ code: "{a();}", options: ["always", { allowSingleLineBlocks: true }] },
{ code: "{\n\na();\n\n}", options: ["always", { allowSingleLineBlocks: true }] },
{ code: "{//comment\na();}", options: ["never"] },
{ code: "{\n//comment\na()\n}", options: ["never"] },
{ code: "{a();//comment\n}", options: ["never"] },
Expand Down