8000 feat(eslint-plugin): [restrict-template-expressions] add option `allo… · ryanwang520/typescript-eslint@d44c0f9 · GitHub
[go: up one dir, main page]

Skip to content

Commit d44c0f9

Browse files
authored
feat(eslint-plugin): [restrict-template-expressions] add option allowAny (typescript-eslint#1762)
1 parent af2c00d commit d44c0f9

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ const msg1 = `arg = ${arg}`;
5959
const msg2 = `arg = ${arg || 'not truthy'}`;
6060
```
6161

62+
### `allowAny`
63+
64+
Examples of additional **correct** code for this rule with `{ allowAny: true }`:
65+
66+
```ts
67+
const user = JSON.parse('{ "name": "foo" }');
68+
const msg1 = `arg = ${user.name}`;
69+
const msg2 = `arg = ${user.name || 'the user with no name'}`;
70+
```
71+
6272
### `allowNullable`
6373

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

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type Options = [
1010
allowNullable?: boolean;
1111
allowNumber?: boolean;
1212
allowBoolean?: boolean;
13+
allowAny?: boolean;
1314
},
1415
];
1516

@@ -32,6 +33,7 @@ export default util.createRule<Options, MessageId>({
3233
{
3334
type: 'object',
3435
properties: {
36+
allowAny: { type: 'boolean' },
3537
allowBoolean: { type: 'boolean' },
3638
allowNullable: { type: 'boolean' },
3739
allowNumber: { type: 'boolean' },
@@ -51,13 +53,15 @@ export default util.createRule<Options, MessageId>({
5153
| 'boolean'
5254
| 'null'
5355
| 'undefined'
56+
| 'any'
5457
| 'other';
5558

5659
const allowedTypes: BaseType[] = [
5760
'string',
5861
...(options.allowNumber ? (['number', 'bigint'] as const) : []),
5962
...(options.allowBoolean ? (['boolean'] as const) : []),
6063
...(options.allowNullable ? (['null', 'undefined'] as const) : []),
64+
...(options.allowAny ? (['any'] as const) : []),
6165
];
6266

6367
function isAllowedType(types: BaseType[]): boolean {
@@ -127,6 +131,9 @@ export default util.createRule<Options, MessageId>({
127131
if (type.flags & ts.TypeFlags.Undefined) {
128132
return ['undefined'];
129133
}
134+
if (type.flags & ts.TypeFlags.Any) {
135+
return ['any'];
136+
}
130137

131138
if (type.isUnion()) {
132139
return type.types
@@ -139,7 +146,8 @@ export default util.createRule<Options, MessageId>({
139146
stringType === 'string' ||
140147
stringType === 'number' ||
141148
stringType === 'bigint' ||
142-
stringType === 'boolean'
149+
stringType === 'boolean' ||
150+
stringType === 'any'
143151
) {
144152
return [stringType];
145153
}

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,43 @@ ruleTester.run('restrict-template-expressions', rule, {
115115
}
116116
`,
117117
},
118+
// allowAny
119+
{
120+
options: [{ allowAny: true }],
121+
code: `
122+
const arg: any = 123;
123+
const msg = \`arg = \${arg}\`;
124+
`,
125+
},
126+
{
127+
options: [{ allowAny: true }],
128+
code: `
129+
const arg: any = undefined;
130+
const msg = \`arg = \${arg || 'some-default'}\`;
131+
`,
132+
},
133+
{
134+
options: [{ allowAny: true }],
135+
code: `
136+
const user = JSON.parse('{ "name": "foo" }');
137+
const msg = \`arg = \${user.name}\`;
138+
`,
139+
},
140+
{
141+
options: [{ allowAny: true }],
142+
code: `
143+
const user = JSON.parse('{ "name": "foo" }');
144+
const msg = \`arg = \${user.name || 'the user with no name'}\`;
145+
`,
146+
},
147+
{
148+
options: [{ allowAny: true }],
149+
code: `
150+
function test<T extends any>(arg: T) {
151+
return \`arg = \${arg}\`;
152+
}
153+
`,
154+
},
118155
// allowNullable
119156
{
120157
options: [{ allowNullable: true }],
@@ -208,5 +245,14 @@ ruleTester.run('restrict-template-expressions', rule, {
208245
`,
209246
errors: [{ messageId: 'invalidType', line: 3, column: 27 }],
210247
},
248+
{
249+
options: [{ allowNumber: true, allowBoolean: true, allowNullable: true }],
250+
code: `
251+
function test(arg: any) {
252+
return \`arg = \${arg}\`;
253+
}
254+
`,
255+
errors: [{ messageId: 'invalidType', line: 3, column: 27 }],
256+
},
211257
],
212258
});

0 commit comments

Comments
 (0)
0