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

Đang mở
#56,472 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
20/100
Loại issue
Tính năng
Độ rõ ràng
Cần làm rõ
Mức độ hoạt động
Đình trệ
Công nghệ
javascript, typescript

Hướng nghiên cứu

Bắt đầu bằng cách xác định phần triển khai JS/TS Quick Fix đề xuất các static import và mọi cách xử lý hiện có cho dynamic import. Xác định các ngữ cảnh được hỗ trợ, hành vi của hàm async, việc hợp nhất các import trùng lặp và liệu có cần một flag hay không. Công việc được xem là hoàn tất khi hành vi đã thống nhất được triển khai và có coverage cho các biến thể Quick Fix được đề xuất.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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 } = ....

Ngôn ngữ chính
Go
Star
111k
Fork
14.4k
Merge trung bình
1 ngày 15 giờ
Pull request đã merge (30 ngày)
106

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của microsoft/TypeScript

Tất cả issue của microsoft/TypeScript

Issue tương tự

Thêm issue về Go

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.