Skip to content

Commit

Permalink
WIP attempt to fix empty exception logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Che Fisher committed May 26, 2019
1 parent d10ba6e commit 41e8a8b
Show file tree
Hide file tree
Showing 2 changed files with 500 additions and 455 deletions.
110 changes: 74 additions & 36 deletions lib/rules/space-in-parens.js
Expand Up @@ -49,7 +49,7 @@ module.exports = {
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",
REQUIRED = context.options[0] === "always",
exceptionsArrayOptions = (context.options[1] && context.options[1].exceptions) || [],
options = {};

Expand Down Expand Up @@ -132,65 +132,98 @@ module.exports = {
return false;
}

if (ALWAYS && !sourceCode.isSpaceBetweenTokens(left, right)) {
return !isOpenerException(right);
// if space required
if (REQUIRED) {
if (!sourceCode.isSpaceBetweenTokens(left, right)) {
return isOpenerException(right);
}

if (astUtils.isClosingParenToken(right)) {
return isCloserException(right);
}
}

if (astUtils.isClosingParenToken(right)) {
return !isCloserException(right);
}

if (sourceCode.isSpaceBetweenTokens(left, right)) {
return false;
return isOpenerException(right);
}

return isOpenerException(right);

}

/**
* 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 is missing the required space
* 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 has a space following it
*/
function closerMissingSpace(left, right) {
if (astUtils.isClosingParenToken(left)) {
function openerRejectsSpace(left, right) {
if (!astUtils.isTokenOnSameLine(left, right)) {
return false;
}

if (ALWAYS) {
if (sourceCode.isSpaceBetweenTokens(left, right)) {
return false;
if (right.type === "Line") {
return false;
}

if (REQUIRED) {
if (!sourceCode.isSpaceBetweenTokens(left, right)) {
return isOpenerException(right);
}

return !isCloserException(left);
if (astUtils.isClosingParenToken(right)) {
return !isCloserException(right);
}
}

return isCloserException(left);
if (astUtils.isClosingParenToken(right)) {
return isCloserException(right);
}

if (sourceCode.isSpaceBetweenTokens(left, right)) {
return isOpenerException(right);
}

return isOpenerException(right);

}

/**
* 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 has a space following 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 is missing the required space
*/
function openerRejectsSpace(left, right) {
function closerMissingSpace(left, right) {
if (!astUtils.isTokenOnSameLine(left, right)) {
return false;
}

if (right.type === "Line") {
return false;
if (REQUIRED) {
if (sourceCode.isSpaceBetweenTokens(left, right)) {
return !isCloserException(left);
}

// always allow "()", excepting "empty"
if (astUtils.isOpeningParenToken(left)) {
return isOpenerException(left);
}
}

if (ALWAYS && sourceCode.isSpaceBetweenTokens(left, right)) {
return isOpenerException(right);
// always allow "()", excepting "empty"
if (astUtils.isOpeningParenToken(left)) {
return !isOpenerException(left);
}

if (!sourceCode.isSpaceBetweenTokens(left, right)) {
return false;
if (sourceCode.isSpaceBetweenTokens(left, right)) {
return isCloserException(left);
}

return !isOpenerException(right);
return isCloserException(left);

}

Expand All @@ -201,23 +234,29 @@ module.exports = {
* @returns {boolean} True if the paren has a space before it
*/
function closerRejectsSpace(left, right) {
if (astUtils.isOpeningParenToken(left)) {
if (!astUtils.isTokenOnSameLine(left, right)) {
return false;
}

if (!astUtils.isTokenOnSameLine(left, right)) {
return false;
if (REQUIRED) {
if (sourceCode.isSpaceBetweenTokens(left, right)) {
return isCloserException(left);
}

if (astUtils.isOpeningParenToken(left)) {
return !isOpenerException(left);
}
}

if (ALWAYS && sourceCode.isSpaceBetweenTokens(left, right)) {
return isCloserException(left);
if (astUtils.isOpeningParenToken(left)) {
return isOpenerException(left);
}

if (!sourceCode.isSpaceBetweenTokens(left, right)) {
return false;
if (sourceCode.isSpaceBetweenTokens(left, right)) {
return isCloserException(left);
}

return !isCloserException(left);
return isCloserException(left);

}

Expand Down Expand Up @@ -289,6 +328,5 @@ module.exports = {
});
}
};

}
};

0 comments on commit 41e8a8b

Please sign in to comment.