Skip to content

Commit 6354bb8

Browse files
authored
no-empty-file: Fix false positive with triple-slash directives (#1605)
1 parent 426f01a commit 6354bb8

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

rules/no-empty-file.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ const isEmpty = node =>
1313
|| node.type === 'EmptyStatement'
1414
|| (node.type === 'ExpressionStatement' && 'directive' in node);
1515

16+
const isTripleSlashDirective = node =>
17+
node.type === 'Line' && node.value.startsWith('/');
18+
19+
const hasTripeSlashDirectives = comments =>
20+
comments.some(currentNode => isTripleSlashDirective(currentNode));
21+
1622
/** @param {import('eslint').Rule.RuleContext} context */
1723
const create = context => {
1824
const filename = context.getPhysicalFilename().toLowerCase();
@@ -27,6 +33,13 @@ const create = context => {
2733
return;
2834
}
2935

36+
const sourceCode = context.getSourceCode();
37+
const comments = sourceCode.getAllComments();
38+
39+
if (hasTripeSlashDirectives(comments)) {
40+
return;
41+
}
42+
3043
return {
3144
node,
3245
messageId: MESSAGE_ID,

test/no-empty-file.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ test.snapshot({
3737
'svelte',
3838
'tsx',
3939
].map(extension => ({code: '', filename: `example.${extension}`})),
40+
...[
41+
'd.ts',
42+
'ts',
43+
].map(extension => ({code: '/// <reference types="example" />', filename: `example.${extension}`})),
4044
],
4145
invalid: [
4246
...[

0 commit comments

Comments
 (0)