microsoft / microsoft/TypeScript

Avoid nesting IIFE for nested namespaces

Abierto
#31,072 1 comentario 2 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Awaiting More Feedback Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

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.

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.

Línea de trabajo

El issue no especifica archivos fuente, tests ni puntos de entrada. Empieza por localizar la ruta del compilador que emite namespaces anidados y los tests existentes de emisión de namespaces; compara el JavaScript actual y el deseado para los casos indicados. Se considera terminado cuando los tests demuestran que se conserva el comportamiento y que solo se eliminan las capas de IIFE vacías especificadas.

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

Evaluación

Stack tecnológico
javascript, typescript
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.