Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metro minify changes code behavior #562

Closed
yulanggong opened this issue May 21, 2020 · 2 comments
Closed

Metro minify changes code behavior #562

yulanggong opened this issue May 21, 2020 · 2 comments

Comments

@yulanggong
Copy link

yulanggong commented May 21, 2020

Do you want to request a feature or report a bug?

report a bug

What is the current behavior?

entry.js

function bnpInvDigit(b, DV) {
    if ((b & 1) == 0) {
        return 0;
    }
    var a = b & 3,
        a = (a * (2 - (b & 15) * a)) & 15,
        a = (a * (2 - (b & 255) * a)) & 255,
        a = (a * (2 - (((b & 65535) * a) & 65535))) & 65535,
        a = (a * (2 - ((b * a) % DV))) % DV;
    return a > 0 ? DV - a : -a;
}

console.log(bnpInvDigit(3, 3)); // 2
console.log(bnpInvDigit(3, 6)); // 5
metro build entry.js -O out.js

The codes passed to minifier in node_modules/metro/src/JSTransformer/worker.js:

__d(function (g, r, i, a, m, e, d) {
  "use strict";

  function bnpInvDigit(b, DV) {
    if ((b & 1) == 0) {
      return 0;
    }

    var _a3 = b & 3,
        // from here caused the bug
        a = _a3 * (2 - (b & 15) * _a3) & 15,
        a = _a3 * (2 - (b & 255) * _a3) & 255,
        a = _a3 * (2 - ((b & 65535) * _a3 & 65535)) & 65535,
        // end of the bug
        _a3 = _a3 * (2 - b * _a3 % DV) % DV;

    return _a3 > 0 ? DV - _a3 : -_a3;
  }

  console.log(bnpInvDigit(3, 3));
  console.log(bnpInvDigit(3, 6));
});

The primary part of the result:

function n(n, o) {
    if (0 == (1 & n)) return 0;
    var t;
    t = 3 & n;
    return (t = (t * (2 - ((n * t) % o))) % o) > 0 ? o - t : -t;
}
console.log(n(3, 3)), // -0
console.log(n(3, 6)); // 3

If the current behavior is a bug, please provide the steps to reproduce and a minimal repository on GitHub that we can yarn install and yarn test.

as above

What is the expected behavior?

do it right

Please provide your exact Metro configuration and mention your Metro, node, yarn/npm version and operating system.

  • Metro 0.59.0
@yulanggong
Copy link
Author

yulanggong commented May 21, 2020

path.scope.rename(shortName, path.scope.generateUid(shortName));

This line caused the bug, maybe a babel bug babel/babel#11591.

Here is a workaround to avoid this bug:

module.exports = {
    transformer: {
        optimizationSizeLimit: 0
    }
};

@yulanggong
Copy link
Author

bug fixed in babel v7.10.0
babel/babel#11595

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant