Skip to content

Commit

Permalink
feat(eslint-plugin): [restrict-template-expressions] add option `allo…
Browse files Browse the repository at this point in the history
…wAny` (#1762)
  • Loading branch information
itowlson committed Apr 12, 2020
1 parent af2c00d commit d44c0f9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
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 }],
},
],
});

0 comments on commit d44c0f9

Please sign in to comment.