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

Add no-empty-file rule #1506

Merged
merged 29 commits into from Nov 2, 2021
Merged

Conversation

manovotny
Copy link
Contributor

Fixes #1477.

rules/no-empty-file.js Outdated Show resolved Hide resolved
@fisker
Copy link
Collaborator

fisker commented Aug 30, 2021

Empty BlockStatement should also consider empty

{
}
{
;;;
}
{
	'use strict';
}
{
	  {
	  }
}

@manovotny manovotny marked this pull request as ready for review August 30, 2021 17:56
rules/no-empty-file.js Outdated Show resolved Hide resolved
@manovotny
Copy link
Contributor Author

@fisker all your changes have been incorporated. Please let me know if there is anything else you'd like to see.

test/no-empty-file.mjs Outdated Show resolved Hide resolved
test/no-empty-file.mjs Outdated Show resolved Hide resolved
@manovotny
Copy link
Contributor Author

@fisker everything should once again be addressed. Please let me know if you notice anything else.

@fisker
Copy link
Collaborator

fisker commented Sep 3, 2021

Everything looks good, I'll take a deeper look ASAP, I've been very busy recently.

@manovotny
Copy link
Contributor Author

@fisker no worries! Was just making sure there wasn't anything else immediate to take care of. Totally understand busyness, so take whatever time you need.

@fisker
Copy link
Collaborator

fisker commented Sep 5, 2021

Can you merge manovotny#1, and fix failed tests? I can't commit to your fork.

@manovotny
Copy link
Contributor Author

manovotny commented Sep 8, 2021

@fisker I'm not sure the Vue tests that were added are correct... I am not a Vue developer (I use React), so I am not an expert here, but in reading the Vue Single File Components documentation, I think this is what we'd want for tests instead. Do you agree / disagree?

test.snapshot({
	testerOptions: {
		parser: parsers.vue,
	},
	valid: [
		outdent`
			<template></template>
			<style></style>
			<script>const x = 0;</script>
			<custom-block></custom-block>
		`,
	],
	invalid: [
		outdent`
			<template></template>
			<style></style>
			<script></script>
			<custom-block></custom-block>
		`,
		outdent`
			<template></template>
			<style></style>
			<script>;</script>
			<custom-block></custom-block>
		`,
	],
});

@manovotny
Copy link
Contributor Author

@fisker @sindresorhus can I get any input on my Vue comment above and if it is correct or incorrect?

@fisker
Copy link
Collaborator

fisker commented Sep 21, 2021

I'm sorry, somehow I missed this comment.

I'm not sure what you mean. Are you saying we should have all those blocks? If true, I disagree. A SFC file only have <script> is valid, the template can be replaced by render function in <script>, and only have <template> is also valid.

@manovotny
Copy link
Contributor Author

@fisker I'm sure you only have one or two comments a day to reply to. 😂 Sincerely no worries.

Good to know. As I stated, I have little to no experience. I will proceed with your tests, but might need some insight. I'll let you know if I have questions.

@manovotny
Copy link
Contributor Author

@fisker I guess where I'm still slightly confused is that you have all of these marked as valid.

test.snapshot({
	testerOptions: {
		parser: parsers.vue,
	},
	valid: [
		'<template></template>',
		'<style></style>',
		'<script></script>',
		'<script>;</script>',
		'<custom-block></custom-block>',
	],
	invalid: [
		'   ',
	],
});

But if they don't contain any logic, markup, or styles, they would be considered "empty", right? Shouldn't these all be considered invalid instead?

Also seems like we should add tests for valid tests for <script src="./component.js"></script> and <style src="./component.css"></style> too.

@fisker
Copy link
Collaborator

fisker commented Sep 21, 2021

Also seems like we should add tests for valid tests for <script src="./component.js"></script> and <style src="./component.css"></style> too.

👍

But if they don't contain any logic, markup, or styles, they would be considered "empty", right?

I'm not really sure about those cases now, maybe we should just ignore .vue files.

@manovotny
Copy link
Contributor Author

@fisker sorry for the long delay. I went down a deep rabbit hole with Vue to understand it a bit better (I had never used it before). I created some experiments in a repo, which also lists some helpful references in it's readme.

As far as I can tell, an SFC is invalid unless it has a render function in the script block or a template block. There's a whole bunch of other scenarios to consider with styles and src attributes too.

I updated the tests to what I think are proper valid and invalid tests. I am interested in your opinion of them.

Even if we agree that these are correct, I still am somewhat unclear on how to best make the tests pass. I debugged the tests for a while, but I didn't feel confident that I understood how to best check the Vue specific code. I've looked at other Vue examples in the repo, but I am still not understanding it well. I'd be interested in some hints or suggestions or like you previously mentioned, we can skip handling Vue completely.

@fisker
Copy link
Collaborator

fisker commented Nov 1, 2021

As far as I can tell, an SFC is invalid unless it has a render function in the script block or a template block.

I think you are right about this, but there is also an edge case, the empty component is used with a global mixin (with render of cause), then it can be a valid component. I'm just pointing the possibility in theory.


I'm also a little worried about svelte SFC files(I don't know about it), and empty code blocks in markdown files.

@fisker
Copy link
Collaborator

fisker commented Nov 1, 2021

This PR has been blocked for quite a long time.

Let's do this, only check .js/.mjs/.cjs/.ts/.mts/.cts files for now.

I've committed changes, hope it's fine.

@sindresorhus WDYT?

@manovotny
Copy link
Contributor Author

@fisker that sounds like a good plan to me. If / When a clearer path forward for Vue, Svelte, etc. becomes more obvious, we can do a follow up PR and address it at that time.

@sindresorhus sindresorhus merged commit a2ba25e into sindresorhus:main Nov 2, 2021
@sindresorhus
Copy link
Owner

Thanks for contributing this rule :)

@manovotny manovotny deleted the no-empty-file branch November 2, 2021 10:26
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.

Rule proposal: no-empty-file
3 participants