Repository: TypeScript
Issue: Avoid nesting IIFE for nested namespaces
Labels: Suggestion,Awaiting More Feedback

<!-- 🚨 STOP 🚨 𝗦𝗧𝗢𝗣 🚨 𝑺𝑻𝑶𝑷 🚨

Half of all issues filed here are duplicates, answered in the FAQ, or not appropriate for the bug tracker. Please read the FAQ first, especially the "Common Feature Requests" section.

-->

## 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

<a href="https://www.typescriptlang.org/play/#src=namespace%20A.B.C%20%7B%0D%0A%20%20%20%20export%20function%20some()%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20'A.B.C.some'%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Avar%20A1%3B%0D%0A(function%20(A1)%20%7B%0D%0A%20%20%20%20var%20B1%20%3D%20A1.B1%20%7C%7C%20(A1.B1%20%3D%20%7B%7D)%3B%0D%0A%20%20%20%20var%20C1%20%3D%20B1.C1%20%7C%7C%20(B1.C1%20%3D%20%7B%7D)%3B%0D%0A%20%20%20%20function%20some()%20%7B%0D%0A%20%20%20%20%20%20%20%20return%20'A1.B1.C1.some'%3B%0D%0A%20%20%20%20%7D%0D%0A%20%20%20%20C1.some%20%3D%20some%3B%0D%0A%7D)(A1%20%7C%7C%20(A1%20%3D%20%7B%7D))%0D%0A%0D%0Aalert(A.B.C.some())%3B%0D%0Aalert(A1.B1.C1.some())%3B">TypeScript Playground</a>

<table>
<tr><th> TypeScript </th><th> JavaScript </th></tr>
<tr><td>

```ts
namespace A.B.C {
    export function some() {
        return 'A.B.C.some';
    }
}
```

</td><td>

```js
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 = {}));
```

</td></tr>
<tr><th></th><th>desired JavaScript</th></tr>
<tr><td></td>
<td>

```js
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 = {}));
```

</td></tr>
</table>

## 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 <a href="https://github.com/Microsoft/TypeScript/issues/447"><b>#447</b> Partial modules and output optimization</a>, 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:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).