-
Notifications
You must be signed in to change notification settings - Fork 150
feat: add no-node-access rule #190
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
Merged
Changes from 21 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
b0a1157
Merge remote-tracking branch 'upstream/v4' into no-node-access
thebinaryfelix 9ebf44e
refactor(utils): add properties and methods
thebinaryfelix 4d41edc
test(no-node-access): add first scenarios
thebinaryfelix 9ebf3d2
feat(no-node-access): add rule
thebinaryfelix cba1f13
test(no-node-access): add scenarios
thebinaryfelix 5092889
refactor(no-node-access): simplify conditions
thebinaryfelix f63f2b0
refactor(no-node-access): add scenario
thebinaryfelix c1ecd66
refactor(no-node-access): remove conditional
thebinaryfelix 934bc1d
refactor(utils): add DOM properties
thebinaryfelix 70a366e
refactor(no-node-access): add scenario
thebinaryfelix 291eddf
docs(no-node-access): add readme
thebinaryfelix e3328ee
refactor(utils): export const
thebinaryfelix 6cc5648
docs(no-node-access): fix file location
thebinaryfelix af0ed98
docs(readme): add no-node-access
thebinaryfelix 8f545d1
refactor(no-node-access): change highlight location
thebinaryfelix 145a89b
docs(no-node-access): fix typo
thebinaryfelix bd13e95
refactor(utils): add missing property
thebinaryfelix f5eacca
refactor(no-node-access): simplify checks
thebinaryfelix 9415c3b
test(no-node-access): add more scenarios
thebinaryfelix 0f1cd36
docs(no-node-access): update examples
thebinaryfelix ba40a64
Merge branch 'v4' of https://github.com/testing-library/eslint-plugin…
thebinaryfelix 9d4f356
refactor(no-node-access): narrow error cases
thebinaryfelix ecb12fe
refactor(no-node-access): check imports
thebinaryfelix eb8d4a3
refactor(no-node-access): rename variable
thebinaryfelix 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
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,42 @@ | ||
# Disallow direct Node access (no-node-access) | ||
|
||
The Testing Library already provides methods for querying DOM elements. | ||
|
||
## Rule Details | ||
|
||
This rule aims to disallow DOM traversal using native HTML methods and properties, such as `closest`, `lastChild` and all that returns another Node element from an HTML tree. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
screen.getByText('Submit').closest('button'); // chaining with Testing Library methods | ||
``` | ||
|
||
```js | ||
const buttons = screen.getAllByRole('button'); | ||
expect(buttons[1].lastChild).toBeInTheDocument(); | ||
``` | ||
|
||
```js | ||
const buttonText = screen.getByText('Submit'); | ||
const button = buttonText.closest('button'); | ||
``` | ||
|
||
```js | ||
document.getElementById('submit-btn').closest('button'); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
const button = screen.getByRole('button'); | ||
expect(button).toHaveTextContent('submit'); | ||
Belco90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
``` | ||
thebinaryfelix marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
## Further Reading | ||
|
||
### Properties / methods that return another Node | ||
|
||
- [`Document`](https://developer.mozilla.org/en-US/docs/Web/API/Document) | ||
- [`Element`](https://developer.mozilla.org/en-US/docs/Web/API/Element) | ||
- [`Node`](https://developer.mozilla.org/en-US/docs/Web/API/Node) |
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,41 @@ | ||
import { ESLintUtils, TSESTree } from '@typescript-eslint/experimental-utils'; | ||
import { getDocsUrl, ALL_RETURNING_NODES } from '../utils'; | ||
import { isIdentifier } from '../node-utils'; | ||
|
||
export const RULE_NAME = 'no-node-access'; | ||
|
||
export default ESLintUtils.RuleCreator(getDocsUrl)({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow direct Node access', | ||
category: 'Best Practices', | ||
recommended: 'error', | ||
}, | ||
messages: { | ||
noNodeAccess: | ||
'Avoid direct Node access. Prefer using the methods from Testing Library.', | ||
}, | ||
fixable: null, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
|
||
create(context) { | ||
function showErrorForNodeAccess(node: TSESTree.Identifier) { | ||
isIdentifier(node) && | ||
ALL_RETURNING_NODES.includes(node.name) && | ||
context.report({ | ||
node: node, | ||
loc: node.loc.start, | ||
messageId: 'noNodeAccess', | ||
}); | ||
} | ||
|
||
return { | ||
['ExpressionStatement MemberExpression Identifier']: showErrorForNodeAccess, | ||
['VariableDeclarator Identifier']: showErrorForNodeAccess, | ||
}; | ||
}, | ||
}); |
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
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.