microsoft / microsoft/TypeScript
Add a modifier for "closed" blocks and functions
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
Search Terms
puppeteer, worker, webpack
Suggestion
Add a closed clause to create blocks/functions that cannot capture any value outside the scope of the block. Any dependencies must be imported explicitly within the block. These dependencies can be inlined to form a self-contained block (using an extra inline keyword in the closed statement or through a boolean field in the compiler options). The resulting inline block can be optimized and unused code can be removed.
Use Cases
Serialize functions
Tools like puppeteer provide functions to perform a callback in the browser context. These callbacks are serialized. This can cause problems with functions in TypeScript, since the language uses tslib and errors like awaiter is not defined can occur. Also, the compiler will not report an error when any value is captured within the callback, but these values will not be available within the browser context.
Workers in webpack
There are some problems when using workers with webpack. The emitted code for the import|require statements contains functions that will not be available in the worker scope. With a closed inline block, these problems are resolved without needs to use any extra plugin or loader.
Examples
const path = require('path')
closed {
const pathSeparator = path.sep // Compiler error - `path` is not defined
import('path').then(module => { // The right way
// ...
})
}
closed inline {
const pathSeparator = path.sep // Compiler error - `path` is not defined
import('path').then(module => { // The right way
// The path module will be inlined within the `closed inline` block
})
}
closed function foo() {
const pathSeparator = path.sep // Compiler error - `path` is not defined
import('path').then(module => { // The right way
// ...
})
}
closed inline foo() {
const pathSeparator = path.sep // Compiler error - `path` is not defined
import('path').then(module => { // The right way
// The path module will be inlined within the `closed inline` function
})
}
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
No se nombran archivos, pruebas ni puntos de entrada del compilador. Empieza revisando la sintaxis propuesta closed y closed inline, así como los casos de uso de serialización y webpack worker. Se considerará terminado cuando se rechacen los valores capturados del ámbito externo, funcionen los imports explícitos y el comportamiento inline solicitado esté definido e implementado.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript, webpack
- Á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
- 25/100