diff --git a/packages/babel-plugin-minify-builtins/__tests__/fixtures/props-polyfilled/actual.js b/packages/babel-plugin-minify-builtins/__tests__/fixtures/props-polyfilled/actual.js new file mode 100644 index 000000000..45a3e42ec --- /dev/null +++ b/packages/babel-plugin-minify-builtins/__tests__/fixtures/props-polyfilled/actual.js @@ -0,0 +1,2 @@ +Math["a"] = "blah"; +Math.a(); diff --git a/packages/babel-plugin-minify-builtins/__tests__/fixtures/props-polyfilled/expected.js b/packages/babel-plugin-minify-builtins/__tests__/fixtures/props-polyfilled/expected.js new file mode 100644 index 000000000..53649d45b --- /dev/null +++ b/packages/babel-plugin-minify-builtins/__tests__/fixtures/props-polyfilled/expected.js @@ -0,0 +1,2 @@ +Math["a"] = "blah"; +Math.a(); \ No newline at end of file diff --git a/packages/babel-plugin-minify-builtins/src/index.js b/packages/babel-plugin-minify-builtins/src/index.js index 53b46bf64..bb55ec519 100644 --- a/packages/babel-plugin-minify-builtins/src/index.js +++ b/packages/babel-plugin-minify-builtins/src/index.js @@ -24,6 +24,19 @@ module.exports = function({ types: t }) { const context = this; const collectVisitor = { + AssignmentExpression(path) { + const left = path.get("left"); + + // Should bail and not run the plugin + // when builtin is polyfilled + if (t.isMemberExpression(left) && isBuiltInComputed(left)) { + let parent = path; + do { + parent.stop(); + } while ((parent = parent.parentPath)); + } + }, + MemberExpression(path) { if (path.parentPath.isCallExpression()) { return; @@ -46,7 +59,7 @@ module.exports = function({ types: t }) { return; } - // computed property should be not optimized + // computed property should not be optimized // Math[max]() -> Math.max() if (!isComputed(callee) && isBuiltin(callee)) { const result = evaluate(path, { tdz: context.tdz }); @@ -115,6 +128,17 @@ module.exports = function({ types: t }) { return result; } + function isBuiltInComputed(memberExpr) { + const { node } = memberExpr; + const { object, computed } = node; + + return ( + computed && + t.isIdentifier(object) && + VALID_CALLEES.indexOf(object.name) >= 0 + ); + } + function isBuiltin(memberExpr) { const { object, property } = memberExpr.node;