Skip to content

Commit

Permalink
implement
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan committed Jan 1, 2025
1 parent aae4b85 commit 665b681
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
15 changes: 15 additions & 0 deletions docs/rules/no-nested-interactive.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,18 @@ module.exports = {
```

## Rule Details

Examples of **incorrect** code for this rule:

```html,incorrect
<button><iframe src="https:..."></iframe></button>
<a href="/foo"> <button>Click Me</button> </a>
```

Examples of **correct** code for this rule:

```html,correct
<label> text: <input type="text"> </label>
<div><button>click me</button></div>
```
12 changes: 10 additions & 2 deletions packages/eslint-plugin/lib/rules/no-nested-interactive.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,20 @@ module.exports = {
return;
}
if (interactiveStack.length) {
const parent = interactiveStack[interactiveStack.length - 1];
if (interactiveStack.length === 1) {
const parentLabel = interactiveStack.find(
(tag) => tag.name.toLowerCase() === "label"
);
if (parentLabel && node.name.toLowerCase() !== "label") {
return;
}
}

context.report({
node,
messageId: MESSAGE_IDS.UNEXPECTED,
data: {
tag: parent.name,
tag: interactiveStack[interactiveStack.length - 1].name,
},
});
}
Expand Down
14 changes: 14 additions & 0 deletions packages/eslint-plugin/tests/rules/no-nested-interactive.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ ruleTester.run("no-nested-interactive", rule, {
{
code: "<button><input type='hidden'> click </button>",
},
{
code: "<label> text: <input type='text'></label>",
},
],
invalid: [
{
Expand Down Expand Up @@ -62,6 +65,17 @@ ruleTester.run("no-nested-interactive", rule, {
},
],
},
{
code: "<button><iframe src='https:...'></iframe></button>",
errors: [
{
messageId: "unexpected",
data: {
tag: "button",
},
},
],
},
],
});

Expand Down

0 comments on commit 665b681

Please sign in to comment.