Skip to content

Commit e832223

Browse files
committed
✨ Adds codemods for emotion11
1 parent 3d0d831 commit e832223

File tree

4 files changed

+79
-0
lines changed

4 files changed

+79
-0
lines changed

community/@emotion/11.0.0/motions/.gitkeep

Whitespace-only changes.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { applyTransform } from '@codeshift/test-utils';
2+
import * as transformer from './transform';
3+
4+
describe('@emotion@11.0.0 transform', () => {
5+
it('should transform imports correctly', () => {
6+
const result = applyTransform(
7+
transformer,
8+
`
9+
import * as core from '@emotion/core';
10+
import * as emotion from 'emotion';
11+
import * as emotionTheming from 'emotion-theming';
12+
import * as emotionServer from 'emotion-server';
13+
import * as createEmotion from 'create-emotion';
14+
import * as createEmotionServer from 'create-emotion-server';
15+
import * as babelPlugin from 'babel-plugin-emotion';
16+
import * as eslintPlugin from 'eslint-plugin-emotion';
17+
import * as jestEmotion from 'jest-emotion';
18+
`,
19+
{ parser: 'tsx' },
20+
);
21+
22+
expect(result).toMatchInlineSnapshot(`
23+
"import * as core from \\"@emotion/react\\";
24+
import * as emotion from \\"@emotion/css\\";
25+
import * as emotionTheming from \\"@emotion/react\\";
26+
import * as emotionServer from \\"@emotion/server\\";
27+
import * as createEmotion from \\"@emotion/css/create-instance\\";
28+
import * as createEmotionServer from \\"@emotion/server/create-instance\\";
29+
import * as babelPlugin from \\"@emotion/babel-plugin\\";
30+
import * as eslintPlugin from \\"@emotion/eslint-plugin\\";
31+
import * as jestEmotion from \\"@emotion/jest\\";"
32+
`);
33+
});
34+
});
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { API, FileInfo, Options } from 'jscodeshift';
2+
import {
3+
hasImportDeclaration,
4+
renameImportDeclaration,
5+
} from '@codeshift/utils';
6+
7+
const importMap = {
8+
'@emotion/core': '@emotion/react',
9+
emotion: '@emotion/css',
10+
'emotion-theming': '@emotion/react',
11+
'emotion-server': '@emotion/server',
12+
'create-emotion': '@emotion/css/create-instance',
13+
'create-emotion-server': '@emotion/server/create-instance',
14+
'babel-plugin-emotion': '@emotion/babel-plugin',
15+
'eslint-plugin-emotion': '@emotion/eslint-plugin',
16+
'jest-emotion': '@emotion/jest',
17+
};
18+
19+
export default function transformer(
20+
file: FileInfo,
21+
{ jscodeshift: j }: API,
22+
options: Options,
23+
) {
24+
const source = j(file.source);
25+
26+
if (
27+
!Object.keys(importMap).some(importSource =>
28+
hasImportDeclaration(j, source, importSource),
29+
)
30+
) {
31+
return file.source;
32+
}
33+
34+
Object.entries(importMap).forEach(([key, value]) =>
35+
renameImportDeclaration(j, source, key, value),
36+
);
37+
38+
return source.toSource(options.printOptions);
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default {
2+
maintainers: [],
3+
transforms: {
4+
'11.0.0': require('./11.0.0/transform'),
5+
}
6+
};

0 commit comments

Comments
 (0)