Skip to content

Commit 8b9d270

Browse files
committed
release: 0.0.1-2
1 parent c82f5a8 commit 8b9d270

7 files changed

Lines changed: 149 additions & 81 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,4 @@ typings/
5959

6060
.idea
6161
.history
62+
!/cjs/

jest.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2-
preset: 'ts-jest',
3-
testEnvironment: 'node',
2+
preset: "ts-jest",
3+
testEnvironment: "node",
4+
rootDir: "src",
45
};

manualRun.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ import { transform } from "@babel/core";
44
import { Config } from "./src";
55

66
const check = `
7-
import { One } from 'one'
7+
import One, { Two } from 'one'
88
`;
99

1010
const options = {
1111
module: {
1212
from: "one",
1313
to: "two",
1414
},
15-
specifiers: {
16-
One: "default",
17-
},
15+
specifiers: { default: "Three" },
1816
} as Config;
1917

2018
const result =

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "import-move-codemod",
3-
"version": "0.0.1-1",
3+
"version": "0.0.1-2",
44
"description": "This babel plugin should be used as codemod for bulk import refactorings a.k.a module move refactorings",
55
"repository": "https://github.com/RIP21/import-move-codemod",
66
"license": "MIT",

src/__snapshots__/index.test.ts.snap

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`import-rename-codemod Should fail when asked to create impossible two or more defaults from named imports: Should fail when asked to create impossible two or more defaults from named imports 1`] = `
4-
5-
import One from 'one'
6-
7-
↓ ↓ ↓ ↓ ↓ ↓
8-
9-
Error: [BABEL] unknown: It's impossible to make two or more named imports into two or more defaults in one import statement! You should only have one 'default' value in your specifiers mapping (While processing: "base$0")
10-
11-
`;
12-
13-
exports[`import-rename-codemod Should fail when provided with wrong config shape: Should fail when provided with wrong config shape 1`] = `
14-
15-
import One from 'one'
16-
17-
↓ ↓ ↓ ↓ ↓ ↓
18-
19-
Error: [BABEL] unknown: Wrong specifiers format provided! It should be non-empty object or array. (While processing: "base$0")
20-
21-
`;
22-
233
exports[`import-rename-codemod Should move default import and named imports: Should move default import and named imports 1`] = `
244
255
import ADefault, { One, Two, Three } from 'one'
@@ -87,6 +67,39 @@ import ADefault from 'two';
8767
import { One, Two, Three } from 'one';
8868
8969
70+
`;
71+
72+
exports[`import-rename-codemod Should not create useless default import if 'from' library import is present but has no required named import: Should not create useless default import if 'from' library import is present but has no required named import 1`] = `
73+
74+
import SomeImport from 'one'
75+
76+
↓ ↓ ↓ ↓ ↓ ↓
77+
78+
import SomeImport from 'one';
79+
80+
81+
`;
82+
83+
exports[`import-rename-codemod Should not produce useless import if no match when default and named specifiers: Should not produce useless import if no match when default and named specifiers 1`] = `
84+
85+
import { Three } from 'one'
86+
87+
↓ ↓ ↓ ↓ ↓ ↓
88+
89+
import { Three } from 'one';
90+
91+
92+
`;
93+
94+
exports[`import-rename-codemod Should not produce useless import if no match: Should not produce useless import if no match 1`] = `
95+
96+
import One, { Two } from 'one'
97+
98+
↓ ↓ ↓ ↓ ↓ ↓
99+
100+
import One, { Two } from 'one';
101+
102+
90103
`;
91104

