BundlerMinifier icon indicating copy to clipboard operation
BundlerMinifier copied to clipboard

ES6 lambda/arrow function minification error

Open mriedel90 opened this issue 6 years ago • 5 comments

Installed product versions

  • Visual Studio: 2017 Professionsal
  • This extension: 2.7.386

Description

When I use arrow/lambda functions in JS, the minifier modifies the logic incorrectly.

I use knockout and am filtering an observable array like this:

this.fileCount = ko.computed(function () {
      return this.files().filter(f => !f.isComplete() || f.isSuccess()).length;
  }, this);
  this.uploadCount = ko.computed(function () {
      return this.files().filter(f => f.isComplete() && f.isSuccess()).length;
  }, this);

The minifier turns this into ultimately the same function, both incorrect:

this.fileCount=ko.computed(function(){return this.files().filter(n=>n.isComplete()&&n.isSuccess()).length},this);
this.uploadCount=ko.computed(function(){return this.files().filter(n=>n.isComplete()&&n.isSuccess()).length},this);

Steps to recreate

  1. Minify the above code snippet

Current behavior

The logic in the .filter() method is incorrect. It seems like it tries to optimize the calculation, but it does it incorrectly.

Expected behavior

It should keep the original logic, just remove whitespace

mriedel90 avatar May 16 '18 20:05 mriedel90

Looks like another NUglify issue to me.

samjudson avatar May 19 '18 15:05 samjudson

As a work around you can write

f => { return (!f.isComplete() || f.isSuccess()); }

This gets minified down to the expected result.

samjudson avatar May 19 '18 15:05 samjudson

Thanks Sam, I changed it to function notation completely (function(f) { return .... }) and that minified just fine. Figured I would raise it as an issue anyway

mriedel90 avatar May 19 '18 18:05 mriedel90

Here I'm not able to minify a file with arrow-syntax lambdas at all. It just gives an error "Stack empty". If I change all lambdas to use the full function syntax, then everything works normally.

andre-ss6 avatar Feb 26 '19 16:02 andre-ss6

@andre-ss6 this is most probably your issue https://github.com/xoofx/NUglify/pull/97 Just had the same thing, think ive fixed it

trullock avatar Jun 05 '20 16:06 trullock