Skip to content

Commit

Permalink
fix(eslint-plugin): [no-misused-promises] fix incorrect detection of …
Browse files Browse the repository at this point in the history
…void functions in JSX attributes (#6638)
  • Loading branch information
bradzacher committed Mar 15, 2023
1 parent 3cd8ce1 commit 9e41cee
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/eslint-plugin/src/rules/no-misused-promises.ts
Expand Up @@ -365,20 +365,27 @@ export default util.createRule<Options, MessageId>({
}

function checkJSXAttribute(node: TSESTree.JSXAttribute): void {
const tsNode = parserServices.esTreeNodeToTSNodeMap.get(node);
const value = tsNode.initializer;
if (
node.value == null ||
value === undefined ||
!ts.isJsxExpression(value) ||
value.expression === undefined
node.value.type !== AST_NODE_TYPES.JSXExpressionContainer
) {
return;
}
const contextualType = checker.getContextualType(value);
const expressionContainer = parserServices.esTreeNodeToTSNodeMap.get(
node.value,
);
const expression = parserServices.esTreeNodeToTSNodeMap.get(
node.value.expression,
);
const contextualType = checker.getContextualType(expressionContainer);
if (
contextualType !== undefined &&
isVoidReturningFunctionType(checker, value, contextualType)
isVoidReturningFunctionType(
checker,
expressionContainer,
contextualType,
) &&
returnsThenable(checker, expression)
) {
context.report({
messageId: 'voidReturnAttribute',
Expand Down
17 changes: 17 additions & 0 deletions packages/eslint-plugin/tests/rules/no-misused-promises.test.ts
Expand Up @@ -460,6 +460,23 @@ restTuple('Hello');
};
}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/6637
{
code: `
type OnSelectNodeFn = (node: string | null) => void;
interface ASTViewerBaseProps {
readonly onSelectNode?: OnSelectNodeFn;
}
declare function ASTViewer(props: ASTViewerBaseProps): null;
declare const onSelectFn: OnSelectNodeFn;
<ASTViewer onSelectNode={onSelectFn} />;
`,
filename: 'react.tsx',
options: [{ checksVoidReturn: { attributes: true } }],
},
],

invalid: [
Expand Down

0 comments on commit 9e41cee

Please sign in to comment.