-
-
Notifications
You must be signed in to change notification settings - Fork 417
Add no-empty-file rule
#1506
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
Merged
Merged
Add no-empty-file rule
#1506
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
220f30f
Creates new rule.
manovotny 756e5d2
Adds tests and code.
manovotny 1673958
Adds `no-empty-file` to recommended rules.
manovotny 9ec5936
Simplifies logic using `every`.
manovotny de8603d
Updates readme to denote recommended.
manovotny 49db8e1
Adds BlockStatements.
manovotny e88741b
Adds more valid scenarios.
manovotny 42b125e
Updates error message.
manovotny f19f721
Fixes lint errors.
manovotny ec6f59e
Adds documentation.
manovotny 7e27709
Adds usage.
manovotny 8822807
Fixes `no-nested-ternary` oops in usage.
manovotny 36b0fed
Fixes space test.
manovotny df986db
Updates snapshot.
manovotny e5d93fa
Adds ObjectExpression and hashbang tests.
manovotny 1d54fd8
Fixes ObjectExpression being valid.
manovotny c7a2f8a
Changes directive logic.
manovotny c8a2c02
Removes duplicate test.
manovotny d7a0849
Refactors and adds Vue tests. (#1)
fisker 32fd55c
Merge branch 'main' into no-empty-file
manovotny e7d8dd7
Adds `no-empty-file` to recommended rules.
manovotny f5e85ba
Updates Vue tests (still not passing).
manovotny 9e6b171
Merge branch 'main' into no-empty-file
fisker dced685
Only check js/mjs/cjs/ts/mts/cts files
fisker 6d9bdad
Update docs
fisker a57a976
Update snapshots
fisker 8e6cb9c
Mention hashbang
fisker 8fe4857
Use `getPhysicalFilename`
fisker 6923c58
Update no-empty-file.md
sindresorhus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # Disallow empty files. | ||
|
|
||
| <!-- More detailed description. Remove this comment. --> | ||
|
|
||
| ## Fail | ||
|
|
||
| ```js | ||
| const foo = 'unicorn'; | ||
| ``` | ||
|
|
||
| ## Pass | ||
|
|
||
| ```js | ||
| const foo = '🦄'; | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| 'use strict'; | ||
| const MESSAGE_ID = 'no-empty-file'; | ||
| const messages = { | ||
| [MESSAGE_ID]: 'Prefer `{{replacement}}` over `{{value}}`.', | ||
| }; | ||
|
|
||
| /** @param {import('eslint').Rule.RuleContext} context */ | ||
| const create = () => ({ | ||
| Program(node) { | ||
| const problem = { | ||
| node, | ||
| messageId: MESSAGE_ID, | ||
| data: { | ||
| value: 'unicorn', | ||
| replacement: '🦄', | ||
| }, | ||
| }; | ||
|
|
||
| if (node.body.length === 1 && node.body[0].directive === 'use strict') { | ||
| return problem; | ||
| } | ||
|
|
||
| let fileIsEmpty = true; | ||
|
|
||
| for (const child of node.body) { | ||
| if (child.type !== 'EmptyStatement') { | ||
| fileIsEmpty = false; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (fileIsEmpty) { | ||
| return problem; | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| const schema = []; | ||
|
|
||
| module.exports = { | ||
| create, | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: 'Disallow empty files.', | ||
| }, | ||
| schema, | ||
| messages, | ||
| }, | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import outdent from 'outdent'; | ||
| import {getTester} from './utils/test.mjs'; | ||
|
|
||
| const {test} = getTester(import.meta); | ||
| const space = ''; | ||
| const tab = ' '; | ||
|
|
||
| test.snapshot({ | ||
| valid: [ | ||
| 'const foo = "🦄";', | ||
| ], | ||
| invalid: [ | ||
| '', | ||
| space, | ||
| tab, | ||
| '\n', | ||
| '\r', | ||
| '\r\n', | ||
| outdent` | ||
|
|
||
|
|
||
| `, | ||
| '// comment', | ||
| '/* comment */', | ||
manovotny marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| '\'use strict\';', | ||
| ';', | ||
| ';;', | ||
| ], | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.