-
Notifications
You must be signed in to change notification settings - Fork 150
feat: add no-global-regex-flag-in-query
rule
#560
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 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
89caf8c
feat: add no-global-regex-flag-in-query
timdeschryver c1953a9
refactor: review feedback
timdeschryver 274cccf
feat: add fixer
timdeschryver 6d606b2
test: add error details
timdeschryver 25e5038
test: add within cases
timdeschryver d662a2f
refactor: use getDeepestIdentifierNode
timdeschryver 1a7bf12
refactor: review feedback
timdeschryver 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# Disallow the use of the global RegExp flag (/g) in queries (`testing-library/no-global-regexp-flag-in-query`) | ||
|
||
Ensure that there are no global RegExp flags used when using queries. | ||
|
||
## Rule Details | ||
|
||
A RegExp instance that's using the global flag `/g` holds state and this might cause false-positives while querying for elements. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
screen.getByText(/hello/gi); | ||
``` | ||
|
||
```js | ||
await screen.findByRole('button', { otherProp: true, name: /hello/g }); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
screen.getByText(/hello/i); | ||
``` | ||
|
||
```js | ||
await screen.findByRole('button', { otherProp: true, name: /hello/ }); | ||
``` | ||
|
||
## Further Reading | ||
|
||
- [MDN documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/lastIndex) |
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
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,83 @@ | ||
import { TSESTree } from '@typescript-eslint/utils'; | ||
|
||
import { createTestingLibraryRule } from '../create-testing-library-rule'; | ||
import { | ||
isMemberExpression, | ||
isIdentifier, | ||
isCallExpression, | ||
isProperty, | ||
isObjectExpression, | ||
} from '../node-utils'; | ||
|
||
export const RULE_NAME = 'no-global-regexp-flag-in-query'; | ||
export type MessageIds = 'noGlobalRegExpFlagInQuery'; | ||
type Options = []; | ||
|
||
export default createTestingLibraryRule<Options, MessageIds>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Disallow the use of the global RegExp flag (/g) in queries', | ||
recommendedConfig: { | ||
dom: 'error', | ||
angular: 'error', | ||
react: 'error', | ||
vue: 'error', | ||
Belco90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
}, | ||
messages: { | ||
noGlobalRegExpFlagInQuery: | ||
'Avoid using the global RegExp flag (/g) in queries', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context, _, helpers) { | ||
function lint( | ||
regexpNode: TSESTree.Literal, | ||
identifier: TSESTree.Identifier | ||
) { | ||
if (helpers.isQuery(identifier)) { | ||
context.report({ | ||
node: regexpNode, | ||
messageId: 'noGlobalRegExpFlagInQuery', | ||
}); | ||
} | ||
} | ||
|
||
return { | ||
[`CallExpression[callee.type=MemberExpression] > Literal[regex.flags=/g/].arguments`]( | ||
node: TSESTree.Literal | ||
) { | ||
if ( | ||
isCallExpression(node.parent) && | ||
isMemberExpression(node.parent.callee) && | ||
isIdentifier(node.parent.callee.property) | ||
) { | ||
lint(node, node.parent.callee.property); | ||
} | ||
}, | ||
[`CallExpression[callee.type=Identifier] > Literal[regex.flags=/g/].arguments`]( | ||
Belco90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
node: TSESTree.Literal | ||
) { | ||
if (isCallExpression(node.parent) && isIdentifier(node.parent.callee)) { | ||
lint(node, node.parent.callee); | ||
} | ||
}, | ||
[`ObjectExpression:has(Property>[name="name"]) Literal[regex.flags=/g/]`]( | ||
node: TSESTree.Literal | ||
) { | ||
if ( | ||
isProperty(node.parent) && | ||
isObjectExpression(node.parent.parent) && | ||
isCallExpression(node.parent.parent.parent) && | ||
isMemberExpression(node.parent.parent.parent.callee) && | ||
isIdentifier(node.parent.parent.parent.callee.property) | ||
) { | ||
lint(node, node.parent.parent.parent.callee.property); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
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,96 @@ | ||
import rule, { | ||
RULE_NAME, | ||
} from '../../../lib/rules/no-global-regexp-flag-in-query'; | ||
import { createRuleTester } from '../test-utils'; | ||
|
||
const ruleTester = createRuleTester(); | ||
|
||
ruleTester.run(RULE_NAME, rule, { | ||
valid: [ | ||
` | ||
import { screen } from '@testing-library/dom' | ||
screen.getByText(/hello/) | ||
`, | ||
` | ||
import { screen } from '@testing-library/dom' | ||
screen.getByText(/hello/i) | ||
`, | ||
` | ||
import { screen } from '@testing-library/dom' | ||
screen.getByText('hello') | ||
`, | ||
|
||
` | ||
import { screen } from '@testing-library/dom' | ||
screen.findByRole('button', {name: /hello/}) | ||
`, | ||
` | ||
import { screen } from '@testing-library/dom' | ||
screen.findByRole('button', {name: /hello/i}) | ||
`, | ||
` | ||
import { screen } from '@testing-library/dom' | ||
screen.findByRole('button', {name: 'hello'}) | ||
`, | ||
` | ||
const utils = render(<Component/>) | ||
utils.findByRole('button', {name: /hello/i}) | ||
`, | ||
` | ||
const {queryAllByPlaceholderText} = render(<Component/>) | ||
queryAllByPlaceholderText(/hello/i) | ||
Belco90 marked this conversation as resolved.
Show resolved
Hide resolved
Belco90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
import { screen } from '@testing-library/dom' | ||
screen.getByText(/hello/g)`, | ||
errors: [ | ||
{ | ||
messageId: 'noGlobalRegExpFlagInQuery', | ||
MichaelDeBoey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
import { screen } from '@testing-library/dom' | ||
screen.findByRole('button', {name: /hello/g})`, | ||
errors: [ | ||
{ | ||
messageId: 'noGlobalRegExpFlagInQuery', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
import { screen } from '@testing-library/dom' | ||
screen.findByRole('button', {otherProp: true, name: /hello/g})`, | ||
errors: [ | ||
{ | ||
messageId: 'noGlobalRegExpFlagInQuery', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
const utils = render(<Component/>) | ||
utils.findByRole('button', {name: /hello/ig})`, | ||
errors: [ | ||
{ | ||
messageId: 'noGlobalRegExpFlagInQuery', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
const {queryAllByLabelText} = render(<Component/>) | ||
queryAllByLabelText(/hello/ig)`, | ||
errors: [ | ||
{ | ||
messageId: 'noGlobalRegExpFlagInQuery', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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.