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

10 changes: 9 additions & 1 deletion rules/no-empty-file.js
Expand Up @@ -13,17 +13,25 @@ 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.every(currentNode => !isTripleSlashDirective(currentNode));
manovotny marked this conversation as resolved.
Show resolved Hide resolved

/** @param {import('eslint').Rule.RuleContext} context */
const create = context => {
const filename = context.getPhysicalFilename().toLowerCase();
const sourceCode = context.getSourceCode();
const comments = sourceCode.getAllComments();

if (!/\.(?:js|mjs|cjs|ts|mts|cts)$/.test(filename)) {
return {};
}

return {
Program(node) {
if (!isEmpty(node)) {
if (!isEmpty(node) || hasTripeSlashDirectives(comments)) {
manovotny marked this conversation as resolved.
Show resolved Hide resolved
return;
}

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