Skip to content

[Bug]: babel/traverse cannot handle typescript function overloading in a default export #13594

@thisissami

Description

@thisissami

💻

  • Would you like to work on a fix?

How are you using Babel?

@babel/cli

Input code

The following code is valid TypeScript but crashes babel.

export default function(choiceField: PDFDict, selectedIndex: number): void;
export default function(choiceField: PDFDict, selectedValue: string): void;
export default function(choiceField: PDFDict, selection: number | string) {
  // ...
}

Configuration file name

babel.config.js

Configuration

{
  presets: ['@babel/preset-typescript']
}

Current and expected behavior

The expected behaviour is that it would transform properly. Instead, it crashes babel with the error stack that follows.

{ TypeError: /home/thisissami/code/s2sBackend/s2sWebApi/src/libraries_v2/PDF/fillPDF/interactiveFields/choiceFields/fillChoiceField.ts: Cannot read property 'name' of null
at PluginPass.enter (/home/thisissami/code/s2sBackend/s2sWebApi/node_modules/@babel/plugin-transform-typescript/lib/index.js:263:59)
at newFn (/home/thisissami/code/s2sBackend/s2sWebApi/node_modules/@babel/core/node_modules/@babel/traverse/lib/visitors.js:171:21)
at NodePath._call (/home/thisissami/code/s2sBackend/s2sWebApi/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:53:20)
at NodePath.call (/home/thisissami/code/s2sBackend/s2sWebApi/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:40:17)
at NodePath.visit (/home/thisissami/code/s2sBackend/s2sWebApi/node_modules/@babel/core/node_modules/@babel/traverse/lib/path/context.js:90:31)
at TraversalContext.visitQueue (/home/thisissami/code/s2sBackend/s2sWebApi/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:99:16)
at TraversalContext.visitSingle (/home/thisissami/code/s2sBackend/s2sWebApi/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:73:19)
at TraversalContext.visit (/home/thisissami/code/s2sBackend/s2sWebApi/node_modules/@babel/core/node_modules/@babel/traverse/lib/context.js:127:19)
at Function.traverse.node (/home/thisissami/code/s2sBackend/s2sWebApi/node_modules/@babel/core/node_modules/@babel/traverse/lib/index.js:76:17)
at traverse (/home/thisissami/code/s2sBackend/s2sWebApi/node_modules/@babel/core/node_modules/@babel/traverse/lib/index.js:56:12) code: 'BABEL_TRANSFORM_ERROR' }

Environment

  • Babel Version: 7.14.8
  • Node: 10.24.0
  • OS: both windows 10 and Ubuntu 18.04

Possible solution

This is not a fix, but it is a workaround. Avoiding overloading the default directly, but instead overloading the function elsewhere and then exporting that overloaded function as default works, as follows:

function fillChoiceField(choiceField: PDFDict, selectedIndex: number): void;
function fillChoiceField(choiceField: PDFDict, selectedValue: string): void;
function fillChoiceField(choiceField: PDFDict, selection: number | string) {
  // ...
}

export default fillChoiceField;

Additional context

No response

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions