-
-
Notifications
You must be signed in to change notification settings - Fork 694
Add option to ignore comments in vue/multiline-html-element-content-newline
#2581
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
Conversation
vue/multiline-html-element-content-newline
(追記ここまで)
If the approach is OK, I'll add to the docs
Looks good to me 🙂
Please go ahead and adjust the docs now 😉
And could you also add the same option to vue/singleline-html-element-content-newline
as well please?
And could you also add the same option to
vue/singleline-html-element-content-newline
as well please?
I'm unsure how to do this, as a straight copy / paste of the changes into that rule doesn't quite work the same. For example, with ignoreComments: true
this test failes:
valid: [{ code: ` <template> <div attr> <!-- comment --> </div> </template>`, options: [ { ignoreComments: true } ] }]
with these errors:
1) singleLineRule
valid
<template>
<div attr> <!-- comment --> </div>
</template>:
AssertionError [ERR_ASSERTION]: Should have no errors but had 2: [
{
ruleId: 'singleLineRule',
severity: 1,
message: 'Expected 1 line break after opening tag (`<div>`), but no line breaks found.',
line: 3,
column: 19,
nodeType: 'HTMLTagClose',
messageId: 'unexpectedAfterClosingBracket',
endLine: 3,
endColumn: 37,
fix: { range: [Array], text: '\n' }
},
{
ruleId: 'singleLineRule',
severity: 1,
message: 'Expected 1 line break before closing tag (`</div>`), but no line breaks found.',
line: 3,
column: 19,
nodeType: 'HTMLEndTagOpen',
messageId: 'unexpectedBeforeOpeningBracket',
endLine: 3,
endColumn: 37,
fix: { range: [Array], text: '\n' }
}
]
But if I remove the whitespace around the comment:
valid: [{ code: ` <template> <div attr><!-- comment --></div> </template>`, options: [ { ignoreComments: true } ] }]
the test passes fine. I didn't see this issue in the multiline rule.
In this section of the rule code here:
if ( ignoreWhenEmpty && elem.children.length === 0 && template.getFirstTokensBetween( elem.startTag, elem.endTag, getTokenOption ).length === 0 ) { return }
I'm not sure why it's required to test elem.children.length === 0
and getFirstTokensBetween().length === 0
—indeed, commenting that line out:
if ( ignoreWhenEmpty && // elem.children.length === 0 && template.getFirstTokensBetween( elem.startTag, elem.endTag, getTokenOption ).length === 0 ) { return }
then makes this valid:
<template> <div attr> <!-- comment --> </div> </template>`
but still reports errors for this:
<template> <div attr> content <!-- comment --> </div> </template>`
I'm not sure why it's required to test
elem.children.length === 0
andgetFirstTokensBetween().length === 0
Probably it is indeed not needed, seems like both were added at the same time in #684. I guess if the other tests are passing, then removing the elem.children.length
check should be fine.
Ah, this is a bit more complicated than I thought. Without my proposed changes, the default settings for singleline-html-element-content-newline
give these test results:
<div></div>
→ OK<div> </div>
→ OK<div attr></div>
→ OK<div attr> </div>
→ Fail
I'm not sure if the behaviour here is really consistent—shouldn't 2 and 4 give the same result? Because the setting is ignoreWhenNoAttributes
and defaults to true
, I can't see a way to configure that case 4 passes the test. Is the only option here to just disable the singleline-html-element-content-newline
rule altogether?
It complicates my use case, as it's very common to put white space around comments, but then that white space counts as 'content':
<div attr><!-- comment --></div>
→ OK
<div attr> <!-- comment --> </div>
→ Fail
But I think that's OK, and is consistent with the existing rules / tests. I'll push an update with documentation for you to see.
It seems that the simple fix is also not working correctly for multiline-html-element-content-newline
, see https://deploy-preview-2581--eslint-plugin-vue.netlify.app/rules/multiline-html-element-content-newline.html#ignorecomments-true:
<template> <!-- should be good, but actually is reported because the div is now 2 line breaks below the template --> <div class="red"> <!-- no error is reported here --> content </div> </template>
So apparently a more elaborate logic is needed after all, probably that logic can then also be used for the singleline-html-element-content-newline
rule.
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.
By the way, while you're at it: Could you please add a "Related rules" section here and link to vue/singleline-html-element-content-newline
from this rule docs page?
And also vice-versa a link from vue/singleline-html-element-content-newline
to vue/multiline-html-element-content-newline
.
EDIT: I've done that now in #2911.
Note: the following comments are replies to #2581 (comment) rather than this comment.
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.
Yes, since comments are ignored, you would almost always want allowEmptyLines: true
since it's not clear how many empty lines you'll have. I couldn't see an easy way around that.
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'd love to spend more time digging in, but I'm afraid I have to get back to the project I'm using this on! I guess I'll just have to disable those rules for now...
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.
@dsl101 Do you think you'll get to this PR at some point? Or should we close it and let someone else start fresh?
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.
Yeah, I would love to, but I'm really not sure when. I guess there have been plenty of other changes since I started on this, and I'd need to refresh my memory or just start over with a better design given where this got to.
If someone else is keen, that's probably the best bet.
Uh oh!
There was an error while loading. Please reload this page.
Fixes #2179
I finally got around to looking at a PR. It seems deceptively simple, so not sure if it broke something elsewhere, but no other tests seemed affected.