92105
exports[`import-rename-codemod When named to default should change import: When named to default should change import 1`] = `

src/index.test.ts

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,21 @@ pluginTester({
157157
specifiers: ["default", "One", "Two"],
158158
} as Config,
159159
},
160+
161+
{
162+
title:
163+
"Should not produce useless import if no match when default and named specifiers",
164+
code: `
165+
import { Three } from 'one'
166+
`,
167+
pluginOptions: {
168+
module: {
169+
from: "one",
170+
to: "two",
171+
},
172+
specifiers: ["default", "One", "Two"],
173+
} as Config,
174+
},
160175
{
161176
title: "Should move default to named",
162177
code: `
@@ -184,7 +199,7 @@ pluginTester({
184199
} as Config,
185200
},
186201
{
187-
title: "Should move default to named and move others along",
202+
title: "Should not produce useless import if no match",
188203
code: `
189204
import One, { Two } from 'one'
190205
`,
@@ -193,40 +208,38 @@ pluginTester({
193208
from: "one",
194209
to: "two",
195210
},
196-
specifiers: { default: "One", Two: "Two" },
211+
specifiers: { default: "Three" },
197212
} as Config,
198213
},
199214
{
200-
title: "Should move default to default",
215+
title:
216+
"Should not create useless default import if 'from' library import is present but has no required named import",
201217
code: `
202-
import One from 'one'
218+
import SomeImport from 'one'
203219
`,
204220
pluginOptions: {
205221
module: {
206222
from: "one",
207223
to: "two",
208224
},
209-
specifiers: ["default"],
225+
specifiers: { One: "default" },
210226
} as Config,
211227
},
212228
{
213-
title:
214-
"Should fail when asked to create impossible two or more defaults from named imports",
229+
title: "Should move default to named and move others along",
215230
code: `
216-
import One from 'one'
231+
import One, { Two } from 'one'
217232
`,
218233
pluginOptions: {
219234
module: {
220235
from: "one",
221236
to: "two",
222237
},
223-
specifiers: { One: "default", Two: "default" },
238+
specifiers: { default: "One", Two: "Two" },
224239
} as Config,
225-
error:
226-
"It's impossible to make two named imports into two defaults! You should only have one 'default' value in your specifiers mapping",
227240
},
228241
{
229-
title: "Should fail when provided with wrong config shape",
242+
title: "Should move default to default",
230243
code: `
231244
import One from 'one'
232245
`,
@@ -235,10 +248,39 @@ pluginTester({
235248
from: "one",
236249
to: "two",
237250
},
238-
specifiers: "Wrong",
239-
},
240-
error:
241-
"Wrong specifiers format provided! It should be non-empty object or array.",
251+
specifiers: ["default"],
252+
} as Config,
242253
},
254+
// {
255+
// title:
256+
// "Should fail when asked to create impossible two or more defaults from named imports",
257+
// code: `
258+
// import One from 'one'
259+
// `,
260+
// pluginOptions: {
261+
// module: {
262+
// from: "one",
263+
// to: "two",
264+
// },
265+
// specifiers: { One: "default", Two: "default" },
266+
// } as Config,
267+
// error:
268+
// "It's impossible to make two named imports into two defaults! You should only have one 'default' value in your specifiers mapping",
269+
// },
270+
// {
271+
// title: "Should fail when provided with wrong config shape",
272+
// code: `
273+
// import One from 'one'
274+
// `,
275+
// pluginOptions: {
276+
// module: {
277+
// from: "one",
278+
// to: "two",
279+
// },
280+
// specifiers: "Wrong",
281+
// },
282+
// error:
283+
// "Wrong specifiers format provided! It should be non-empty object or array.",
284+
// },
243285
],
244286
});

src/index.ts

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ const shouldMoveDefaultToNamed = (config: Config) => {
4747
);
4848
};
4949

50+
const getShouldMoveDefaultToNamedName = (config: Config) => {
51+
return !Array.isArray(config.specifiers) && config.specifiers.default;
52+
};
53+
5054
const shouldMoveAll = (config: Config) => {
5155
return Array.isArray(config.specifiers) && config.specifiers[0] === "*";
5256
};
@@ -58,26 +62,32 @@ const getNamedIdentifierToDefault = (config: Config) => {
5862
};
5963

