Skip to content

Commit 142c67a

Browse files
authored
fix(javascript): support member renaming for aliased imports in ChangeImport (#7102)
* test replicated and passing * testing scenario on failing case
1 parent e5e1d4a commit 142c67a

2 files changed

Lines changed: 90 additions & 22 deletions

File tree

rewrite-javascript/rewrite/src/javascript/recipes/change-import.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {Option, Recipe} from "../../recipe";
18-
import {TreeVisitor} from "../../visitor";
19-
import {ExecutionContext} from "../../execution";
20-
import {JavaScriptVisitor, JS} from "../index";
21-
import {maybeAddImport} from "../add-import";
22-
import {J, isIdentifier, Type} from "../../java";
23-
import {create as produce, Draft} from "mutative";
17+
import { Option, Recipe } from "../../recipe";
18+
import { TreeVisitor } from "../../visitor";
19+
import { ExecutionContext } from "../../execution";
20+
import { JavaScriptVisitor, JS } from "../index";
21+
import { maybeAddImport } from "../add-import";
22+
import { J, isIdentifier, Type } from "../../java";
23+
import { create as produce, Draft } from "mutative";
2424

2525
/**
2626
* Changes an import from one module to another, updating all type attributions.
@@ -223,6 +223,13 @@ export class ChangeImport extends Recipe {
223223
if (specifier.specifier.kind === J.Kind.Identifier &&
224224
specifier.specifier.simpleName === oldMember) {
225225
specifier.specifier.simpleName = newMember;
226+
} else if (specifier.specifier.kind === JS.Kind.Alias) {
227+
const aliasNode = specifier.specifier as Draft<JS.Alias>;
228+
const propertyName = aliasNode.propertyName.element;
229+
if (propertyName.kind === J.Kind.Identifier &&
230+
propertyName.simpleName === oldMember) {
231+
propertyName.simpleName = newMember;
232+
}
226233
}
227234
}
228235
}
@@ -520,7 +527,7 @@ export class ChangeImport extends Recipe {
520527

521528
// Also update the method name if we're renaming the member
522529
const updatedName = (oldMember !== 'default' && oldMember !== '*' &&
523-
methodType.name === oldMember && newMember !== oldMember)
530+
methodType.name === oldMember && newMember !== oldMember)
524531
? newMember
525532
: methodType.name;
526533

rewrite-javascript/rewrite/test/javascript/recipes/change-import.test.ts

Lines changed: 75 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import {RecipeSpec} from "../../../src/test";
17-
import {ChangeImport, npm, packageJson, tsx, typescript} from "../../../src/javascript";
18-
import {withDir} from "tmp-promise";
16+
import { RecipeSpec } from "../../../src/test";
17+
import { ChangeImport, npm, packageJson, tsx, typescript } from "../../../src/javascript";
18+
import { withDir } from "tmp-promise";
1919

2020
describe("change-import", () => {
2121
describe("named imports", () => {
@@ -52,7 +52,7 @@ describe("change-import", () => {
5252
}`)
5353
)
5454
);
55-
}, {unsafeCleanup: true});
55+
}, { unsafeCleanup: true });
5656
});
5757

5858
test("changes named import with double quotes", async () => {
@@ -88,7 +88,7 @@ describe("change-import", () => {
8888
}`)
8989
)
9090
);
91-
}, {unsafeCleanup: true});
91+
}, { unsafeCleanup: true });
9292
});
9393

9494
test("preserves other imports from the same module", async () => {
@@ -127,7 +127,7 @@ describe("change-import", () => {
127127
}`)
128128
)
129129
);
130-
}, {unsafeCleanup: true});
130+
}, { unsafeCleanup: true });
131131
});
132132

133133
test("does not change import from different module", async () => {
@@ -157,7 +157,7 @@ describe("change-import", () => {
157157
}`)
158158
)
159159
);
160-
}, {unsafeCleanup: true});
160+
}, { unsafeCleanup: true });
161161
});
162162

163163
test("does not change unrelated imports from the same module", async () => {
@@ -188,7 +188,7 @@ describe("change-import", () => {
188188
}`)
189189
)
190190
);
191-
}, {unsafeCleanup: true});
191+
}, { unsafeCleanup: true });
192192
});
193193

194194
test("adds import from target module", async () => {
@@ -228,7 +228,7 @@ describe("change-import", () => {
228228
}`)
229229
)
230230
);
231-
}, {unsafeCleanup: true});
231+
}, { unsafeCleanup: true });
232232
});
233233

234234
test("preserves aliased import", async () => {
@@ -264,7 +264,7 @@ describe("change-import", () => {
264264
}`)
265265
)
266266
);
267-
}, {unsafeCleanup: true});
267+
}, { unsafeCleanup: true });
268268
});
269269
});
270270

@@ -299,7 +299,7 @@ describe("change-import", () => {
299299
}`)
300300
)
301301
);
302-
}, {unsafeCleanup: true});
302+
}, { unsafeCleanup: true });
303303
});
304304
});
305305

@@ -334,7 +334,7 @@ describe("change-import", () => {
334334
}`)
335335
)
336336
);
337-
}, {unsafeCleanup: true});
337+
}, { unsafeCleanup: true });
338338
});
339339
});
340340

@@ -370,7 +370,68 @@ describe("change-import", () => {
370370
}`)
371371
)
372372
);
373-
}, {unsafeCleanup: true});
373+
}, { unsafeCleanup: true });
374+
});
375+
376+
test("renames aliased member when changing import", async () => {
377+
const spec = new RecipeSpec();
378+
spec.recipe = new ChangeImport({
379+
oldModule: "lodash",
380+
oldMember: "extend",
381+
newModule: "lodash",
382+
newMember: "assign"
383+
});
384+
385+
await withDir(async (repo) => {
386+
await spec.rewriteRun(
387+
npm(
388+
repo.path,
389+
typescript(
390+
`
391+
import { extend as myExtend } from 'lodash';
392+
`,
393+
`
394+
import { assign as myExtend } from 'lodash';
395+
`
396+
),
397+
packageJson(`{
398+
"name": "test",
399+
"dependencies": {
400+
"lodash": "^4.17.21"
401+
}
402+
}`)
403+
)
404+
);
405+
}, { unsafeCleanup: true });
406+
});
407+
408+
test("does not rename if member is local alias only", async () => {
409+
const spec = new RecipeSpec();
410+
spec.recipe = new ChangeImport({
411+
oldModule: "lodash",
412+
oldMember: "extend",
413+
newModule: "lodash",
414+
newMember: "assign"
415+
});
416+
417+
await withDir(async (repo) => {
418+
await spec.rewriteRun(
419+
npm(
420+
repo.path,
421+
typescript(
422+
`
423+
import { flatten as extend } from 'lodash';
424+
`
425+
),
426+
packageJson(`{
427+
"name": "test",
428+
"dependencies": {
429+
"lodash": "^4.17.21"
430+
}
431+
}`)
432+
)
433+
);
434+
}, { unsafeCleanup: true });
374435
});
375436
});
376437

@@ -414,7 +475,7 @@ describe("change-import", () => {
414475
}`)
415476
)
416477
);
417-
}, {unsafeCleanup: true});
478+
}, { unsafeCleanup: true });
418479
});
419480
});
420481
});

0 commit comments

Comments
 (0)