Skip to content

Commit

Permalink
fix(shaker): ignore identifiers references in types (#1286)
Browse files Browse the repository at this point in the history
  • Loading branch information
Anber committed Jul 18, 2023
1 parent 59ca36e commit e59bf80
Show file tree
Hide file tree
Showing 27 changed files with 1,185 additions and 968 deletions.
18 changes: 18 additions & 0 deletions .changeset/spotty-glasses-live.md
@@ -0,0 +1,18 @@
---
'@linaria/atomic': patch
'@linaria/babel-preset': patch
'@linaria/cli': patch
'@linaria/core': patch
'@linaria/esbuild': patch
'@linaria/griffel': patch
'@linaria/babel-plugin-interop': patch
'@linaria/postcss-linaria': patch
'@linaria/react': patch
'@linaria/shaker': patch
'@linaria/tags': patch
'@linaria/testkit': patch
'@linaria/utils': patch
'linaria-website': patch
---

Shaker mistakenly counts references in types as valuable and keeps referenced variables alive.
2 changes: 1 addition & 1 deletion examples/astro-solid/package.json
Expand Up @@ -6,7 +6,7 @@
"type": "module",
"devDependencies": {
"@astrojs/solid-js": "^1.2.3",
"@babel/core": "^7.20.2",
"@babel/core": "^7.22.9",
"@linaria/core": "workspace:^",
"@linaria/shaker": "workspace:^",
"@linaria/vite": "workspace:^",
Expand Down
4 changes: 2 additions & 2 deletions examples/rollup/package.json
Expand Up @@ -7,8 +7,8 @@
"linaria-website": "workspace:^"
},
"devDependencies": {
"@babel/core": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@babel/core": "^7.22.9",
"@babel/preset-react": "^7.22.5",
"@linaria/rollup": "workspace:^",
"@rollup/plugin-babel": "^5.3.1",
"@rollup/plugin-commonjs": "^22.0.1",
Expand Down
2 changes: 1 addition & 1 deletion examples/webpack5/package.json
Expand Up @@ -8,7 +8,7 @@
"linaria-website": "workspace:^4.1.8"
},
"devDependencies": {
"@babel/core": "^7.20.2",
"@babel/core": "^7.22.9",
"@linaria/webpack5-loader": "workspace:^4.1.8",
"babel-loader": "^9.1.0",
"cross-env": "^7.0.3",
Expand Down
14 changes: 7 additions & 7 deletions package.json
Expand Up @@ -42,14 +42,14 @@
"git-raw-commits": "^2.0.3"
},
"devDependencies": {
"@babel/cli": "^7.19.3",
"@babel/core": "^7.20.2",
"@babel/eslint-parser": "^7.19.1",
"@babel/cli": "^7.22.9",
"@babel/core": "^7.22.9",
"@babel/eslint-parser": "^7.22.9",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-syntax-jsx": "^7.18.6",
"@babel/preset-env": "^7.20.2",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.18.6",
"@babel/plugin-syntax-jsx": "^7.22.5",
"@babel/preset-env": "^7.22.9",
"@babel/preset-react": "^7.22.5",
"@babel/preset-typescript": "^7.22.5",
"@changesets/cli": "^2.22.0",
"@commitlint/config-conventional": "^8.3.4",
"@types/jest": "^28.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/atomic/package.json
Expand Up @@ -60,7 +60,7 @@
"ts-invariant": "^0.10.3"
},
"devDependencies": {
"@babel/types": "^7.20.2",
"@babel/types": "^7.22.5",
"@types/node": "^17.0.39"
},
"peerDependencies": {
Expand Down
12 changes: 6 additions & 6 deletions packages/babel/package.json
Expand Up @@ -33,12 +33,12 @@
"watch": "pnpm build:lib --watch & pnpm build:declarations --watch"
},
"dependencies": {
"@babel/core": "^7.20.2",
"@babel/generator": "^7.20.4",
"@babel/helper-module-imports": "^7.18.6",
"@babel/template": "^7.18.10",
"@babel/traverse": "^7.20.1",
"@babel/types": "^7.20.2",
"@babel/core": "^7.22.9",
"@babel/generator": "^7.22.9",
"@babel/helper-module-imports": "^7.22.5",
"@babel/template": "^7.22.5",
"@babel/traverse": "^7.22.8",
"@babel/types": "^7.22.5",
"@linaria/core": "workspace:^",
"@linaria/logger": "workspace:^",
"@linaria/shaker": "workspace:^",
Expand Down
10 changes: 6 additions & 4 deletions packages/babel/src/module.ts
Expand Up @@ -187,11 +187,11 @@ class Module {
return Array.from(this.#lazyValues.keys());
},
set: (target, key, value) => {
if (value !== undefined) {
if (key !== '__esModule') {
this.debug('evaluated', 'set %s: %o', key, value);
}
if (key !== '__esModule') {
this.debug('evaluated', 'set %s: %o', key, value);
}

if (value !== undefined) {
this.#lazyValues.set(key, () => value);
}

Expand Down Expand Up @@ -459,6 +459,8 @@ class Module {
return;
} catch (e) {
if (e instanceof EvalError) {
this.debug('evaluate:error', '%O', e);

throw e;
}

Expand Down
36 changes: 0 additions & 36 deletions packages/babel/src/utils/getLinariaComment.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/cli/package.json
Expand Up @@ -35,7 +35,7 @@
"watch": "pnpm build --watch"
},
"dependencies": {
"@babel/core": "^7.20.2",
"@babel/core": "^7.22.9",
"@linaria/babel-preset": "workspace:^",
"@linaria/utils": "workspace:^",
"glob": "^7.1.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Expand Up @@ -63,8 +63,8 @@
"@linaria/utils": "workspace:^"
},
"devDependencies": {
"@babel/traverse": "^7.20.1",
"@babel/types": "^7.20.2",
"@babel/traverse": "^7.22.8",
"@babel/types": "^7.22.5",
"@types/babel__core": "^7.1.19",
"@types/babel__traverse": "^7.17.1",
"@types/node": "^17.0.39"
Expand Down
2 changes: 1 addition & 1 deletion packages/esbuild/package.json
Expand Up @@ -36,7 +36,7 @@
"watch": "pnpm build:dist --watch & pnpm build:declarations --watch"
},
"dependencies": {
"@babel/core": "^7.20.2",
"@babel/core": "^7.22.9",
"@linaria/babel-preset": "workspace:^",
"@linaria/utils": "workspace:^"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/griffel/package.json
Expand Up @@ -44,7 +44,7 @@
"ts-invariant": "^0.10.3"
},
"devDependencies": {
"@babel/types": "^7.20.2"
"@babel/types": "^7.22.5"
},
"engines": {
"node": "^12.16.0 || >=13.7.0"
Expand Down
6 changes: 3 additions & 3 deletions packages/interop/package.json
Expand Up @@ -26,9 +26,9 @@
"watch": "pnpm build:lib --watch & pnpm build:esm --watch & pnpm build:declarations --watch"
},
"devDependencies": {
"@babel/core": "^7.20.2",
"@babel/traverse": "^7.20.1",
"@babel/types": "^7.20.2",
"@babel/core": "^7.22.9",
"@babel/traverse": "^7.22.8",
"@babel/types": "^7.22.5",
"@types/babel__core": "^7.1.19",
"@types/babel__traverse": "^7.17.1",
"dedent": "^0.7.0"
Expand Down
8 changes: 4 additions & 4 deletions packages/postcss-linaria/package.json
Expand Up @@ -38,13 +38,13 @@
"watch": "pnpm build:lib --watch & pnpm build:esm --watch & pnpm build:declarations --watch"
},
"dependencies": {
"@babel/generator": "^7.20.4",
"@babel/parser": "^7.20.3",
"@babel/traverse": "^7.20.1",
"@babel/generator": "^7.22.9",
"@babel/parser": "^7.22.7",
"@babel/traverse": "^7.22.8",
"stylelint": "^14.11.0"
},
"devDependencies": {
"@babel/types": "^7.20.2",
"@babel/types": "^7.22.5",
"@types/babel__generator": "^7.6.4",
"@types/babel__traverse": "^7.17.1",
"postcss": "^8.3.11"
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Expand Up @@ -67,7 +67,7 @@
"ts-invariant": "^0.10.3"
},
"devDependencies": {
"@babel/types": "^7.20.2",
"@babel/types": "^7.22.5",
"@types/babel__core": "^7.1.19",
"@types/node": "^17.0.39",
"@types/react": ">=16",
Expand Down
14 changes: 7 additions & 7 deletions packages/shaker/package.json
Expand Up @@ -33,19 +33,19 @@
"watch": "pnpm build:lib --watch & pnpm build:esm --watch & pnpm build:declarations --watch"
},
"dependencies": {
"@babel/core": "^7.20.2",
"@babel/generator": "^7.20.4",
"@babel/plugin-transform-modules-commonjs": "^7.19.6",
"@babel/plugin-transform-runtime": "^7.19.6",
"@babel/plugin-transform-template-literals": "^7.18.9",
"@babel/preset-env": "^7.20.2",
"@babel/core": "^7.22.9",
"@babel/generator": "^7.22.9",
"@babel/plugin-transform-modules-commonjs": "^7.22.5",
"@babel/plugin-transform-runtime": "^7.22.9",
"@babel/plugin-transform-template-literals": "^7.22.5",
"@babel/preset-env": "^7.22.9",
"@linaria/logger": "workspace:^",
"@linaria/utils": "workspace:^",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
"ts-invariant": "^0.10.3"
},
"devDependencies": {
"@babel/types": "^7.20.2",
"@babel/types": "^7.22.5",
"@types/babel__core": "^7.1.19",
"@types/babel__generator": "^7.6.4",
"@types/babel__traverse": "^7.17.1",
Expand Down
6 changes: 3 additions & 3 deletions packages/tags/package.json
Expand Up @@ -31,13 +31,13 @@
"watch": "pnpm build:lib --watch & pnpm build:esm --watch & pnpm build:declarations --watch"
},
"dependencies": {
"@babel/generator": "^7.20.4",
"@babel/generator": "^7.22.9",
"@linaria/logger": "workspace:^",
"@linaria/utils": "workspace:^"
},
"devDependencies": {
"@babel/traverse": "^7.20.1",
"@babel/types": "^7.20.2",
"@babel/traverse": "^7.22.8",
"@babel/types": "^7.22.5",
"@types/babel__core": "^7.1.19",
"@types/babel__generator": "^7.6.4",
"@types/babel__traverse": "^7.17.1",
Expand Down
16 changes: 8 additions & 8 deletions packages/testkit/package.json
Expand Up @@ -21,9 +21,9 @@
"typecheck": "tsc --noEmit --composite false"
},
"dependencies": {
"@babel/core": "^7.20.2",
"@babel/generator": "^7.20.4",
"@babel/traverse": "^7.20.1",
"@babel/core": "^7.22.9",
"@babel/generator": "^7.22.9",
"@babel/traverse": "^7.22.8",
"@linaria/babel-preset": "workspace:^",
"@linaria/extractor": "workspace:^",
"@linaria/react": "workspace:^",
Expand All @@ -36,11 +36,11 @@
"typescript": "^4.7.4"
},
"devDependencies": {
"@babel/plugin-syntax-jsx": "^7.18.6",
"@babel/plugin-syntax-typescript": "^7.20.0",
"@babel/plugin-transform-modules-commonjs": "^7.19.6",
"@babel/runtime": "^7.20.1",
"@babel/types": "^7.20.2",
"@babel/plugin-syntax-jsx": "^7.22.5",
"@babel/plugin-syntax-typescript": "^7.22.5",
"@babel/plugin-transform-modules-commonjs": "^7.22.5",
"@babel/runtime": "^7.22.6",
"@babel/types": "^7.22.5",
"@linaria/atomic": "workspace:^",
"@linaria/core": "workspace:^",
"@linaria/griffel": "workspace:^",
Expand Down
28 changes: 28 additions & 0 deletions packages/testkit/src/__snapshots__/babel.test.ts.snap
Expand Up @@ -2265,6 +2265,34 @@ CSS:
Dependencies: NA
`;
exports[`strategy shaker should shake out identifiers that are referenced only in types 1`] = `
"import { styled } from \\"@linaria/react\\";
import * as yup from \\"yup\\";
import { Form } from \\"./__fixtures__/linaria-ui-library/components/index\\";
const validationSchema = yup.object();
type IModel = yup.InferType<typeof validationSchema>;
const Editor = () => {
const initial: IModel = {};
return <Form schema={validationSchema} data={initial} />;
};
const _exp = /*#__PURE__*/() => Editor;
export const StyledEditor = /*#__PURE__*/styled(_exp())({
name: \\"StyledEditor\\",
class: \\"StyledEditor_s18alru3\\",
propsAsIs: true
});"
`;
exports[`strategy shaker should shake out identifiers that are referenced only in types 2`] = `
CSS:
.StyledEditor_s18alru3 {}
Dependencies: NA
`;
Expand Down
24 changes: 24 additions & 0 deletions packages/testkit/src/babel.test.ts
Expand Up @@ -2825,6 +2825,30 @@ describe('strategy shaker', () => {
expect(metadata).toMatchSnapshot();
});

it('should shake out identifiers that are referenced only in types', async () => {
const { code, metadata } = await transform(
dedent`
import { styled } from "@linaria/react";
import * as yup from "yup";
import { Form } from "./__fixtures__/linaria-ui-library/components/index";
const validationSchema = yup.object();
type IModel = yup.InferType<typeof validationSchema>;
const Editor = () => {
const initial: IModel = {};
return <Form schema={validationSchema} data={initial} />;
};
export const StyledEditor = styled(Editor)\`\`;
`,
[evaluator, {}, 'tsx']
);

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

xit('should shake out side effect because its definition uses DOM API', async () => {
const { code, metadata } = await transform(
dedent`
Expand Down
@@ -1,10 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`removeWithRelated should keep alive used import specifier 1`] = `
"import { b } from './source';
/* remove */"
`;
exports[`removeWithRelated should keep alive used import specifier 1`] = `"import { b } from './source';"`;

exports[`removeWithRelated should keep logical expression 1`] = `
"const c = 3;
Expand Down

0 comments on commit e59bf80

Please sign in to comment.