Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
madbence committed Oct 10, 2018
1 parent 105b985 commit 7a25462
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions lib/rules/space-infix-ops.js
Expand Up @@ -40,25 +40,19 @@ module.exports = {
* Returns the first token which violates the rule
* @param {ASTNode} left - The left node of the main node
* @param {ASTNode} right - The right node of the main node
* @param {string} op - The operator of the main node
* @returns {Object} The violator token or null
* @private
*/
function getFirstNonSpacedToken(left, right) {
const tokens = sourceCode.getTokensBetween(left, right, 1);
function getFirstNonSpacedToken(left, right, op) {
const operator = sourceCode.getTokensBetween(left, right, token => token.value === op)[0];
const prev = sourceCode.getTokenBefore(operator);
const next = sourceCode.getTokenAfter(operator);

for (let i = 1; i < tokens.length - 1; i++) {
const prev = tokens[i - 1];
const curr = tokens[i];
const next = tokens[i + 1];

if (curr.value === "(" || curr.value === ")") {
continue;
}

if (!sourceCode.isSpaceBetweenTokens(prev, curr) || !sourceCode.isSpaceBetweenTokens(curr, next)) {
return curr;
}
if (!sourceCode.isSpaceBetweenTokens(prev, operator) || !sourceCode.isSpaceBetweenTokens(operator, next)) {
return operator;
}

return null;
}

Expand Down Expand Up @@ -104,7 +98,10 @@ module.exports = {
const leftNode = (node.left.typeAnnotation) ? node.left.typeAnnotation : node.left;
const rightNode = node.right;

const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode);
// search for = in AssignmentPattern nodes
const operator = node.operator || "=";

const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, operator);

if (nonSpacedNode) {
if (!(int32Hint && sourceCode.getText(node).endsWith("|0"))) {
Expand All @@ -120,8 +117,8 @@ module.exports = {
* @private
*/
function checkConditional(node) {
const nonSpacedConsequesntNode = getFirstNonSpacedToken(node.test, node.consequent);
const nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate);
const nonSpacedConsequesntNode = getFirstNonSpacedToken(node.test, node.consequent, "?");
const nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate, ":");

if (nonSpacedConsequesntNode) {
report(node, nonSpacedConsequesntNode);
Expand All @@ -141,7 +138,7 @@ module.exports = {
const rightNode = node.init;

if (rightNode) {
const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode);
const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, "=");

if (nonSpacedNode) {
report(node, nonSpacedNode);
Expand Down

0 comments on commit 7a25462

Please sign in to comment.