From 2ec0e2fc03e7499f592a9b4ff183079bea3234ef Mon Sep 17 00:00:00 2001 From: Boopathi Rajaa Date: Mon, 14 Nov 2016 19:02:56 +0100 Subject: [PATCH] Fix dce: don't remove fn param from setters (close #267) --- .../__tests__/dead-code-elimination-test.js | 16 ++++++++++++++++ .../src/index.js | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js b/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js index 4eb96d171..e4e3ce2c8 100644 --- a/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js +++ b/packages/babel-plugin-minify-dead-code-elimination/__tests__/dead-code-elimination-test.js @@ -2209,4 +2209,20 @@ describe("dce-plugin", () => { `); expect(transform(source)).toBe(expected); }); + it("should NOT remove fn params for setters", () => { + const source = unpad(` + function foo() { + var x = { + set a(b) {} + }; + class A { + set c(d) { + x.a = 5; + } + } + return new A(); + } + `); + expect(transform(source)).toBe(source); + }); }); diff --git a/packages/babel-plugin-minify-dead-code-elimination/src/index.js b/packages/babel-plugin-minify-dead-code-elimination/src/index.js index 161521cea..ff41b3fbb 100644 --- a/packages/babel-plugin-minify-dead-code-elimination/src/index.js +++ b/packages/babel-plugin-minify-dead-code-elimination/src/index.js @@ -108,7 +108,8 @@ module.exports = ({ types: t, traverse }) => { // if the scope is created by a function, we obtain its // parameter list - const paramsList = path.isFunction() ? path.get("params") : []; + const canRemoveParams = path.isFunction() && path.node.kind !== "set"; + const paramsList = canRemoveParams ? path.get("params") : []; for (let i = paramsList.length - 1; i >= 0; i--) { const param = paramsList[i];