From b7cc1e6fe995d52e581fcb2b1a44e37a18680e90 Mon Sep 17 00:00:00 2001 From: Reyad Attiyat Date: Fri, 16 Jun 2017 20:03:38 -0500 Subject: [PATCH] Fix: Space-infix-ops should ignore type annotations in TypeScript (#8341) * Fix: Space-infix-ops should ignore type annotations in TypeScript Typescript allows type annotations in variable declarations. This commit ensures they are skipped over and not considered when checking Variable Declaration nodes. For Assigment Expression Nodes we also check if it has a type annotation and we ensure it is skipped over. * Make test fixtures more generic --- lib/rules/space-infix-ops.js | 14 +- ...on-declaration-type-annotation-no-space.js | 486 +++++++++++++++ .../function-expression-type-annotation.js | 585 ++++++++++++++++++ .../function-parameter-type-annotation.js} | 10 +- .../function-return-type-annotation.js | 297 +++++++++ ...claration-init-type-annotation-no-space.js | 293 +++++++++ ...riable-declaration-init-type-annotation.js | 294 +++++++++ tests/lib/rules/space-infix-ops.js | 41 +- 8 files changed, 2005 insertions(+), 15 deletions(-) create mode 100644 tests/fixtures/parsers/type-annotations/function-declaration-type-annotation-no-space.js create mode 100644 tests/fixtures/parsers/type-annotations/function-expression-type-annotation.js rename tests/fixtures/parsers/{flow-stub-parser.js => type-annotations/function-parameter-type-annotation.js} (98%) create mode 100644 tests/fixtures/parsers/type-annotations/function-return-type-annotation.js create mode 100644 tests/fixtures/parsers/type-annotations/variable-declaration-init-type-annotation-no-space.js create mode 100644 tests/fixtures/parsers/type-annotations/variable-declaration-init-type-annotation.js diff --git a/lib/rules/space-infix-ops.js b/lib/rules/space-infix-ops.js index d919a1225a2..ad514b28f5f 100644 --- a/lib/rules/space-infix-ops.js +++ b/lib/rules/space-infix-ops.js @@ -106,11 +106,10 @@ module.exports = { * @private */ function checkBinary(node) { - if (node.left.typeAnnotation) { - return; - } + const leftNode = (node.left.typeAnnotation) ? node.left.typeAnnotation : node.left; + const rightNode = node.right; - const nonSpacedNode = getFirstNonSpacedToken(node.left, node.right); + const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode); if (nonSpacedNode) { if (!(int32Hint && sourceCode.getText(node).substr(-2) === "|0")) { @@ -143,8 +142,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); diff --git a/tests/fixtures/parsers/type-annotations/function-declaration-type-annotation-no-space.js b/tests/fixtures/parsers/type-annotations/function-declaration-type-annotation-no-space.js new file mode 100644 index 00000000000..452ce84a902 --- /dev/null +++ b/tests/fixtures/parsers/type-annotations/function-declaration-type-annotation-no-space.js @@ -0,0 +1,486 @@ +/** + * Source code: + * function foo(a: number=0): Foo { } + */ + +exports.parse = () => ({ + "type": "Program", + "range": [ + 0, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "body": [ + { + "type": "FunctionDeclaration", + "range": [ + 0, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "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": [ + { + "type": "AssignmentPattern", + "range": [ + 13, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "left": { + "type": "Identifier", + "range": [ + 13, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "range": [ + 16, + 22 + ], + "typeAnnotation": { + "type": "NumberKeyword", + "range": [ + 16, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 22 + } + } + } + } + }, + "right": { + "type": "Literal", + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + } + }, + "value": 0, + "raw": "0" + } + } + ], + "body": { + "type": "BlockStatement", + "range": [ + 31, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "body": [] + }, + "returnType": { + "type": "TypeAnnotation", + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "range": [ + 27, + 30 + ], + "typeAnnotation": { + "type": "TypeReference", + "range": [ + 27, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "typeName": { + "type": "Identifier", + "range": [ + 27, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "name": "Foo" + } + } + } + } + ], + "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": "Identifier", + "value": "a", + "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": "number", + "start": 16, + "end": 22, + "range": [ + 16, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 16 + }, + "end": { + "line": 1, + "column": 22 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "start": 22, + "end": 23, + "range": [ + 22, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + { + "type": "Numeric", + "value": "0", + "start": 23, + "end": 24, + "range": [ + 23, + 24 + ], + "loc": { + "start": { + "line": 1, + "column": 23 + }, + "end": { + "line": 1, + "column": 24 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "start": 24, + "end": 25, + "range": [ + 24, + 25 + ], + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 25 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "start": 25, + "end": 26, + "range": [ + 25, + 26 + ], + "loc": { + "start": { + "line": 1, + "column": 25 + }, + "end": { + "line": 1, + "column": 26 + } + } + }, + { + "type": "Identifier", + "value": "Foo", + "start": 27, + "end": 30, + "range": [ + 27, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 27 + }, + "end": { + "line": 1, + "column": 30 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "start": 31, + "end": 32, + "range": [ + 31, + 32 + ], + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 32 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "start": 33, + "end": 34, + "range": [ + 33, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 34 + } + } + } + ], + "comments": [] +}); diff --git a/tests/fixtures/parsers/type-annotations/function-expression-type-annotation.js b/tests/fixtures/parsers/type-annotations/function-expression-type-annotation.js new file mode 100644 index 00000000000..8f4f94c320d --- /dev/null +++ b/tests/fixtures/parsers/type-annotations/function-expression-type-annotation.js @@ -0,0 +1,585 @@ +/** + * Source code: + * const foo = function(a: number = 0): Bar { }; + */ + +exports.parse = () => ({ + "type": "Program", + "range": [ + 0, + 45 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "range": [ + 0, + 45 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 45 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "range": [ + 6, + 44 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "id": { + "type": "Identifier", + "range": [ + 6, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + }, + "name": "foo" + }, + "init": { + "type": "FunctionExpression", + "range": [ + 12, + 44 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "id": null, + "generator": false, + "params": [ + { + "type": "AssignmentPattern", + "range": [ + 21, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "left": { + "type": "Identifier", + "range": [ + 21, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 22 + } + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 30 + } + }, + "range": [ + 24, + 30 + ], + "typeAnnotation": { + "type": "NumberKeyword", + "range": [ + 24, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 30 + } + } + } + } + }, + "right": { + "type": "Literal", + "range": [ + 33, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 34 + } + }, + "value": 0, + "raw": "0" + } + } + ], + "body": { + "type": "BlockStatement", + "range": [ + 41, + 44 + ], + "loc": { + "start": { + "line": 1, + "column": 41 + }, + "end": { + "line": 1, + "column": 44 + } + }, + "body": [] + }, + "async": false, + "expression": false, + "returnType": { + "type": "TypeAnnotation", + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "range": [ + 37, + 40 + ], + "typeAnnotation": { + "type": "TypeReference", + "range": [ + 37, + 40 + ], + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "typeName": { + "type": "Identifier", + "range": [ + 37, + 40 + ], + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 40 + } + }, + "name": "Bar" + } + } + } + } + } + ], + "kind": "const" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "const", + "start": 0, + "end": 5, + "range": [ + 0, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Identifier", + "value": "foo", + "start": 6, + "end": 9, + "range": [ + 6, + 9 + ], + "loc": { + "start": { + "line": 1, + "column": 6 + }, + "end": { + "line": 1, + "column": 9 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "start": 10, + "end": 11, + "range": [ + 10, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "type": "Keyword", + "value": "function", + "start": 12, + "end": 20, + "range": [ + 12, + 20 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 20 + } + } + }, + { + "type": "Punctuator", + "value": "(", + "start": 20, + "end": 21, + "range": [ + 20, + 21 + ], + "loc": { + "start": { + "line": 1, + "column": 20 + }, + "end": { + "line": 1, + "column": 21 + } + } + }, + { + "type": "Identifier", + "value": "a", + "start": 21, + "end": 22, + "range": [ + 21, + 22 + ], + "loc": { + "start": { + "line": 1, + "column": 21 + }, + "end": { + "line": 1, + "column": 22 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "start": 22, + "end": 23, + "range": [ + 22, + 23 + ], + "loc": { + "start": { + "line": 1, + "column": 22 + }, + "end": { + "line": 1, + "column": 23 + } + } + }, + { + "type": "Identifier", + "value": "number", + "start": 24, + "end": 30, + "range": [ + 24, + 30 + ], + "loc": { + "start": { + "line": 1, + "column": 24 + }, + "end": { + "line": 1, + "column": 30 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "start": 31, + "end": 32, + "range": [ + 31, + 32 + ], + "loc": { + "start": { + "line": 1, + "column": 31 + }, + "end": { + "line": 1, + "column": 32 + } + } + }, + { + "type": "Numeric", + "value": "0", + "start": 33, + "end": 34, + "range": [ + 33, + 34 + ], + "loc": { + "start": { + "line": 1, + "column": 33 + }, + "end": { + "line": 1, + "column": 34 + } + } + }, + { + "type": "Punctuator", + "value": ")", + "start": 34, + "end": 35, + "range": [ + 34, + 35 + ], + "loc": { + "start": { + "line": 1, + "column": 34 + }, + "end": { + "line": 1, + "column": 35 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "start": 35, + "end": 36, + "range": [ + 35, + 36 + ], + "loc": { + "start": { + "line": 1, + "column": 35 + }, + "end": { + "line": 1, + "column": 36 + } + } + }, + { + "type": "Identifier", + "value": "Bar", + "start": 37, + "end": 40, + "range": [ + 37, + 40 + ], + "loc": { + "start": { + "line": 1, + "column": 37 + }, + "end": { + "line": 1, + "column": 40 + } + } + }, + { + "type": "Punctuator", + "value": "{", + "start": 41, + "end": 42, + "range": [ + 41, + 42 + ], + "loc": { + "start": { + "line": 1, + "column": 41 + }, + "end": { + "line": 1, + "column": 42 + } + } + }, + { + "type": "Punctuator", + "value": "}", + "start": 43, + "end": 44, + "range": [ + 43, + 44 + ], + "loc": { + "start": { + "line": 1, + "column": 43 + }, + "end": { + "line": 1, + "column": 44 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "start": 44, + "end": 45, + "range": [ + 44, + 45 + ], + "loc": { + "start": { + "line": 1, + "column": 44 + }, + "end": { + "line": 1, + "column": 45 + } + } + } + ], + "comments": [] +}); + diff --git a/tests/fixtures/parsers/flow-stub-parser.js b/tests/fixtures/parsers/type-annotations/function-parameter-type-annotation.js similarity index 98% rename from tests/fixtures/parsers/flow-stub-parser.js rename to tests/fixtures/parsers/type-annotations/function-parameter-type-annotation.js index 70178cccd03..341df31d867 100644 --- a/tests/fixtures/parsers/flow-stub-parser.js +++ b/tests/fixtures/parsers/type-annotations/function-parameter-type-annotation.js @@ -1,6 +1,9 @@ +/** + * Source code: + * function foo(a: number = 0) { } + */ -exports.parse = function() { - return { +exports.parse = () => ({ "type": "Program", "loc": { "source": null, @@ -387,5 +390,4 @@ exports.parse = function() { ] } ] - }; -}; +}); diff --git a/tests/fixtures/parsers/type-annotations/function-return-type-annotation.js b/tests/fixtures/parsers/type-annotations/function-return-type-annotation.js new file mode 100644 index 00000000000..73900d058cf --- /dev/null +++ b/tests/fixtures/parsers/type-annotations/function-return-type-annotation.js @@ -0,0 +1,297 @@ +/** + * 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": "TypeReference", + "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": [] +}); diff --git a/tests/fixtures/parsers/type-annotations/variable-declaration-init-type-annotation-no-space.js b/tests/fixtures/parsers/type-annotations/variable-declaration-init-type-annotation-no-space.js new file mode 100644 index 00000000000..d551f4e95db --- /dev/null +++ b/tests/fixtures/parsers/type-annotations/variable-declaration-init-type-annotation-no-space.js @@ -0,0 +1,293 @@ +/** + * Source code: + * var a: Foo= b; + */ + +exports.parse = () => ({ + "type": "Program", + "range": [ + 0, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "range": [ + 0, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 14 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "range": [ + 4, + 13 + ], + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "id": { + "type": "Identifier", + "range": [ + 4, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + } + }, + "name": "a", + "typeAnnotation": { + "type": "TypeAnnotation", + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "range": [ + 7, + 10 + ], + "typeAnnotation": { + "type": "TypeReference", + "range": [ + 7, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "typeName": { + "type": "Identifier", + "range": [ + 7, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + }, + "name": "Foo" + } + } + } + }, + "init": { + "type": "Identifier", + "range": [ + 12, + 13 + ], + "loc": { + "start": { + "line": 1, + "column": 12 + }, + "end": { + "line": 1, + "column": 13 + } + }, + "name": "b" + } + } + ], + "kind": "var" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "var", + "start": 0, + "end": 3, + "range": [ + 0, + 3 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "value": "a", + "start": 4, + "end": 5, + "range": [ + 4, + 5 + ], + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 5 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "start": 5, + "end": 6, + "range": [ + 5, + 6 + ], + "loc": { + "start": { + "line": 1, + "column": 5 + }, + "end": { + "line": 1, + "column": 6 + } + } + }, + { + "type": "Identifier", + "value": "Foo", + "start": 7, + "end": 10, + "range": [ + 7, + 10 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 10 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "start": 10, + "end": 11, + "range": [ + 10, + 11 + ], + "loc": { + "start": { + "line": 1, + "column": 10 + }, + "end": { + "line": 1, + "column": 11 + } + } + }, + { + "type": "Identifier", + "value": "b", + "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 + } + } + } + ], + "comments": [] +}); diff --git a/tests/fixtures/parsers/type-annotations/variable-declaration-init-type-annotation.js b/tests/fixtures/parsers/type-annotations/variable-declaration-init-type-annotation.js new file mode 100644 index 00000000000..abe1be7621e --- /dev/null +++ b/tests/fixtures/parsers/type-annotations/variable-declaration-init-type-annotation.js @@ -0,0 +1,294 @@ +/** + * Source code: + * var foo: Bar = ''; + */ + +exports.parse = () => ({ + "type": "Program", + "range": [ + 0, + 18 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "body": [ + { + "type": "VariableDeclaration", + "range": [ + 0, + 18 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 18 + } + }, + "declarations": [ + { + "type": "VariableDeclarator", + "range": [ + 4, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "id": { + "type": "Identifier", + "range": [ + 4, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + } + }, + "name": "foo", + "typeAnnotation": { + "type": "TypeAnnotation", + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "range": [ + 9, + 12 + ], + "typeAnnotation": { + "type": "TypeReference", + "range": [ + 9, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "typeName": { + "type": "Identifier", + "range": [ + 9, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + }, + "name": "Bar" + } + } + } + }, + "init": { + "type": "Literal", + "range": [ + 15, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 17 + } + }, + "value": "", + "raw": "''" + } + } + ], + "kind": "var" + } + ], + "sourceType": "script", + "tokens": [ + { + "type": "Keyword", + "value": "var", + "start": 0, + "end": 3, + "range": [ + 0, + 3 + ], + "loc": { + "start": { + "line": 1, + "column": 0 + }, + "end": { + "line": 1, + "column": 3 + } + } + }, + { + "type": "Identifier", + "value": "foo", + "start": 4, + "end": 7, + "range": [ + 4, + 7 + ], + "loc": { + "start": { + "line": 1, + "column": 4 + }, + "end": { + "line": 1, + "column": 7 + } + } + }, + { + "type": "Punctuator", + "value": ":", + "start": 7, + "end": 8, + "range": [ + 7, + 8 + ], + "loc": { + "start": { + "line": 1, + "column": 7 + }, + "end": { + "line": 1, + "column": 8 + } + } + }, + { + "type": "Identifier", + "value": "Bar", + "start": 9, + "end": 12, + "range": [ + 9, + 12 + ], + "loc": { + "start": { + "line": 1, + "column": 9 + }, + "end": { + "line": 1, + "column": 12 + } + } + }, + { + "type": "Punctuator", + "value": "=", + "start": 13, + "end": 14, + "range": [ + 13, + 14 + ], + "loc": { + "start": { + "line": 1, + "column": 13 + }, + "end": { + "line": 1, + "column": 14 + } + } + }, + { + "type": "String", + "value": "''", + "start": 15, + "end": 17, + "range": [ + 15, + 17 + ], + "loc": { + "start": { + "line": 1, + "column": 15 + }, + "end": { + "line": 1, + "column": 17 + } + } + }, + { + "type": "Punctuator", + "value": ";", + "start": 17, + "end": 18, + "range": [ + 17, + 18 + ], + "loc": { + "start": { + "line": 1, + "column": 17 + }, + "end": { + "line": 1, + "column": 18 + } + } + } + ], + "comments": [] +}); diff --git a/tests/lib/rules/space-infix-ops.js b/tests/lib/rules/space-infix-ops.js index 532a1b73909..9afa4881c13 100644 --- a/tests/lib/rules/space-infix-ops.js +++ b/tests/lib/rules/space-infix-ops.js @@ -9,9 +9,9 @@ // Requirements //------------------------------------------------------------------------------ -const path = require("path"), - rule = require("../../../lib/rules/space-infix-ops"), - RuleTester = require("../../../lib/testers/rule-tester"); +const rule = require("../../../lib/rules/space-infix-ops"), + RuleTester = require("../../../lib/testers/rule-tester"), + parser = require("../../fixtures/fixture-parser"); const ruleTester = new RuleTester(); @@ -30,10 +30,15 @@ ruleTester.run("space-infix-ops", rule, { { code: "const my_object = {key: 'value'};", parserOptions: { ecmaVersion: 6 } }, { code: "var {a = 0} = bar;", parserOptions: { ecmaVersion: 6 } }, { code: "function foo(a = 0) { }", parserOptions: { ecmaVersion: 6 } }, - { code: "function foo(a: number = 0) { }", parser: path.resolve(__dirname, "../../fixtures/parsers/flow-stub-parser.js"), parserOptions: { ecmaVersion: 6 } }, { code: "a ** b", parserOptions: { ecmaVersion: 7 } }, { code: "a|0", options: [{ int32Hint: true }] }, - { code: "a |0", options: [{ int32Hint: true }] } + { code: "a |0", options: [{ int32Hint: true }] }, + + // Type Annotations + { code: "function foo(a: number = 0) { }", parser: parser("type-annotations/function-parameter-type-annotation"), parserOptions: { ecmaVersion: 6 } }, + { code: "function foo(): Bar { }", parser: parser("type-annotations/function-return-type-annotation"), parserOptions: { ecmaVersion: 6 } }, + { code: "var foo: Bar = '';", parser: parser("type-annotations/variable-declaration-init-type-annotation"), parserOptions: { ecmaVersion: 6 } }, + { code: "const foo = function(a: number = 0): Bar { };", parser: parser("type-annotations/function-expression-type-annotation"), parserOptions: { ecmaVersion: 6 } } ], invalid: [ { @@ -347,6 +352,32 @@ ruleTester.run("space-infix-ops", rule, { column: 6, nodeType: "BinaryExpression" }] + }, + + // Type Annotations + + { + code: "var a: Foo= b;", + output: "var a: Foo = b;", + parser: parser("type-annotations/variable-declaration-init-type-annotation-no-space"), + errors: [{ + message: "Infix operators must be spaced.", + type: "VariableDeclarator", + line: 1, + column: 11 + }] + }, + { + code: "function foo(a: number=0): Foo { }", + output: "function foo(a: number = 0): Foo { }", + parser: parser("type-annotations/function-declaration-type-annotation-no-space"), + parserOptions: { ecmaVersion: 6 }, + errors: [{ + message: "Infix operators must be spaced.", + line: 1, + column: 23, + nodeType: "AssignmentPattern" + }] } ] });