-
Notifications
You must be signed in to change notification settings - Fork 547
Generate an exportable and ambient version of the TypeScript types #136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import { assert } from "console"; | ||
| import { readFile } from "fs/promises"; | ||
| import * as ts from "typescript"; | ||
| import { createMemoryProgram } from "./program"; | ||
| export interface ParsedTypeDefinition { | ||
| program: ts.Program; | ||
| source: ts.SourceFile; | ||
| checker: ts.TypeChecker; | ||
| parsed: { | ||
| functions: Map<string, ts.FunctionDeclaration>; | ||
| interfaces: Map<string, ts.InterfaceDeclaration>; | ||
| vars: Map<string, ts.VariableDeclaration>; | ||
| types: Map<string, ts.TypeAliasDeclaration>; | ||
| classes: Map<string, ts.ClassDeclaration>; | ||
| }; | ||
| } | ||
|
|
||
| // Collate standards (to support lib.(dom|webworker).iterable.d.ts being defined separately) | ||
| export async function collateStandards( | ||
| ...standardTypes: string[] | ||
| ): Promise<ParsedTypeDefinition> { | ||
| const STANDARDS_PATH = "/source.ts"; | ||
| const text: string = ( | ||
| await Promise.all( | ||
| standardTypes.map( | ||
| async (s) => | ||
| // Remove the Microsoft copyright notices from the file, to prevent them being copied in as TS comments | ||
| (await readFile(s, "utf-8")).split(`/////////////////////////////`)[2] | ||
| ) | ||
| ) | ||
| ).join("\n"); | ||
| const program = createMemoryProgram(new Map([[STANDARDS_PATH, text]])); | ||
| const source = program.getSourceFile(STANDARDS_PATH)!; | ||
| const checker = program.getTypeChecker(); | ||
| const parsed = { | ||
| functions: new Map<string, ts.FunctionDeclaration>(), | ||
| interfaces: new Map<string, ts.InterfaceDeclaration>(), | ||
| vars: new Map<string, ts.VariableDeclaration>(), | ||
| types: new Map<string, ts.TypeAliasDeclaration>(), | ||
| classes: new Map<string, ts.ClassDeclaration>(), | ||
| }; | ||
| ts.forEachChild(source, (node) => { | ||
| let name = ""; | ||
| if (node && ts.isFunctionDeclaration(node)) { | ||
| name = node.name?.text ?? ""; | ||
| parsed.functions.set(name, node); | ||
| } else if (ts.isVariableStatement(node)) { | ||
| assert(node.declarationList.declarations.length === 1); | ||
| name = node.declarationList.declarations[0].name.getText(source); | ||
| parsed.vars.set(name, node.declarationList.declarations[0]); | ||
| } else if (ts.isInterfaceDeclaration(node)) { | ||
| name = node.name.text; | ||
| parsed.interfaces.set(name, node); | ||
| } else if (ts.isTypeAliasDeclaration(node)) { | ||
| name = node.name.text; | ||
| parsed.types.set(name, node); | ||
| } else if (ts.isClassDeclaration(node)) { | ||
| name = node.name?.text ?? ""; | ||
| parsed.classes.set(name, node); | ||
| } | ||
| }); | ||
| return { | ||
| program, | ||
| source, | ||
| checker, | ||
| parsed, | ||
| }; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import ts from "typescript"; | ||
| import { ensureStatementModifiers } from "./helpers"; | ||
|
|
||
| // This ensures that all nodes have the `declare` keyword | ||
| export function createAmbientTransformer(): ts.TransformerFactory<ts.SourceFile> { | ||
| return (ctx) => { | ||
| return (node) => { | ||
| const visitor = createVisitor(ctx); | ||
| return ts.visitEachChild(node, visitor, ctx); | ||
| }; | ||
| }; | ||
| } | ||
|
|
||
| function createVisitor(ctx: ts.TransformationContext) { | ||
| return (node: ts.Node) => { | ||
| return ensureStatementModifiers(ctx, node, false); | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.