kach / nearley

📜🔜🌲 Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.59k stars 232 forks source link

FYI: moo deprecated .has() and tweak to typescript output #576

Open mceachen opened 3 years ago

mceachen commented 3 years ago

First off: thanks for Nearley! :100: Great work.

When using moo as a lexer, Nearley's call to lexer.has() in ParserRules are going to behave unexpectedly: .has() just always returns true now..

This update to moo was released Nov 2020, fwiw: https://github.com/no-context/moo/releases/tag/v0.5.0

Completely unrelated: would you want a PR that helps address modern typescript compilation? There are a couple issues that I currently fix with sed:

    "precompile": "printf '/* eslint-disable */\n\n' > grammar.ts",
    "compile": "node_modules/.bin/nearleyc grammar.ne | sed 's/declare var/declare const/g' | sed 's/@ts-ignore//g' >> grammar.ts",
  1. You probably don't want @ts-ignore. You may want eslint-disable instead?
  2. The declare var ... could be declare const ... (if they're building typescript, they're certainly on es6).
lrowe commented 2 years ago

I ended up with the following to strip out the unused vars and deprecated calls to lexer.has() but still check the result with typescript:

In package.json my compile script is:

    "grammar": "nearleyc src/grammar.ne | sed -f grammar.ts.sed | prettier --stdin-filepath src/grammar.ts > src/grammar.ts",

grammar.ts.sed:

/^declare var/d
s/(lexer.has("\([^"]*\)") ? {type: "\1"} : \1)/{type: "\1"}/g
s/^\(type NearleySymbol =\)/\1 { type: any }|/
canac commented 2 years ago

Would you be open to a PR fixing use of moo's deprecated .has()? I'd love to get rid of the deprecation warnings in my app.