Skip to content

Commit

Permalink
feat: #302 - Add CompilerNodeToWrappedType.
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jun 3, 2018
1 parent 5bd2926 commit 0ce5af5
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 19 deletions.
6 changes: 3 additions & 3 deletions scripts/code-generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createCompilerApiLayer } from "./createCompilerApiLayer";
import { createCodeBlockWriterFile } from "./createCodeBlockWriterFile";
import { createKindToNodeMappings } from "./createKindToNodeMappings";
import { createCompilerNodeBrandPropertyNamesType } from "./createCompilerNodeBrandPropertyNamesType";
import { createCompilerNodeToWrapperMappings } from "./createCompilerNodeToWrapperMappings";
import { createCompilerNodeToWrappedType } from "./createCompilerNodeToWrappedType";
import { createStructurePrinterFactory } from "./createStructurePrinterFactory";
import { InspectorFactory } from "./inspectors";

Expand All @@ -28,7 +28,7 @@ console.log("Creating kind to node mappings...");
createKindToNodeMappings(inspector, tsInspector);
console.log("Creating compiler node brand property names type...");
createCompilerNodeBrandPropertyNamesType(tsInspector);
console.log("Creating compiler node to wrapper mappings type...");
createCompilerNodeToWrapperMappings(inspector);
console.log("Creating compiler node to wrapped type...");
createCompilerNodeToWrappedType(inspector);

