lezer-parser/javascript

generic type as return type of arrow function

Closed this issue · 2 comments

Using a generic type as the return type of arrow functions seems to confuse the parser.

const a = (): Array<string> => {
  return []
}

is parsed to

[
  "Script",
  "VariableDeclaration",
  "const",
  "VariableDefinition",
  "Equals",
  "BinaryExpression", // <- ?
  "BinaryExpression",
  "ArrowFunction",
  "ParamList",
  "(",
  ")",
  "TypeAnnotation",
  ":",
  "TypeName",
  "⚠",
  "CompareOp",
  "VariableName",
  "CompareOp",
  "⚠",
  "Arrow",
  "ObjectExpression",
  "{",
  "Property",
  "PropertyDefinition",
  "⚠",
  "}"
]

It only happens if specify dialect: "tsx", not dialect: "ts".

I tested that with following code:

import { parser } from "@lezer/javascript";

const script = "...";
const list: string[] = [];
parser.configure({ dialect: "tsx" }).parse(script).iterate({
  enter: (node) => {
    list.push(node.name);
  },
});
console.log(list);

There is no tsx dialect, so the parser tries to parse this as plain JavaScript. Use dialect: "ts jsx".

oh i missed that, thank you!