Skip to content

Commit

Permalink
prefer-default-export: don't warn on TypeAlias & TSTypeAliasDeclaration
Browse files Browse the repository at this point in the history
Fixes #1332.
  • Loading branch information
sharmilajesupaul authored and ljharb committed Jul 17, 2019
1 parent 1caa402 commit f4e3f1b
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
9 changes: 8 additions & 1 deletion src/rules/prefer-default-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,16 @@ 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
67 changes: 65 additions & 2 deletions tests/src/rules/prefer-default-export.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
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')
Expand Down Expand Up @@ -83,7 +85,6 @@ ruleTester.run('prefer-default-export', rule, {
code: 'export { a, b } from "foo.js"',
parser: require.resolve('babel-eslint'),
}),

// ...SYNTAX_CASES,
],
invalid: [
Expand Down Expand Up @@ -129,4 +130,66 @@ ruleTester.run('prefer-default-export', rule, {
}],
}),
],
})
});

context('Typescript', function() {
// Typescript
const parsers = [require.resolve('babel-eslint')];

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

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

parsers.forEach((parser) => {
const parserConfig = {
parser: parser,
settings: {
'import/parsers': { [parser]: ['.ts'] },
'import/resolver': { 'eslint-import-resolver-typescript': true },
},
};

ruleTester.run('prefer-default-export', rule, {
valid: [
// Exporting types
test(
{
code: `
export type foo = string;
export type bar = number;`,
parser,
},
parserConfig,
),
test(
{
code: `
export type foo = string;
export type bar = number;`,
parser,
},
parserConfig,
),
test(
{
code: 'export type foo = string',
parser,
},
parserConfig,
),
test(
{
code: 'export type foo = string',
parser,
},
parserConfig,
),
],
invalid: [],
});
});
});

0 comments on commit f4e3f1b

Please sign in to comment.