NUglify icon indicating copy to clipboard operation
NUglify copied to clipboard

overloading `async` as identifier and invoking as a method is not supported

Open trullock opened this issue 5 years ago • 4 comments

if you do this:

function (async) {
    async();
}

i.e. if you overload async as an identifier, and then call it as a function (with or without arguments) the parser will not understand this line and will strip it, removing the invocation

I don't know how to fix this without some serious rewriting of the parser, which I can't be arsed to do for such a sadistic case.

function (async) {
    async = 1; // this will work
    async[1] = 2; // this will work
    async(); // this line will be stripped
    async(a,b); // this will end up as: a,b
}

The issue can be seen in JSParser, line ~4075 inside the Async case. You end up in here because async() is an expression, but the parser (quite reasonably) expects you do be either doing async () => { } or async function() { }. The 2nd case is easily handled because of the unique presence of function, however diffrentiating between the former is not currently easy - at least I can't see how.

Marking as wontfix because its such a mad case, but someone is welcome to try

trullock avatar Aug 21 '20 08:08 trullock

@trullock I haven't looked at the parser code, but maybe some logic like this could work?

  • Try to parse the sequence starting with ( as an argument list; if that fails then it can't be async () => .
  • If it succeeds then check to see if the next token is =>; if so then this is a lambda named async.

madelson avatar Nov 23 '20 14:11 madelson

@madelson perhaps take a look at the code

trullock avatar Nov 23 '20 19:11 trullock

Another case that's a little different than the examples above:

function foo(out, async) {
  return async
    ? out.replace('x', 'y')
    : out.replace('w', 'y');
}

test.js(3,17-18): run-time error JS1195: Expected expression: .
test.js(4,12-13): run-time error JS1195: Expected expression: :

function foo(){return async()=>.replace("x","y")}

drwestco avatar Mar 15 '21 17:03 drwestco

Thanks for the example

As per the readme and above, I've not got the time to fix this.

PRs welcome

trullock avatar Mar 15 '21 18:03 trullock