From 769653f6511cb5a94dbe5b4f7d73b917c5ae0648 Mon Sep 17 00:00:00 2001 From: Anton Evzhakov Date: Mon, 8 Jan 2024 17:48:15 +0200 Subject: [PATCH] fix(transform): remove constant violations with their bindings (#34) --- .changeset/two-zoos-smoke.md | 5 +++++ .../__snapshots__/shaker.test.ts.snap | 13 +++++++++++++ .../transform/src/__tests__/shaker.test.ts | 18 ++++++++++++++++++ packages/transform/src/plugins/shaker.ts | 1 + 4 files changed, 37 insertions(+) create mode 100644 .changeset/two-zoos-smoke.md diff --git a/.changeset/two-zoos-smoke.md b/.changeset/two-zoos-smoke.md new file mode 100644 index 00000000..15937f55 --- /dev/null +++ b/.changeset/two-zoos-smoke.md @@ -0,0 +1,5 @@ +--- +'@wyw-in-js/transform': patch +--- + +Sometimes, usages of variables survive the shaker even when their bindings are removed. Fixed. diff --git a/packages/transform/src/__tests__/__snapshots__/shaker.test.ts.snap b/packages/transform/src/__tests__/__snapshots__/shaker.test.ts.snap index 130a6704..d78e62d3 100644 --- a/packages/transform/src/__tests__/__snapshots__/shaker.test.ts.snap +++ b/packages/transform/src/__tests__/__snapshots__/shaker.test.ts.snap @@ -15,3 +15,16 @@ exports.__wywPreval = { _exp: _exp };" `; + +exports[`shaker should remove enum 1`] = ` +""use strict"; + +Object.defineProperty(exports, "__esModule", { + value: true +}); +exports.__wywPreval = void 0; +const _exp2 = /*#__PURE__*/() => "t2nn9pk"; +const __wywPreval = exports.__wywPreval = { + _exp2: _exp2 +};" +`; diff --git a/packages/transform/src/__tests__/shaker.test.ts b/packages/transform/src/__tests__/shaker.test.ts index 5a517451..ff6fa2cb 100644 --- a/packages/transform/src/__tests__/shaker.test.ts +++ b/packages/transform/src/__tests__/shaker.test.ts @@ -52,4 +52,22 @@ describe('shaker', () => { expect(code).toMatchSnapshot(); }); + + it('should remove enum', () => { + const code = run(['__wywPreval'])` + "use strict"; + + var PanelKinds; + (function (PanelKinds) { + PanelKinds["DEFAULT"] = "default"; + PanelKinds["TRANSPARENT"] = "transparent"; + })(PanelKinds = exports.PanelKinds || (exports.PanelKinds = {})); + const _exp2 = /*#__PURE__*/() => "t2nn9pk"; + export const __wywPreval = { + _exp2: _exp2, + }; + `; + + expect(code).toMatchSnapshot(); + }); }); diff --git a/packages/transform/src/plugins/shaker.ts b/packages/transform/src/plugins/shaker.ts index b703a794..4ec7b320 100644 --- a/packages/transform/src/plugins/shaker.ts +++ b/packages/transform/src/plugins/shaker.ts @@ -395,6 +395,7 @@ export default function shakerPlugin( binding.path.isVariableDeclarator() && !forDeleting.includes(binding.path.get('id')) ) { + forDeleting.push(...binding.constantViolations); forDeleting.push(binding.path.get('id')); changed = true; }