Skip to content

Commit

Permalink
fix(utils): code remover should not delete classes if the last member…
Browse files Browse the repository at this point in the history
… was deleted (#1354)
  • Loading branch information
Anber committed Sep 26, 2023
1 parent 5a32f4f commit 70000ec
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/gorgeous-jobs-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@linaria/utils': patch
---

The code remover should not delete classes if the last member was deleted.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`removeDangerousCode should not remove class 1`] = `"class Test {}"`;

exports[`removeDangerousCode should remove \`window\` but keep \`setVersion\` function untouched 1`] = `
"let _win = undefined;
export function setVersion(packageName, packageVersion) {
Expand Down
12 changes: 12 additions & 0 deletions packages/utils/src/__tests__/removeDangerousCode.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,4 +148,16 @@ describe('removeDangerousCode', () => {

expect(result).toMatchSnapshot();
});

it('should not remove class', () => {
const result = run`
class Test {
constructor() {
window.fetch();
}
}
`;

expect(result).toMatchSnapshot();
});
});
6 changes: 6 additions & 0 deletions packages/utils/src/scopeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ export function findActionForNode(
return ['remove', path];
}

if (parent.isClassDeclaration() || parent.isClassExpression()) {
if (path.key === 'body') {
return ['replace', path, { type: 'ClassBody', body: [] }];
}
}

if (parent.isFunction()) {
if (path.listKey === 'params') {
// Do not remove params of functions
Expand Down

0 comments on commit 70000ec

Please sign in to comment.