Skip to content

Commit

Permalink
Merge pull request #2067 from Kirill89/add-space-infix-ops-options
Browse files Browse the repository at this point in the history
New: Added option int32Hint for space-infix-ops (fixes #1295)
  • Loading branch information
nzakas committed Mar 18, 2015
2 parents 66b43cb + 09bd11c commit cd4e45d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
18 changes: 17 additions & 1 deletion docs/rules/space-infix-ops.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,20 @@ a + b

```js
a ? b : c
```
```

### Options

This rule accepts a single options argument with the following defaults:

```json
"space-infix-ops": [2, {"int32Hint": false}]
```

### `int32Hint`

Set the `int32Hint` option to `true` (default is `false`) to allow write `a|0` without space.

```js
var foo = bar|0; // `foo` is forced to be signed 32 bit integer
```
5 changes: 4 additions & 1 deletion lib/rules/space-infix-ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
//------------------------------------------------------------------------------

module.exports = function(context) {
var int32Hint = context.options[0] ? context.options[0].int32Hint === true : false;

var OPERATORS = [
"*", "/", "%", "+", "-", "<<", ">>", ">>>", "<", "<=", ">", ">=", "in",
Expand Down Expand Up @@ -38,7 +39,9 @@ module.exports = function(context) {

function checkBinary(node) {
if (!isSpaced(node.left, node.right)) {
report(node);
if (!(int32Hint && context.getSource(node).substr(-2) === "|0")) {
report(node);
}
}
}

Expand Down
7 changes: 5 additions & 2 deletions tests/lib/rules/space-infix-ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ eslintTester.addRuleTest("lib/rules/space-infix-ops", {
"(a + b) + (c + d)",
"a = b",
"a ? b : c",
"var a = b"
"var a = b",
{ code: "a|0", args: [2, { int32Hint: true }] },
{ code: "a |0", args: [2, { int32Hint: true }] }
],
invalid: [
{ code: "a+b", errors: [{ message: "Infix operators must be spaced.", type: "BinaryExpression" }] },
Expand All @@ -46,6 +48,7 @@ eslintTester.addRuleTest("lib/rules/space-infix-ops", {
{ code: "var a=b;", errors: [{ message: "Infix operators must be spaced.", type: "VariableDeclarator" }] },
{ code: "var a= b;", errors: [{ message: "Infix operators must be spaced.", type: "VariableDeclarator" }] },
{ code: "var a =b;", errors: [{ message: "Infix operators must be spaced.", type: "VariableDeclarator" }] },
{ code: "var a = b, c=d;", errors: [{ message: "Infix operators must be spaced.", type: "VariableDeclarator" }] }
{ code: "var a = b, c=d;", errors: [{ message: "Infix operators must be spaced.", type: "VariableDeclarator" }] },
{ code: "a| 0", args: [2, { int32Hint: true }], errors: [{ message: "Infix operators must be spaced.", type: "BinaryExpression" }] }
]
});

0 comments on commit cd4e45d

Please sign in to comment.