microsoft / microsoft/TypeScript

new `Move to file` refactoring should avoid name collisions

Abierto
#54,130 0 comentarios 3 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Experience Enhancement Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.4k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

Suggestion

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

export const getData = () => ({
  foo: true
})
const data = getData()
// target file
const getData = () => ({
  bar: false
})

const data = getData()

After moving

Actual:

const getData = () => ({
    bar: false
})

const data = getData()
export const getData = () => ({
    foo: true
})

Expected:

const getData = () => ({
    bar: false
})

const data = getData()
export const getData2 = () => ({
    foo: true
})
// original file:
import { getData2 as getData } from "./file2"

const data = getData()

I can even see the test that explicitly captures this case (added by @navya9singh in https://github.com/microsoft/TypeScript/pull/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 file
import { groupBy } from 'lodash'

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

groupBy([], (val) => val)

After refactor:

// actual
import { groupBy } from 'lodash'
import { groupBy } from 'rambda'

groupBy([], (val) => val)
groupBy((val) => val, [])
// expected
import { groupBy } from 'lodash'
import { groupBy as groupBy2 } 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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con tests/cases/fourslash/moveToFile_unresolvedImport.ts y la implementación de la refactorización move-to-file; el issue también señala src/services/refactors/extractSymbol.ts para consultar el comportamiento existente de evitación de colisiones. Reproduce los ejemplos de variables locales y nombres de importación, y después añade cobertura que demuestre que las declaraciones y las importaciones movidas reciben nombres únicos sin romper las referencias.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
tooling
Tipo de issue
Nueva funcionalidad
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.