Skip to content

Default export of anonymous function declaration is misclassified as function expression #323

Open
@jackschu

Description

@jackschu

The following piece of code is valid but it is parsed incorrectly:

export default function() {}

Here's a link to the TypeScript Playground showing that the snippet above is valid JavaScript or TypeScript

The output of tree-sitter parse is the following:

(program [0, 0] - [1, 0]
  (export_statement [0, 0] - [0, 28]
    value: (function_expression [0, 15] - [0, 28]
      parameters: (formal_parameters [0, 23] - [0, 25])
      body: (statement_block [0, 26] - [0, 28]))))

This example shows treesitter classifying this as a 'function_expression' when i think it should be classified as a 'function_declaration'.

This is because function declaration requires a name.

MDN mentions

As a special case, functions and classes are exported as declarations, not expressions, and these declarations can be anonymous. This means functions will be hoisted.

I havent dug through the spec but vaguely trust MDN, also Acorn parses this as a function declaration. Misclassifying it as a function expression can leave autoformatters wanting to parenthesize this expression and likely can leave highlighters to use the wrong color.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions