UglifyJS icon indicating copy to clipboard operation
UglifyJS copied to clipboard

[Moment.js, Angular, es2015] Strange daysInMonth function transformation

Open mantyz opened this issue 8 years ago • 4 comments

Bug report or feature request? Bug

The initial problem

ES5 or ES6+ input? ES6

Uglify version (uglifyjs -V)

npm list uglify-es

└─┬ @angular/[email protected]
  └─┬ [email protected]
    └── [email protected] 

JavaScript input The Moment.js has the function daysInMonth.

export function daysInMonth(year, month) {
    if (isNaN(year) || isNaN(month)) {
        return NaN;
    }
    var modMonth = mod(month, 12);
    year += (month - modMonth) / 12;
    return modMonth === 1 ? (isLeapYear(year) ? 29 : 28) : (31 - modMonth % 7 % 2);
}

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

JavaScript output or error produced.

When Angular builded project with tsconfig.json target = es2015, then the function is transformed to:

function je(e, t) {
    if (isNaN(e) || isNaN(t))
        return NaN;
    return e += (t - NaN) / 12,
    NaN
}

mantyz avatar Apr 05 '18 19:04 mantyz

@mantyz could you facilitate what options uglifyjs was called with? I can't seem to reproduce

fabiosantoscode avatar Apr 05 '18 21:04 fabiosantoscode

@fabiosantoscode, If I understand correctly, then these options are used: https://github.com/angular/angular-cli/blob/e48d54558f3640920a6adf6793bcf5eb1b9e4070/packages/%40angular/cli/models/webpack-configs/production.ts#L164

@filipesilva, Correct me, please, if I'm wrong. And maybe this problem does not Uglify?

mantyz avatar Apr 06 '18 13:04 mantyz

A code fragment out of context is not a bug reproduction. Please follow the issue template.

**JavaScript input**

    A complete parsable JS program exhibiting the issue with
    UglifyJS alone - without third party tools or libraries.
    Ideally the input should be as small as possible.
    Post a link to a gist if necessary.

    Issues without a reproducible test case will be closed.

kzc avatar Apr 06 '18 18:04 kzc

It seems to be related to the Angular Build Optimizer. Excuse for troubling.

mantyz avatar Apr 12 '18 13:04 mantyz