|
| 1 | +import test from 'ava'; |
| 2 | +import avaRuleTester from 'eslint-ava-rule-tester'; |
| 3 | +import rule from '../rules/explicit-length-check'; |
| 4 | + |
| 5 | +const ruleTester = avaRuleTester(test, { |
| 6 | + env: { |
| 7 | + es6: true |
| 8 | + } |
| 9 | +}); |
| 10 | + |
| 11 | +const errors = [{ |
| 12 | + ruleId: 'explicit-length-check', |
| 13 | + message: '`length` property should be compared to a value' |
| 14 | +}]; |
| 15 | + |
| 16 | +ruleTester.run('explicit-length-check', rule, { |
| 17 | + valid: [ |
| 18 | + 'array.foo', |
| 19 | + 'array.length', |
| 20 | + 'array.length === 0', |
| 21 | + 'array.length !== 0', |
| 22 | + 'array.length > 0', |
| 23 | + 'if (array.foo) {}', |
| 24 | + 'if (length) {}', |
| 25 | + 'if ([].length > 0) {}', |
| 26 | + 'if ("".length > 0) {}', |
| 27 | + 'if (array.length === 0) {}', |
| 28 | + 'if (array.length !== 0) {}', |
| 29 | + 'if (array.length !== 0 && array[0] === 1) {}' |
| 30 | + ], |
| 31 | + invalid: [ |
| 32 | + { |
| 33 | + code: 'if ([].length) {}', |
| 34 | + errors |
| 35 | + }, |
| 36 | + { |
| 37 | + code: 'if ("".length) {}', |
| 38 | + errors |
| 39 | + }, |
| 40 | + { |
| 41 | + code: 'if (array.length) {}', |
| 42 | + errors |
| 43 | + }, |
| 44 | + { |
| 45 | + code: 'if (!array.length) {}', |
| 46 | + errors |
| 47 | + }, |
| 48 | + { |
| 49 | + code: 'if (array.foo.length) {}', |
| 50 | + errors |
| 51 | + }, |
| 52 | + { |
| 53 | + code: 'if (!!array.length) {}', |
| 54 | + errors |
| 55 | + }, |
| 56 | + { |
| 57 | + code: 'if (array.length && array[0] === 1) {}', |
| 58 | + errors |
| 59 | + }, |
| 60 | + { |
| 61 | + code: 'if (array[0] === 1 || array.length) {}', |
| 62 | + errors |
| 63 | + } |
| 64 | + ] |
| 65 | +}); |
0 commit comments