Skip to content

Commit

Permalink
feat: consider whitespaces and break lines
Browse files Browse the repository at this point in the history
  • Loading branch information
tyankatsu0105 committed Jun 26, 2020
1 parent ab6f268 commit fbbb360
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
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)
)
}

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

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

0 comments on commit fbbb360

Please sign in to comment.