Skip to content

Commit 040e5ee

Browse files
author
Daniel Del Core
committed
fixes imports
1 parent 677ae76 commit 040e5ee

File tree

4 files changed

+128
-5
lines changed

4 files changed

+128
-5
lines changed

community/@atlaskit__button/15.0.0/transform.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,11 @@ function changeImportFor(
9191
.find(j.ImportDeclaration)
9292
.filter(path => path.node.source.value === oldPackagePath)
9393
.find(j.ImportSpecifier)
94-
.find(j.Identifier)
95-
.filter(identifier => {
96-
if (identifier.value.name === currentName) {
94+
.filter(specifier => {
95+
if (specifier.value.imported!.name === currentName) {
9796
return true;
9897
}
99-
if (identifier.value.name === existingAlias) {
98+
if (specifier.value.imported!.name === existingAlias) {
10099
return true;
101100
}
102101
return false;

packages/utils/src/imports.spec.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { API, FileInfo } from 'jscodeshift';
2+
import { applyTransform } from '@codeshift/test-utils';
3+
import * as importUtils from './imports';
4+
5+
describe('imports', () => {
6+
describe('insertImportSpecifier', () => {
7+
it('inserts import specifier to existing import', async () => {
8+
const transform = (file: FileInfo, api: API) => {
9+
const j = api.jscodeshift;
10+
const source = j(file.source);
11+
importUtils.insertImportSpecifier(
12+
j,
13+
source,
14+
j.importSpecifier(j.identifier('bar')),
15+
'bar',
16+
);
17+
18+
return source.toSource();
19+
};
20+
const result = await applyTransform(
21+
transform,
22+
`import { foo } from 'bar';`,
23+
);
24+
expect(result).toMatchInlineSnapshot(`"import { foo, bar } from 'bar';"`);
25+
});
26+
27+
it('inserts import specifier to empty import', async () => {
28+
const transform = (file: FileInfo, api: API) => {
29+
const j = api.jscodeshift;
30+
const source = j(file.source);
31+
importUtils.insertImportSpecifier(
32+
j,
33+
source,
34+
j.importSpecifier(j.identifier('bar')),
35+
'bar',
36+
);
37+
38+
return source.toSource();
39+
};
40+
const result = await applyTransform(transform, `import { } from 'bar';`);
41+
expect(result).toMatchInlineSnapshot(`"import { bar } from 'bar';"`);
42+
});
43+
44+
it('maintains a reference to importDec and is able to inserts import specifier', async () => {
45+
const transform = (file: FileInfo, api: API) => {
46+
const j = api.jscodeshift;
47+
const source = j(file.source);
48+
49+
importUtils
50+
.getImportDeclaration(j, source, '@foo/myModule')
51+
.find(j.ImportSpecifier)
52+
.filter(specifier => specifier.value.imported.name === 'bar')
53+
.forEach(() => {
54+
importUtils.insertImportSpecifier(
55+
j,
56+
source,
57+
j.importSpecifier(j.identifier('foo')),
58+
'@foo/myModule',
59+
);
60+
})
61+
.remove();
62+
return source.toSource();
63+
};
64+
const result = await applyTransform(
65+
transform,
66+
`import { bar } from '@foo/myModule';`,
67+
);
68+
expect(result).toMatchInlineSnapshot(
69+
`"import { foo } from '@foo/myModule';"`,
70+
);
71+
});
72+
});
73+
});
74+
75+
// const result = applyTransform(
76+
// codemod,
77+
// `import { elevation as AkElevations, colors } from '@atlaskit/theme';
78+
79+
// export const Unlinked = styled.span\`color: \${colors.N800};\`;
80+
81+
// export const Card = styled.div\`
82+
// position: absolute;
83+
// \${props => (props.isElevated ? AkElevations.e300 : AkElevations.e100)};
84+
// \`;
85+
// `,
86+
// );

packages/utils/src/imports.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,12 @@ export function insertImportSpecifier(
128128

129129
importDeclaration.get().value.specifiers.push(importSpecifier);
130130
}
131+
132+
export function removeImportSpecifier(
133+
j: core.JSCodeshift,
134+
source: Collection<any>,
135+
specifier: string,
136+
sourcePath: string,
137+
) {
138+
getImportSpecifier(j, source, specifier, sourcePath).remove();
139+
}

website/docs/api/codeshift-utils.mdx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ import React, { useEffect } from 'react';
323323
```
324324

325325
```js
326-
import { getImportSpecifier } from '@codeshift/utils';
326+
import { insertImportSpecifier } from '@codeshift/utils';
327327

328328
insertImportSpecifier(j, source, 'useMemo', 'react'); // Collection containing 'useEffect'
329329
```
@@ -334,6 +334,35 @@ insertImportSpecifier(j, source, 'useMemo', 'react'); // Collection containing '
334334
+import React, { useEffect, useMemo } from 'react';
335335
```
336336

337+
### `removeImportSpecifier`
338+
339+
`removeImportSpecifier(j, source, specifier)`
340+
341+
Removes an import specifier
342+
343+
**Returns**
344+
345+
`void`
346+
347+
**Example**
348+
349+
```jsx
350+
// src/App.js
351+
import React, { useEffect } from 'react';
352+
```
353+
354+
```js
355+
import { removeImportSpecifier } from '@codeshift/utils';
356+
357+
removeImportSpecifier(j, source, 'useMemo', 'react'); // Collection containing 'useEffect'
358+
```
359+
360+
```diff
361+
// src/App.js
362+
-import React, { useEffect } from 'react';
363+
+import React, { useEffect, useMemo } from 'react';
364+
```
365+
337366
## JSX
338367

339368
### `getJSXAttributes`

0 commit comments

Comments
 (0)