microsoft / microsoft/TypeScript

Expose a function for constructing a new System based on a custom FS implementation

Aperta
#44,379 0 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.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

Suggestion

Currently TypeScript constructs a System and sets it on the sys property on the TypeScript module. Many parts of the Compiler APIs accept a custom implementation for System, which is useful for example for testing purposes when a virtual file system is being used.

But it is not easily mockable, as the System interface is quite extensive. Internally, TypeScript constructs this by invoking an anonymous internal function, like so:

ts.sys = (function () {
 // ...
})();

🔍 Search Terms

createSystem, Mock System, Virtual Filesystem

✅ Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

I would like to propose exposing a function, createSystem, that takes an Partial of the following structure:

interface CreateSystemOptions {
    // An FS implementation
    fs: typeof import("fs");

    // These are the only two properties you use from the os module
    os: Pick<typeof import("os"), "platform"|"EOL">;
    
    // You default to using process.cwd() here, but the current working directory could in theory be something else
    cwd: string;
}

export function createSystem (options: Partial<CreateSystemOptions>): System {
    // ...
}

This function should also be used internally by TypeScript, and constructs a new System. This would allow for easier mocking.

📃 Motivating Example

Imagine building a Program programmatically that relies on an incremental CompilerHost. With the createSystem function, you would very easily be able to swap out the fs implementation and pass it a virtual file system based on the test(s) you're running:

import * as ts from "typescript";
import virtualFs from "memfs";
import realFs from "fs";

function createCompilerHost (fs: typeof import("fs") = realFs): ts.CompilerHost {
    return ts.createIncrementalCompilerHost({}, ts.createSystem({fs}));
}

const program = ts.createProgram({
    host: createCompilerHost(virtualFs),
    rootNames: ["/foo/bar/baz.ts"],
    options: {}
})

💻 Use Cases

Use cases:

  • Testing
  • Tooling around TypeScript's Compiler APIs
  • Virtual file systems, such as is the case with some bundlers running in watch mode

Workarounds:

  • Manually reimplementing most of the System interface, and manually maintaining it across TypeScript versions.

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

Inizia individuando la funzione anonima interna che costruisce ts.sys e verificando come createIncrementalCompilerHost utilizza un System. Definisci il punto di ingresso pubblico createSystem attorno alle opzioni proposte e assicurati che TypeScript lo utilizzi internamente. Il lavoro è completato quando i chiamanti possono fornire un FS virtuale senza dover reimplementare manualmente la maggior parte di System.

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

Valutazione

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.