Skip to content

Commit

Permalink
feat: support function overloads (#26)
Browse files Browse the repository at this point in the history
Closes #17
  • Loading branch information
renato-bohler authored and JamieMason committed Feb 25, 2024
1 parent 4afdec8 commit f60923a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/prefer-arrow-functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,28 @@ const alwaysValid = [
{
code: 'function foo(val: any): asserts val is string {}',
},
// function overloading is unavailable in arrow functions
{
code: 'function foo(): any;',
},
{
code: 'function foo(): any; function foo() {}',
},
{
code: 'function foo(val: string): void; function foo(val: number): void; function foo(val: string | number): void {}',
},
{
code: 'const foo = () => { function bar(val: string): void; function bar(val: number): void; function bar(val: string | number): void {} }',
},
{
code: 'export function foo(): any;',
},
{
code: 'export function foo(): any; export function foo() {}',
},
{
code: 'export function foo(val: string): void; export function foo(val: number): void; export function foo(val: string | number): void {}',
},
];

const validWhenSingleReturnOnly = [
Expand Down
30 changes: 30 additions & 0 deletions src/prefer-arrow-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,19 @@ export default {
const getFunctionName = (node) =>
node && node.id && node.id.name ? node.id.name : '';

const getPreviousNode = (node) => {
if (isNamedExport(node)) {
node = node.parent;
}

if (!Array.isArray(node.parent.body)) return null;

const nodeIndex = node.parent.body.indexOf(node);
if (nodeIndex === 0) return null;

return node.parent.body[nodeIndex - 1];
};

const isGenericFunction = (node) => Boolean(node.typeParameters);
const getGenericSource = (node) => sourceCode.getText(node.typeParameters);
const isAsyncFunction = (node) => node.async === true;
Expand All @@ -90,6 +103,19 @@ export default {
node.returnType &&
node.returnType.typeAnnotation &&
node.returnType.typeAnnotation.asserts;
const isOverloadedFunction = (node) => {
const previousNode = getPreviousNode(node);

if (!previousNode) return false;
if (previousNode.type === 'TSDeclareFunction') return true;
if (
previousNode.type === 'ExportNamedDeclaration' &&
previousNode.declaration.type === 'TSDeclareFunction'
)
return true;

return false;
};

const getReturnType = (node) =>
node.returnType &&
Expand Down Expand Up @@ -206,10 +232,14 @@ export default {
const isNamedDefaultExport = (node) =>
isNamed(node) && node.parent.type === 'ExportDefaultDeclaration';

const isNamedExport = (node) =>
node.parent.type === 'ExportNamedDeclaration';

const isSafeTransformation = (node) => {
return (
!isGeneratorFunction(node) &&
!isAssertionFunction(node) &&
!isOverloadedFunction(node) &&
!containsThis(node) &&
!containsSuper(node) &&
!containsArguments(node) &&
Expand Down

0 comments on commit f60923a

Please sign in to comment.