Skip to content

Commit

Permalink
no-empty-file: Fix false positive with triple-slash directives (#1605)
Browse files Browse the repository at this point in the history
  • Loading branch information
manovotny committed Nov 15, 2021
1 parent 426f01a commit 6354bb8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
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

0 comments on commit 6354bb8

Please sign in to comment.