Skip to content

Commit

Permalink
Change how function overloads are recognized
Browse files Browse the repository at this point in the history
  • Loading branch information
golopot committed Jul 13, 2019
1 parent 5703fb0 commit c0bbcb6
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 18 deletions.
18 changes: 11 additions & 7 deletions src/rules/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ const tsTypePrefix = 'type:'

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

module.exports = {
Expand All @@ -47,7 +52,6 @@ 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 @@ -137,7 +141,7 @@ module.exports = {
for (let [name, nodes] of named) {
if (nodes.size <= 1) continue

if (isTypescriptFile && isTypescriptFunctionOverloads(nodes)) continue
if (isTypescriptFunctionOverloads([...nodes])) continue

for (let node of nodes) {
if (name === 'default') {
Expand Down
14 changes: 3 additions & 11 deletions tests/src/rules/export.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,10 @@ context('Typescript', function () {

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

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

// namespace
Expand Down

0 comments on commit c0bbcb6

Please sign in to comment.