esm icon indicating copy to clipboard operation
esm copied to clipboard

Optional Chaining support

Open rwproj opened this issue 4 years ago • 39 comments

with latest node (13.8.0)

// example.js
let a
a?.b

works with when running node --harmony

but doesnot work with esm: node --harmony -r esm example.js

// example.js
import path from 'path'
let a
a?.b
a?.b
  ^

SyntaxError: Invalid or unexpected token
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)

rwproj avatar Feb 06 '20 10:02 rwproj

Are there any updates on this issue?

DaniloBarros avatar Mar 18 '20 15:03 DaniloBarros

Some update on this issue, I tried on the last NodeJS version (14.1.0) because you don't need --harmony flag anymore. But it still not working :disappointed:

Without esm, optional chaining works fine.

Taranys avatar Apr 30 '20 14:04 Taranys

This is too bad. Same problem with nullish coalescing in Node 14+

markamccann avatar May 07 '20 15:05 markamccann

FWIW, until esm may support it, it's working for me to use @babel/register with the plugin @babel/plugin-proposal-optional-chaining (and I also needed the plugin babel-plugin-transform-import-meta for import.meta.url which, unlike esm, babel/register doesn't understand as well as babel-plugin-transform-async-to-promises to avoid regenerator-runtime for async).

brettz9 avatar May 11 '20 09:05 brettz9

Just stumbled on this issue too. Looking forward for the fix.

canonic-epicure avatar May 15 '20 08:05 canonic-epicure

Same issue.

id: options.params?.id
                   ^
SyntaxError: Invalid or unexpected token
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)

The reason I use esm is so that I don't have to build my code using Babel. So using babel plugins isn't an option for me.

Node v14.3.0 with and without --harmony flag

twixr avatar May 28 '20 09:05 twixr

I'll update the parser. Thanks!

jdalton avatar May 28 '20 13:05 jdalton

@jdalton will this parser update include nullish coalescing? 🙏

matthewharwood avatar May 31 '20 03:05 matthewharwood

@jdalton Excited for this one!

hroland avatar Jun 03 '20 12:06 hroland

@jdalton Any update on when we can expect this?

matthewharwood avatar Jun 19 '20 17:06 matthewharwood

It works after updating acorn.

jsg2021 avatar Jul 08 '20 03:07 jsg2021

Awesome! Hope this gets released soon 👍

prashantpalikhe avatar Jul 17 '20 08:07 prashantpalikhe

Any ETA on this?

patspam avatar Sep 12 '20 06:09 patspam

It's fixed in my PR. #883

You can use it or fork it. I'm not a maintainer, so I can't merge/publish.

jsg2021 avatar Sep 12 '20 06:09 jsg2021

Can you please update the parser?

luluhoc avatar Oct 13 '20 13:10 luluhoc

@luluhoc there is a PR waiting to merge.

jsg2021 avatar Oct 13 '20 14:10 jsg2021

We've been using @jsg2021's PR in production for 4 months now without an issue! 🙌

hroland avatar Dec 07 '20 22:12 hroland

May be considered #offtopic: but how is this not supported yet? Please, pretty please merge the PR as it seems to be working for many.

chrisspiegl avatar Apr 08 '21 11:04 chrisspiegl

@chrisspiegl Maybe this does answer your question. 😉

loilo avatar Apr 08 '21 11:04 loilo

@chrisspiegl Maybe this does answer your question. wink

Far out.

So esm does not have a maintainer?

aksel avatar Apr 09 '21 08:04 aksel

So esm does not have a maintainer?

I can't say for sure. JDD had obviously done most of the housekeeping in this repo. The standard-things project has multiple members, but

  1. I'm not sure about their permissions for individual repos.
  2. I'm not sure about their responsibility for individual repos.
  3. They probably are pretty busy themselves.

🤷‍♂️

loilo avatar Apr 09 '21 09:04 loilo

So esm does not have a maintainer?

If the recent activity is an indicator, looks like the parachute landed in the Salesforce jungle 😉

twelve17 avatar Apr 30 '21 15:04 twelve17

I believe it's worth noting that ECMAScript modules support is now considered "stable" in node 16, and after a quick glance seems like using esm would be redundant (that is, if you can blindly upgrade to node16).

CleyFaye avatar Apr 30 '21 17:04 CleyFaye

@CleyFaye indeed. Though my main hangup is testing with mocks.

twelve17 avatar May 01 '21 00:05 twelve17

More over, EcmaModules support is available in Node 12 already

canonic-epicure avatar May 02 '21 07:05 canonic-epicure

That seems true that with V12+ and especially with V16, you have pretty good support for ECMAScript modules. However, the ESM module here gave a lot more 'leeway'. Moving to a clean implementation was a good chunk of work for me, but at this point I got rid of ESM in my code base at least.

chrisspiegl avatar May 05 '21 07:05 chrisspiegl

We needed this issue fixed so have forked and published our forked repo if anyone needs it.

smcenlly avatar May 21 '21 00:05 smcenlly

Ah wallaby! @smcenlly. I should revisit this stuff.

jdalton avatar May 21 '21 01:05 jdalton

This code fails. Any import word found in the code will make it fail:

node -r esm script.js

// import smart things from china
const xx = process?.version; console.log(xx)

alcalawil avatar Jul 15 '21 13:07 alcalawil

We needed this issue fixed so have forked and published our forked repo if anyone needs it.

Thanks for this @smcenlly! Unfortunately I'm still having issues with it. I was using esm to allow mocha to use esm imports, but using esm-wallaby gives me the Cannot use import statement outside a module error, which the original esm package solved for me. (Unfortunately the original esm package interprets optional chaining as syntax errors though).

I'm running node 14.17.

***EDIT The above issue was related to Mocha, not esm-wallaby. Thanks again for publishing this!

cezarcarvalhaes avatar Oct 27 '21 16:10 cezarcarvalhaes