Skip to content

Commit

Permalink
[Fix] prefer-default-export: Skips warning on a single type declara…
Browse files Browse the repository at this point in the history
…tion or type alias.

Fixes #1332.
  • Loading branch information
sharmilajesupaul authored and ljharb committed Jun 10, 2019
1 parent 6512110 commit 3ea8b4a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/rules/prefer-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ module.exports = {
// if there are specifiers, node.declaration should be null
if (!node.declaration) return

// don't count flow types exports
// don't warn on single type aliases or declarations
if (node.exportKind === 'type') return

if (node.declaration.type === 'TSTypeAliasDeclaration'
|| node.declaration.type === 'TypeAlias') return

if (node.declaration.declarations) {
node.declaration.declarations.forEach(function(declaration) {
captureDeclaration(declaration.id)
Expand Down
37 changes: 36 additions & 1 deletion tests/src/rules/prefer-default-export.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
import { test } from '../utils'

import { RuleTester } from 'eslint'
import eslintPkg from 'eslint/package.json'
import semver from 'semver';

const ruleTester = new RuleTester()
, rule = require('rules/prefer-default-export')

// Typescript
const parsers = [require.resolve('babel-eslint')];
if (semver.satisfies(eslintPkg.version, '>5.0.0')) {
parsers.push(require.resolve('@typescript-eslint/parser'))
}

if (semver.satisfies(eslintPkg.version, '<6.0.0')) {
parsers.push(require.resolve('typescript-eslint-parser'))
}

ruleTester.run('prefer-default-export', rule, {
valid: [
test({
Expand Down Expand Up @@ -72,7 +84,7 @@ ruleTester.run('prefer-default-export', rule, {
test({
code: `export type UserId = number;`,
parser: require.resolve('babel-eslint'),
}),
}),

// issue #653
test({
Expand All @@ -84,6 +96,29 @@ ruleTester.run('prefer-default-export', rule, {
parser: require.resolve('babel-eslint'),
}),

// Exporting types
...parsers.map(parser => test({
code: `
export type foo = string;
export type bar = number;`,
parser,
})),
...parsers.map(parser => test({
code: 'export type foo = string',
parser,
})),
...parsers.map(parser => test({
code: `
type foo = 'foo';
export { foo };`,
parser,
})),
...parsers.map(parser => test({
code: `
type foo = 'foo';
export { foo as bar };`,
parser,
})),
// ...SYNTAX_CASES,
],
invalid: [
Expand Down

0 comments on commit 3ea8b4a

Please sign in to comment.