|
| 1 | +/** |
| 2 | + * @author Wayne Zhang |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const RuleTester = require('../../eslint-compat').RuleTester |
| 8 | +const rule = require('../../../lib/rules/no-negated-condition') |
| 9 | + |
| 10 | +const tester = new RuleTester({ |
| 11 | + languageOptions: { |
| 12 | + parser: require('vue-eslint-parser'), |
| 13 | + ecmaVersion: 2020, |
| 14 | + sourceType: 'module' |
| 15 | + } |
| 16 | +}) |
| 17 | + |
| 18 | +tester.run('no-negated-condition', rule, { |
| 19 | + valid: [ |
| 20 | + { |
| 21 | + filename: 'test.vue', |
| 22 | + code: ` |
| 23 | + <template> |
| 24 | + <div :class="foo ? 'baz' : 'bar'" /> |
| 25 | + </template> |
| 26 | + ` |
| 27 | + }, |
| 28 | + { |
| 29 | + filename: 'test.vue', |
| 30 | + code: ` |
| 31 | + <template> |
| 32 | + <div :style="{ color: isActive ? 'red' : 'blue' }" /> |
| 33 | + </template> |
| 34 | + ` |
| 35 | + } |
| 36 | + ], |
| 37 | + invalid: [ |
| 38 | + { |
| 39 | + filename: 'test.vue', |
| 40 | + code: ` |
| 41 | + <template> |
| 42 | + <div :class="!foo ? 'bar' : 'baz'" /> |
| 43 | + </template> |
| 44 | + `, |
| 45 | + errors: [ |
| 46 | + { |
| 47 | + message: 'Unexpected negated condition.', |
| 48 | + line: 3, |
| 49 | + column: 22, |
| 50 | + endLine: 3, |
| 51 | + endColumn: 42 |
| 52 | + } |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + filename: 'test.vue', |
| 57 | + code: ` |
| 58 | + <template> |
| 59 | + <div :style="{ |
| 60 | + 'display': !isVisible ? 'none' : 'block', |
| 61 | + 'color': 'red' |
| 62 | + }" /> |
| 63 | + </template> |
| 64 | + `, |
| 65 | + errors: [ |
| 66 | + { |
| 67 | + message: 'Unexpected negated condition.', |
| 68 | + line: 4, |
| 69 | + column: 22, |
| 70 | + endLine: 4, |
| 71 | + endColumn: 51 |
| 72 | + } |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + filename: 'test.vue', |
| 77 | + code: ` |
| 78 | + <template> |
| 79 | + <div :class="{ |
| 80 | + 'hidden': !foo ? true : false, |
| 81 | + 'visible': bar |
| 82 | + }" /> |
| 83 | + </template> |
| 84 | + `, |
| 85 | + errors: [ |
| 86 | + { |
| 87 | + message: 'Unexpected negated condition.', |
| 88 | + line: 4, |
| 89 | + column: 21, |
| 90 | + endLine: 4, |
| 91 | + endColumn: 40 |
| 92 | + } |
| 93 | + ] |
| 94 | + }, |
| 95 | + { |
| 96 | + filename: 'test.vue', |
| 97 | + code: ` |
| 98 | + <template> |
| 99 | + <div :disabled="!enabled ? true : false" /> |
| 100 | + </template> |
| 101 | + `, |
| 102 | + errors: [ |
| 103 | + { |
| 104 | + message: 'Unexpected negated condition.', |
| 105 | + line: 3, |
| 106 | + column: 25, |
| 107 | + endLine: 3, |
| 108 | + endColumn: 48 |
| 109 | + } |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + filename: 'test.vue', |
| 114 | + code: ` |
| 115 | + <template> |
| 116 | + <div :class="a !== b ? 'different' : 'same'" /> |
| 117 | + </template> |
| 118 | + `, |
| 119 | + errors: [ |
| 120 | + { |
| 121 | + message: 'Unexpected negated condition.', |
| 122 | + line: 3, |
| 123 | + column: 22, |
| 124 | + endLine: 3, |
| 125 | + endColumn: 52 |
| 126 | + } |
| 127 | + ] |
| 128 | + }, |
| 129 | + { |
| 130 | + filename: 'test.vue', |
| 131 | + code: ` |
| 132 | + <template> |
| 133 | + <div :class="a != b ? 'not-equal' : 'equal'" /> |
| 134 | + </template> |
| 135 | + `, |
| 136 | + errors: [ |
| 137 | + { |
| 138 | + message: 'Unexpected negated condition.', |
| 139 | + line: 3, |
| 140 | + column: 22, |
| 141 | + endLine: 3, |
| 142 | + endColumn: 52 |
| 143 | + } |
| 144 | + ] |
| 145 | + }, |
| 146 | + { |
| 147 | + filename: 'test.vue', |
| 148 | + code: ` |
| 149 | + <template> |
| 150 | + <div :style="{ |
| 151 | + opacity: !(x && y) ? 0 : 1 |
| 152 | + }" /> |
| 153 | + </template> |
| 154 | + `, |
| 155 | + errors: [ |
| 156 | + { |
| 157 | + message: 'Unexpected negated condition.', |
| 158 | + line: 4, |
| 159 | + column: 20, |
| 160 | + endLine: 4, |
| 161 | + endColumn: 37 |
| 162 | + } |
| 163 | + ] |
| 164 | + } |
| 165 | + ] |
| 166 | +}) |
0 commit comments