From 15ba863914fc041dcdc399df1a79784b0ba8354f Mon Sep 17 00:00:00 2001 From: Chiawen Chen Date: Fri, 18 Oct 2019 18:03:49 +0800 Subject: [PATCH] [Fix] false positive for prefer-default-export with type export --- src/rules/prefer-default-export.js | 8 ++++---- tests/src/rules/prefer-default-export.js | 9 ++++++++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/rules/prefer-default-export.js b/src/rules/prefer-default-export.js index f0af00d02..17a07688c 100644 --- a/src/rules/prefer-default-export.js +++ b/src/rules/prefer-default-export.js @@ -14,6 +14,7 @@ module.exports = { let specifierExportCount = 0 let hasDefaultExport = false let hasStarExport = false + let hasTypeExport = false let namedExportNode = null function captureDeclaration(identifierOrPattern) { @@ -50,9 +51,6 @@ module.exports = { // if there are specifiers, node.declaration should be null if (!node.declaration) return - // don't warn on single type aliases, declarations, or interfaces - if (node.exportKind === 'type') return - const { type } = node.declaration if ( @@ -61,6 +59,8 @@ module.exports = { type === 'TSInterfaceDeclaration' || type === 'InterfaceDeclaration' ) { + specifierExportCount++ + hasTypeExport = true return } @@ -86,7 +86,7 @@ module.exports = { }, 'Program:exit': function() { - if (specifierExportCount === 1 && !hasDefaultExport && !hasStarExport) { + if (specifierExportCount === 1 && !hasDefaultExport && !hasStarExport && !hasTypeExport) { context.report(namedExportNode, 'Prefer default export.') } }, diff --git a/tests/src/rules/prefer-default-export.js b/tests/src/rules/prefer-default-export.js index 452192737..19aef41e0 100644 --- a/tests/src/rules/prefer-default-export.js +++ b/tests/src/rules/prefer-default-export.js @@ -3,7 +3,7 @@ import { test, getNonDefaultParsers } from '../utils' import { RuleTester } from 'eslint' const ruleTester = new RuleTester() - , rule = require('rules/prefer-default-export') + , rule = require('../../../src/rules/prefer-default-export') ruleTester.run('prefer-default-export', rule, { valid: [ @@ -194,6 +194,13 @@ context('TypeScript', function() { }, parserConfig, ), + test ( + { + code: 'export interface foo { bar: string; }; export function goo() {}', + parser, + }, + parserConfig, + ), ], invalid: [], })