Skip to content

Commit

Permalink
add fecs-space-infix-ops, see #46
Browse files Browse the repository at this point in the history
  • Loading branch information
chriswong committed Jan 14, 2015
1 parent 0b66774 commit 0908e12
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/js/eslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"fecs-key-spacing": [2, {"beforeColon": false, "afterColon": true, "align": "colon"}],
"fecs-eol-last": [2, true],
"fecs-camelcase": 2,
"fecs-space-infix-ops": 2,

"no-console": 1,
"no-constant-condition": 1,
Expand Down Expand Up @@ -111,7 +112,6 @@
"space-before-blocks": [2, "always"],
"space-in-brackets": [2, "never"],
"space-in-parens": [2, "never"],
"space-infix-ops": 2,
"space-return-throw-case": 1,
"space-unary-ops": 1,
"spaced-line-comment": [2, "always", {"exceptions": ["-", "+", ""]}],
Expand Down
67 changes: 67 additions & 0 deletions lib/js/rules/space-infix-ops.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* @file Require spaces around infix operators
* @author Michael Ficarra
*/
'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------

module.exports = function (context) {

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

function reportNoneSpace(left, right) {
var tokens = context.getTokensBetween(left, right, 1);
for (var i = 1, l = tokens.length - 1, op, noLeft, noRight; i < l; ++i) {
op = tokens[i];
if (op.type === 'Punctuator' && OPERATORS.indexOf(op.value) >= 0) {
noLeft = tokens[i - 1].range[1] >= op.range[0];
noRight = op.range[1] >= tokens[i + 1].range[0];
if (noLeft) {
report(op);
}

if (noRight) {
report(right);
}

if (noLeft || noRight) {
break;
}
}
}
}

function report(node) {
context.report(node, 'Infix operators must be spaced.');
}

function checkBinary(node) {
reportNoneSpace(node.left, node.right);
}

function checkConditional(node) {
reportNoneSpace(node.test, node.consequent);
reportNoneSpace(node.consequent, node.alternate);
}

function checkVar(node) {
node.init && reportNoneSpace(node.id, node.init);
}

return {
AssignmentExpression: checkBinary,
BinaryExpression: checkBinary,
LogicalExpression: checkBinary,
ConditionalExpression: checkConditional,
VariableDeclarator: checkVar
};

};
2 changes: 1 addition & 1 deletion lib/reporter/baidu/eslint-map.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"fecs-key-spacing": "008",
"fecs-eol-last": "002",
"fecs-camelcase": "025",
"fecs-space-infix-ops": "005",

"no-console": "997",
"no-constant-condition": "997",
Expand Down Expand Up @@ -80,7 +81,6 @@
"space-before-blocks": "006",
"space-in-brackets": "012",
"space-in-parens": "012",
"space-infix-ops": "005",
"space-return-throw-case": "997",
"space-unary-ops": "997",
"spaced-line-comment": "038",
Expand Down

0 comments on commit 0908e12

Please sign in to comment.