diff --git a/docs/rules/no-empty-file.md b/docs/rules/no-empty-file.md index 16fa4ac302..9b314652ed 100644 --- a/docs/rules/no-empty-file.md +++ b/docs/rules/no-empty-file.md @@ -68,3 +68,24 @@ const x = 0; const x = 0; } ``` + +## Options + +### allow + +Type: `boolean`\ +Default: `false` + +You can set the `allow` option like this: + +```js +"unicorn/no-empty-file": { + "allowComments": true +} +``` + +If you set it to `true`, you can store files that contain only comments + +```js +// Comment +``` diff --git a/rules/no-empty-file.js b/rules/no-empty-file.js index f833886e05..df31949923 100644 --- a/rules/no-empty-file.js +++ b/rules/no-empty-file.js @@ -17,6 +17,10 @@ const hasTripeSlashDirectives = comments => /** @param {import('eslint').Rule.RuleContext} context */ const create = context => { const filename = context.physicalFilename; + const options = { + allowComments: false, + ...context.options[0], + }; if (!/\.(?:js|mjs|cjs|jsx|ts|mts|cts|tsx)$/i.test(filename)) { return; @@ -35,6 +39,10 @@ const create = context => { return; } + if (options.allowComments && comments.length > 0) { + return; + } + return { node, messageId: MESSAGE_ID, @@ -43,6 +51,18 @@ const create = context => { }; }; +const schema = [ + { + type: 'object', + additionalProperties: false, + properties: { + allowComments: { + type: 'boolean', + }, + }, + }, +]; + /** @type {import('eslint').Rule.RuleModule} */ module.exports = { create, @@ -52,6 +72,7 @@ module.exports = { description: 'Disallow empty files.', recommended: true, }, + schema, messages, }, }; diff --git a/test/no-empty-file.mjs b/test/no-empty-file.mjs index 2d28d85749..6dfd4842e4 100644 --- a/test/no-empty-file.mjs +++ b/test/no-empty-file.mjs @@ -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: [{allowComments: true}]})), '', ...[ 'md',