Skip to content

Commit

Permalink
fix(eslint-plugin): allow explicit any for no-unsafe-return (#3498)
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Goldberg committed Jun 13, 2021
1 parent 288092a commit b15a2b2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
17 changes: 10 additions & 7 deletions packages/eslint-plugin/src/rules/no-unsafe-return.ts
Expand Up @@ -91,6 +91,16 @@ export default util.createRule({
functionType = checker.getTypeAtLocation(functionTSNode);
}

// If there is an explicit type annotation *and* that type matches the actual
// function return type, we shouldn't complain (it's intentional, even if unsafe)
if (functionTSNode.type) {
for (const signature of functionType.getCallSignatures()) {
if (returnNodeType === signature.getReturnType()) {
return;
}
}
}

if (anyType !== util.AnyType.Safe) {
// Allow cases when the declared return type of the function is either unknown or unknown[]
// and the function is returning any or any[].
Expand Down Expand Up @@ -140,13 +150,6 @@ export default util.createRule({

for (const signature of functionType.getCallSignatures()) {
const functionReturnType = signature.getReturnType();
if (returnNodeType === functionReturnType) {
// don't bother checking if they're the same
// either the function is explicitly declared to return the same type
// or there was no declaration, so the return type is implicit
return;
}

const result = util.isUnsafeAssignment(
returnNodeType,
functionReturnType,
Expand Down
12 changes: 12 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts
Expand Up @@ -40,6 +40,18 @@ function foo() {
`
function foo() {
return [];
}
`,
// explicit any return type is allowed, if you want to be unsafe like that
`
function foo(): any {
return {} as any;
}
`,
// explicit any array return type is allowed, if you want to be unsafe like that
`
function foo(): any[] {
return [] as any[];
}
`,
// explicit any generic return type is allowed, if you want to be unsafe like that
Expand Down

0 comments on commit b15a2b2

Please sign in to comment.