proposal-extensions icon indicating copy to clipboard operation
proposal-extensions copied to clipboard

Potential hazard

Open MadProbe opened this issue 4 years ago • 4 comments

const ::example = { get() { console.log(1); } }
const get = function () { console.log(2); }
const cond = Math.random() > 0.5;

cond ? null::example:get(); // How will this be parsed?
cond ? null::example:get() : get(); // and this?

(To avoid this hazard i think we can change ':' ext name operator to '.:', which seems to have no hazard at all)

cond ? null::example.:get() : get();

MadProbe avatar Mar 22 '21 19:03 MadProbe

This doesn't seem ambiguous to me. The first ternary is cond ? (null::example) : get(), the second is a syntax error.

As to your suggestion, remember that 1. is legal syntax:

const ? 1.:get()

ljharb avatar Mar 22 '21 19:03 ljharb

I think we could parse .: only when <value>::<symbol> pattern is encountered and your example functions as it is now, and

1 ? 1..:1

is a syntax error

MadProbe avatar Mar 22 '21 19:03 MadProbe

Is null::example:get() a valid expression at all? Seems like it would have to be a valid expression all by itself for it to be a problem with ternaries.

justinmchase avatar Aug 13 '21 15:08 justinmchase

@MadProbe This issue already be listed in the docs (https://github.com/tc39/proposal-extensions/blob/master/docs/syntax.md#syntax-ambiguity-of-abc) though I'm sorry it not listed in the repo readme page.

There are many possible solutions, .: may work, but as the author of this proposal I insist that, if we really want change token, it should be only one character. The doc I mentioned list all possible chars, and consider other factors, the alternatives are:

  • o::Ext~method()
  • o::Ext#method()
  • o::Ext'method() (to be honest, I slightly prefer this 😅)

Is null::example:get() a valid expression at all? Seems like it would have to be a valid expression all by itself for it to be a problem with ternaries.

@justinmchase Yes, it's valid. It just use null as receiver and send to example. Basically null::example:get(...args) is just the syntax sugar of Extension.from(example).invoke(null, 'get', args).

hax avatar Sep 04 '21 04:09 hax