microsoft / microsoft/TypeScript

Add a modifier for "closed" blocks and functions

Aperta
#37,028 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

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
  })
}

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Non sono indicati file, test o punti di ingresso del compilatore. Inizia esaminando la sintassi proposta closed e closed inline, nonché i casi d’uso della serializzazione e di webpack worker. Il lavoro è completato quando i valori catturati dall’ambito esterno vengono rifiutati, gli import espliciti funzionano e il comportamento inline richiesto è definito e implementato.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript, webpack
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.