Skip to content

Commit

Permalink
feat: make Structure.isX function more flexible
Browse files Browse the repository at this point in the history
Closes #1219
  • Loading branch information
dsherret committed Nov 20, 2021
1 parent bcf694f commit a54dd69
Show file tree
Hide file tree
Showing 45 changed files with 510 additions and 745 deletions.
308 changes: 77 additions & 231 deletions deno/ts_morph.d.ts

Large diffs are not rendered by default.

231 changes: 154 additions & 77 deletions deno/ts_morph.js

Large diffs are not rendered by default.

308 changes: 77 additions & 231 deletions packages/ts-morph/lib/ts-morph.d.ts

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* Code Manipulation - Create type guards on `Structure`
* -------------------------------------------------
* This modifies the Structure.ts file that is used
* This modifies the Structure.generated.ts file that is used
* for doing type guards on structures.
* -------------------------------------------------
*/
import { tsMorph } from "../deps.ts";
import { Structure, TsMorphInspector } from "../inspectors/mod.ts";

export function createStructureTypeGuards(inspector: TsMorphInspector) {
const structureTypeGuardsFile = inspector.getProject().getSourceFileOrThrow("Structure.ts");
const structureTypeGuardsFile = inspector.getProject().getSourceFileOrThrow("Structure.generated.ts");
const typeGuardsExpr = structureTypeGuardsFile
.getVariableDeclarationOrThrow("Structure")
.getInitializerIfKindOrThrow(tsMorph.SyntaxKind.AsExpression)
Expand Down Expand Up @@ -86,9 +86,9 @@ function addNewMethods(typeGuardsExpr: tsMorph.ObjectLiteralExpression, structur
typeParameters: getTypeParameters(info),
statements: writer => {
if (info.kinds.size === 1)
writer.write(`return structure.kind === StructureKind.${Array.from(info.kinds)[0]};`);
writer.write(`return (structure as any)?.kind === StructureKind.${Array.from(info.kinds)[0]};`);
else {
writer.write("switch (structure.kind)").block(() => {
writer.write("switch ((structure as any)?.kind)").block(() => {
for (const kind of info.kinds)
writer.writeLine(`case StructureKind.${kind}:`);

Expand All @@ -102,14 +102,14 @@ function addNewMethods(typeGuardsExpr: tsMorph.ObjectLiteralExpression, structur

function getTypeParameters(info: StructureInfo): tsMorph.OptionalKind<tsMorph.TypeParameterDeclarationStructure>[] {
if (info.kind == null)
return [{ name: "T", constraint: "Structure & { kind: StructureKind; }" }];
return [{ name: "T" }];
return [];
}

function getParameterType(info: StructureInfo) {
if (info.kind == null)
return "T";
return "Structure & { kind: StructureKind; }";
return "unknown";
}

function getReturnType(info: StructureInfo) {
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AmbientableNodeStructure, ExportableNodeStructure } from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";
import { ClassLikeDeclarationBaseStructure } from "./base";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JSDocableNodeStructure } from "../base";
import { StatementedNodeStructure } from "../statement";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface ClassStaticBlockDeclarationStructure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { JSDocableNodeStructure, ScopedNodeStructure, SignaturedDeclarationStructure, TypeParameteredNodeStructure } from "../base";
import { FunctionLikeDeclarationStructure } from "../function";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";
import { OptionalKind } from "../types";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbstractableNodeStructure, DecoratableNodeStructure, PropertyNamedNodeStructure, ScopedNodeStructure, StaticableNodeStructure } from "../base";
import { FunctionLikeDeclarationStructure } from "../function";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface GetAccessorDeclarationStructure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
TypeParameteredNodeStructure,
} from "../base";
import { FunctionLikeDeclarationStructure } from "../function";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";
import { OptionalKind } from "../types";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
StaticableNodeStructure,
TypedNodeStructure,
} from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface PropertyDeclarationStructure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AbstractableNodeStructure, DecoratableNodeStructure, PropertyNamedNodeStructure, ScopedNodeStructure, StaticableNodeStructure } from "../base";
import { FunctionLikeDeclarationStructure } from "../function";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface SetAccessorDeclarationStructure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WriterFunction } from "../../types";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface DecoratorStructure extends Structure, DecoratorSpecificStructure {
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-morph/src/structures/doc/JSDocStructure.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WriterFunction } from "../../types";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";
import { OptionalKind } from "../types";
import { JSDocTagStructure } from "./JSDocTagStructure";
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-morph/src/structures/doc/JSDocTagStructure.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WriterFunction } from "../../types";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface JSDocTagStructure extends Structure, JSDocTagSpecificStructure {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AmbientableNodeStructure, ExportableNodeStructure, JSDocableNodeStructure, NamedNodeStructure } from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";
import { OptionalKind } from "../types";
import { EnumMemberStructure } from "./EnumMemberStructure";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { InitializerExpressionableNodeStructure, JSDocableNodeStructure, PropertyNamedNodeStructure } from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface EnumMemberStructure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WriterFunction } from "../../../types";
import { PropertyNamedNodeStructure } from "../../base";
import { KindedStructure, Structure } from "../../Structure";
import { KindedStructure, Structure } from "../../Structure.generated";
import { StructureKind } from "../../StructureKind";

export interface PropertyAssignmentStructure extends Structure, PropertyAssignmentSpecificStructure, PropertyNamedNodeStructure {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NamedNodeStructure } from "../../base";
import { KindedStructure, Structure } from "../../Structure";
import { KindedStructure, Structure } from "../../Structure.generated";
import { StructureKind } from "../../StructureKind";

export interface ShorthandPropertyAssignmentStructure extends Structure, ShorthandPropertyAssignmentSpecificStructure, NamedNodeStructure {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KindedStructure, Structure } from "../../Structure";
import { KindedStructure, Structure } from "../../Structure.generated";
import { StructureKind } from "../../StructureKind";
import { ExpressionedNodeStructure } from "../expressioned";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
SignaturedDeclarationStructure,
TypeParameteredNodeStructure,
} from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";
import { OptionalKind } from "../types";
import { FunctionLikeDeclarationStructure } from "./FunctionLikeDeclarationStructure";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ScopeableNodeStructure,
TypedNodeStructure,
} from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface ParameterDeclarationStructure
Expand Down
2 changes: 1 addition & 1 deletion packages/ts-morph/src/structures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export * from "./interface";
export * from "./jsx";
export * from "./module";
export * from "./statement";
export * from "./Structure";
export * from "./Structure.generated";
export * from "./StructureKind";
export * from "./type";
export * from "./types";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSDocableNodeStructure, SignaturedDeclarationStructure, TypeParameteredNodeStructure } from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface CallSignatureDeclarationStructure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSDocableNodeStructure, SignaturedDeclarationStructure, TypeParameteredNodeStructure } from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface ConstructSignatureDeclarationStructure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSDocableNodeStructure, ReadonlyableNodeStructure, ReturnTypedNodeStructure } from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface IndexSignatureDeclarationStructure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
TypeElementMemberedNodeStructure,
TypeParameteredNodeStructure,
} from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface InterfaceDeclarationStructure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
SignaturedDeclarationStructure,
TypeParameteredNodeStructure,
} from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface MethodSignatureStructure
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
ReadonlyableNodeStructure,
TypedNodeStructure,
} from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface PropertySignatureStructure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NamedNodeStructure } from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface JsxAttributeStructure extends Structure, JsxAttributeSpecificStructure, NamedNodeStructure {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";
import { OptionalKind } from "../types";
import { JsxAttributeStructure } from "./JsxAttributeStructure";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";
import { JsxAttributedNodeStructure, JsxTagNamedNodeStructure } from "./base";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface JsxSpreadAttributeStructure extends Structure, JsxSpreadAttributeSpecificStructure {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AssertionKeyNamedNodeStructure } from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface AssertEntryStructure extends Structure, AssertEntryStructureSpecificStructure, AssertionKeyNamedNodeStructure {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WriterFunction } from "../../types";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface ExportAssignmentStructure extends Structure, ExportAssignmentSpecificStructure {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WriterFunction } from "../../types";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";
import { OptionalKind } from "../types";
import { AssertEntryStructure } from "./AssertEntryStructure";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface ExportSpecifierStructure extends Structure, ExportSpecifierSpecificStructure {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { WriterFunction } from "../../types";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";
import { OptionalKind } from "../types";
import { AssertEntryStructure } from "./AssertEntryStructure";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface ImportSpecifierStructure extends Structure, ImportSpecifierSpecificStructure {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ModuleDeclarationKind } from "../../compiler";
import { AmbientableNodeStructure, ExportableNodeStructure, JSDocableNodeStructure, ModuleNamedNodeStructure } from "../base";
import { StatementedNodeStructure } from "../statement";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface ModuleDeclarationStructure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { StatementedNodeStructure } from "../statement";
import { Structure } from "../Structure";
import { Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface SourceFileStructure extends Structure, SourceFileSpecificStructure, StatementedNodeStructure {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BindingNamedNodeStructure, ExclamationTokenableNodeStructure, InitializerExpressionableNodeStructure, TypedNodeStructure } from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface VariableDeclarationStructure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { VariableDeclarationKind } from "../../compiler";
import { AmbientableNodeStructure, ExportableNodeStructure, JSDocableNodeStructure } from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";
import { OptionalKind } from "../types";
import { VariableDeclarationStructure } from "./VariableDeclarationStructure";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
TypedNodeStructure,
TypeParameteredNodeStructure,
} from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface TypeAliasDeclarationStructure
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { WriterFunction } from "../../types";
import { NamedNodeStructure } from "../base";
import { KindedStructure, Structure } from "../Structure";
import { KindedStructure, Structure } from "../Structure.generated";
import { StructureKind } from "../StructureKind";

export interface TypeParameterDeclarationStructure extends Structure, TypeParameterDeclarationSpecificStructure, NamedNodeStructure {
Expand Down

0 comments on commit a54dd69

Please sign in to comment.