Skip to content

Commit

Permalink
[fix] export: false positive for typescript overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
golopot committed Jul 12, 2019
1 parent 6512110 commit 5703fb0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/rules/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ ambient namespaces:
const rootProgram = 'root'
const tsTypePrefix = 'type:'

/**
* Detect function overloads like:
* ```js
* export function foo(a: number) {}
* export function foo(a: string) {}
* ```
* @param {Set<Object>} nodes
* @returns {boolean}
*/
function isTypescriptFunctionOverloads(nodes) {
return [...nodes].every(node => node.parent.type === 'FunctionDeclaration')
}

module.exports = {
meta: {
type: 'problem',
Expand All @@ -34,6 +47,7 @@ module.exports = {

create: function (context) {
const namespace = new Map([[rootProgram, new Map()]])
const isTypescriptFile = /\.ts|\.tsx$/.test(context.getFilename())

function addNamed(name, node, parent, isType) {
if (!namespace.has(parent)) {
Expand Down Expand Up @@ -123,6 +137,8 @@ module.exports = {
for (let [name, nodes] of named) {
if (nodes.size <= 1) continue

if (isTypescriptFile && isTypescriptFunctionOverloads(nodes)) continue

for (let node of nodes) {
if (name === 'default') {
context.report(node, 'Multiple default exports.')
Expand Down
16 changes: 16 additions & 0 deletions tests/src/rules/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ context('Typescript', function () {
`,
}, parserConfig)),

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

test(Object.assign({
code: `
export function ggg(a: string) {};
export function ggg(a: number) {};
`,
filename: 'foo.tsx',
}, parserConfig)),

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

0 comments on commit 5703fb0

Please sign in to comment.