-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add prefer-to-have-length * AST helper
- Loading branch information
Showing
6 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Suggest using `toHaveLength()` (`prefer-to-have-length`) | ||
|
||
In order to have a better failure message, `toHaveLength()` should be used upon | ||
asserting expectations on objects length property. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if `toBe()`, `toEqual()` or `toStrictEqual()` is | ||
used to assert objects length property. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```javascript | ||
expect(files.length).toBe(1); | ||
expect(files.length).toEqual(1); | ||
expect(files.length).toStrictEqual(1); | ||
``` | ||
|
||
The following pattern is **not** a warning: | ||
|
||
```javascript | ||
expect(files).toHaveLength(1); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Rule } from 'eslint'; | ||
import { | ||
isExpectCall, | ||
getNodeName, | ||
getMatchers, | ||
isPropertyAccessor, | ||
} from '../utils/ast'; | ||
|
||
const matchers = new Set(['toBe', 'toEqual', 'toStrictEqual']); | ||
|
||
export default { | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (!isExpectCall(node)) { | ||
return; | ||
} | ||
|
||
const [argument] = node.arguments; | ||
const [matcher] = getMatchers(node).slice(-1); | ||
|
||
if ( | ||
!matcher || | ||
!matchers.has(getNodeName(matcher) ?? '') || | ||
argument?.type !== 'MemberExpression' || | ||
!isPropertyAccessor(argument, 'length') | ||
) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
fix(fixer) { | ||
return [ | ||
// remove the "length" property accessor | ||
fixer.removeRange([ | ||
argument.property.range![0] - 1, | ||
argument.range![1], | ||
]), | ||
// replace the current matcher with "toHaveLength" | ||
fixer.replaceText(matcher, 'toHaveLength'), | ||
]; | ||
}, | ||
messageId: 'useToHaveLength', | ||
node: matcher, | ||
}); | ||
}, | ||
}; | ||
}, | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Suggest using `toHaveLength()`', | ||
recommended: false, | ||
url: 'https://github.com/playwright-community/eslint-plugin-playwright/tree/main/docs/rules/prefer-to-have-length.md', | ||
}, | ||
messages: { | ||
useToHaveLength: 'Use toHaveLength() instead', | ||
}, | ||
fixable: 'code', | ||
type: 'suggestion', | ||
schema: [], | ||
}, | ||
} as Rule.RuleModule; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import rule from '../../src/rules/prefer-to-have-length'; | ||
import { runRuleTester } from '../utils/rule-tester'; | ||
|
||
runRuleTester('prefer-to-have-length', rule, { | ||
valid: [ | ||
'expect(files).toHaveLength(1)', | ||
"expect(files.name).toBe('file')", | ||
"expect(files['name']).toBe('file')", | ||
"expect(files[`name`]).toBe('file')", | ||
'expect(result).toBe(true)', | ||
`expect(user.getUserName(5)).not.toEqual('Paul')`, | ||
`expect(user.getUserName(5)).not.toEqual('Paul')`, | ||
'expect(a)', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'expect(files.length).toBe(1)', | ||
output: 'expect(files).toHaveLength(1)', | ||
errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(files.length).not.toBe(1)', | ||
output: 'expect(files).not.toHaveLength(1)', | ||
errors: [{ messageId: 'useToHaveLength', column: 26, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(files["length"]).not.toBe(1)', | ||
output: 'expect(files).not.toHaveLength(1)', | ||
errors: [{ messageId: 'useToHaveLength', column: 29, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(files["length"]).not.toBe(1)', | ||
output: 'expect(files).not.toHaveLength(1)', | ||
errors: [{ messageId: 'useToHaveLength', column: 29, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(files.length).toEqual(1)', | ||
output: 'expect(files).toHaveLength(1)', | ||
errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(files.length).toStrictEqual(1)', | ||
output: 'expect(files).toHaveLength(1)', | ||
errors: [{ messageId: 'useToHaveLength', column: 22, line: 1 }], | ||
}, | ||
{ | ||
code: 'expect(files.length).not.toStrictEqual(1)', | ||
output: 'expect(files).not.toHaveLength(1)', | ||
errors: [{ messageId: 'useToHaveLength', column: 26, line: 1 }], | ||
}, | ||
], | ||
}); |