Skip to content

Commit

Permalink
Chore: Simplify space-infix-ops
Browse files Browse the repository at this point in the history
  • Loading branch information
madbence committed Oct 7, 2018
1 parent 066f7e0 commit 5ed8100
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions lib/rules/space-infix-ops.js
Expand Up @@ -34,14 +34,6 @@ module.exports = {

create(context) {
const int32Hint = context.options[0] ? context.options[0].int32Hint === true : false;

const OPERATORS = [
"*", "/", "%", "+", "-", "<<", ">>", ">>>", "<", "<=", ">", ">=", "in",
"instanceof", "==", "!=", "===", "!==", "&", "^", "|", "&&", "||", "=",
"+=", "-=", "*=", "/=", "%=", "<<=", ">>=", ">>>=", "&=", "^=", "|=",
"?", ":", ",", "**"
];

const sourceCode = context.getSourceCode();

/**
Expand All @@ -54,15 +46,17 @@ module.exports = {
function getFirstNonSpacedToken(left, right) {
const tokens = sourceCode.getTokensBetween(left, right, 1);

for (let i = 1, l = tokens.length - 1; i < l; ++i) {
const op = tokens[i];
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 (
(op.type === "Punctuator" || op.type === "Keyword") &&
OPERATORS.indexOf(op.value) >= 0 &&
(tokens[i - 1].range[1] >= op.range[0] || op.range[1] >= tokens[i + 1].range[0])
) {
return op;
if (!sourceCode.isSpaceBetweenTokens(prev, curr) || !sourceCode.isSpaceBetweenTokens(curr, next)) {
return curr;
}
}
return null;
Expand Down

0 comments on commit 5ed8100

Please sign in to comment.