Skip to content

Commit

Permalink
enforce consistent spacing inside all parenthesis
Browse files Browse the repository at this point in the history
update tests to respect new rule logic
add more specific linting messages
  • Loading branch information
Che Fisher committed Aug 10, 2019
1 parent a675c89 commit 213b179
Show file tree
Hide file tree
Showing 2 changed files with 287 additions and 161 deletions.
148 changes: 92 additions & 56 deletions lib/rules/space-in-parens.js
@@ -1,6 +1,6 @@
/**
* @fileoverview Disallows or enforces spaces inside of parentheses.
* @author Jonathan Rajavuori
* @author Jonathan Rajavuori and Che Fisher
*/
"use strict";

Expand Down Expand Up @@ -45,18 +45,29 @@ module.exports = {

create(context) {

const MISSING_SPACE_MESSAGE = "There must be a space inside this paren.",
REJECTED_SPACE_MESSAGE = "There should be no spaces inside this paren.",
const MISSING_OPENING_SPACE_MESSAGE = "There must be a space after this paren",
MISSING_CLOSING_SPACE_MESSAGE = "There must be a space before this paren",
REJECTED_OPENING_SPACE_MESSAGE = "There should be no space after this paren",
REJECTED_CLOSING_SPACE_MESSAGE = "There should be no space before this paren",
ALWAYS = context.options[0] === "always",
exceptionsArrayOptions = (context.options[1] && context.options[1].exceptions) || [],
options = {};

let exceptions;

if (exceptionsArrayOptions.length) {
options.braceException = exceptionsArrayOptions.indexOf("{}") !== -1;
options.bracketException = exceptionsArrayOptions.indexOf("[]") !== -1;
options.parenException = exceptionsArrayOptions.indexOf("()") !== -1;
options.empty = exceptionsArrayOptions.indexOf("empty") !== -1;
options.braceException = exceptionsArrayOptions.includes("{}");
options.bracketException = exceptionsArrayOptions.includes("[]");
options.parenException = exceptionsArrayOptions.includes("()");
options.empty = exceptionsArrayOptions.includes("empty");
}

/*
* workaround to prevent empty paren false positives
* if we never allow a space inside parentheses this should include empty parentheses too
*/
if (ALWAYS && options.parenException) {
options.empty = true;
}

/**
Expand Down Expand Up @@ -106,7 +117,7 @@ module.exports = {
* @returns {boolean} True if the token is one of the exceptions for the opener paren
*/
function isOpenerException(token) {
return token.type === "Punctuator" && exceptions.openers.indexOf(token.value) >= 0;
return exceptions.openers.includes(token.value);
}

/**
Expand All @@ -115,38 +126,17 @@ module.exports = {
* @returns {boolean} True if the token is one of the exceptions for the closer paren
*/
function isCloserException(token) {
return token.type === "Punctuator" && exceptions.closers.indexOf(token.value) >= 0;
return exceptions.closers.includes(token.value);
}

/**
* Determines if an opener paren should have a missing space after it
* Determines if an opening paren has a space immediatley after it
* @param {Object} left The paren token
* @param {Object} right The token after it
* @returns {boolean} True if the paren should have a space
* @returns {boolean} True if the opening paren is missing a required space
*/
function shouldOpenerHaveSpace(left, right) {
if (sourceCode.isSpaceBetweenTokens(left, right)) {
return false;
}

if (ALWAYS) {
if (astUtils.isClosingParenToken(right)) {
return false;
}
return !isOpenerException(right);
}
return isOpenerException(right);

}

/**
* Determines if an closer paren should have a missing space after it
* @param {Object} left The token before the paren
* @param {Object} right The paren token
* @returns {boolean} True if the paren should have a space
*/
function shouldCloserHaveSpace(left, right) {
if (astUtils.isOpeningParenToken(left)) {
function openerMissingSpace(left, right) {
if (!astUtils.isTokenOnSameLine(left, right)) {
return false;
}

Expand All @@ -155,24 +145,30 @@ module.exports = {
}

if (ALWAYS) {
return !isCloserException(left);
if (right.type !== "Punctuator") {
return !isOpenerException(left);
}
return !isOpenerException(right);
}
return isCloserException(left);

if (right.type !== "Punctuator") {
return isOpenerException(left);
}
return isOpenerException(right);
}

/**
* Determines if an opener paren should not have an existing space after it
* Determines if an opening paren has a space immediately after it
* @param {Object} left The paren token
* @param {Object} right The token after it
* @returns {boolean} True if the paren should reject the space
* @returns {boolean} True if the opening paren has a disallowed space following it
*/
function shouldOpenerRejectSpace(left, right) {
if (right.type === "Line") {
function openerRejectsSpace(left, right) {
if (!astUtils.isTokenOnSameLine(left, right)) {
return false;
}

if (!astUtils.isTokenOnSameLine(left, right)) {
if (right.type === "Line") {
return false;
}

Expand All @@ -181,23 +177,49 @@ module.exports = {
}

if (ALWAYS) {
if (right.type !== "Punctuator") {
return isOpenerException(left);
}
return isOpenerException(right);
}
return !isOpenerException(right);

if (right.type !== "Punctuator") {
return !isOpenerException(left);
}
return !isOpenerException(right);
}

/**
* Determines if an closer paren should not have an existing space after it
* Determines if a closing paren has a space immediately before it
* @param {Object} left The token before the paren
* @param {Object} right The paren token
* @returns {boolean} True if the paren should reject the space
* @returns {boolean} True if the paren is missing the required space
*/
function shouldCloserRejectSpace(left, right) {
if (astUtils.isOpeningParenToken(left)) {
function closerMissingSpace(left, right) {
if (sourceCode.isSpaceBetweenTokens(left, right)) {
return false;
}

if (ALWAYS) {
if (left.type !== "Punctuator") {
return !isCloserException(right);
}
return !isCloserException(left);
}

if (left.type !== "Punctuator") {
return isCloserException(right);
}
return isCloserException(left);
}

/**
* Determines if a closer paren has a space immediately before it
* @param {Object} left The token before the paren
* @param {Object} right The paren token
* @returns {boolean} True if the paren has a space before it
*/
function closerRejectsSpace(left, right) {
if (!astUtils.isTokenOnSameLine(left, right)) {
return false;
}
Expand All @@ -207,8 +229,14 @@ module.exports = {
}

if (ALWAYS) {
if (left.type !== "Punctuator") {
return isCloserException(right);
}
return isCloserException(left);
}
if (left.type !== "Punctuator") {
return !isCloserException(right);
}
return !isCloserException(left);

}
Expand All @@ -226,44 +254,53 @@ module.exports = {
const prevToken = tokens[i - 1];
const nextToken = tokens[i + 1];

// if token is not an opening or closing paren token, do nothing
if (!astUtils.isOpeningParenToken(token) && !astUtils.isClosingParenToken(token)) {
return;
}

if (token.value === "(" && shouldOpenerHaveSpace(token, nextToken)) {
// if token is an opening paren and is not followed by a required space
if (token.value === "(" && openerMissingSpace(token, nextToken)) {
context.report({
node,
loc: token.loc.start,
message: MISSING_SPACE_MESSAGE,
message: MISSING_OPENING_SPACE_MESSAGE,
fix(fixer) {
return fixer.insertTextAfter(token, " ");
}
});
} else if (token.value === "(" && shouldOpenerRejectSpace(token, nextToken)) {
}

// if token is an opening paren and is followed by a disallowed space
if (token.value === "(" && openerRejectsSpace(token, nextToken)) {
context.report({
node,
loc: token.loc.start,
message: REJECTED_SPACE_MESSAGE,
message: REJECTED_OPENING_SPACE_MESSAGE,
fix(fixer) {
return fixer.removeRange([token.range[1], nextToken.range[0]]);
}
});
} else if (token.value === ")" && shouldCloserHaveSpace(prevToken, token)) {
}

// context.report(node, token.loc.start, MISSING_SPACE_MESSAGE);
// if token is a closing paren and is not preceded by a required space
if (token.value === ")" && closerMissingSpace(prevToken, token)) {
context.report({
node,
loc: token.loc.start,
message: MISSING_SPACE_MESSAGE,
message: MISSING_CLOSING_SPACE_MESSAGE,
fix(fixer) {
return fixer.insertTextBefore(token, " ");
}
});
} else if (token.value === ")" && shouldCloserRejectSpace(prevToken, token)) {
}

// if token is a closing paren and is preceded by a disallowed space
if (token.value === ")" && closerRejectsSpace(prevToken, token)) {
context.report({
node,
loc: token.loc.start,
message: REJECTED_SPACE_MESSAGE,
message: REJECTED_CLOSING_SPACE_MESSAGE,
fix(fixer) {
return fixer.removeRange([prevToken.range[1], token.range[0]]);
}
Expand All @@ -272,6 +309,5 @@ module.exports = {
});
}
};

}
};

0 comments on commit 213b179

Please sign in to comment.