lezer-parser / javascript

A JavaScript lezer grammar

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

generic type as return type of arrow function

kawmra opened this issue · 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!