Skip to content

Commit

Permalink
fix: make skip options handle nested tags (fixes #160) (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
yeonjuan authored Nov 22, 2023
1 parent e7bd5e8 commit 221b048
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
13 changes: 5 additions & 8 deletions packages/eslint-plugin/lib/rules/element-newline.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ module.exports = {
create(context) {
const option = context.options[0] || { skip: [] };
const skipTags = option.skip;

let isInSkipTags = false;

let skipTagCount = 0;
/**
* @param {ChildType<TagNode | ProgramNode>[]} siblings
*/
Expand Down Expand Up @@ -109,15 +107,14 @@ module.exports = {
checkSiblings(node.body);
},
Tag(node) {
if (isInSkipTags) {
if (skipTagCount > 0) {
return;
}

checkSiblings(node.children);
if (skipTags.includes(node.name)) {
isInSkipTags = true;
skipTagCount++;
return;
}
checkSiblings(node.children);
checkChild(node, node.children);
},
/**
Expand All @@ -126,7 +123,7 @@ module.exports = {
*/
"Tag:exit"(node) {
if (skipTags.includes(node.name)) {
isInSkipTags = false;
skipTagCount--;
return;
}
},
Expand Down
20 changes: 20 additions & 0 deletions packages/eslint-plugin/tests/rules/element-newline.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ ruleTester.run("element-newline", rule, {
},
],
},
{
code: `
<div>
<div></div><span></span><a></a>
</div>
`,
options: [
{
skip: ["div"],
},
],
},
{
code: `<pre><div></div><code><span></span></code><a></a></pre>`,
options: [
{
skip: ["pre", "code"],
},
],
},
],
invalid: [
{
Expand Down

0 comments on commit 221b048

Please sign in to comment.