Skip to content

fix(eslint-plugin): [no-misused-promises] improve report loc for methods #10216

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
Show all changes
22 commits
Select commit Hold shift + click to select a range
359e052
fix(eslint-plugin): correctly report errors for async methods returni…
gyumong Oct 27, 2024
d8c6635
test add coverage
gyumong Oct 27, 2024
3d26e12
fix: remove unreachable code
gyumong Oct 27, 2024
a5dbd87
fix: simplify redundant if checks in context.report logic
gyumong Oct 27, 2024
a762f40
test: specify error location in voidReturnProperty test cases
gyumong Oct 27, 2024
8b1393c
Merge branch 'main' into fix-no-misused-promises
gyumong Oct 27, 2024
432b792
fix lint
gyumong Oct 27, 2024
2207c17
Merge branch 'main' into fix-no-misused-promises
gyumong Oct 28, 2024
aa8f8c6
Merge branch 'main' into fix-no-misused-promises
gyumong Oct 29, 2024
4c7d787
test: add full loc (line, endLine, column, endColumn) to existing tes…
gyumong Oct 31, 2024
65ae628
test: add new invalid test cases for promise-returning methods withou…
gyumong Oct 31, 2024
801bf4c
fix : test col
gyumong Oct 31, 2024
f367c20
fix: log
gyumong Oct 31, 2024
09be707
Merge branch 'main' into fix-no-misused-promises
gyumong Oct 31, 2024
e79283c
Update packages/eslint-plugin/src/rules/no-misused-promises.ts
gyumong Nov 1, 2024
1278e3d
Merge branch 'main' into fix-no-misused-promises
gyumong Nov 4, 2024
2bc7666
refactor(no-misused-promises): improve function type checking in void…
gyumong Nov 4, 2024
2da0b5c
test(no-misused-promises): add test cases for isFunction utility usage
gyumong Nov 4, 2024
ac34e36
Merge branch 'main' into fix-no-misused-promises
gyumong Nov 5, 2024
c7ac4e6
test(no-misused-promises): add test cases
gyumong Nov 5, 2024
50a6252
Merge branch 'main' into fix-no-misused-promises
gyumong Nov 6, 2024
eb66dbd
Merge branch 'main' into fix-no-misused-promises
gyumong Nov 8, 2024
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
42 changes: 34 additions & 8 deletions packages/eslint-plugin/src/rules/no-misused-promises.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as ts from 'typescript';

import {
createRule,
getFunctionHeadLoc,
getParserServices,
isArrayMethodCallWithPredicate,
isFunction,
Expand Down Expand Up @@ -436,10 +437,25 @@ export default createRule<Options, MessageId>({
) &&
returnsThenable(checker, tsNode.initializer)
) {
context.report({
node: node.value,
messageId: 'voidReturnProperty',
});
if (isFunction(node.value)) {
const functionNode = node.value;
if (functionNode.returnType) {
context.report({
node: functionNode.returnType.typeAnnotation,
messageId: 'voidReturnProperty',
});
} else {
context.report({
loc: getFunctionHeadLoc(functionNode, context.sourceCode),
messageId: 'voidReturnProperty',
});
}
} else {
context.report({
node: node.value,
messageId: 'voidReturnProperty',
});
}
}
} else if (ts.isShorthandPropertyAssignment(tsNode)) {
const contextualType = checker.getContextualType(tsNode.name);
Expand Down Expand Up @@ -490,10 +506,19 @@ export default createRule<Options, MessageId>({
);

if (isVoidReturningFunctionType(checker, tsNode.name, contextualType)) {
context.report({
node: node.value,
messageId: 'voidReturnProperty',
});
const functionNode = node.value as TSESTree.FunctionExpression;

if (functionNode.returnType) {
context.report({
node: functionNode.returnType.typeAnnotation,
messageId: 'voidReturnProperty',
});
} else {
context.report({
loc: getFunctionHeadLoc(functionNode, context.sourceCode),
messageId: 'voidReturnProperty',
});
}
}
return;
}
Expand All @@ -513,6 +538,7 @@ export default createRule<Options, MessageId>({
}
return nullThrows(current, NullThrowsReasons.MissingParent);
})();

