-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create
prefer-to-be-object
rule
- Loading branch information
Showing
6 changed files
with
260 additions
and
1 deletion.
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,44 @@ | ||
# Suggest using `toBeObject()` (prefer-to-be-object) | ||
|
||
For expecting a value to be an object, `jest-extended` provides the `toBeObject` | ||
matcher. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if an `expect` assertion is found asserting that a | ||
value is an object using one of the following methods: | ||
|
||
- Comparing the result of `<value> instanceof Object` to a boolean value, | ||
- Calling the `toBeInstanceOf` matcher with the `Object` class. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
expect([] instanceof Object).toBe(true); | ||
|
||
expect(myValue instanceof Object).toStrictEqual(false); | ||
|
||
expect(theResults() instanceof Object).not.toBeFalse(); | ||
|
||
expect([]).toBeInstanceOf(true); | ||
|
||
expect(myValue).resolves.toBeInstanceOf(Object); | ||
|
||
expect(theResults()).not.toBeInstanceOf(Object); | ||
``` | ||
|
||
The following patterns are _not_ considered warnings: | ||
|
||
```js | ||
expect({}).toBeObject(); | ||
|
||
expect(myValue).not.toBeObject(); | ||
|
||
expect(queryApi()).resolves.toBeObject(); | ||
|
||
expect(theResults()).toBeObject(); | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [`jest-extended#toBeObject` matcher](https://github.com/jest-community/jest-extended#tobeobject) |
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
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,99 @@ | ||
import { TSESLint } from '@typescript-eslint/experimental-utils'; | ||
import rule, { MessageIds, Options } from '../prefer-to-be-object'; | ||
|
||
const ruleTester = new TSESLint.RuleTester(); | ||
|
||
// makes ts happy about the dynamic test generation | ||
const messageId = 'preferToBeObject' as const; | ||
|
||
const createTestsForEqualityMatchers = (): Array<TSESLint.InvalidTestCase< | ||
MessageIds, | ||
Options | ||
>> => | ||
['toBe', 'toEqual', 'toStrictEqual'] | ||
.map(matcher => [ | ||
{ | ||
code: `expect({} instanceof Object).${matcher}(true);`, | ||
errors: [{ messageId, column: 30, line: 1 }], | ||
output: `expect({}).toBeObject();`, | ||
}, | ||
{ | ||
code: `expect({} instanceof Object).not.${matcher}(true);`, | ||
errors: [{ messageId, column: 34, line: 1 }], | ||
output: `expect({}).not.toBeObject();`, | ||
}, | ||
{ | ||
code: `expect({} instanceof Object).${matcher}(false);`, | ||
errors: [{ messageId, column: 30, line: 1 }], | ||
output: `expect({}).not.toBeObject();`, | ||
}, | ||
{ | ||
code: `expect({} instanceof Object).not.${matcher}(false);`, | ||
errors: [{ messageId, column: 34, line: 1 }], | ||
output: `expect({}).toBeObject();`, | ||
}, | ||
]) | ||
.reduce((arr, cur) => arr.concat(cur), []); | ||
|
||
ruleTester.run('prefer-to-be-object', rule, { | ||
valid: [ | ||
'expect.hasAssertions', | ||
'expect', | ||
'expect().not', | ||
'expect().toBe', | ||
'expect().toBe(true)', | ||
'expect({}).toBe(true)', | ||
'expect({}).toBeObject()', | ||
'expect({}).not.toBeObject()', | ||
'expect([] instanceof Array).not.toBeObject()', | ||
'expect({}).not.toBeInstanceOf(Array)', | ||
], | ||
invalid: [ | ||
...createTestsForEqualityMatchers(), | ||
{ | ||
code: 'expect(({} instanceof Object)).toBeTrue();', | ||
errors: [{ messageId, column: 32, line: 1 }], | ||
output: 'expect(({})).toBeObject();', | ||
}, | ||
{ | ||
code: 'expect({} instanceof Object).toBeTrue();', | ||
errors: [{ messageId, column: 30, line: 1 }], | ||
output: 'expect({}).toBeObject();', | ||
}, | ||
{ | ||
code: 'expect({} instanceof Object).not.toBeTrue();', | ||
errors: [{ messageId, column: 34, line: 1 }], | ||
output: 'expect({}).not.toBeObject();', | ||
}, | ||
{ | ||
code: 'expect({} instanceof Object).toBeFalse();', | ||
errors: [{ messageId, column: 30, line: 1 }], | ||
output: 'expect({}).not.toBeObject();', | ||
}, | ||
{ | ||
code: 'expect({} instanceof Object).not.toBeFalse();', | ||
errors: [{ messageId, column: 34, line: 1 }], | ||
output: 'expect({}).toBeObject();', | ||
}, | ||
{ | ||
code: 'expect({}).toBeInstanceOf(Object);', | ||
errors: [{ messageId, column: 12, line: 1 }], | ||
output: 'expect({}).toBeObject();', | ||
}, | ||
{ | ||
code: 'expect({}).not.toBeInstanceOf(Object);', | ||
errors: [{ messageId, column: 16, line: 1 }], | ||
output: 'expect({}).not.toBeObject();', | ||
}, | ||
{ | ||
code: 'expect(requestValues()).resolves.toBeInstanceOf(Object);', | ||
errors: [{ messageId, column: 34, line: 1 }], | ||
output: 'expect(requestValues()).resolves.toBeObject();', | ||
}, | ||
{ | ||
code: 'expect(queryApi()).resolves.not.toBeInstanceOf(Object);', | ||
errors: [{ messageId, column: 33, line: 1 }], | ||
output: 'expect(queryApi()).resolves.not.toBeObject();', | ||
}, | ||
], | ||
}); |
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,113 @@ | ||
import { AST_NODE_TYPES } from '@typescript-eslint/experimental-utils'; | ||
import { | ||
ModifierName, | ||
createRule, | ||
followTypeAssertionChain, | ||
isBooleanEqualityMatcher, | ||
isExpectCall, | ||
isInstanceOfBinaryExpression, | ||
isParsedInstanceOfMatcherCall, | ||
parseExpectCall, | ||
} from './utils'; | ||
|
||
export type MessageIds = 'preferToBeObject'; | ||
export type Options = []; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: __filename, | ||
meta: { | ||
docs: { | ||
category: 'Stylistic Issues', | ||
description: 'Suggest using `toBeObject()`', | ||
recommended: false, | ||
}, | ||
messages: { | ||
preferToBeObject: | ||
'Prefer using `toBeObject()` to test if a value is an Object.', | ||
}, | ||
fixable: 'code', | ||
type: 'suggestion', | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (!isExpectCall(node)) { | ||
return; | ||
} | ||
|
||
const { expect, modifier, matcher } = parseExpectCall(node); | ||
|
||
if (!matcher) { | ||
return; | ||
} | ||
|
||
if (isParsedInstanceOfMatcherCall(matcher, 'Object')) { | ||
context.report({ | ||
node: matcher.node.property, | ||
messageId: 'preferToBeObject', | ||
fix: fixer => [ | ||
fixer.replaceTextRange( | ||
[ | ||
matcher.node.property.range[0], | ||
matcher.node.property.range[1] + '(Object)'.length, | ||
], | ||
'toBeObject()', | ||
), | ||
], | ||
}); | ||
|
||
return; | ||
} | ||
|
||
const [expectArg] = expect.arguments; | ||
|
||
if ( | ||
!expectArg || | ||
!isBooleanEqualityMatcher(matcher) || | ||
!isInstanceOfBinaryExpression(expectArg, 'Object') | ||
) { | ||
return; | ||
} | ||
|
||
context.report({ | ||
node: matcher.node.property, | ||
messageId: 'preferToBeObject', | ||
fix(fixer) { | ||
const fixes = [ | ||
fixer.replaceText(matcher.node.property, 'toBeObject'), | ||
fixer.removeRange([expectArg.left.range[1], expectArg.range[1]]), | ||
]; | ||
|
||
let invertCondition = matcher.name === 'toBeFalse'; | ||
|
||
if (matcher.arguments?.length) { | ||
const [matcherArg] = matcher.arguments; | ||
|
||
fixes.push(fixer.remove(matcherArg)); | ||
|
||
// toBeFalse can't have arguments, so this won't be true beforehand | ||
invertCondition = | ||
matcherArg.type === AST_NODE_TYPES.Literal && | ||
followTypeAssertionChain(matcherArg).value === false; | ||
} | ||
|
||
if (invertCondition) { | ||
fixes.push( | ||
modifier && modifier.name === ModifierName.not | ||
? fixer.removeRange([ | ||
modifier.node.property.range[0] - 1, | ||
modifier.node.property.range[1], | ||
]) | ||
: fixer.insertTextBefore(matcher.node.property, 'not.'), | ||
); | ||
} | ||
|
||
return fixes; | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |