peggyjs / peggy

Peggy: Parser generator for JavaScript
https://peggyjs.org/
MIT License
936 stars 64 forks source link

Typescript error: Only a void function can be called with the 'new' keyword. #171

Closed darlanalves closed 8 months ago

darlanalves commented 3 years ago

Hi! FOA thanks for reviving peg.js. I'm using for a hobby language and is great!

I recently switched to peggy and I'm writing a transpiler in Typescript that uses the output of Peggy parser. Now the problem is: the generated parser code causes this TS error:

error TS2350: Only a void function can be called with the 'new' keyword.

    588     return new peg$SyntaxError(message, null, null, location);

I can help fixing it but I don't want to break anything. Any suggestions?

Some details: I'm using commonjs as format. This happens if I try to use es as the output format.

hildjj commented 3 years ago

The code in peg$SyntaxError is pretty touchy. It needs to work in old versions of JS as well. It's just this conceptually:

class peg$SyntaxError extends Error {
  constructor(message, expected, found, location) {
    super(message)
    this.expected = expected
    ...
  }
}

See https://stackoverflow.com/questions/41102060/typescript-extending-error-class for some more hints. If you come up with a good fix, we'd very much appreciate it.

hildjj commented 1 year ago

@darlanalves are you still having this issue?

hildjj commented 8 months ago

Having not heard back, I think this problem has resolved itself. Please re-open if it still exists.