Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: fisker Cheung <lionkay@gmail.com> Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
- Loading branch information
1 parent
46f8638
commit a2ba25e
Showing
7 changed files
with
603 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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,65 @@ | ||
# Disallow empty files | ||
|
||
Meaningless files clutter a codebase. | ||
|
||
Disallow any files only containing the following: | ||
|
||
- Whitespace | ||
- Comments | ||
- Directives | ||
- Empty statements | ||
- Empty block statements | ||
- Hashbang | ||
|
||
## Fail | ||
|
||
```js | ||
|
||
``` | ||
|
||
```js | ||
// Comment | ||
``` | ||
|
||
```js | ||
/* Comment */ | ||
``` | ||
|
||
```js | ||
'use strict'; | ||
``` | ||
|
||
```js | ||
; | ||
``` | ||
|
||
```js | ||
{ | ||
} | ||
``` | ||
|
||
```js | ||
#!/usr/bin/env node | ||
``` | ||
|
||
## Pass | ||
|
||
```js | ||
const x = 0; | ||
``` | ||
|
||
```js | ||
'use strict'; | ||
const x = 0; | ||
``` | ||
|
||
```js | ||
;; | ||
const x = 0; | ||
``` | ||
|
||
```js | ||
{ | ||
const x = 0; | ||
} | ||
``` |
This file contains 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 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,47 @@ | ||
'use strict'; | ||
|
||
const MESSAGE_ID = 'no-empty-file'; | ||
const messages = { | ||
[MESSAGE_ID]: 'Empty files are not allowed.', | ||
}; | ||
|
||
const isEmpty = node => | ||
( | ||
(node.type === 'Program' || node.type === 'BlockStatement') | ||
&& node.body.every(currentNode => isEmpty(currentNode)) | ||
) | ||
|| node.type === 'EmptyStatement' | ||
|| (node.type === 'ExpressionStatement' && 'directive' in node); | ||
|
||
const create = context => { | ||
const filename = context.getPhysicalFilename().toLowerCase(); | ||
|
||
if (!/\.(?:js|mjs|cjs|ts|mts|cts)$/.test(filename)) { | ||
return {}; | ||
} | ||
|
||
return { | ||
Program(node) { | ||
if (!isEmpty(node)) { | ||
return; | ||
} | ||
|
||
return { | ||
node, | ||
messageId: MESSAGE_ID, | ||
}; | ||
}, | ||
}; | ||
}; | ||
|
||
module.exports = { | ||
create, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'Disallow empty files.', | ||
}, | ||
schema: [], | ||
messages, | ||
}, | ||
}; |
This file contains 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,74 @@ | ||
import outdent from 'outdent'; | ||
import {getTester} from './utils/test.mjs'; | ||
|
||
const {test} = getTester(import.meta); | ||
|
||
test.snapshot({ | ||
valid: [ | ||
...[ | ||
'const x = 0;', | ||
';; const x = 0;', | ||
'{{{;;const x = 0;}}}', | ||
outdent` | ||
'use strict'; | ||
const x = 0; | ||
`, | ||
';;\'use strict\';', | ||
'{\'use strict\';}', | ||
'("use strict")', | ||
'`use strict`', | ||
'({})', | ||
outdent` | ||
#!/usr/bin/env node | ||
console.log('done'); | ||
`, | ||
'false', | ||
'("")', | ||
'NaN', | ||
'undefined', | ||
'null', | ||
'[]', | ||
'(() => {})()', | ||
].map(code => ({code, filename: 'example.js'})), | ||
'', | ||
...[ | ||
'md', | ||
'vue', | ||
'svelte', | ||
'tsx', | ||
].map(extension => ({code: '', filename: `example.${extension}`})), | ||
], | ||
invalid: [ | ||
...[ | ||
'', | ||
'\uFEFF', | ||
' ', | ||
'\t', | ||
'\n', | ||
'\r', | ||
'\r\n', | ||
outdent` | ||
`, | ||
'// comment', | ||
'/* comment */', | ||
'#!/usr/bin/env node', | ||
'\'use asm\';', | ||
'\'use strict\';', | ||
'"use strict"', | ||
'""', | ||
';', | ||
';;', | ||
'{}', | ||
'{;;}', | ||
'{{}}', | ||
].map(code => ({code, filename: 'example.js'})), | ||
...[ | ||
'mjs', | ||
'cjs', | ||
'ts', | ||
'mts', | ||
'cts', | ||
].map(extension => ({code: '{}', filename: `example.${extension}`})), | ||
], | ||
}); |
Oops, something went wrong.