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

13 changes: 13 additions & 0 deletions rules/no-empty-file.js
Expand Up @@ -13,6 +13,12 @@ const isEmpty = node =>
|| node.type === 'EmptyStatement'
|| (node.type === 'ExpressionStatement' && 'directive' in node);

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

const hasTripeSlashDirectives = comments =>
comments.some(currentNode => isTripleSlashDirective(currentNode));

/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const filename = context.getPhysicalFilename().toLowerCase();
Expand All @@ -27,6 +33,13 @@ const create = context => {
return;
}

const sourceCode = context.getSourceCode();
const comments = sourceCode.getAllComments();

if (hasTripeSlashDirectives(comments)) {
return;
}

return {
node,
messageId: MESSAGE_ID,
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