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
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
220f30f
Creates new rule.
manovotny Aug 23, 2021
756e5d2
Adds tests and code.
manovotny Aug 30, 2021
1673958
Adds `no-empty-file` to recommended rules.
manovotny Aug 30, 2021
9ec5936
Simplifies logic using `every`.
manovotny Aug 30, 2021
de8603d
Updates readme to denote recommended.
manovotny Aug 30, 2021
49db8e1
Adds BlockStatements.
manovotny Aug 30, 2021
e88741b
Adds more valid scenarios.
manovotny Aug 30, 2021
42b125e
Updates error message.
manovotny Aug 30, 2021
f19f721
Fixes lint errors.
manovotny Aug 30, 2021
ec6f59e
Adds documentation.
manovotny Aug 30, 2021
7e27709
Adds usage.
manovotny Aug 30, 2021
8822807
Fixes `no-nested-ternary` oops in usage.
manovotny Aug 30, 2021
36b0fed
Fixes space test.
manovotny Aug 30, 2021
df986db
Updates snapshot.
manovotny Aug 30, 2021
e5d93fa
Adds ObjectExpression and hashbang tests.
manovotny Aug 30, 2021
1d54fd8
Fixes ObjectExpression being valid.
manovotny Aug 30, 2021
c7a2f8a
Changes directive logic.
manovotny Aug 31, 2021
c8a2c02
Removes duplicate test.
manovotny Aug 31, 2021
d7a0849
Refactors and adds Vue tests. (#1)
fisker Sep 7, 2021
32fd55c
Merge branch 'main' into no-empty-file
manovotny Sep 21, 2021
e7d8dd7
Adds `no-empty-file` to recommended rules.
manovotny Sep 21, 2021
f5e85ba
Updates Vue tests (still not passing).
manovotny Oct 23, 2021
9e6b171
Merge branch 'main' into no-empty-file
fisker Nov 1, 2021
dced685
Only check js/mjs/cjs/ts/mts/cts files
fisker Nov 1, 2021
6d9bdad
Update docs
fisker Nov 1, 2021
a57a976
Update snapshots
fisker Nov 1, 2021
8e6cb9c
Mention hashbang
fisker Nov 1, 2021
8fe4857
Use `getPhysicalFilename`
fisker Nov 1, 2021
6923c58
Update no-empty-file.md
sindresorhus Nov 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 15 additions & 0 deletions docs/rules/no-empty-file.md
@@ -0,0 +1,15 @@
# Disallow empty files.

<!-- More detailed description. Remove this comment. -->

## Fail

```js
const foo = 'unicorn';
```

## Pass

```js
const foo = '🦄';
```
1 change: 1 addition & 0 deletions readme.md
Expand Up @@ -178,6 +178,7 @@ Each rule has emojis denoting:
| [no-array-reduce](docs/rules/no-array-reduce.md) | Disallow `Array#reduce()` and `Array#reduceRight()`. | ✅ | | |
| [no-console-spaces](docs/rules/no-console-spaces.md) | Do not use leading/trailing space between `console.log` parameters. | ✅ | 🔧 | |
| [no-document-cookie](docs/rules/no-document-cookie.md) | Do not use `document.cookie` directly. | ✅ | | |
| [no-empty-file](docs/rules/no-empty-file.md) | Disallow empty files. | | | |
| [no-for-loop](docs/rules/no-for-loop.md) | Do not use a `for` loop that can be replaced with a `for-of` loop. | ✅ | 🔧 | |
| [no-hex-escape](docs/rules/no-hex-escape.md) | Enforce the use of Unicode escapes instead of hexadecimal escapes. | ✅ | 🔧 | |
| [no-instanceof-array](docs/rules/no-instanceof-array.md) | Require `Array.isArray()` instead of `instanceof Array`. | ✅ | 🔧 | |
Expand Down
50 changes: 50 additions & 0 deletions rules/no-empty-file.js
@@ -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') {
manovotny marked this conversation as resolved.
Show resolved Hide resolved
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,
},
};
29 changes: 29 additions & 0 deletions test/no-empty-file.mjs
@@ -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.
Show resolved Hide resolved
'\'use strict\';',
';',
';;',
],
});