Skip to content

Commit

Permalink
feat: add es-roikoren/no-regexp-d-flag rule
Browse files Browse the repository at this point in the history
  • Loading branch information
roikoren755 committed Nov 11, 2021
1 parent 75ff4d1 commit f425126
Show file tree
Hide file tree
Showing 8 changed files with 96 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-zoos-tie.md
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
1 change: 1 addition & 0 deletions docs/rules/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ There is a config that enables the rules in this category: `plugin:es-roikoren/n
| Rule ID | Description | |
|:--------|:------------|:--:|
| [es-roikoren/no-class-fields](./no-class-fields.md) | disallow class fields. | |
| [es-roikoren/no-regexp-d-flag](./no-regexp-d-flag.md) | disallow RegExp `d` flag. | |

## ES2021

Expand Down
20 changes: 20 additions & 0 deletions docs/rules/no-regexp-d-flag.md
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)
4 changes: 2 additions & 2 deletions scripts/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const rules: IRule[] = [];
meta: { docs, fixable },
},
} = content;
const description = docs?.description;
const rule = { ruleId, description: description ?? '', fixable: !!fixable };
const description = docs?.description ?? '';
const rule = { ruleId, description, fixable: !!fixable };

if (category) {
categories[category].rules.push(rule);
Expand Down
5 changes: 4 additions & 1 deletion src/configs/no-new-in-esnext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
* DON'T EDIT THIS FILE.
* This file was generated by 'scripts/update-src-configs.ts' script.
*/
export default { plugins: ['es-roikoren'], rules: { 'es-roikoren/no-class-fields': 'error' } };
export default {
plugins: ['es-roikoren'],
rules: { 'es-roikoren/no-class-fields': 'error', 'es-roikoren/no-regexp-d-flag': 'error' },
};
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ import noPromisePrototypeFinally from './rules/no-promise-prototype-finally';
import noPropertyShorthands from './rules/no-property-shorthands';
import noProxy from './rules/no-proxy';
import noReflect from './rules/no-reflect';
import noRegexpDFlag from './rules/no-regexp-d-flag';
import noRegexpLookbehindAssertions from './rules/no-regexp-lookbehind-assertions';
import noRegexpNamedCaptureGroups from './rules/no-regexp-named-capture-groups';
import noRegexpPrototypeFlags from './rules/no-regexp-prototype-flags';
Expand Down Expand Up @@ -302,6 +303,7 @@ export default {
'no-property-shorthands': noPropertyShorthands,
'no-proxy': noProxy,
'no-reflect': noReflect,
'no-regexp-d-flag': noRegexpDFlag,
'no-regexp-lookbehind-assertions': noRegexpLookbehindAssertions,
'no-regexp-named-capture-groups': noRegexpNamedCaptureGroups,
'no-regexp-prototype-flags': noRegexpPrototypeFlags,
Expand Down
35 changes: 35 additions & 0 deletions src/rules/no-regexp-d-flag.ts
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' });
}
}
},
};
},
});
27 changes: 27 additions & 0 deletions tests/src/rules/no-regexp-d-flag.ts
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 }] },
],
});
}

0 comments on commit f425126

Please sign in to comment.