Skip to content

Commit

Permalink
fix(shaker): handle object patterns in exports (#1179)
Browse files Browse the repository at this point in the history
* fix: handle object patterns in exports

* chore: changeset for #1179

Co-authored-by: Victor Lin <victor.lin@airbnb.com>
Co-authored-by: Anton Evzhakov <anton@evz.name>
  • Loading branch information
3 people committed Jan 15, 2023
1 parent 28f3f93 commit b27f328
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/bright-coats-vanish.md
@@ -0,0 +1,5 @@
---
'@linaria/shaker': patch
---

Handle object patterns in exports.
15 changes: 15 additions & 0 deletions packages/shaker/src/plugins/__tests__/shaker-plugin.test.ts
Expand Up @@ -196,4 +196,19 @@ describe('shaker', () => {
expect(code).toMatchSnapshot();
expect(metadata.imports.size).toBe(0);
});
it('should handle object patterns in exports', () => {
const { code } = keep(['Alive'])`
import foo from "foo";
export const { Alive, Dead } = foo();
`;

expect(code).toMatchInlineSnapshot(`
"import foo from \\"foo\\";
export const {
Alive,
Dead
} = foo();"
`);
});
});
8 changes: 8 additions & 0 deletions packages/shaker/src/plugins/shaker-plugin.ts
Expand Up @@ -209,6 +209,14 @@ export default function shakerPlugin(
importNames.includes((exp.local.node as NodeWithName).name || '')
) {
aliveExports.add(exp);
} else if (
[...aliveExports].some((liveExp) => liveExp.local === exp.local)
) {
// It's possible to export multiple values from a single variable initializer, e.g
// export const { foo, bar } = baz();
// We need to treat all of them as used if any of them are used, since otherwise
// we'll attempt to delete the baz() call
aliveExports.add(exp);
}
});

Expand Down

0 comments on commit b27f328

Please sign in to comment.