Skip to content

Commit 8022b32

Browse files
committed
feat: consider whitespaces and break lines
1 parent b845da5 commit 8022b32

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

Diff for: lib/rules/no-empty-component-block.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// ------------------------------------------------------------------------------
1212
// Rule Definition
1313
// ------------------------------------------------------------------------------
14+
1415
/**
1516
* check whether has attribute `src`
1617
*/
@@ -26,6 +27,17 @@ function hasAttributeSrc(componentBlock) {
2627
return hasAttribute && hasSrc
2728
}
2829

30+
/**
31+
* check whether value under the component block is only whitespaces or break lines
32+
*/
33+
function isValueOnlyWhiteSpacesOrLineBreaks(componentBlock) {
34+
return (
35+
componentBlock.children.length === 1 &&
36+
componentBlock.children[0].type === 'VText' &&
37+
/^(\s|\n)+$/.test(componentBlock.children[0].value)
38+
)
39+
}
40+
2941
module.exports = {
3042
meta: {
3143
type: 'suggestion',
@@ -67,7 +79,10 @@ module.exports = {
6779
// https://vue-loader.vuejs.org/spec.html#src-imports
6880
if (hasAttributeSrc(componentBlock)) return
6981

70-
if (componentBlock.children.length === 0) {
82+
if (
83+
isValueOnlyWhiteSpacesOrLineBreaks(componentBlock) ||
84+
componentBlock.children.length === 0
85+
) {
7186
context.report({
7287
node: componentBlock,
7388
loc: componentBlock.loc,

Diff for: tests/lib/rules/no-empty-component-block.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,36 @@ const tester = new RuleTester({
1414
tester.run('no-empty-component-block', rule, {
1515
valid: [
1616
`<template><p>foo</p></template>`,
17+
`<template> foobar </template>`,
1718
`<template><p>foo</p></template><script>console.log('foo')</script>`,
1819
`<template><p>foo</p></template><script>console.log('foo')</script><style>p{display: inline;}</style>`,
19-
2020
`<template src="./template.html"></template>`,
2121
`<template src="./template.html" />`,
22-
2322
`<template src="./template.html"></template><script src="./script.js"></script>`,
2423
`<template src="./template.html" /><script src="./script.js" />`,
25-
2624
`<template src="./template.html"></template><script src="./script.js"></script><style src="./style.css"></style>`,
2725
`<template src="./template.html" /><script src="./script.js" /><style src="./style.css" />`
2826
],
2927
invalid: [
3028
{
31-
code: '<template></template>',
29+
code: `<template></template>`,
30+
errors: [
31+
{
32+
message: '`<template>` is empty. Empty block is not allowed.'
33+
}
34+
]
35+
},
36+
{
37+
code: `<template> </template>`,
38+
errors: [
39+
{
40+
message: '`<template>` is empty. Empty block is not allowed.'
41+
}
42+
]
43+
},
44+
{
45+
code: `<template>
46+
</template>`,
3247
errors: [
3348
{
3449
message: '`<template>` is empty. Empty block is not allowed.'

0 commit comments

Comments
 (0)