-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import * as generated from "../../../wasm/index.mjs"; | ||
|
||
const InternalCompilationBuilder = generated.compilation.InternalCompilationBuilder; | ||
type InternalCompilationBuilder = generated.compilation.InternalCompilationBuilder; | ||
|
||
export interface CompilationOptions { | ||
/** | ||
* The language version to parse files with. | ||
*/ | ||
version: string; | ||
|
||
resolveImport: (fileId: string, importString: string) => Promise<string | undefined>; | ||
|
||
/** | ||
* Callback used by this builder to load the contents of a file. | ||
* The user is responsible for fetching the file from the filesystem. | ||
* If the file is not found, the callback should return undefined. | ||
* If the file cannot be read, the callback should throw an error, and it will be propagated to the caller. | ||
*/ | ||
loadFileContents: (fileId: string) => Promise<string | undefined>; | ||
} | ||
|
||
/** | ||
* A builder for creating compilation units. | ||
* Allows incrementally building a transitive list of all files and their imports. | ||
*/ | ||
export class CompilationBuilder { | ||
private readonly pendingTasks: Promise<void>[] = []; | ||
private readonly resolvedFileIds: Set<string> = new Set(); | ||
|
||
private constructor( | ||
private readonly options: CompilationOptions, | ||
private readonly internal: InternalCompilationBuilder, | ||
) {} | ||
|
||
/** | ||
* Creates a new compilation builder for the specified language version. | ||
*/ | ||
public create(options: CompilationOptions): CompilationBuilder { | ||
const internal = InternalCompilationBuilder.create(options.version); | ||
return new CompilationBuilder(options, internal); | ||
} | ||
|
||
/** | ||
* Adds a source file to the compilation unit. | ||
*/ | ||
public async addFile(fileId: string, contents: string): Promise<void> { | ||
const { importStrings } = this.internal.addFile(fileId, contents); | ||
|
||
for (const importString of importStrings) { | ||
this.pendingTasks.push(this.addImport(fileId, importString)); | ||
} | ||
|
||
await this.executePendingTasks(); | ||
} | ||
|
||
private async addImport(fileId: string, importString: string): Promise<void> { | ||
const importedFileId = await this.options.resolveImport(fileId, importString); | ||
if (importedFileId === undefined) { | ||
return; | ||
} | ||
|
||
this.internal.addImport(fileId, importString, importedFileId); | ||
} | ||
|
||
private async executePendingTasks(): Promise<void> { | ||
while (this.pendingTasks.length > 0) { | ||
const pendingTasks = this.pendingTasks.splice(0); | ||
await Promise.all(pendingTasks); | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.