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

Conversation

tyankatsu0105
Copy link
Contributor

close #952

This PR adds vue/no-empty-component-block rule.

create(context) {
return {
Program(node) {
const componentBlocks = node.templateBody.parent.children
Copy link
Member

Choose a reason for hiding this comment

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

Since vue-eslint-parser v7.0.0+ you can use parserServices.getDocumentFragment().
This allows you to get the root element without the <template> block.

https://github.com/mysticatea/vue-eslint-parser/releases/tag/v7.0.0

I'm using this API in the padding-line-between-blocks rule.

https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/padding-line-between-blocks.js#L146

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool feature👍
Thanks for sharing.

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

Copy link
Member

@ota-meshi ota-meshi left a comment

Choose a reason for hiding this comment

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

Thank you for this PR!

I have some requests.

]
},
{
code: '<template></template><script></script><style></style>',
Copy link
Member

Choose a reason for hiding this comment

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

Could you add testcases that contains whitespaces such as line breaks? It probably won't report because it contains VText.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Wow. Good point!
I'll fix it.

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 added some invalid cases.

@tyankatsu0105 tyankatsu0105 force-pushed the add-rule/no-empty-component-block branch from fbbb360 to 4e94cdf Compare June 26, 2020 10:30
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.

componentBlock.name !== 'script' &&
componentBlock.name !== 'style'
)
return
Copy link
Member

Choose a reason for hiding this comment

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

I just noticed. Use continue here.

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

return

// 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

Copy link
Member

@ota-meshi ota-meshi left a 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! Thank you!

@ota-meshi ota-meshi merged commit c210505 into vuejs:master Jun 27, 2020
@tyankatsu0105 tyankatsu0105 deleted the add-rule/no-empty-component-block branch June 28, 2020 05:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New rule: no-empty-component-block
2 participants