[js/ts] Feature request: In Quick Fix menu, show also a dynamic import variant

Abierto
#56,472 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
20/100
Tipo de issue
Nueva funcionalidad
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
javascript, typescript

Línea de trabajo

Comienza localizando la implementación de JS/TS Quick Fix que sugiere imports estáticos y cualquier tratamiento existente para imports dinámicos. Determina los contextos compatibles, el comportamiento de las funciones async, la combinación de imports duplicados y si se necesita un flag. Se considera terminado cuando el comportamiento acordado está implementado y existe cobertura para las variantes de Quick Fix propuestas.

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

Descripción

Effort: Difficult Experience Enhancement Help Wanted Suggestion

Context

In JS/TS file, when using an identifier which is not found in the file, but is found somewhere else in the repo (e.g. another open file, or somewhere else), VSCode will show a Quick Fix (cmd-.) suggestion to import the file.

image

When followed, this will add a top-level static import

import { FOO_BAR } from 'web-ui/foobar';

Feature request

It would be useful for Quick Fix menu to also have a variant of a suggestion which would add a local, dynamic import(), which would be inserted just above the line where the identifier is first used:

So the menu would be like:

  • Quick Fix
    • Add import from "web-ui/foobar"
    • Add dynamic import from "web-ui/foobar"

And the code would look like:

        if (doSomething) {
            const { FOO_BAR } = await import('web-ui/foobar'); 
            // or, depending on how the export is defined:
            // const { FOO_BAR } = (await import('web-ui/foobar')).default;
            const value = FOO_BAR;

Potential drawbacks

  • If for every static import suggested, there's an equivalent dynamic import, the Quick Fix list may grow substantially, especially if FOO_BAR identifier is present in many files.

Open questions

  • To be decided if the dynamic import suggestion would show up:

    • everywhere
    • or only in functions?
    • or only in functions and if blocks? etc.
  • If a dynamic import is added in a function, should the function be converted to async function automatically?

  • Perhaps the dynamic import variant could be behind a flag (enabled by default for discoverability; easy to disable if someone doesn't like it, and prefers to only have static imports in the quick fix)

Extra considerations

There might be already existing dynamic imports that could be amended. For example, say the code is:

        if (doSomething) {
            const { QUUX } = await import('web-ui/foobar'); 
            const value = FOO_BAR;

In that case, all dynamic imports in scope should be analyzed, and the editor should suggest to update the existing

const { QUUX } = await import('web-ui/foobar');

with const { FOO_BAR, QUUX } = ....

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

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.

Más de microsoft/TypeScript

Todos los issues de microsoft/TypeScript

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.