From 6354bb86a01fbb4b4db472391734e76d68b9c04b Mon Sep 17 00:00:00 2001 From: Michael Novotny Date: Sun, 14 Nov 2021 21:03:36 -0600 Subject: [PATCH] `no-empty-file`: Fix false positive with triple-slash directives (#1605) --- rules/no-empty-file.js | 13 +++++++++++++ test/no-empty-file.mjs | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/rules/no-empty-file.js b/rules/no-empty-file.js index be8a30e6e0..948a8e6969 100644 --- a/rules/no-empty-file.js +++ b/rules/no-empty-file.js @@ -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(); @@ -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, diff --git a/test/no-empty-file.mjs b/test/no-empty-file.mjs index 1669056099..1fa0816494 100644 --- a/test/no-empty-file.mjs +++ b/test/no-empty-file.mjs @@ -37,6 +37,10 @@ test.snapshot({ 'svelte', 'tsx', ].map(extension => ({code: '', filename: `example.${extension}`})), + ...[ + 'd.ts', + 'ts', + ].map(extension => ({code: '/// ', filename: `example.${extension}`})), ], invalid: [ ...[