Uglifyjs: [ES6] bug: yield and new

Created on 29 Dec 2017  路  5Comments  路  Source: mishoo/UglifyJS

Bug report or feature request?

Bug

ES5 or ES6+ input?

ES6

Uglify version (uglifyjs -V)

3.3.3

JavaScript input

function x() {
}

function* y() {
  let t = yield x();
  return new t();
}

The uglifyjs CLI command executed or minify() options used.

uglifyjs test.js --compress

JavaScript output or error produced.

function x(){}function*y(){return new yield x()}

return new yield doesn't seem to be a valid construct.

This is breaking code generated by webpack/typescript with dynamic imports, as the dynamic import is transpiled to use yield. (eg. await import(....))

bug harmony

All 5 comments

A workaround is to use collapse_vars: false as a compression option.

Just needs a paren rule in output.

$ echo 'function* f(x){return new yield x();}' | node
[stdin]:1
function* f(x){return new yield x();}
                          ^^^^^
SyntaxError: Unexpected identifier
$ echo 'function* f(x){return new (yield x());}' | node
$ 

@andreialecu thanks for report & test case - indeed Node.js doesn't like the uglified output:

$ cat test.js | node

$ uglifyjs test.js -bc
function* y() {
    return new yield x()();
}

$ uglifyjs test.js -bc | node
[stdin]:2
    return new yield x()();
               ^^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:152:10)

... and as I was typing this message, @kzc ninja-ed me to the solution :+1:

Question: do we allow new yield x() in the parser, or should we raise JS_Parse_Error?

Acorn brings some clarity to the issue:

$ echo 'function* f(x){return new yield x();}' | node_modules/.bin/acorn 
Can not use 'yield' as identifier inside a generator (1:26)

We should match Acorn's behavior in the parser.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jimbly picture Jimbly  路  4Comments

chrismanley picture chrismanley  路  5Comments

JoeUX picture JoeUX  路  3Comments

alexlamsl picture alexlamsl  路  5Comments

diegocr picture diegocr  路  3Comments