microsoft / microsoft/TypeScript

Avoid nesting IIFE for nested namespaces

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

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

Awaiting More Feedback Suggestion
Ngôn ngữ chính
Go
Star
111k
Fork
14.3k
Merge trung bình
2 ngày 4 giờ
Pull request đã merge (30 ngày)
132

Mô tả

Search Terms

iife namespace nest

Suggestion

Produce a single IIFE for a nested namespace, where applicable.

The point is to reduce onion-like layering in scenarios where it is both trivial and safe to do. There would still be at least 1 level of IIFE wrapping. Every aspect of runtime/semantic/scoping behaviours is preserved as in the current emit.

Example

TypeScript Playground

TypeScript JavaScript
namespace A.B.C {
    export function some() {
        return 'A.B.C.some';
    }
}
var A;
(function (A) {
    var B;
    (function (B) {
        var C;
        (function (C) {
            function some() {
                return 'A.B.C.some';
            }
            C.some = some;
        })(C = B.C || (B.C = {}));
    })(B = A.B || (A.B = {}));
})(A || (A = {}));
desired JavaScript
var A;
(function (A) {
    var B = A.B || (A.B = {});
    var C = B.C || (B.C = {});
    function some() {
        return 'A.B.C.some';
    }
    C.some = some;
})(A || (A = {}));

Use Cases

Nested IIFE can affect performance, epecially in older/constrained environments. Of course, in general case they cannot be avoided, as they create lexical scopes and may have code living in all those nesting levels.

But there are several easy-to-detect scenarios where avoiding IIFE infestation is cheap and easy:

  • namespace A.B.C with single statement of dot-separated nest chain
  • namespace A { namespace B {} } with namespace having a single AST node child of another namespace
    • may have 2 slightly different cases depends on export modifier
  • combination of the above

Note that this feature only affects JS emits, but not parsing neither declaration handling.

Also, this may feel related to #447 Partial modules and output optimization, but the suggestion there is much more broad and heavy to implementation.

Here I do not suggest merging of scopes, just eliminating an easy-to-detect subset of empty scopes that have no code in them.

Non-targeted cases

  1. This feature explicitly DOES NOT suggest merging consecutive scopes, whether in one file or multiple (that may be a valid thing to do elsewhere).
  2. This feature explicitly DOES NOT suggest eliding scopes that have code in them (that would be an error, and should be tested against by a valid implementation).
  3. This feature explicitly DOES NOT intend to elide all possible scopes/layers, only a very few specific easy to check cases.

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

Issue không nêu tên tệp nguồn, test hoặc entry point nào. Hãy bắt đầu bằng cách xác định đường dẫn của compiler phát ra các namespace lồng nhau và các test phát namespace hiện có; so sánh JavaScript hiện tại và JavaScript mong muốn cho các trường hợp được liệt kê. Công việc được xem là hoàn tất khi các test chứng minh hành vi được giữ nguyên, đồng thời chỉ loại bỏ các lớp IIFE rỗng được chỉ định.

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

Đánh giá

Công nghệ
javascript, 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.