Skip to content

Commit 363b3dc

Browse files
authored
feat(eslint-plugin): [restrict-template-expressions] add option to allow RegExp (typescript-eslint#3709)
1 parent 4bfa437 commit 363b3dc

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

packages/eslint-plugin/docs/rules/restrict-template-expressions.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,16 @@ type Options = {
3535
allowAny?: boolean;
3636
// if true, also allow null and undefined in template expressions
3737
allowNullish?: boolean;
38+
// if true, also allow RegExp in template expressions
39+
allowRegExp?: boolean;
3840
};
3941

4042
const defaults = {
4143
allowNumber: true,
4244
allowBoolean: false,
4345
allowAny: false,
4446
allowNullish: false,
47+
allowRegExp: false,
4548
};
4649
```
4750

@@ -83,3 +86,17 @@ Examples of additional **correct** code for this rule with `{ allowNullish: true
8386
const arg = condition ? 'ok' : null;
8487
const msg1 = `arg = ${arg}`;
8588
```
89+
90+
### `allowRegExp`
91+
92+
Examples of additional **correct** code for this rule with `{ allowRegExp: true }`:
93+
94+
```ts
95+
const arg = new RegExp('foo');
96+
const msg1 = `arg = ${arg}`;
97+
```
98+
99+
```ts
100+
const arg = /foo/;
101+
const msg1 = `arg = ${arg}`;
102+
```

packages/eslint-plugin/src/rules/restrict-template-expressions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type Options = [
1111
allowBoolean?: boolean;
1212
allowAny?: boolean;
1313
allowNullish?: boolean;
14+
allowRegExp?: boolean;
1415
},
1516
];
1617

@@ -37,6 +38,7 @@ export default util.createRule<Options, MessageId>({
3738
allowBoolean: { type: 'boolean' },
3839
allowAny: { type: 'boolean' },
3940
allowNullish: { type: 'boolean' },
41+
allowRegExp: { type: 'boolean' },
4042
},
4143
},
4244
],
@@ -76,6 +78,13 @@ export default util.createRule<Options, MessageId>({
7678
return true;
7779
}
7880

81+
if (
82+
options.allowRegExp &&
83+
util.getTypeName(typeChecker, type) === 'RegExp'
84+
) {
85+
return true;
86+
}
87+
7988
if (
8089
options.allowNullish &&
8190
util.isTypeFlagSet(type, ts.TypeFlags.Null | ts.TypeFlags.Undefined)

packages/eslint-plugin/tests/rules/restrict-template-expressions.test.ts

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,56 @@ ruleTester.run('restrict-template-expressions', rule, {
189189
}
190190
`,
191191
},
192+
// allowRegExp
193+
{
194+
options: [{ allowRegExp: true }],
195+
code: `
196+
const arg = new RegExp('foo');
197+
const msg = \`arg = \${arg}\`;
198+
`,
199+
},
200+
{
201+
options: [{ allowRegExp: true }],
202+
code: `
203+
const arg = /foo/;
204+
const msg = \`arg = \${arg}\`;
205+
`,
206+
},
207+
{
208+
options: [{ allowRegExp: true }],
209+
code: `
210+
declare const arg: string | RegExp;
211+
const msg = \`arg = \${arg}\`;
212+
`,
213+
},
214+
{
215+
options: [{ allowRegExp: true }],
216+
code: `
217+
function test<T extends RegExp>(arg: T) {
218+
return \`arg = \${arg}\`;
219+
}
220+
`,
221+
},
222+
{
223+
options: [{ allowRegExp: true }],
224+
code: `
225+
function test<T extends string | RegExp>(arg: T) {
226+
return \`arg = \${arg}\`;
227+
}
228+
`,
229+
},
192230
// allow ALL
193231
{
194-
options: [{ allowNumber: true, allowBoolean: true, allowNullish: true }],
232+
options: [
233+
{
234+
allowNumber: true,
235+
allowBoolean: true,
236+
allowNullish: true,
237+
allowRegExp: true,
238+
},
239+
],
195240
code: `
196-
type All = string | number | boolean | null | undefined;
241+
type All = string | number | boolean | null | undefined | RegExp;
197242
function test<T extends All>(arg: T) {
198243
return \`arg = \${arg}\`;
199244
}
@@ -338,6 +383,36 @@ ruleTester.run('restrict-template-expressions', rule, {
338383
},
339384
],
340385
},
386+
{
387+
options: [{ allowRegExp: false }],
388+
code: `
389+
const arg = new RegExp('foo');
390+
const msg = \`arg = \${arg}\`;
391+
`,
392+
errors: [
393+
{
394+
messageId: 'invalidType',
395+
data: { type: 'RegExp' },
396+
line: 3,
397+
column: 30,
398+
},
399+
],
400+
},
401+
{
402+
options: [{ allowRegExp: false }],
403+
code: `
404+
const arg = /foo/;
405+
const msg = \`arg = \${arg}\`;
406+
`,
407+
errors: [
408+
{
409+
messageId: 'invalidType',
410+
data: { type: 'RegExp' },
411+
line: 3,
412+
column: 30,
413+
},
414+
],
415+
},
341416
// TS 3.9 change
342417
{
343418
options: [{ allowAny: true }],

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy