Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(shaker): shaker doesn't treat exports as variable references (fixes #1008) #1009

Merged
merged 1 commit into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/empty-papayas-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@linaria/shaker': patch
'@linaria/utils': patch
---

In some cases, the shaker mistakenly removed assignment expressions. Fixes #1008.
1 change: 1 addition & 0 deletions packages/shaker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"build:declarations": "tsc --emitDeclarationOnly --outDir types",
"build:esm": "babel src --out-dir esm --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
"build:lib": "cross-env NODE_ENV=legacy babel src --out-dir lib --extensions '.js,.jsx,.ts,.tsx' --source-maps --delete-dir-on-start",
"test": "jest --config ./jest.config.js --rootDir src",
"typecheck": "tsc --noEmit --composite false",
"watch": "npm run build --watch"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`shaker should keep assigment even if export is marked for removing 1`] = `
"let _a;

exports.a = _a = {};
exports.b = _a;"
`;

exports[`shaker should keep referenced exports 1`] = `
"var _foo;

Expand Down Expand Up @@ -46,4 +53,11 @@ export { a };"

exports[`shaker should remove unused export 1`] = `"export const a = 1;"`;

exports[`shaker should respect implicit references 1`] = `
"let _a;

exports.a = _a = {};
exports.b = _a;"
`;

exports[`shaker should throw out unused referenced exports 1`] = `"exports.defaultValue = 20;"`;
22 changes: 22 additions & 0 deletions packages/shaker/src/plugins/__tests__/shaker-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,26 @@ describe('shaker', () => {
expect(code).toMatchSnapshot();
expect(metadata.imports.size).toBe(0);
});

it('should respect implicit references', () => {
const { code, metadata } = keep(['a'])`
let _a;
exports.a = _a = {};
exports.b = _a;
`;

expect(code).toMatchSnapshot();
expect(metadata.imports.size).toBe(0);
});

it('should keep assigment even if export is marked for removing', () => {
const { code, metadata } = keep(['b'])`
let _a;
exports.a = _a = {};
exports.b = _a;
`;

expect(code).toMatchSnapshot();
expect(metadata.imports.size).toBe(0);
});
});
18 changes: 18 additions & 0 deletions packages/shaker/src/plugins/shaker-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ function getBindingForExport(exportPath: NodePath): Binding | undefined {
}
}

if (exportPath.isAssignmentExpression()) {
const left = exportPath.get('left');
if (left.isIdentifier()) {
return exportPath.scope.getBinding(left.node.name);
}
}

return undefined;
}

Expand Down Expand Up @@ -157,6 +164,17 @@ export default function shakerPlugin(
collected.exports
);

collected.exports.forEach(({ local }) => {
if (local.isAssignmentExpression()) {
const left = local.get('left');
if (left.isIdentifier()) {
// For some reason babel does not mark id in AssignmentExpression as a reference
// So we need to do it manually
reference(left, left, true);
}
}
});

if (
onlyExports.length === 1 &&
onlyExports[0] === '__linariaPreval' &&
Expand Down
9 changes: 4 additions & 5 deletions packages/utils/src/scopeHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ function getBinding(path: NodePath<Identifier | JSXIdentifier>) {

export function reference(
path: NodePath<Identifier | JSXIdentifier>,
referencePath?: NodePath
referencePath: NodePath = path,
force = false
): void {
if (!path.isReferencedIdentifier()) return;
if (!force && !path.isReferencedIdentifier()) return;

const binding = getBinding(path);
if (!binding) return;

if (binding.referencePaths.includes(referencePath ?? path)) {
if (binding.referencePaths.includes(referencePath)) {
return;
}

Expand Down Expand Up @@ -55,8 +56,6 @@ function isReferenced(binding: Binding) {
export function dereference(
path: NodePath<Identifier | JSXIdentifier>
): Binding | null {
if (!path.isReferencedIdentifier()) return null;

const binding = getBinding(path);
if (!binding) return null;

Expand Down