Skip to content

Commit

Permalink
feat(eslint-plugin): [restrict-template-expressions] add option to al…
Browse files Browse the repository at this point in the history
…low RegExp (#3709)
  • Loading branch information
a-tarasyuk committed Sep 3, 2021
1 parent 4bfa437 commit 363b3dc
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
17 changes: 17 additions & 0 deletions packages/eslint-plugin/docs/rules/restrict-template-expressions.md
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}`;
```
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
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

0 comments on commit 363b3dc

Please sign in to comment.