Skip to content

Commit

Permalink
Fix: Space-infix-ops should ignore type annotations in TypeScript
Browse files Browse the repository at this point in the history
Typescript allows type annotations in variable declarations. This commit
ensures they are skipped over and not considered when checking Variable
Declaration nodes.
  • Loading branch information
soda0289 committed Mar 26, 2017
1 parent 6a718ba commit 3aa5fba
Show file tree
Hide file tree
Showing 5 changed files with 1,196 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lib/rules/space-infix-ops.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,11 @@ module.exports = {
* @private
*/
function checkVar(node) {
if (node.init) {
const nonSpacedNode = getFirstNonSpacedToken(node.id, node.init);
const leftNode = (node.id.typeAnnotation) ? node.id.typeAnnotation : node.id;
const rightNode = node.init;

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

if (nonSpacedNode) {
report(node, nonSpacedNode);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,298 @@
/**
* Parser: typescript-eslint-parser v2.0.0 (TS 2.2.1)
* Source code:
* function foo(): Bar { }
*/

exports.parse = () => ({
"type": "Program",
"range": [
0,
23
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"body": [
{
"type": "FunctionDeclaration",
"range": [
0,
23
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 23
}
},
"id": {
"type": "Identifier",
"range": [
9,
12
],
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 12
}
},
"name": "foo"
},
"generator": false,
"expression": false,
"async": false,
"params": [],
"body": {
"type": "BlockStatement",
"range": [
20,
23
],
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 23
}
},
"body": []
},
"returnType": {
"type": "TypeAnnotation",
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 19
}
},
"range": [
16,
19
],
"typeAnnotation": {
"type": "TSTypeReference",
"range": [
16,
19
],
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 19
}
},
"typeName": {
"type": "Identifier",
"range": [
16,
19
],
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 19
}
},
"name": "Bar"
}
}
}
}
],
"sourceType": "script",
"tokens": [
{
"type": "Keyword",
"value": "function",
"start": 0,
"end": 8,
"range": [
0,
8
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
}
},
{
"type": "Identifier",
"value": "foo",
"start": 9,
"end": 12,
"range": [
9,
12
],
"loc": {
"start": {
"line": 1,
"column": 9
},
"end": {
"line": 1,
"column": 12
}
}
},
{
"type": "Punctuator",
"value": "(",
"start": 12,
"end": 13,
"range": [
12,
13
],
"loc": {
"start": {
"line": 1,
"column": 12
},
"end": {
"line": 1,
"column": 13
}
}
},
{
"type": "Punctuator",
"value": ")",
"start": 13,
"end": 14,
"range": [
13,
14
],
"loc": {
"start": {
"line": 1,
"column": 13
},
"end": {
"line": 1,
"column": 14
}
}
},
{
"type": "Punctuator",
"value": ":",
"start": 14,
"end": 15,
"range": [
14,
15
],
"loc": {
"start": {
"line": 1,
"column": 14
},
"end": {
"line": 1,
"column": 15
}
}
},
{
"type": "Identifier",
"value": "Bar",
"start": 16,
"end": 19,
"range": [
16,
19
],
"loc": {
"start": {
"line": 1,
"column": 16
},
"end": {
"line": 1,
"column": 19
}
}
},
{
"type": "Punctuator",
"value": "{",
"start": 20,
"end": 21,
"range": [
20,
21
],
"loc": {
"start": {
"line": 1,
"column": 20
},
"end": {
"line": 1,
"column": 21
}
}
},
{
"type": "Punctuator",
"value": "}",
"start": 22,
"end": 23,
"range": [
22,
23
],
"loc": {
"start": {
"line": 1,
"column": 22
},
"end": {
"line": 1,
"column": 23
}
}
}
],
"comments": []
});

0 comments on commit 3aa5fba

Please sign in to comment.