From 4047639773537e66e1bd20a29c5684e9f5b1c8e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20HERVIER?= Date: Mon, 14 Jan 2019 20:20:44 +0100 Subject: [PATCH 1/2] New: add allowSingleLineBlocks opt. to padded-blocks rule (fixes #7145) --- lib/rules/padded-blocks.js | 41 +++++++++++++++++++++++++++++--------- 1 file changed, 32 insertions(+), 9 deletions(-) diff --git a/lib/rules/padded-blocks.js b/lib/rules/padded-blocks.js index 7c0b56ba7f8..e4dd37f4cdb 100644 --- a/lib/rules/padded-blocks.js +++ b/lib/rules/padded-blocks.js @@ -5,6 +5,12 @@ "use strict"; +//------------------------------------------------------------------------------ +// Requirements +//------------------------------------------------------------------------------ + +const astUtils = require("../util/ast-utils"); + //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ @@ -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."; @@ -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({ From 193fb918b7e68ae7675c40918022dbe6eee5879f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20HERVIER?= Date: Fri, 18 Jan 2019 22:30:52 +0100 Subject: [PATCH 2/2] Chore: Add tests & docs to padded-blocks (allowSingleLineBlocks) --- docs/rules/padded-blocks.md | 45 +++++++++++++++++++++++++++++++- tests/lib/rules/padded-blocks.js | 2 ++ 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/docs/rules/padded-blocks.md b/docs/rules/padded-blocks.md index 5c7908cff5a..d8b855ed37f 100644 --- a/docs/rules/padded-blocks.md +++ b/docs/rules/padded-blocks.md @@ -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: @@ -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: @@ -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. diff --git a/tests/lib/rules/padded-blocks.js b/tests/lib/rules/padded-blocks.js index fb363cdf307..08ed2856cc6 100644 --- a/tests/lib/rules/padded-blocks.js +++ b/tests/lib/rules/padded-blocks.js @@ -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"] },