Skip to content

Commit

Permalink
[fix] export: false positive for typescript overloads
Browse files Browse the repository at this point in the history
Fixes #1357.
  • Loading branch information
golopot authored and ljharb committed Jul 12, 2019
1 parent 5abd5ed commit 22d5440
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/rules/export.js
Expand Up @@ -24,6 +24,21 @@ ambient namespaces:
const rootProgram = 'root'
const tsTypePrefix = 'type:'

/**
* Detect function overloads like:
* ```ts
* export function foo(a: number);
* export function foo(a: string);
* export function foo(a: number|string) { return a; }
* ```
* @param {Set<Object>} nodes
* @returns {boolean}
*/
function isTypescriptFunctionOverloads(nodes) {
const types = new Set(Array.from(nodes, node => node.parent.type))
return types.size === 2 && types.has('TSDeclareFunction') && types.has('FunctionDeclaration')
}

module.exports = {
meta: {
type: 'problem',
Expand Down Expand Up @@ -123,6 +138,8 @@ module.exports = {
for (let [name, nodes] of named) {
if (nodes.size <= 1) continue

if (isTypescriptFunctionOverloads(nodes)) continue

for (let node of nodes) {
if (name === 'default') {
context.report(node, 'Multiple default exports.')
Expand Down
10 changes: 9 additions & 1 deletion tests/src/rules/export.js
Expand Up @@ -118,7 +118,7 @@ context('Typescript', function () {
parsers.push(require.resolve('@typescript-eslint/parser'))
}

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

Expand Down Expand Up @@ -147,6 +147,14 @@ context('Typescript', function () {
`,
}, parserConfig)),

test(Object.assign({
code: `
export function fff(a: string);
export function fff(a: number);
export function fff(a: string|number) {};
`,
}, parserConfig)),

// namespace
test(Object.assign({
code: `
Expand Down

0 comments on commit 22d5440

Please sign in to comment.