Skip to content

fix: 'polymorphicPropName' not work with 'no-void-elements-with-children' #1108

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 1 commit into from
May 16, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ ruleTester.run(RULE_NAME, rule, {
},
],
},
{
code: tsx`<PolyComponent as="img" {...props} children="Foo" />;`,
errors: [
{
messageId: "noVoidElementsWithChildren",
data: { element: "img" },
},
],
settings: {
"react-x": {
polymorphicPropName: "as",
},
},
},
{
code: tsx`<br dangerouslySetInnerHTML={{ __html: "Foo" }} />;`,
errors: [
Expand All @@ -46,6 +60,14 @@ ruleTester.run(RULE_NAME, rule, {
...allValid,
"<div>Foo</div>;",
'<div children="Foo" />;',
{
code: tsx`<PolyComponent as="div" children="Foo" />;`,
settings: {
"react-x": {
polymorphicPropName: "as",
},
},
},
'<div dangerouslySetInnerHTML={{ __html: "Foo" }} />;',
],
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { RuleListener } from "@typescript-eslint/utils/ts-eslint";
import type { CamelCase } from "string-ts";
import * as ER from "@eslint-react/core";

import { createRule } from "../utils";
import { createJsxElementResolver, createRule } from "../utils";

export const RULE_NAME = "no-void-elements-with-children";

Expand Down Expand Up @@ -49,9 +49,10 @@ export default createRule<[], MessageID>({
});

export function create(context: RuleContext<MessageID, []>): RuleListener {
const resolver = createJsxElementResolver(context);
return {
JSXElement(node) {
const elementName = ER.getElementType(context, node);
const { domElementType: elementName } = resolver.resolve(node);
if (elementName.length === 0 || !voidElements.has(elementName)) {
return;
}
Expand Down