Thanks to visit codestin.com
Credit goes to github.com

Skip to content

feat(eslint-plugin): add allowNever support to restrict-template-expressions #6554

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ const arg = /foo/;
const msg1 = `arg = ${arg}`;
```

### `allowNever`

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

```ts
const arg = 'something';
const msg1 = typeof arg === 'string' ? arg : `arg = ${arg}`;
```

## Related To

- [`no-base-to-string`](./no-base-to-string.md)
Expand Down
10 changes: 10 additions & 0 deletions packages/eslint-plugin/src/rules/restrict-template-expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Options = [
allowAny?: boolean;
allowNullish?: boolean;
allowRegExp?: boolean;
allowNever?: boolean;
},
];

Expand Down Expand Up @@ -58,6 +59,11 @@ export default util.createRule<Options, MessageId>({
'Whether to allow `regexp` typed values in template expressions.',
type: 'boolean',
},
allowNever: {
description:
'Whether to allow `never` typed values in template expressions.',
type: 'boolean',
},
},
},
],
Expand Down Expand Up @@ -111,6 +117,10 @@ export default util.createRule<Options, MessageId>({
return true;
}

if (options.allowNever && util.isTypeNeverType(type)) {
return true;
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,50 @@ ruleTester.run('restrict-template-expressions', rule, {
}
`,
},
// allowNever
{
options: [{ allowNever: true }],
code: `
declare const value: never;
const stringy = \`\${value}\`;
`,
},
{
options: [{ allowNever: true }],
code: `
const arg = 'hello';
const msg = typeof arg === 'string' ? arg : \`arg = \${arg}\`;
`,
},
{
options: [{ allowNever: true }],
code: `
function test(arg: 'one' | 'two') {
switch (arg) {
case 'one':
return 1;
case 'two':
return 2;
default:
throw new Error(\`Unrecognised arg: \${arg}\`);
}
}
`,
},
{
options: [{ allowNever: true }],
code: `
// more variants may be added to Foo in the future
type Foo = { type: 'a'; value: number };

function checkFoosAreMatching(foo1: Foo, foo2: Foo) {
if (foo1.type !== foo2.type) {
// since Foo currently only has one variant, this code is never run, and \`foo1.type\` has type \`never\`.
throw new Error(\`expected \${foo1.type}, found \${foo2.type}\`);
}
}
`,
},
// allow ALL
{
options: [
Expand All @@ -235,10 +279,11 @@ ruleTester.run('restrict-template-expressions', rule, {
allowBoolean: true,
allowNullish: true,
allowRegExp: true,
allowNever: true,
},
],
code: `
type All = string | number | boolean | null | undefined | RegExp;
type All = string | number | boolean | null | undefined | RegExp | never;
function test<T extends All>(arg: T) {
return \`arg = \${arg}\`;
}
Expand Down Expand Up @@ -418,6 +463,21 @@ ruleTester.run('restrict-template-expressions', rule, {
},
],
},
{
options: [{ allowNever: false }],
code: `
declare const value: never;
const stringy = \`\${value}\`;
`,
errors: [
{
messageId: 'invalidType',
data: { type: 'never' },
line: 3,
column: 28,
},
],
},
// TS 3.9 change
{
options: [{ allowAny: true }],
Expand Down