Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[fix] export: false positive for typescript overloads #1412

Merged
merged 1 commit into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/rules/export.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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