microsoft / microsoft/TypeScript
Add a modifier for "closed" blocks and functions
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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
})
}
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Es werden keine Dateien, Tests oder Compiler-Einstiegspunkte genannt. Beginne mit der Überprüfung der vorgeschlagenen Syntax closed und closed inline sowie der Anwendungsfälle für Serialisierung und webpack worker. Fertig ist die Aufgabe, wenn erfasste Werte aus dem äußeren Kontext abgelehnt werden, explizite Imports funktionieren und das angeforderte inline-Verhalten definiert und implementiert ist.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript, webpack
- Bereich
- compilers
- Issue-Typ
- Feature
- Schwierigkeit
- 5/5
- Geschätzter Aufwand
- Über eine Woche
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 25/100