|
| 1 | +/** |
| 2 | + * @fileoverview prefer toHaveAttribute over checking getAttribute/hasAttribute |
| 3 | + * @author Ben Monro |
| 4 | + */ |
| 5 | +'use strict'; |
| 6 | + |
| 7 | +//------------------------------------------------------------------------------ |
| 8 | +// Rule Definition |
| 9 | +//------------------------------------------------------------------------------ |
| 10 | + |
| 11 | +module.exports = { |
| 12 | + meta: { |
| 13 | + docs: { |
| 14 | + description: |
| 15 | + 'prefer toHaveAttribute over checking getAttribute/hasAttribute ', |
| 16 | + recommended: false, |
| 17 | + }, |
| 18 | + fixable: 'code', |
| 19 | + }, |
| 20 | + |
| 21 | + create: function(context) { |
| 22 | + return { |
| 23 | + [`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeNull/]`]( |
| 24 | + node |
| 25 | + ) { |
| 26 | + context.report({ |
| 27 | + node: node.parent, |
| 28 | + message: `Use toHaveAttribute instead of asserting on getAttribute`, |
| 29 | + fix(fixer) { |
| 30 | + return [ |
| 31 | + fixer.removeRange([node.callee.object.end, node.end]), |
| 32 | + fixer.replaceTextRange( |
| 33 | + [ |
| 34 | + node.parent.parent.property.start, |
| 35 | + node.parent.parent.parent.end, |
| 36 | + ], |
| 37 | + `not.toHaveAttribute(${node.arguments[0].raw})` |
| 38 | + ), |
| 39 | + ]; |
| 40 | + }, |
| 41 | + }); |
| 42 | + }, |
| 43 | + [`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toContain$|toMatch$/]`]( |
| 44 | + node |
| 45 | + ) { |
| 46 | + context.report({ |
| 47 | + node: node.parent, |
| 48 | + message: `Use toHaveAttribute instead of asserting on getAttribute`, |
| 49 | + fix(fixer) { |
| 50 | + return [ |
| 51 | + fixer.removeRange([node.callee.object.end, node.end]), |
| 52 | + fixer.replaceText(node.parent.parent.property, 'toHaveAttribute'), |
| 53 | + fixer.replaceText( |
| 54 | + node.parent.parent.parent.arguments[0], |
| 55 | + `${ |
| 56 | + node.arguments[0].raw |
| 57 | + }, expect.string${node.parent.parent.property.name.slice( |
| 58 | + 2 |
| 59 | + )}ing(${node.parent.parent.parent.arguments[0].raw})` |
| 60 | + ), |
| 61 | + ]; |
| 62 | + }, |
| 63 | + }); |
| 64 | + }, |
| 65 | + [`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Strict)?Equal/]`]( |
| 66 | + node |
| 67 | + ) { |
| 68 | + const arg = node.parent.parent.parent.arguments; |
| 69 | + const isNullOrEmpty = |
| 70 | + arg.length > 0 && (arg[0].value === null || arg[0].value === ''); |
| 71 | + |
| 72 | + context.report({ |
| 73 | + node: node.parent, |
| 74 | + message: `Use toHaveAttribute instead of asserting on getAttribute`, |
| 75 | + fix(fixer) { |
| 76 | + let lastFixer; |
| 77 | + if (isNullOrEmpty) { |
| 78 | + lastFixer = fixer.replaceText( |
| 79 | + node.parent.parent.parent.arguments[0], |
| 80 | + node.arguments[0].raw |
| 81 | + ); |
| 82 | + } else { |
| 83 | + lastFixer = fixer.insertTextBefore( |
| 84 | + node.parent.parent.parent.arguments[0], |
| 85 | + `${node.arguments[0].raw}, ` |
| 86 | + ); |
| 87 | + } |
| 88 | + return [ |
| 89 | + fixer.removeRange([node.callee.object.end, node.end]), |
| 90 | + fixer.replaceText( |
| 91 | + node.parent.parent.property, |
| 92 | + `${isNullOrEmpty ? 'not.' : ''}toHaveAttribute` |
| 93 | + ), |
| 94 | + lastFixer, |
| 95 | + ]; |
| 96 | + }, |
| 97 | + }); |
| 98 | + }, |
| 99 | + [`CallExpression[callee.property.name='hasAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeNull|toBeUndefined|toBeDefined/]`]( |
| 100 | + node |
| 101 | + ) { |
| 102 | + context.report({ |
| 103 | + node: node.parent.parent.property, |
| 104 | + message: 'Invalid matcher for hasAttribute', |
| 105 | + }); |
| 106 | + }, |
| 107 | + [`CallExpression[callee.property.name='getAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeUndefined|toBeDefined/]`]( |
| 108 | + node |
| 109 | + ) { |
| 110 | + context.report({ |
| 111 | + node: node.parent.parent.property, |
| 112 | + message: 'Invalid matcher for getAttribute', |
| 113 | + }); |
| 114 | + }, |
| 115 | + [`CallExpression[callee.property.name='hasAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBe$|to(Striclty)?Equal/]`]( |
| 116 | + node |
| 117 | + ) { |
| 118 | + if (typeof node.parent.parent.parent.arguments[0].value === 'boolean') { |
| 119 | + context.report({ |
| 120 | + node: node.parent, |
| 121 | + message: `Use toHaveAttribute instead of asserting on hasAttribute`, |
| 122 | + fix(fixer) { |
| 123 | + return [ |
| 124 | + fixer.removeRange([node.callee.object.end, node.end]), |
| 125 | + fixer.replaceText( |
| 126 | + node.parent.parent.property, |
| 127 | + `${ |
| 128 | + node.parent.parent.parent.arguments[0].value === false |
| 129 | + ? 'not.' |
| 130 | + : '' |
| 131 | + }toHaveAttribute` |
| 132 | + ), |
| 133 | + fixer.replaceText( |
| 134 | + node.parent.parent.parent.arguments[0], |
| 135 | + node.arguments[0].raw |
| 136 | + ), |
| 137 | + ]; |
| 138 | + }, |
| 139 | + }); |
| 140 | + } else { |
| 141 | + context.report({ |
| 142 | + node: node.parent.parent.property, |
| 143 | + message: 'Invalid matcher for hasAttribute', |
| 144 | + }); |
| 145 | + } |
| 146 | + }, |
| 147 | + [`CallExpression[callee.property.name='hasAttribute'][parent.callee.name='expect'][parent.parent.property.name=/toBeTruthy|toBeFalsy/]`]( |
| 148 | + node |
| 149 | + ) { |
| 150 | + context.report({ |
| 151 | + node: node.parent, |
| 152 | + message: `Use toHaveAttribute instead of asserting on hasAttribute`, |
| 153 | + fix(fixer) { |
| 154 | + return [ |
| 155 | + fixer.removeRange([node.callee.object.end, node.end]), |
| 156 | + fixer.replaceTextRange( |
| 157 | + [ |
| 158 | + node.parent.parent.property.start, |
| 159 | + node.parent.parent.parent.end, |
| 160 | + ], |
| 161 | + `${ |
| 162 | + node.parent.parent.property.name === 'toBeFalsy' ? 'not.' : '' |
| 163 | + }toHaveAttribute(${node.arguments[0].raw})` |
| 164 | + ), |
| 165 | + ]; |
| 166 | + }, |
| 167 | + }); |
| 168 | + }, |
| 169 | + }; |
| 170 | + }, |
| 171 | +}; |
0 commit comments