Skip to content
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

feat: Add no-empty-component-block rule #1222

Merged
17 changes: 16 additions & 1 deletion lib/rules/no-empty-component-block.js
Expand Up @@ -11,6 +11,7 @@
// ------------------------------------------------------------------------------
// Rule Definition
// ------------------------------------------------------------------------------

/**
* check whether has attribute `src`
*/
Expand All @@ -26,6 +27,17 @@ function hasAttributeSrc(componentBlock) {
return hasAttribute && hasSrc
}

/**
* check whether value under the component block is only whitespaces or break lines
*/
function isValueOnlyWhiteSpacesOrLineBreaks(componentBlock) {
return (
componentBlock.children.length === 1 &&
componentBlock.children[0].type === 'VText' &&
/^(\s|\n)+$/.test(componentBlock.children[0].value)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this regex is cover all cases...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe using /^\s*$/ will also work.
However, I'm not good at using regular expressions, so I often do the following.

!componentBlock.children[0].value.trim()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems nice.
I reflected this code.

)
}

module.exports = {
meta: {
type: 'suggestion',
Expand Down Expand Up @@ -67,7 +79,10 @@ module.exports = {
// https://vue-loader.vuejs.org/spec.html#src-imports
if (hasAttributeSrc(componentBlock)) return
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use continue here as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


if (componentBlock.children.length === 0) {
if (
isValueOnlyWhiteSpacesOrLineBreaks(componentBlock) ||
componentBlock.children.length === 0
) {
context.report({
node: componentBlock,
loc: componentBlock.loc,
Expand Down
23 changes: 19 additions & 4 deletions tests/lib/rules/no-empty-component-block.js
Expand Up @@ -14,21 +14,36 @@ const tester = new RuleTester({
tester.run('no-empty-component-block', rule, {
valid: [
`<template><p>foo</p></template>`,
`<template> foobar </template>`,
`<template><p>foo</p></template><script>console.log('foo')</script>`,
`<template><p>foo</p></template><script>console.log('foo')</script><style>p{display: inline;}</style>`,

`<template src="./template.html"></template>`,
`<template src="./template.html" />`,

`<template src="./template.html"></template><script src="./script.js"></script>`,
`<template src="./template.html" /><script src="./script.js" />`,

`<template src="./template.html"></template><script src="./script.js"></script><style src="./style.css"></style>`,
`<template src="./template.html" /><script src="./script.js" /><style src="./style.css" />`
],
invalid: [
{
code: '<template></template>',
code: `<template></template>`,
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
}
]
},
{
code: `<template> </template>`,
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
}
]
},
{
code: `<template>
</template>`,
errors: [
{
message: '`<template>` is empty. Empty block is not allowed.'
Expand Down