Skip to content

new Move to file refactoring should avoid name collisions #54130

Description

@zardoy

Suggestion

I tested it in insiders + nightly in the following way:

exportconstgetData=()=>({foo: true})constdata=getData()
// target fileconstgetData=()=>({bar: false})constdata=getData()

After moving

Actual:

constgetData=()=>({bar: false})constdata=getData()exportconstgetData=()=>({foo: true})

Expected:

constgetData=()=>({bar: false})constdata=getData()exportconstgetData2=()=>({foo: true})// original file:import{getData2asgetData}from"./file2"constdata=getData()

I can even see the test that explicitly captures this case (added by navya9singh in #53542):
https://github.com/microsoft/TypeScript/blob/ea6441d3d77963b1c692c8aca023a822e039dc67/tests/cases/fourslash/moveToFile_unresolvedImport.ts that would obviously break real world code after. I would expect a to be named as a2 after moving I guess?

THe real problem is that sometimes you just can't tell wether specific file has confliciting top-level identifiers and you have to rename these symbols either in source or target file, but user also could rename these symbols after the refactoring instead (e.g. getData2 to something else). Introduce variable / function just use one function call to avoid name collisions:

https://github.com/microsoft/TypeScript/blob/7c378dbab3ab590835c3adbd260f37c97fba1111/src/services/refactors/extractSymbol.ts#LL1042C33-L1042C39

The same thing also applies for names coming from imports:

// source fileimport{groupBy}from'lodash'groupBy([],(val)=>val)
// target fileimport{groupBy}from'lodash'groupBy([],(val)=>val)

After refactor:

// actualimport{groupBy}from'lodash'import{groupBy}from'rambda'groupBy([],(val)=>val)groupBy((val)=>val,[])
// expectedimport{groupBy}from'lodash'import{groupByasgroupBy2}from'rambda'groupBy([],(val)=>val)groupBy2((val)=>val,[])

This code is not broken :shrug :.

This is painful when you have huge project that in process of migration from lodash to rambda api.

🔍 Search Terms

✅ Viability Checklist

📃 Motivating Example

💻 Use Cases

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions