Skip to content

Commit

Permalink
feat: Add functions and variables to typescript declaration file.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Feb 15, 2018
1 parent c64009d commit 451840b
Show file tree
Hide file tree
Showing 3 changed files with 1,552 additions and 51 deletions.
27 changes: 14 additions & 13 deletions code-generation/common/cloning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export function cloneInterfaces(node: StatementedNode, cloningInterfaces: Interf
name: method.getName(),
hasQuestionToken: method.hasQuestionToken(),
returnType: method.getReturnTypeNodeOrThrow().getText(),
docs: method.getJsDocs().map(d => ({ description: d.getInnerText().replace(/\r?\n/g, "\r\n") })),
typeParameters: method.getTypeParameters().map(p => ({
name: p.getName(),
constraint: p.getConstraintNode() == null ? undefined : p.getConstraintNode()!.getText()
Expand Down Expand Up @@ -71,6 +72,7 @@ export function cloneClasses(node: StatementedNode, classes: ClassDeclaration[])
node.addClasses(classes.map(c => ({
name: c.getName(),
isExported: true,
hasDeclareKeyword: true,
typeParameters: c.getTypeParameters().map(p => ({
name: p.getName(),
constraint: p.getConstraintNode() == null ? undefined : p.getConstraintNode()!.getText()
Expand All @@ -85,6 +87,7 @@ export function cloneClasses(node: StatementedNode, classes: ClassDeclaration[])
methods: c.getInstanceMethods().map(method => ({
name: method.getName(),
returnType: method.getReturnTypeNodeOrThrow().getText(),
docs: method.getJsDocs().map(d => ({ description: d.getInnerText().replace(/\r?\n/g, "\r\n") })),
typeParameters: method.getTypeParameters().map(p => ({
name: p.getName(),
constraint: p.getConstraintNode() == null ? undefined : p.getConstraintNode()!.getText()
Expand All @@ -101,6 +104,7 @@ export function cloneClasses(node: StatementedNode, classes: ClassDeclaration[])
export function cloneFunctions(node: StatementedNode, functions: FunctionDeclaration[]) {
node.addFunctions(functions.map(f => ({
name: f.getName(),
hasDeclareKeyword: true,
isExported: true,
typeParameters: f.getTypeParameters().map(p => ({
name: p.getName(),
Expand All @@ -109,26 +113,23 @@ export function cloneFunctions(node: StatementedNode, functions: FunctionDeclara
docs: f.getJsDocs().map(d => ({ description: d.getInnerText().replace(/\r?\n/g, "\r\n") })),
parameters: f.getParameters().map(p => ({
name: p.getNameOrThrow(),
hasQuestionToken: p.isOptional(),
type: p.getTypeNodeOrThrow().getText()
})),
returnType: f.getReturnTypeNodeOrThrow().getText()
})));
}

/*
export function cloneVariables(node: StatementedNode, variables: VariableStatement[]) {
node.addVariableStatement(variables.map(v => ({
node.addVariableStatements(variables.map(v => ({
isExported: true,
typeParameters: f.getTypeParameters().map(p => ({
name: p.getName(),
constraint: p.getConstraintNode() == null ? undefined : p.getConstraintNode()!.getText()
})),
docs: f.getJsDocs().map(d => ({ description: d.getInnerText().replace(/\r?\n/g, "\r\n") })),
parameters: f.getParameters().map(p => ({
name: p.getNameOrThrow(),
type: p.getTypeNodeOrThrow().getText()
})),
returnType: f.getReturnTypeNodeOrThrow().getText()
declarationType: v.getDeclarationType(),
hasDeclareKeyword: true,
docs: v.getJsDocs().map(d => ({ description: d.getInnerText().replace(/\r?\n/g, "\r\n") })),
declarations: v.getDeclarations().map(d => ({
name: d.getName(),
type: d.getTypeNode() == null ? undefined : d.getTypeNodeOrThrow().getText(),
initializer: d.getInitializer() == null ? undefined : d.getInitializerOrThrow().getText()
}))
})));
}
*/
20 changes: 12 additions & 8 deletions code-generation/createCompilerApiLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import * as path from "path";
import {rootFolder} from "./config";
import {InspectorFactory} from "./inspectors";
import {EnumDeclaration, InterfaceDeclaration, TypeAliasDeclaration, ClassDeclaration, TypeGuards, ts, SyntaxKind,
UnionTypeNode, FunctionDeclaration} from "./../src/main";
import {cloneEnums, cloneInterfaces, cloneTypeAliases, cloneClasses, cloneFunctions} from "./common/cloning";
UnionTypeNode, FunctionDeclaration, VariableStatement} from "./../src/main";
import {cloneEnums, cloneInterfaces, cloneTypeAliases, cloneClasses, cloneFunctions, cloneVariables} from "./common/cloning";

const enumsToSeparate = ["SyntaxKind", "ScriptTarget", "ScriptKind", "LanguageVariant", "EmitHint", "JsxEmit", "ModuleKind", "ModuleResolutionKind",
"NewLineKind", "TypeFlags", "ObjectFlags", "SymbolFlags", "TypeFormatFlags", "DiagnosticCategory"];
Expand Down Expand Up @@ -50,14 +50,18 @@ export function createCompilerApiLayer(factory: InspectorFactory) {
cloneTypeAliases(tsNamespace, allTypeAliases.filter(t => typeAliasesToSeparate.indexOf(t.getName()) === -1));
cloneClasses(tsNamespace, declarationFile.getDescendantsOfKind(SyntaxKind.ClassDeclaration) as ClassDeclaration[]);
cloneFunctions(tsNamespace, declarationFile.getDescendantsOfKind(SyntaxKind.FunctionDeclaration) as FunctionDeclaration[]);
tsNamespace.getStatements().forEach(s => {
if (TypeGuards.isAmbientableNode(s))
s.setHasDeclareKeyword(true);
});
cloneVariables(tsNamespace, declarationFile.getDescendantsOfKind(SyntaxKind.VariableStatement) as VariableStatement[]);

sourceFile.insertStatements(0, writer => {
writer.writeLine("/* tslint:disable */");
writer.writeLine("// DO NOT EDIT - This file is automatically generated by createCompilerApiLayer.ts");
writer.writeLine("/* tslint:disable */")
.writeLine("/*")
.writeLine(" * TypeScript Compiler Declaration File")
.writeLine(" * ====================================")
.writeLine(" * DO NOT EDIT - This file is automatically generated by createCompilerApiLayer.ts")
.writeLine(" *")
.writeLine(" * This file is contains the TypeScript compiler declarations slightly modified.")
.writeLine(" * Note: The TypeScript compiler is licensed under the Apache 2.0 license.")
.writeLine(" */");
});

tsNamespace.addStatements(writer => {
Expand Down

0 comments on commit 451840b

Please sign in to comment.