Skip to content

fix(eslint-plugin): [no-unsafe-return] differentiate a types-error any from a true any #9254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/eslint-plugin/src/rules/no-unsafe-return.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export default createRule({
requiresTypeChecking: true,
},
messages: {
unsafeReturn: 'Unsafe return of an `{{type}}` typed value.',
unsafeReturn: 'Unsafe return of an {{type}} typed value.',
unsafeReturnThis: [
'Unsafe return of an `{{type}}` typed value. `this` is typed as `any`.',
'You can try to fix this by turning on the `noImplicitThis` compiler option, or adding a `this` parameter to the function.',
Expand Down Expand Up @@ -137,6 +137,7 @@ export default createRule({
}

let messageId: 'unsafeReturn' | 'unsafeReturnThis' = 'unsafeReturn';
const isErrorType = tsutils.isIntrinsicErrorType(returnNodeType);

if (!isNoImplicitThis) {
// `return this`
Expand All @@ -156,7 +157,11 @@ export default createRule({
node: reportingNode,
messageId,
data: {
type: anyType === AnyType.Any ? 'any' : 'any[]',
type: isErrorType
? 'error'
: anyType === AnyType.Any
? '`any`'
: '`any[]`',
},
});
}
Expand Down
49 changes: 39 additions & 10 deletions packages/eslint-plugin/tests/rules/no-unsafe-return.test.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Testing] As with #8304, please include the data in existing tests. If there's a bug in the new isErrorType ? ... : ... logic, the tests should be able to catch it.

Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ function foo() {
{
messageId: 'unsafeReturn',
data: {
type: 'any',
type: '`any`',
},
},
],
Expand All @@ -154,7 +154,7 @@ function foo() {
{
messageId: 'unsafeReturn',
data: {
type: 'any',
type: '`any`',
},
},
],
Expand All @@ -169,7 +169,7 @@ const foo = () => {
{
messageId: 'unsafeReturn',
data: {
type: 'any',
type: '`any`',
},
},
],
Expand All @@ -180,7 +180,7 @@ const foo = () => {
{
messageId: 'unsafeReturn',
data: {
type: 'any',
type: '`any`',
},
},
],
Expand All @@ -195,7 +195,7 @@ function foo() {
{
messageId: 'unsafeReturn',
data: {
type: 'any[]',
type: '`any[]`',
},
},
],
Expand All @@ -210,7 +210,7 @@ function foo() {
{
messageId: 'unsafeReturn',
data: {
type: 'any[]',
type: '`any[]`',
},
},
],
Expand All @@ -225,7 +225,7 @@ function foo() {
{
messageId: 'unsafeReturn',
data: {
type: 'any[]',
type: '`any[]`',
},
},
],
Expand All @@ -240,7 +240,7 @@ function foo() {
{
messageId: 'unsafeReturn',
data: {
type: 'any[]',
type: '`any[]`',
},
},
],
Expand All @@ -255,7 +255,7 @@ const foo = () => {
{
messageId: 'unsafeReturn',
data: {
type: 'any[]',
type: '`any[]`',
},
},
],
Expand All @@ -266,7 +266,7 @@ const foo = () => {
{
messageId: 'unsafeReturn',
data: {
type: 'any[]',
type: '`any[]`',
},
},
],
Expand Down Expand Up @@ -407,12 +407,18 @@ function bar() {
line: 3,
column: 3,
endColumn: 15,
data: {
type: '`any`',
},
},
{
messageId: 'unsafeReturnThis',
line: 7,
column: 16,
endColumn: 20,
data: {
type: '`any`',
},
},
],
},
Expand All @@ -427,6 +433,29 @@ foo(() => 'foo' as any);
line: 3,
column: 11,
endColumn: 23,
data: {
type: '`any`',
},
},
],
},
{
code: `
let value: NotKnown;

function example() {
return value;
}
`,
errors: [
{
messageId: 'unsafeReturn',
line: 5,
column: 3,
endColumn: 16,
data: {
type: 'error',
},
},
],
},
Expand Down
Loading