inspector.getProject().save();
2 changes: 1 addition & 1 deletion scripts/createCompilerNodeBrandPropertyNamesType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ export function createCompilerNodeBrandPropertyNamesType(tsInspector: TsInspecto
sourceFile.addTypeAlias({
isExported: true,
name: typeAliasName,
type: brandNames.map(n "\"" + n + "\"").join(" | ")
type: brandNames.map(n => `"${n}"`).join(" | ")
});
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Code Manipulation - Create compiler node to wrapper mappings
* Code Manipulation - Create compiler node to wrapped type
* ------------------------------------------------------------
* This creates the compilerNodeToWrapperMappings.ts file.
* This creates the CompilerNodeToWrappedType.ts file.
* ------------------------------------------------------------
*/
import { ClassDeclaration, MethodDeclaration, MethodDeclarationStructure, MethodSignature, MethodSignatureStructure, JSDocStructure,
Expand All @@ -11,9 +11,9 @@ import { TsSimpleAstInspector } from "./inspectors";

// this can go away once conditional types are well supported (maybe a few versions after)

export function createCompilerNodeToWrapperMappings(inspector: TsSimpleAstInspector) {
export function createCompilerNodeToWrappedType(inspector: TsSimpleAstInspector) {
const project = inspector.getProject();
const kindToNodeMappingsFile = project.getSourceFileOrThrow("compilerNodeToWrapperMappings.ts");
const kindToNodeMappingsFile = project.getSourceFileOrThrow("CompilerNodeToWrappedType.ts");
const wrappedNodes = inspector.getWrappedNodes();
kindToNodeMappingsFile.removeText();

Expand All @@ -30,7 +30,7 @@ export function createCompilerNodeToWrapperMappings(inspector: TsSimpleAstInspec
// create the type
kindToNodeMappingsFile.addTypeAlias({
isExported: true,
name: "CompilerNodeToWrapperMappings",
name: "CompilerNodeToWrappedType",
typeParameters: [{ name: "T", constraint: "ts.Node" }],
type: writer => {
let isFirst = true;
Expand All @@ -49,5 +49,5 @@ export function createCompilerNodeToWrapperMappings(inspector: TsSimpleAstInspec
});

kindToNodeMappingsFile.insertText(0, writer =>
writer.writeLine("// DO NOT EDIT - Automatically maintained by createCompilerNodeToWrapperMappings.ts"));
writer.writeLine("// DO NOT EDIT - Automatically maintained by createCompilerNodeToWrappedType.ts"));
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// DO NOT EDIT - Automatically maintained by createCompilerNodeToWrapperMappings.ts
// DO NOT EDIT - Automatically maintained by createCompilerNodeToWrappedType.ts
import * as compiler from "./index";
import { ts } from "../typescript";

export type CompilerNodeToWrapperMappings<T extends ts.Node> = [T] extends [ts.ClassDeclaration] ? compiler.ClassDeclaration :
export type CompilerNodeToWrappedType<T extends ts.Node> = [T] extends [ts.ClassDeclaration] ? compiler.ClassDeclaration :
[T] extends [ts.ConstructorDeclaration] ? compiler.ConstructorDeclaration :
[T] extends [ts.TypeParameterDeclaration] ? compiler.TypeParameterDeclaration :
[T] extends [ts.GetAccessorDeclaration] ? compiler.GetAccessorDeclaration :
[T] extends [ts.MethodDeclaration] ? compiler.MethodDeclaration :
[T] extends [ts.PropertyDeclaration] ? compiler.PropertyDeclaration :
Expand Down Expand Up @@ -120,6 +119,7 @@ export type CompilerNodeToWrapperMappings<T extends ts.Node> = [T] extends [ts.C
[T] extends [ts.TupleTypeNode] ? compiler.TupleTypeNode :
[T] extends [ts.TypeAliasDeclaration] ? compiler.TypeAliasDeclaration :
[T] extends [ts.TypeLiteralNode] ? compiler.TypeLiteralNode :
[T] extends [ts.TypeParameterDeclaration] ? compiler.TypeParameterDeclaration :
[T] extends [ts.TypeReferenceNode] ? compiler.TypeReferenceNode :
[T] extends [ts.UnionTypeNode] ? compiler.UnionTypeNode :
[T] extends [ts.ArrayDestructuringAssignment] ? compiler.ArrayDestructuringAssignment :
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/common/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ import { QuoteKind } from "../literal/QuoteKind";
import { NamespaceDeclaration } from "../namespace";
import { Statement, StatementedNode } from "../statement";
import { KindToNodeMappings } from "../kindToNodeMappings";
import { CompilerNodeToWrapperMappings } from "../compilerNodeToWrapperMappings";
import { CompilerNodeToWrappedType } from "../CompilerNodeToWrappedType";
import { Symbol } from "./Symbol";
import { SyntaxList } from "./SyntaxList";
import { CommentRange } from "./CommentRange";

export type NodePropertyToWrappedType<NodeType extends ts.Node, KeyName extends keyof NodeType, NonNullableNodeType = NonNullable<NodeType[KeyName]>> =
NodeType[KeyName] extends ts.NodeArray<infer ArrayNodeTypeForNullable> | undefined ? CompilerNodeToWrapperMappings<ArrayNodeTypeForNullable>[] | undefined :
NodeType[KeyName] extends ts.NodeArray<infer ArrayNodeType> ? CompilerNodeToWrapperMappings<ArrayNodeType>[] :
NodeType[KeyName] extends ts.Node ? CompilerNodeToWrapperMappings<NodeType[KeyName]> :
NonNullableNodeType extends ts.Node ? CompilerNodeToWrapperMappings<NonNullableNodeType> | undefined :
NodeType[KeyName] extends ts.NodeArray<infer ArrayNodeTypeForNullable> | undefined ? CompilerNodeToWrappedType<ArrayNodeTypeForNullable>[] | undefined :
NodeType[KeyName] extends ts.NodeArray<infer ArrayNodeType> ? CompilerNodeToWrappedType<ArrayNodeType>[] :
NodeType[KeyName] extends ts.Node ? CompilerNodeToWrappedType<NodeType[KeyName]> :
NonNullableNodeType extends ts.Node ? CompilerNodeToWrappedType<NonNullableNodeType> | undefined :
NodeType[KeyName];

export class Node<NodeType extends ts.Node = ts.Node> {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export * from "./doc";
export * from "./enum";
export * from "./expression";
export * from "./kindToNodeMappings";
export * from "./compilerNodeToWrapperMappings";
export * from "./CompilerNodeToWrappedType";
export * from "./file";
export * from "./function";
export * from "./general";
Expand Down
1 change: 1 addition & 0 deletions src/compiler/kindToNodeMappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export interface KindToNodeMappings {
[SyntaxKind.JsxText]: compiler.JsxText;
[SyntaxKind.LabeledStatement]: compiler.LabeledStatement;
[SyntaxKind.LiteralType]: compiler.LiteralTypeNode;
[SyntaxKind.LastTypeNode]: compiler.LiteralTypeNode;
[SyntaxKind.MetaProperty]: compiler.MetaProperty;
[SyntaxKind.MethodDeclaration]: compiler.MethodDeclaration;
[SyntaxKind.MethodSignature]: compiler.MethodSignature;
Expand Down

0 comments on commit 0ce5af5

Please sign in to comment.