Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for ES modules #481

Merged
merged 5 commits into from
Jun 12, 2017
Merged

Add support for ES modules #481

merged 5 commits into from
Jun 12, 2017

Conversation

fatfisz
Copy link
Contributor

@fatfisz fatfisz commented Dec 10, 2016

Related to #423.

A couple of notes:

  • I noticed that the only place where the options are being validated is in the bin/pegjs script, shouldn't it be in the generate method instead?
  • I couldn't find any existing tests for different output formats, are there any?

This does not yet contain changes to README, which I will add later if the code is good first.

Copy link
Contributor

@dmajda dmajda left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good beside my small notes. With these addressed and README.md updated this can be merged.

bin/pegjs Outdated
if (args[0] !== "amd" && args[0] !== "commonjs" && args[0] !== "globals" && args[0] !== "umd") {
abort("Module format must be one of \"amd\", \"commonjs\", \"globals\", and \"umd\".");
if (args[0] !== "amd" && args[0] !== "commonjs" && args[0] !== "esm" && args[0] !== "globals" && args[0] !== "umd") {
abort("Module format must be one of \"amd\", \"commonjs\", \"esm\", \"globals\", and \"umd\".");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be slightly better to identify the format as es2015 — it is more readily understandable. But if most common tools that work with module formats use esm, then it would be probably better to keep it (I’d like to see examples of such tools then).

The condition also got a bit too long — maybe extract the list of formats into a const named e.g. MODULE_FORMATS and rewrite the test as MODULE_FORMATS.indexOf(args[0]) === -1? Up to you. Best done as a separate commit before the addition itself.

bin/pegjs Outdated
@@ -253,7 +253,7 @@ while (args.length > 0 && isOption(args[0])) {
}

if (Object.keys(options.dependencies).length > 0) {
if (options.format !== "amd" && options.format !== "commonjs" && options.format !== "umd") {
if (options.format !== "amd" && options.format !== "commonjs" && options.format !== "esm" && options.format !== "umd") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above (es2015, maybe long condition → MODULE_FORMATS_WITH_DEPS).

@dmajda
Copy link
Contributor

dmajda commented Dec 16, 2016

@fatfisz Thanks! I just reviewed.

  • I noticed that the only place where the options are being validated is in the bin/pegjs script, shouldn't it be in the generate method instead?

My philosophy is not doing parameter validation in functions/methods when I work in dynamic languages because it would pretty much lead to duplication of the (absent) type system if done consistently. On the other hand, a program shouldn’t crash or produce an exception because of bad user input and it should produce appropriate error messages for given interface, which logically leads to validation in bin/pegjs.

  • I couldn't find any existing tests for different output formats, are there any?

No. I think the right way to test this would be using integration tests — that is, compile a parser, load it in a suitable environment for given module format, and verify that the exports are there and they work.

Setting this up is obviously a bit of work, so I chose not to do it given that all module-related code is self-contained and unlikely to break without directly touching it. But I would accept such tests as a contribution.

For this PR this means relying on manual testing.

@fatfisz
Copy link
Contributor Author

fatfisz commented Dec 19, 2016

About the name: Babel uses "es2015", Rollup uses "es", Webpack doesn't have the target yet, though the suggested name at the moment is "module".

Also, I suggested "esm" based on what I've seen in the discussions about what should be the extension for ES modules in Node.js. Though "es" and "jsm" was also listed there.

Out of those my personal preference is "es" - the same module system will be there in ES2017, ES2018, etc., so sticking to the version in this case seems a bit pointless, but I could be convinced otherwise.

Still, the decision is not up to me ;)

@dmajda
Copy link
Contributor

dmajda commented Dec 20, 2016

I think you convinced me. Having a year in the module format name may be important for projects that deal with multiple ECMAScript versions (like Babel or TypeScript), but not so much here. And it’s unlikely that the module system will ever change in a way that would justify using the year for versioning.

Let’s go with es.

@arcanis
Copy link

arcanis commented May 6, 2017

Any news about this? I currently have some troubles making Rollup understand that the parser actually does export a parse symbol, and I think this PR could help.

@tilgovi
Copy link

tilgovi commented May 19, 2017

This branch works great for me!

@futagoza
Copy link
Member

futagoza commented Jun 11, 2017

@fatfisz Github is not letting me merge this due to conflicts in README.md, so if you can just fix those, I'm happy to merge it straight away

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 11, 2017

Sure, will see if I can get it done today!

@fatfisz
Copy link
Contributor Author

fatfisz commented Jun 11, 2017

Rebased + added a missing piece of info to bin/pegjs.

@futagoza futagoza merged commit 6939d1e into pegjs:master Jun 12, 2017
@futagoza
Copy link
Member

@fatfisz Thanks, I've merged it now 😄

@allnulled
Copy link

allnulled commented Sep 30, 2019

Hi folks, I suggest to add this feature information into the real official documentation of the project.
I mean here: https://pegjs.org/documentation
Thanks for your time, folks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants