Skip to content

Commit

Permalink
Fix: Space-infix-ops should ignore type annotations in TypeScript (#8341
Browse files Browse the repository at this point in the history
)

* 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
  • Loading branch information
soda0289 authored and ilyavolodin committed Jun 17, 2017
1 parent 46e73ee commit b7cc1e6
Show file tree
Hide file tree
Showing 8 changed files with 2,005 additions and 15 deletions.
14 changes: 8 additions & 6 deletions lib/rules/space-infix-ops.js
Expand Up @@ -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")) {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit b7cc1e6

Please sign in to comment.