-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
es-roikoren/no-regexp-d-flag
rule
- Loading branch information
1 parent
75ff4d1
commit f425126
Showing
8 changed files
with
96 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'eslint-plugin-es-roikoren': patch | ||
--- | ||
|
||
feat: add `es-roikoren/no-regexp-d-flag` rule |
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,20 @@ | ||
# es-roikoren/no-regexp-d-flag | ||
> disallow RegExp `d` flag. | ||
- ✅ The following configurations enable this rule: `plugin:es-roikoren/no-new-in-esnext` | ||
|
||
This rule reports ES2022 [RegExp `d` flag](https://github.com/tc39/proposal-regexp-match-indices#readme) as errors. | ||
|
||
## Examples | ||
|
||
⛔ Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
/*eslint es-roikoren/no-regexp-d-flag: error */ | ||
const r1 = /./d; | ||
``` | ||
|
||
## 📚 References | ||
|
||
- [Rule source](https://github.com/roikoren755/eslint-plugin-es/blob/v0.0.3/src/rules/no-regexp-d-flag.ts) | ||
- [Test source](https://github.com/roikoren755/eslint-plugin-es/blob/v0.0.3/tests/src/rules/no-regexp-d-flag.ts) |
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
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,35 @@ | ||
import type { TSESTree } from '@typescript-eslint/typescript-estree'; | ||
|
||
import { createRule } from '../util/create-rule'; | ||
import { getRegExpCalls } from '../util/get-regexp-calls'; | ||
|
||
export const category = 'ES2022'; | ||
export default createRule<[], 'forbidden'>({ | ||
name: 'no-regexp-d-flag', | ||
meta: { | ||
type: 'problem', | ||
docs: { description: 'disallow RegExp `d` flag.', recommended: false }, | ||
schema: [], | ||
messages: { forbidden: "ES2022 RegExp 'd' flag is forbidden." }, | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
return { | ||
'Literal[regex]'(node: TSESTree.RegExpLiteral) { | ||
if (node.regex.flags.includes('d')) { | ||
context.report({ node, messageId: 'forbidden' }); | ||
} | ||
}, | ||
|
||
'Program:exit'() { | ||
const scope = context.getScope(); | ||
|
||
for (const { node, flags } of getRegExpCalls(scope)) { | ||
if (flags?.includes('d')) { | ||
context.report({ node, messageId: 'forbidden' }); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
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,27 @@ | ||
import { RuleTester } from '../../tester'; | ||
import rule from '../../../src/rules/no-regexp-d-flag'; | ||
import { AST_NODE_TYPES } from '@typescript-eslint/types'; | ||
|
||
const error = { messageId: 'forbidden' as const, line: 1, column: 1, type: AST_NODE_TYPES.NewExpression, data: {} }; | ||
|
||
if (!RuleTester.isSupported(2022)) { | ||
console.log('Skip the tests of no-regexp-d-flag.'); | ||
} else { | ||
new RuleTester().run('no-regexp-d-flag', rule, { | ||
valid: [ | ||
'/foo/gimuys', | ||
'a\n/b/d', | ||
"new RegExp('foo', 'gimuys')", | ||
"new RegExp('foo')", | ||
"new RegExp('foo', flags)", | ||
"const flags = 'gimuys'; new RegExp('foo', flags)", | ||
], | ||
invalid: [ | ||
{ code: '/foo/d', errors: [{ ...error, type: AST_NODE_TYPES.Literal }] }, | ||
{ code: '/foo/gimsuyd', errors: [{ ...error, type: AST_NODE_TYPES.Literal }] }, | ||
{ code: "new RegExp('foo', 'd')", errors: [error] }, | ||
{ code: "new RegExp('foo', 'gimsuyd')", errors: [error] }, | ||
{ code: "const flags = 'd'; new RegExp('foo', flags)", errors: [{ ...error, column: 20 }] }, | ||
], | ||
}); | ||
} |