if (
functionNode.returnType &&
!isPossiblyFunctionType(functionNode.returnType)
Expand Down
149 changes: 148 additions & 1 deletion packages/eslint-plugin/tests/rules/no-misused-promises.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1405,6 +1405,9 @@ const obj: O = {
`,
errors: [
{
column: 3,
endColumn: 12,
endLine: 4,
line: 4,
messageId: 'voidReturnProperty',
},
Expand All @@ -1419,6 +1422,9 @@ const obj: O = {
`,
errors: [
{
column: 3,
endColumn: 12,
endLine: 4,
line: 4,
messageId: 'voidReturnProperty',
},
Expand Down Expand Up @@ -1451,6 +1457,9 @@ const obj: O = {
`,
errors: [
{
column: 3,
endColumn: 10,
endLine: 4,
line: 4,
messageId: 'voidReturnProperty',
},
Expand All @@ -1472,14 +1481,23 @@ function f(): O {
`,
errors: [
{
column: 5,
endColumn: 12,
endLine: 6,
line: 6,
messageId: 'voidReturnProperty',
},
{
column: 5,
endColumn: 14,
endLine: 9,
line: 9,
messageId: 'voidReturnProperty',
},
{
column: 5,
endColumn: 6,
endLine: 10,
line: 10,
messageId: 'voidReturnProperty',
},
Expand Down Expand Up @@ -1783,7 +1801,15 @@ const test: ReturnsRecord = () => {
return { asynchronous: async () => {} };
};
`,
errors: [{ line: 5, messageId: 'voidReturnProperty' }],
errors: [
{
column: 12,
endColumn: 32,
endLine: 5,
line: 5,
messageId: 'voidReturnProperty',
},
],
},
{
code: `
Expand Down Expand Up @@ -2429,5 +2455,126 @@ arrayFn<() => void>(
},
],
},
{
code: `
type HasVoidMethod = {
f(): void;
};

const o: HasVoidMethod = {
async f() {
return 3;
},
};
`,
errors: [
{
column: 3,
endColumn: 10,
endLine: 7,
line: 7,
messageId: 'voidReturnProperty',
},
],
},
{
code: `
type HasVoidMethod = {
f(): void;
};

const o: HasVoidMethod = {
async f(): Promise<number> {
return 3;
},
};
`,
errors: [
{
column: 14,
endColumn: 29,
endLine: 7,
line: 7,
messageId: 'voidReturnProperty',
},
],
},
{
code: `
type HasVoidMethod = {
f(): void;
};
const obj: HasVoidMethod = {
f() {
return Promise.resolve('foo');
},
};
`,
errors: [
{
column: 3,
endColumn: 4,
endLine: 6,
line: 6,
messageId: 'voidReturnProperty',
},
],
},
{
code: `
type HasVoidMethod = {
f(): void;
};
const obj: HasVoidMethod = {
f(): Promise<void> {
throw new Error();
},
};
`,
errors: [
{
column: 8,
endColumn: 21,
endLine: 6,
line: 6,
messageId: 'voidReturnProperty',
},
],
},
{
code: `
type O = { f: () => void };
const asyncFunction = async () => 'foo';
const obj: O = {
f: asyncFunction,
};
`,
errors: [
{
column: 6,
endColumn: 19,
endLine: 5,
line: 5,
messageId: 'voidReturnProperty',
},
],
},
{
code: `
type O = { f: () => void };
const obj: O = {
f: async (): Promise<string> => 'foo',
};
`,
errors: [
{
column: 16,
endColumn: 31,
endLine: 4,
line: 4,
messageId: 'voidReturnProperty',
},
],
},
],
});
Loading