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
Changes from 1 commit
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
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