microsoft / microsoft/TypeScript

Include CompilerHost in TransformationContext

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

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

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

Mô tả

Search Terms

compiler host, CompilerHost,
transformation context, TransformationContext,
transformer, custom transformer

Suggestion

I'd like to access the CompilerHost instance from my custom transformers (via TransformationContext).

As far as I can see this wouldn't expose any "risky" patterns like #25147.

Use Cases

My specific use-case is resolving import declarations. This can be done using ts.resolveModuleName (https://github.com/microsoft/TypeScript/issues/28276#issuecomment-435016356), but this requires a CompilerHost instance.

Example

An example for my specific use-case would be:

const transformer: ts.TransformerFactory<ts.SourceFile> = context => {
    return file => {
        return ts.visitEachChild(
            file,
            node => {
                if (ts.isImportDeclaration(node)) {
                    const resolved = ts.resolveModuleName(
                        (node.moduleSpecifier as ts.StringLiteral).text,
                        file.fileName,
                        context.getCompilerOptions(),
                        context.getCompilerHost() // this is currently impossible
                    )
                    
                    // do something interesting with resolved
                }
                return node;
            },
            context
        )
    }
}

Workaround

Luckily there is a workaround for this:

const host = ts.createCompilerHost(context.getCompilerOptions());

But this feels redundant and somewhat "wrong".

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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.

Hướng nghiên cứu

Bắt đầu với các API TransformationContext và CompilerHost, sau đó lần theo cách các transformer tùy chỉnh nhận context và cách ts.resolveModuleName sử dụng một host. Xác định hình dạng API dự kiến và kiểm tra khả năng tương thích của nó với các transformer hiện có. Hoàn thành khi một transformer có thể truy cập CompilerHost liên quan để phân giải module mà không tạo một host dư thừa.

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

Đánh giá

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

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.