6064
export default declare((_: any, options: Config) => {
61-
if (
62-
options.specifiers?.length === 0 ||
63-
typeof options.specifiers !== "object"
64-
) {
65-
throw new Error(
66-
"Wrong specifiers format provided! It should be non-empty object or array."
67-
);
68-
}
69-
if (
70-
typeof options.specifiers === "object" &&
71-
Object.values(options.specifiers).filter((it) => it === "default").length >
72-
1
73-
) {
74-
throw new Error(
75-
"It's impossible to make two or more named imports into two or more defaults in one import statement! You should only have one 'default' value in your specifiers mapping"
76-
);
65+
// if (
66+
// options.specifiers?.length === 0 ||
67+
// typeof options.specifiers !== "object"
68+
// ) {
69+
// throw new Error(
70+
// "Wrong specifiers format provided! It should be non-empty object or array."
71+
// );
72+
// }
73+
// if (
74+
// typeof options.specifiers === "object" &&
75+
// Object.values(options.specifiers).filter((it) => it === "default").length >
76+
// 1
77+
// ) {
78+
// throw new Error(
79+
// "It's impossible to make two or more named imports into two or more defaults in one import statement! You should only have one 'default' value in your specifiers mapping"
80+
// );
81+
// }
82+
83+
if (typeof options === "object" && Object.keys(options).length === 0) {
84+
return {
85+
visitor: {},
86+
};
7787
}
7888

7989
return {
80-
name: "import-rename-codemod",
90+
name: "import-move-codemod",
8191
inherits: typescript,
8292
visitor: {
8393
Program(path) {
@@ -108,20 +118,29 @@ export default declare((_: any, options: Config) => {
108118
[],
109119
t.stringLiteral(to)
110120
);
121+
// { One: "default" } case
111122
if (shouldChangeNamedToDefault(options)) {
112-
newImportDeclaration.specifiers = [
113-
t.importDefaultSpecifier(
114-
t.identifier(getNamedIdentifierToDefault(options))
115-
),
116-
];
123+
if (
124+
path.node.specifiers.find(
125+
(it) =>
126+
(it as ImportSpecifier)?.imported?.name ===
127+
getNamedIdentifierToDefault(options)
128+
)
129+
) {
130+
newImportDeclaration.specifiers = [
131+
t.importDefaultSpecifier(
132+
t.identifier(getNamedIdentifierToDefault(options))
133+
),
134+
];
117135

118-
const filterMovedToDefaultNamedImport = (it: any) =>
119-
t.isImportSpecifier(it) &&
120-
!(it.imported.name === getNamedIdentifierToDefault(options));
136+
const filterMovedToDefaultNamedImport = (it: any) =>
137+
t.isImportSpecifier(it) &&
138+
!(it.imported.name === getNamedIdentifierToDefault(options));
121139

122-
path.node.specifiers = path.node.specifiers.filter(
123-
filterMovedToDefaultNamedImport
124-
);
140+
path.node.specifiers = path.node.specifiers.filter(
141+
filterMovedToDefaultNamedImport
142+
);
143+
}
125144
}
126145
const namedSpecifiersToMove = node.specifiers.filter(
127146
(it) =>
@@ -166,18 +185,12 @@ export default declare((_: any, options: Config) => {
166185
aDefaultSpecifier &&
167186
newImportDeclaration.specifiers.unshift(aDefaultSpecifier);
168187
}
169-
// { One: "default" } case
170-
if (shouldChangeNamedToDefault(options)) {
171-
newImportDeclaration.specifiers.concat(
172-
t.importDefaultSpecifier(
173-
t.identifier(getNamedIdentifierToDefault(options))
174-
)
175-
);
176-
}
177188
// { "default" : "One" } case
178189
if (shouldMoveDefaultToNamed(options)) {
179-
const aDefaultSpecifier = node.specifiers.find((it) =>
180-
t.isImportDefaultSpecifier(it)
190+
const aDefaultSpecifier = node.specifiers.find(
191+
(it) =>
192+
t.isImportDefaultSpecifier(it) &&
193+
getShouldMoveDefaultToNamedName(options) === it.local.name
181194
);
182195
if (aDefaultSpecifier) {
183196
const newNamedSpecifier = t.importSpecifier(

0 commit comments

Comments
 (0)