Skip to content

Commit

Permalink
fix(shaker): do not remove args of functions if they aren't used (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Oct 25, 2022
1 parent a488dae commit 963508a
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 86 deletions.
7 changes: 7 additions & 0 deletions .changeset/many-beers-accept.md
@@ -0,0 +1,7 @@
---
'@linaria/shaker': patch
'@linaria/testkit': patch
'@linaria/utils': patch
---

Shaker shouldn't remove parameters of functions if they aren't used.
@@ -1,5 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`shaker should delete import 1`] = `"export const Alive = \\"\\";"`;

exports[`shaker should keep assigment even if export is marked for removing 1`] = `
"let _a;
Expand Down
17 changes: 17 additions & 0 deletions packages/shaker/src/plugins/__tests__/shaker-plugin.test.ts
Expand Up @@ -179,4 +179,21 @@ describe('shaker', () => {
expect(code).toMatchSnapshot();
expect(metadata.imports.size).toBe(0);
});

it('should delete import', () => {
const { code, metadata } = keep(['Alive'])`
import { A, B } from "ABC";
const AB = {
A,
B,
};
export const Alive = "";
export default AB;
`;

expect(code).toMatchSnapshot();
expect(metadata.imports.size).toBe(0);
});
});
21 changes: 18 additions & 3 deletions packages/shaker/src/plugins/shaker-plugin.ts
Expand Up @@ -17,8 +17,9 @@ import {
removeWithRelated,
sideEffectImport,
reference,
findParentForDelete,
findActionForNode,
dereference,
mutate,
} from '@linaria/utils';

type Core = typeof core;
Expand Down Expand Up @@ -257,7 +258,8 @@ export default function shakerPlugin(
// eslint-disable-next-line no-restricted-syntax
for (const path of forDeleting) {
const binding = getBindingForExport(path);
const parent = findParentForDelete(path);
const action = findActionForNode(path);
const parent = action?.[1];
const outerReferences = (binding?.referencePaths || []).filter(
(ref) => ref !== parent && !parent?.isAncestor(ref)
);
Expand All @@ -271,7 +273,20 @@ export default function shakerPlugin(
!deleted.has(path) &&
(!binding || outerReferences.length === 0)
) {
removeWithRelated([parent ?? path]);
if (action) {
mutate(action[1], (p) => {
if (isRemoved(p)) return;

if (action[0] === 'remove') {
p.remove();
} else if (action[0] === 'replace') {
p.replaceWith(action[2]);
}
});
} else {
removeWithRelated([path]);
}

deleted.add(path);
changed = true;
}
Expand Down
Expand Up @@ -10,13 +10,16 @@ exports[`removeWithRelated should keep logical expression 1`] = `
const res = false && c;"
`;

exports[`removeWithRelated should remove export 1`] = `
"const b = 1;
export { b };"
exports[`removeWithRelated should not delete params of functions 1`] = `
"function test(arg) {
return null;
}"
`;

exports[`removeWithRelated should remove node if it becomes invalid after removing its children 1`] = `""`;

exports[`removeWithRelated should remove the whole import 1`] = `""`;

exports[`removeWithRelated should remove try/catch block 1`] = `"const a = 1;"`;

exports[`removeWithRelated should shake try/catch 1`] = `""`;
40 changes: 21 additions & 19 deletions packages/testkit/src/utils/removeWithRelated.test.ts
Expand Up @@ -57,34 +57,24 @@ describe('removeWithRelated', () => {
expect(code).toMatchSnapshot();
});

it('should remove try/catch block', () => {
it('should remove the whole import', () => {
const code = run`
const a = 1;
import { a } from './source';
try {
/* remove */42;
} catch (e) {
}
/* remove */a;
`;

expect(code).toMatchSnapshot();
});

it('should remove export', () => {
it('should remove try/catch block', () => {
const code = run`
function a() {
/* remove */42;
}
const a = 1;
function checkIsBrowser() {
return /* remove */42;
try {
/* remove */42;
} catch (e) {
}
const canUseDOM = checkIsBrowser();
const b = 1;
export { a, b, canUseDOM };
`;

expect(code).toMatchSnapshot();
Expand Down Expand Up @@ -123,7 +113,19 @@ describe('removeWithRelated', () => {
const code = run`
/* remove */const mode = "DEV";
export { mode };
if (mode !== "DEV") {
}
`;

expect(code).toMatchSnapshot();
});

it('should not delete params of functions', () => {
const code = run`
function test(arg) {
/* remove */console.log(arg);
return null;
}
`;

expect(code).toMatchSnapshot();
Expand Down

0 comments on commit 963508a

Please sign in to comment.