Skip to content

feat(eslint-plugin): [restrict-template-expressions] add option to allow RegExp #3709

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 2 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/eslint-plugin/docs/rules/restrict-template-expressions.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ type Options = {
allowAny?: boolean;
// if true, also allow null and undefined in template expressions
allowNullish?: boolean;
// if true, also allow RegExp in template expressions
allowRegExp?: boolean;
};

const defaults = {
allowNumber: true,
allowBoolean: false,
allowAny: false,
allowNullish: false,
allowRegExp: false,
};
```

Expand Down Expand Up @@ -83,3 +86,17 @@ Examples of additional **correct** code for this rule with `{ allowNullish: true
const arg = condition ? 'ok' : null;
const msg1 = `arg = ${arg}`;
```

### `allowRegExp`

Examples of additional **correct** code for this rule with `{ allowRegExp: true }`:

```ts
const arg = new RegExp('foo');
const msg1 = `arg = ${arg}`;
```

```ts
const arg = /foo/;
const msg1 = `arg = ${arg}`;
```
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Options = [
allowBoolean?: boolean;
allowAny?: boolean;
allowNullish?: boolean;
allowRegExp?: boolean;
},
];

Expand All @@ -37,6 +38,7 @@ export default util.createRule<Options, MessageId>({
allowBoolean: { type: 'boolean' },
allowAny: { type: 'boolean' },
allowNullish: { type: 'boolean' },
allowRegExp: { type: 'boolean' },
},
},
],
Expand Down Expand Up @@ -76,6 +78,13 @@ export default util.createRule<Options, MessageId>({
return true;
}

if (
options.allowRegExp &&
util.getTypeName(typeChecker, type) === 'RegExp'
) {
return true;
}

if (
options.allowNullish &&
util.isTypeFlagSet(type, ts.TypeFlags.Null | ts.TypeFlags.Undefined)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,56 @@ ruleTester.run('restrict-template-expressions', rule, {
}
`,
},
// allowRegExp
{
options: [{ allowRegExp: true }],
code: `
const arg = new RegExp('foo');
const msg = \`arg = \${arg}\`;
`,
},
{
options: [{ allowRegExp: true }],
code: `
const arg = /foo/;
const msg = \`arg = \${arg}\`;
`,
},
{
options: [{ allowRegExp: true }],
code: `
declare const arg: string | RegExp;
const msg = \`arg = \${arg}\`;
`,
},
{
options: [{ allowRegExp: true }],
code: `
function test<T extends RegExp>(arg: T) {
return \`arg = \${arg}\`;
}
`,
},
{
options: [{ allowRegExp: true }],
code: `
function test<T extends string | RegExp>(arg: T) {
return \`arg = \${arg}\`;
}
`,
},
// allow ALL
{
options: [{ allowNumber: true, allowBoolean: true, allowNullish: true }],
options: [
{
allowNumber: true,
allowBoolean: true,
allowNullish: true,
allowRegExp: true,
},
],
code: `
type All = string | number | boolean | null | undefined;
type All = string | number | boolean | null | undefined | RegExp;
function test<T extends All>(arg: T) {
return \`arg = \${arg}\`;
}
Expand Down Expand Up @@ -338,6 +383,36 @@ ruleTester.run('restrict-template-expressions', rule, {
},
],
},
{
options: [{ allowRegExp: false }],
code: `
const arg = new RegExp('foo');
const msg = \`arg = \${arg}\`;
`,
errors: [
{
messageId: 'invalidType',
data: { type: 'RegExp' },
line: 3,
column: 30,
},
],
},
{
options: [{ allowRegExp: false }],
code: `
const arg = /foo/;
const msg = \`arg = \${arg}\`;
`,
errors: [
{
messageId: 'invalidType',
data: { type: 'RegExp' },
line: 3,
column: 30,
},
],
},
// TS 3.9 change
{
options: [{ allowAny: true }],
Expand Down