-
Notifications
You must be signed in to change notification settings - Fork 39
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
Simplify no-unclosed-tag
rule
#340
base: master
Are you sure you want to change the base?
Simplify no-unclosed-tag
rule
#340
Conversation
…f closing syntax.
maybe we can actually just check that there is an end tag? for example: <p>i never close will have a similarly: <p /> will behave the same since you can't self-close a if you do the same with so maybe we can just check i'm not sure why we didn't do that all along... unless i'm missing something 🤔 |
Over the weekend I came to a similar conclusion @43081j so I totally agree with you. The check can skip void tags, and otherwise check for the presence of the |
isSelfClosed
check more explicitno-unclosed-tag
rule
the CI failure is because one of the tests actually self-closes a custom element FYI 😬 so it already caught one! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good to me 👍
we should probably double check if it does solve the issue you mentioned too. that was more about a space tripping it up i think
Excellent! :D Thank you!
|
location: rangeFromHtmlNode(htmlNode), | ||
message: `This tag isn't closed.${isCustomElement ? " Custom elements cannot be self closing." : ""}` | ||
}); | ||
if (VOID_ELEMENTS.has(htmlNode.tagName.toLowerCase()) || htmlNode.location.endTag != null) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we warn on an invalid closing element for a void element? Seems like it'd be good to do so. For example, this should be a warning on the </input>
<input><div></div></input>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a good idea but maybe something we can add incrementally to the no-unclosed-tag
diagnostic.
I'll make an issue.
@@ -7,7 +7,49 @@ tsTest("Report unclosed tags", t => { | |||
hasDiagnostic(t, diagnostics, "no-unclosed-tag"); | |||
}); | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In an ideal world, we might want to allow:
<p>One
<p>Two
Or just:
<p>Hi
And we'd want to put warnings on dangling closing tags that don't match up to any opening tags.
All of those may be somewhat tricky to do with how parse5 represents the tree though, requiring us to notice spans in the original text that aren't represented in the parsed tree.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are very tricky, and another good case to add to an issue for followup.
Philosophically though, I wonder if the lit-analyzer should avoid HTML diagnostics and leave that to an HTML specific tool. It may even be worth scoping the no-unclosed-tag
to only check custom element tags.
Context
This was inspired (and maybe closes) #283.
The first commit went in a bad direction (which encouraged self-closing syntax (that doesn't exist in HTML)). Second commit changes the lint to use a simpler check which expects non void elements to be explicitly closed.
What is wrong with the current check?
The current
no-unclosed-tag
check grabs the source code locations for the tag fromparse5
, and then tries to guess if the tag was closed. This guess is inaccurate, and boils down to:startTag.endOffset
location matches thenode.endOffset
location.This results in edge cases.
Edge case 1
The first edge case can be seen with
<p>
which can be closed implicitly if followed by a<div>
.Consider:
Currently, the first case without content will not raise the
no-unclosed-tag
check, but the second case does raise theno-unclosed-tag
check.This is because the first case passes both the condition that no children are present, and the implicitly closed tag will have the same offset location as the authored tag. But this is a false negative, because the resulting DOM is probably not what the author intended.
The second case is more correct because the
<p>
element created contains theSome content
text and thus is not empty.Fix
Make this lint require explicit closing tags by checking whether parse5 has got an authored location for the
endTag
. Because void elements do not have an endTag we also must handle them.Test plan
All these edge cases have been captured in the tests.