From acf626aa8dc71191906b30c0111ac0885bded3f0 Mon Sep 17 00:00:00 2001 From: auvred Date: Thu, 4 Jul 2024 12:15:32 +0300 Subject: [PATCH] fix(eslint-plugin): [restrict-template-expressions] don't report tuples if `allowArray` option is enabled --- .../rules/restrict-template-expressions.ts | 2 +- .../restrict-template-expressions.test.ts | 59 ++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/restrict-template-expressions.ts b/packages/eslint-plugin/src/rules/restrict-template-expressions.ts index cc719fe7fb7b..5aa90084ac63 100644 --- a/packages/eslint-plugin/src/rules/restrict-template-expressions.ts +++ b/packages/eslint-plugin/src/rules/restrict-template-expressions.ts @@ -30,7 +30,7 @@ const optionTesters = ( [ 'Array', (type, checker, recursivelyCheckType): boolean => - checker.isArrayType(type) && + (checker.isArrayType(type) || checker.isTupleType(type)) && // eslint-disable-next-line @typescript-eslint/no-non-null-assertion recursivelyCheckType(type.getNumberIndexType()!), ], diff --git a/packages/eslint-plugin/tests/rules/restrict-template-expressions.test.ts b/packages/eslint-plugin/tests/rules/restrict-template-expressions.test.ts index 698471ae00a3..7a69ff34cbc9 100644 --- a/packages/eslint-plugin/tests/rules/restrict-template-expressions.test.ts +++ b/packages/eslint-plugin/tests/rules/restrict-template-expressions.test.ts @@ -149,12 +149,40 @@ ruleTester.run('restrict-template-expressions', rule, { { options: [{ allowArray: true }], code: ` - const arg = []; function test(arg: T) { return \`arg = \${arg}\`; } `, }, + { + options: [{ allowArray: true }], + code: ` + declare const arg: [number, string]; + const msg = \`arg = \${arg}\`; + `, + }, + { + options: [{ allowArray: true }], + code: ` + const arg = [1, 'a'] as const; + const msg = \`arg = \${arg || 'default'}\`; + `, + }, + { + options: [{ allowArray: true }], + code: ` + function test(arg: T) { + return \`arg = \${arg}\`; + } + `, + }, + { + code: ` + declare const arg: [number | undefined, string]; + const msg = \`arg = \${arg}\`; + `, + options: [{ allowNullish: true, allowArray: true }], + }, // allowAny { options: [{ allowAny: true }], @@ -365,6 +393,20 @@ ruleTester.run('restrict-template-expressions', rule, { ], options: [{ allowNullish: false }], }, + { + code: ` + declare const arg: number[]; + const msg = \`arg = \${arg}\`; + `, + errors: [ + { + messageId: 'invalidType', + data: { type: 'number[]' }, + line: 3, + column: 30, + }, + ], + }, { code: ` const msg = \`arg = \${[, 2]}\`; @@ -379,6 +421,21 @@ ruleTester.run('restrict-template-expressions', rule, { ], options: [{ allowNullish: false, allowArray: true }], }, + { + code: ` + declare const arg: [number | undefined, string]; + const msg = \`arg = \${arg}\`; + `, + errors: [ + { + messageId: 'invalidType', + data: { type: '[number | undefined, string]' }, + line: 3, + column: 30, + }, + ], + options: [{ allowNullish: false, allowArray: true }], + }, { code: ` declare const arg: number;