tc39 / proposal-extensions

Extensions proposal for ECMAScript
MIT License
150 stars 6 forks source link

Potential hazard #10

Open MadProbe opened 3 years ago

MadProbe commented 3 years ago
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();
ljharb commented 3 years ago

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()
MadProbe commented 3 years ago

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

justinmchase commented 3 years ago

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.

hax commented 3 years ago

@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:

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).