import * as path from 'path' ; import { RuleTester } from '../rule-tester' ; import rule = require ( '../../src/rules/no-collection-size-mischeck' ) ; const ruleTester = new RuleTester ( { parser : '@typescript-eslint/parser' , } ) ; ruleTester . run ( 'Collection sizes and array length comparisons should make sense' , rule , { valid : [ { code : `
      if (collections.length < 1)    {}
      if (collections.length > 0)    {}
      if (collections.length <= 1)   {}
      if (collections.length >= 1)   {}
      if (collections.length < 50)   {}
      if (collections.length < 5 + 0){}
      if (collections.size() >= 0)   {}
      ` , } , ] , invalid : [ { code : `if (collection.size < 0) {}` , errors : [ { messageId : 'fixCollectionSizeCheck' , data : { propertyName : 'size' , objectName : 'collection' , } , line : 1 , endLine : 1 , column : 5 , endColumn : 24 , } , ] , } , { code : `if (collection.length < 0) {}` , errors : [ { messageId : 'fixCollectionSizeCheck' , data : { propertyName : 'length' , objectName : 'collection' , } , line : 1 , endLine : 1 , column : 5 , endColumn : 26 , } , ] , } , { code : `if (collection.length >= 0) {}` , errors : [ { messageId : 'fixCollectionSizeCheck' , data : { propertyName : 'length' , objectName : 'collection' , } , line : 1 , endLine : 1 , column : 5 , endColumn : 27 , } , ] , } , ] , } ) ; const ruleTesterTs = new RuleTester ( { parser : '@typescript-eslint/parser' , parserOptions : { ecmaVersion : 2018 , project : path . resolve ( `${ __dirname }/../resources/tsconfig.json` ) , } , } ) ; const filename = path . resolve ( `${ __dirname }/../resources/file.ts` ) ; ruleTesterTs . run ( 'Collection sizes and array length comparisons should make sense' , rule , { valid : [ { code : `
      const arr = [];
      if (arr.length < 1)  {}
      if (arr.length > 0)  {}
      if (arr.length <= 1) {}
      if (arr.length >= 1) {}
      if (arr.length < 50) {}
      if (arr.length < 5 + 0) {}
      ` , filename , } , { code : `
      const obj = {length: -4, size: -5, foobar: 42};
      if (obj.foobar >= 0) {}
      if (obj.size >= 0)   {}
      if (obj.length >= 0) {}
      if (obj.length < 0)  {}
      if (obj.length < 53) {}
      if (obj.length > 0)  {}
      ` , filename , } , ] , invalid : [ { code : `
      const arr = [];
      if (arr.length < 0) {}
      ` , filename , errors : [ { messageId : 'fixCollectionSizeCheck' , data : { propertyName : 'length' , objectName : 'arr' , } , line : 3 , endLine : 3 , column : 11 , endColumn : 25 , } , ] , } , { code : `
      const arr = [];
      if (arr.length >= 0) {}
      ` , filename , errors : [ { messageId : 'fixCollectionSizeCheck' , data : { propertyName : 'length' , objectName : 'arr' , } , line : 3 , endLine : 3 , column : 11 , endColumn : 26 , } , ] , } , { code : `
      const arr = new Array();
      if (arr.length >= 0) {}
      ` , filename , errors : [ { messageId : 'fixCollectionSizeCheck' , data : { propertyName : 'length' , objectName : 'arr' , } , line : 3 , endLine : 3 , column : 11 , endColumn : 26 , } , ] , } , { code : `
      const set = new Set();
      if (set.length < 0) {}
      ` , filename , errors : [ { messageId : 'fixCollectionSizeCheck' , data : { propertyName : 'length' , objectName : 'set' , } , line : 3 , endLine : 3 , column : 11 , endColumn : 25 , } , ] , } , { code : `
      const map = new Map();
      if (map.length < 0) {}
      ` , filename , errors : [ { messageId : 'fixCollectionSizeCheck' , data : { propertyName : 'length' , objectName : 'map' , } , line : 3 , endLine : 3 , column : 11 , endColumn : 25 , } , ] , } , { code : `
      const set = new WeakSet();
      if (set.length < 0) {}
      ` , filename , errors : [ { messageId : 'fixCollectionSizeCheck' , data : { propertyName : 'length' , objectName : 'set' , } , line : 3 , endLine : 3 , column : 11 , endColumn : 25 , } , ] , } , { code : `
      const map = new WeakMap();
      if (map.length < 0) {}
      ` , filename , errors : [ { messageId : 'fixCollectionSizeCheck' , data : { propertyName : 'length' , objectName : 'map' , } , line : 3 , endLine : 3 , column : 11 , endColumn : 25 , } , ] , } , ] , } ) ;