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

no-empty-file: Added option to allow comments #2300

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
23 changes: 23 additions & 0 deletions docs/rules/no-empty-file.md
Expand Up @@ -68,3 +68,26 @@ const x = 0;
const x = 0;
}
```

## Options

### allow

Type: `string[]`\
Default: `[]`

You can set the `allow` option like this:

```js
"unicorn/no-empty-file": {
"allow": [
"comments"
Copy link
Owner

Choose a reason for hiding this comment

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

Tab-indentation

]
}
```

If you set it to `comments`, you can only store files with comments
Copy link
Owner

Choose a reason for hiding this comment

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

This sentence could be made more clear.


```js
// Comment
```
24 changes: 24 additions & 0 deletions rules/no-empty-file.js
Expand Up @@ -17,6 +17,10 @@ const hasTripeSlashDirectives = comments =>
/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const filename = context.physicalFilename;
const options = {
allow: [],
...context.options[0],
};

if (!/\.(?:js|mjs|cjs|jsx|ts|mts|cts|tsx)$/i.test(filename)) {
return;
Expand All @@ -35,6 +39,10 @@ const create = context => {
return;
}

if (options.allow.includes('comments') && comments.length > 0) {
return;
}

return {
node,
messageId: MESSAGE_ID,
Expand All @@ -43,6 +51,21 @@ const create = context => {
};
};

const schema = [
{
type: 'object',
additionalProperties: false,
properties: {
allow: {
type: 'array',
items: {
type: 'string',
},
},
},
},
];

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
Expand All @@ -52,6 +75,7 @@ module.exports = {
description: 'Disallow empty files.',
recommended: true,
},
schema,
messages,
},
};
17 changes: 17 additions & 0 deletions test/no-empty-file.mjs
Expand Up @@ -30,6 +30,23 @@ test.snapshot({
'[]',
'(() => {})()',
].map(code => ({code, filename: 'example.js'})),
...[
'// comment',
'/* comment */',
'/// comment',
'/* comment */ {}',
outdent`
/*
comment
*/
`,
outdent`
/*
comment
*/
console.log('done');
`,
].map(code => ({code, filename: 'example.js', options: [{allow: ['comments']}]})),
'',
...[
'md',
Expand Down