metadevpro/ts-pegjs

Errors when format set to "es"

jamesfoster opened this issue · 5 comments

Just tried generating the parser using the es format and I get the following errors. Thanks for any help!

Cannot redeclare exported variable 'SyntaxError'.
  > export class SyntaxError extends Error {
Cannot find name 'IParseOptions'.
  > function peg$parse(input: string, options?: IParseOptions) {
Cannot redeclare exported variable 'SyntaxError'.
  > export {
  >   SyntaxError as SyntaxError,
  >   peg$parse as parse
  > };

I just also ran into the issue.

Syntax error has two exports which triggers the error. If you remove SyntaxError as SyntaxError, from the export. The options IParseOptions I replaced with type any.

This is a fix applied by hand.

Hi. I did the following using the test cases:

node test/test.js --cache --trace --format es
npm run compile:samples && npm run lint:samples && npm run lint && npm run test:samples

What extra step should I take to fully reproduce your problem?

I adopted my script to run with one of your examples. I use a shell script which runs from the root of your repository.

#!/bin/bash

./node_modules/peggy/bin/peggy --plugin ./src/tspegjs -o examples/arithmetics.ts --format es examples/arithmetics.pegjs

--format es will add the export at the end of the file.

export {
  SyntaxError as SyntaxError,
  peg$parse as parse
};

Now we also run tspegjs to transform the code from js to ts. Which executes

"export class SyntaxError extends Error {",

and thus adds the second export of SyntaxError.

Hi @pjmolina

New day fresh eyes.

I got my configuration to work. The answer is do not use --format es. With --format commonjs the double declaration of SyntaxError got resolved.
The remaining issue that I faced was an options.customParameter check that I had to change in the pegjs file by changing it to options?.customParameter. Because options might be undefined.

Thanks for making this library.

Excellent! Thanks!
Yes, maybe we should recommend in the docs to use the --format commonjs version (or bare). (applies to JS gennerated by peggy only)
Because TS will be compiled later by tsc so it will be retargeted later on TS transpilation.
Closing the issue.