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: Fix false positive with triple-slash directives #1605

17 changes: 14 additions & 3 deletions rules/no-empty-file.js
Expand Up @@ -5,13 +5,24 @@ const messages = {
[MESSAGE_ID]: 'Empty files are not allowed.',
};

const isTripleSlashDirective = node =>
node.type === 'Line' && node.value.startsWith('/');

const isEmpty = node =>
(
(node.type === 'Program' || node.type === 'BlockStatement')
node.type === 'Program'
&& node.body.every(currentNode => isEmpty(currentNode))
&& node.comments.every(currentNode => !isTripleSlashDirective(currentNode))
manovotny marked this conversation as resolved.
Show resolved Hide resolved
)
|| (
node.type === 'BlockStatement'
&& node.body.every(currentNode => isEmpty(currentNode))
)
|| node.type === 'EmptyStatement'
|| (node.type === 'ExpressionStatement' && 'directive' in node);
|| (
node.type === 'ExpressionStatement'
&& 'directive' in node
)
|| node.type === 'EmptyStatement';

const create = context => {
const filename = context.getPhysicalFilename().toLowerCase();
Expand Down
4 changes: 4 additions & 0 deletions test/no-empty-file.mjs
Expand Up @@ -37,6 +37,10 @@ test.snapshot({
'svelte',
'tsx',
].map(extension => ({code: '', filename: `example.${extension}`})),
...[
'd.ts',
'ts',
].map(extension => ({code: '/// <reference types="example" />', filename: `example.${extension}`})),
],
invalid: [
...[
Expand Down