-
Notifications
You must be signed in to change notification settings - Fork 49
Add no-incorrect-deep-equal
rule
#264
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sindresorhus
merged 9 commits into
avajs:master
from
MrHen:feature/158-no-incorrect-deep-equal
Jul 6, 2019
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
47e77aa
Add no-incorrect-deep-equal rule
MrHen b402ce9
Apply suggestions from code review
MrHen 54847f1
PR feedback
MrHen 2670480
Update no-incorrect-deep-equal.md
sindresorhus 521889c
Merge branch 'master' into feature/158-no-incorrect-deep-equal
MrHen deae648
Remove redundant messaging
MrHen 1cf9f5f
PR feedback
MrHen 1a5c105
Merge branch 'master' into feature/158-no-incorrect-deep-equal
MrHen c96ca4c
Lint
MrHen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,28 @@ | ||
# Avoid using `deepEqual` with primitives | ||
|
||
The `deepEqual` and `notDeepEqual` assertions are unnecessary when comparing primitives. Use `is` or `not` instead. | ||
|
||
This rule is fixable. | ||
|
||
## Fail | ||
|
||
```js | ||
t.deepEqual(expression, 'foo'); | ||
t.deepEqual(expression, 1); | ||
t.deepEqual(expression, `foo${bar}`); | ||
t.deepEqual(expression, null); | ||
t.deepEqual(expression, undefined); | ||
t.notDeepEqual(expression, undefined); | ||
``` | ||
|
||
|
||
## Pass | ||
|
||
```js | ||
t.is(expression, 'foo'); | ||
|
||
t.deepEqual(expression, otherExpression); | ||
t.deepEqual(expression, {}); | ||
t.deepEqual(expression, []); | ||
t.notDeepEqual(expression, []); | ||
``` |
This file contains hidden or 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 hidden or 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 hidden or 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,125 @@ | ||
'use strict'; | ||
const {visitIf} = require('enhance-visitors'); | ||
const util = require('../util'); | ||
const createAvaRule = require('../create-ava-rule'); | ||
|
||
const MESSAGE_ID_LITERAL = 'no-deep-equal-with-literal'; | ||
const MESSAGE_ID_TEMPLATE = 'no-deep-equal-with-template'; | ||
const MESSAGE_ID_UNDEFINED = 'no-deep-equal-with-undefined'; | ||
|
||
const fixIs = (fixer, node) => { | ||
return fixer.replaceText(node.callee.property, 'is'); | ||
}; | ||
|
||
const fixNot = (fixer, node) => { | ||
return fixer.replaceText(node.callee.property, 'not'); | ||
}; | ||
|
||
const create = context => { | ||
const ava = createAvaRule(); | ||
|
||
const callExpression = 'CallExpression'; | ||
const deepEqual = '[callee.property.name="deepEqual"]'; | ||
const notDeepEqual = '[callee.property.name="notDeepEqual"]'; | ||
|
||
const argumentsLiteral = ':matches([arguments.0.type="Literal"],[arguments.1.type="Literal"])'; | ||
const argumentsUndefined = ':matches([arguments.0.type="Identifier"][arguments.0.name="undefined"],[arguments.1.type="Identifier"][arguments.1.name="undefined"])'; | ||
const argumentsTemplate = ':matches([arguments.0.type="TemplateLiteral"],[arguments.1.type="TemplateLiteral"])'; | ||
|
||
return ava.merge({ | ||
[`${callExpression}${deepEqual}${argumentsLiteral}`]: visitIf([ | ||
ava.isInTestFile, | ||
ava.isInTestNode | ||
])(node => { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_LITERAL, | ||
data: { | ||
callee: node.callee.property.name | ||
}, | ||
fix: fixer => fixIs(fixer, node) | ||
}); | ||
}), | ||
[`${callExpression}${deepEqual}${argumentsUndefined}`]: visitIf([ | ||
ava.isInTestFile, | ||
ava.isInTestNode | ||
])(node => { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_UNDEFINED, | ||
data: { | ||
callee: node.callee.property.name | ||
}, | ||
fix: fixer => fixIs(fixer, node) | ||
}); | ||
}), | ||
[`${callExpression}${deepEqual}${argumentsTemplate}`]: visitIf([ | ||
ava.isInTestFile, | ||
ava.isInTestNode | ||
])(node => { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_TEMPLATE, | ||
data: { | ||
callee: node.callee.property.name | ||
}, | ||
fix: fixer => fixIs(fixer, node) | ||
}); | ||
}), | ||
[`${callExpression}${notDeepEqual}${argumentsLiteral}`]: visitIf([ | ||
ava.isInTestFile, | ||
ava.isInTestNode | ||
])(node => { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_LITERAL, | ||
data: { | ||
callee: node.callee.property.name | ||
}, | ||
fix: fixer => fixNot(fixer, node) | ||
}); | ||
}), | ||
[`${callExpression}${notDeepEqual}${argumentsUndefined}`]: visitIf([ | ||
ava.isInTestFile, | ||
ava.isInTestNode | ||
])(node => { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_UNDEFINED, | ||
data: { | ||
callee: node.callee.property.name | ||
}, | ||
fix: fixer => fixNot(fixer, node) | ||
}); | ||
}), | ||
[`${callExpression}${notDeepEqual}${argumentsTemplate}`]: visitIf([ | ||
ava.isInTestFile, | ||
ava.isInTestNode | ||
])(node => { | ||
context.report({ | ||
node, | ||
messageId: MESSAGE_ID_TEMPLATE, | ||
data: { | ||
callee: node.callee.property.name | ||
}, | ||
fix: fixer => fixNot(fixer, node) | ||
}); | ||
}) | ||
}); | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
docs: { | ||
url: util.getDocsUrl(__filename) | ||
}, | ||
fixable: true, | ||
messages: { | ||
[MESSAGE_ID_LITERAL]: 'Avoid using `{{callee}}` with literals', | ||
sindresorhus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[MESSAGE_ID_TEMPLATE]: 'Avoid using `{{callee}}` with templates', | ||
[MESSAGE_ID_UNDEFINED]: 'Avoid using `{{callee}}` with `undefined`' | ||
}, | ||
type: 'suggestion' | ||
} | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.