buxlabs/abstract-syntax-tree

[Typescript Error]: Function declaration must have a name in this context

felipelealdefaria opened this issue · 2 comments

Hi, I'm working on a project where I need to convert snippets of code to AST. I have two simple examples of functions using Typescript:

export const arrowFunction = (value: any): string => {
  const { type } = value
  return type
}

export function regularFunction (value: any): string {
  // this is a regular function
  const { type } = value
  return type
}

For the case of regularFunction I don't have any problem, but when I use the parse function in arrowFunction I get the error below:

<rootDir>/node_modules/meriyah/dist/meriyah.cjs:187
    throw new ParseError(parser.index, parser.line, parser.column, type, ...params);
    ^

ParseError [SyntaxError]: [1:10]: Function declaration must have a name in this context
    at report (<rootDir>/node_modules/meriyah/dist/meriyah.cjs:187:11)
    at parseFunctionDeclaration (<rootDir>/node_modules/meriyah/dist/meriyah.cjs:6592:13)
    at parseStatementListItem (<rootDir>/node_modules/meriyah/dist/meriyah.cjs:4853:20)
    at parseModuleItem (<rootDir>/node_modules/meriyah/dist/meriyah.cjs:4840:26)
    at parseModuleItemList (<rootDir>/node_modules/meriyah/dist/meriyah.cjs:4825:25)
    at parseSource (<rootDir>/node_modules/meriyah/dist/meriyah.cjs:4769:16)
    at Object.parseModule (<rootDir>/node_modules/meriyah/dist/meriyah.cjs:8780:12)
    at parse (<rootDir>/node_modules/abstract-syntax-tree/src/parse.js:4:18)
    at parse (<rootDir>/node_modules/abstract-syntax-tree/index.js:75:12)
    at Object.<anonymous> (<rootDir>/dist/clones/arrow-and-regular/index.js:10:43) {
  index: 10,
  line: 1,
  column: 10,
  description: '[1:10]: Function declaration must have a name in this context',
  loc: { line: 1, column: 10 }
}

I realized that it can be related to the newer syntaxes of the language and remembering that I only have this error when using Typescript.

Library Version
abstract-syntax-tree 2.19.1
typescript ^4.2.3

hey @felipelealdefaria, thanks for using the library. Are you trying to parse the output that's compiled via typescript? How does the compiled output look like?

Hi @emilos, thanks for answering!

I was trying to parse the typescript code directly. However, today I got the answer that the library meriyah doesn't understand Typescript syntax, only Javascript.

Sorry for the misunderstanding, I will follow the project using only Javascript :)