Skip to content
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

feat(eslint-plugin): [restrict-template-expressions] add option allow any #1762

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/eslint-plugin/docs/rules/restrict-template-expressions.md
Expand Up @@ -59,6 +59,16 @@ const msg1 = `arg = ${arg}`;
const msg2 = `arg = ${arg || 'not truthy'}`;
```

### `allowAny`

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

```ts
const user = JSON.parse('{ "name": "foo" }');
const msg1 = `arg = ${user.name}`;
const msg2 = `arg = ${user.name || 'the user with no name'}`;
```

### `allowNullable`

Examples of additional **correct** code for this rule with `{ allowNullable: true }`:
Expand Down
Expand Up @@ -10,6 +10,7 @@ type Options = [
allowNullable?: boolean;
allowNumber?: boolean;
allowBoolean?: boolean;
allowAny?: boolean;
},
];

Expand All @@ -32,6 +33,7 @@ export default util.createRule<Options, MessageId>({
{
type: 'object',
properties: {
allowAny: { type: 'boolean' },
allowBoolean: { type: 'boolean' },
allowNullable: { type: 'boolean' },
allowNumber: { type: 'boolean' },
Expand All @@ -51,13 +53,15 @@ export default util.createRule<Options, MessageId>({
| 'boolean'
| 'null'
| 'undefined'
| 'any'
| 'other';

const allowedTypes: BaseType[] = [
'string',
...(options.allowNumber ? (['number', 'bigint'] as const) : []),
...(options.allowBoolean ? (['boolean'] as const) : []),
...(options.allowNullable ? (['null', 'undefined'] as const) : []),
...(options.allowAny ? (['any'] as const) : []),
];

function isAllowedType(types: BaseType[]): boolean {
Expand Down Expand Up @@ -127,6 +131,9 @@ export default util.createRule<Options, MessageId>({
if (type.flags & ts.TypeFlags.Undefined) {
return ['undefined'];
}
if (type.flags & ts.TypeFlags.Any) {
return ['any'];
}

if (type.isUnion()) {
return type.types
Expand All @@ -139,7 +146,8 @@ export default util.createRule<Options, MessageId>({
stringType === 'string' ||
stringType === 'number' ||
stringType === 'bigint' ||
stringType === 'boolean'
stringType === 'boolean' ||
stringType === 'any'
) {
return [stringType];
}
Expand Down
Expand Up @@ -115,6 +115,43 @@ ruleTester.run('restrict-template-expressions', rule, {
}
`,
},
// allowAny
{
options: [{ allowAny: true }],
code: `
const arg: any = 123;
const msg = \`arg = \${arg}\`;
`,
},
{
options: [{ allowAny: true }],
code: `
const arg: any = undefined;
const msg = \`arg = \${arg || 'some-default'}\`;
`,
},
{
options: [{ allowAny: true }],
code: `
const user = JSON.parse('{ "name": "foo" }');
const msg = \`arg = \${user.name}\`;
`,
},
{
options: [{ allowAny: true }],
code: `
const user = JSON.parse('{ "name": "foo" }');
const msg = \`arg = \${user.name || 'the user with no name'}\`;
`,
},
{
options: [{ allowAny: true }],
code: `
function test<T extends any>(arg: T) {
return \`arg = \${arg}\`;
}
`,
},
// allowNullable
{
options: [{ allowNullable: true }],
Expand Down Expand Up @@ -208,5 +245,14 @@ ruleTester.run('restrict-template-expressions', rule, {
`,
errors: [{ messageId: 'invalidType', line: 3, column: 27 }],
},
{
options: [{ allowNumber: true, allowBoolean: true, allowNullable: true }],
code: `
function test(arg: any) {
return \`arg = \${arg}\`;
}
`,
errors: [{ messageId: 'invalidType', line: 3, column: 27 }],
},
],
});