diff --git a/packages/eslint-plugin/src/rules/no-shadow.ts b/packages/eslint-plugin/src/rules/no-shadow.ts index 1f746a3201c..7d8eff0b9e2 100644 --- a/packages/eslint-plugin/src/rules/no-shadow.ts +++ b/packages/eslint-plugin/src/rules/no-shadow.ts @@ -147,8 +147,9 @@ export default util.createRule({ return false; } - const id = variable.identifiers[0]; - return util.isFunctionType(id.parent); + return variable.defs.every( + def => def.node.type === AST_NODE_TYPES.TSFunctionType, + ); } function isGenericOfStaticMethod( diff --git a/packages/eslint-plugin/src/rules/promise-function-async.ts b/packages/eslint-plugin/src/rules/promise-function-async.ts index ba3c3d9478f..4387bc52c9b 100644 --- a/packages/eslint-plugin/src/rules/promise-function-async.ts +++ b/packages/eslint-plugin/src/rules/promise-function-async.ts @@ -175,7 +175,10 @@ export default util.createRule({ } // if current token is a keyword like `static` or `public` then skip it - while (keyToken.type === AST_TOKEN_TYPES.Keyword) { + while ( + keyToken.type === AST_TOKEN_TYPES.Keyword && + keyToken.range[0] < method.key.range[0] + ) { keyToken = sourceCode.getTokenAfter(keyToken)!; } diff --git a/packages/eslint-plugin/tests/rules/no-shadow/no-shadow.test.ts b/packages/eslint-plugin/tests/rules/no-shadow/no-shadow.test.ts index 1097f466a18..1616051f0e3 100644 --- a/packages/eslint-plugin/tests/rules/no-shadow/no-shadow.test.ts +++ b/packages/eslint-plugin/tests/rules/no-shadow/no-shadow.test.ts @@ -12,6 +12,25 @@ const ruleTester = new RuleTester({ ruleTester.run('no-shadow TS tests', rule, { valid: [ + 'function foo any>(arg: T) {}', + 'function foo any>(arg: T) {}', + 'function foo any>(arg: T) {}', + 'function foo any>(fn: T, args: any[]) {}', + 'function foo any>(fn: T, args: any[]) {}', + 'function foo any>(fn: T, ...args: any[]) {}', + 'function foo any>(fn: T, args: any[]) {}', + 'function foo any>(fn: T, args: any[]) {}', + 'function foo any>(fn: T, args: any) {}', + ` +function foo any>( + fn: T, + ...args: any[] +) {} + `, + ` +type Args = 1; +function foo void>(arg: T) {} + `, // nested conditional types ` export type ArrayInput = Func extends (arg0: Array) => any @@ -375,6 +394,22 @@ type T = 1; }, { code: ` +type T = 1; +function foo(arg: T) {} + `, + errors: [ + { + messageId: 'noShadow', + data: { + name: 'T', + shadowedLine: 2, + shadowedColumn: 6, + }, + }, + ], + }, + { + code: ` function foo() { return function () {}; } @@ -392,6 +427,22 @@ function foo() { }, { code: ` +type T = string; +function foo void>(arg: T) {} + `, + errors: [ + { + messageId: 'noShadow', + data: { + name: 'T', + shadowedLine: 2, + shadowedColumn: 6, + }, + }, + ], + }, + { + code: ` const x = 1; { type x = string; @@ -703,5 +754,29 @@ let y; }, ], }, + { + code: ` +function foo any>(fn: T, args: any[]) {} + `, + options: [ + { + ignoreTypeValueShadow: false, + builtinGlobals: true, + }, + ], + globals: { + args: 'writable', + }, + errors: [ + { + messageId: 'noShadowGlobal', + data: { + name: 'args', + shadowedLine: 2, + shadowedColumn: 5, + }, + }, + ], + }, ], }); diff --git a/packages/eslint-plugin/tests/rules/promise-function-async.test.ts b/packages/eslint-plugin/tests/rules/promise-function-async.test.ts index 69b604c811c..ccf99b6afcb 100644 --- a/packages/eslint-plugin/tests/rules/promise-function-async.test.ts +++ b/packages/eslint-plugin/tests/rules/promise-function-async.test.ts @@ -102,6 +102,13 @@ const invalidAsyncModifiers = { constructor() {} } `, + ` +class Foo { + async catch(arg: Promise) { + return arg; + } +} + `, { code: ` function returnsAny(): any { @@ -670,5 +677,80 @@ class Test { } `, }, + // https://github.com/typescript-eslint/typescript-eslint/issues/5729 + { + code: ` +class Foo { + catch() { + return Promise.resolve(1); + } + + public default() { + return Promise.resolve(2); + } + + @decorator + private case() { + return Promise.resolve(3); + } +} + `, + output: ` +class Foo { + async catch() { + return Promise.resolve(1); + } + + public async default() { + return Promise.resolve(2); + } + + @decorator + private async case() { + return Promise.resolve(3); + } +} + `, + errors: [ + { + line: 3, + column: 3, + messageId, + }, + { + line: 7, + column: 3, + messageId, + }, + { + line: 12, + column: 3, + messageId, + }, + ], + }, + { + code: ` +const foo = { + catch() { + return Promise.resolve(1); + }, +}; + `, + output: ` +const foo = { + async catch() { + return Promise.resolve(1); + }, +}; + `, + errors: [ + { + line: 3, + column: 3, + messageId, + }, + ], + }, ], });