Skip to content
This repository has been archived by the owner on Oct 15, 2018. It is now read-only.

Commit

Permalink
#218 - Assigning to text will parse the type and create the appropria…
Browse files Browse the repository at this point in the history
…te types.

Sidenote: These code generated functions from ts-state-test-generator and ts-cloneable-generator are awesome!
  • Loading branch information
dsherret committed Feb 22, 2017
1 parent 9c8026c commit 05d1086
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 26 deletions.
2 changes: 1 addition & 1 deletion src/binders/base/base/BaseFunctionBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export abstract class BaseFunctionBinder<ParameterType extends definitions.BaseP
def.userDefinedTypeGuard = this.getUserDefinedTypeGuard();

if (def.userDefinedTypeGuard != null) {
def.returnType.text = (def.userDefinedTypeGuard.parameterName || "this") + " is " + def.userDefinedTypeGuard.type.text;
def.returnType._text = (def.userDefinedTypeGuard.parameterName || "this") + " is " + def.userDefinedTypeGuard.type.text;
}
}
}
2 changes: 1 addition & 1 deletion src/binders/base/expression/ExpressionBinder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export abstract class ExpressionBinder {

bind(def: ExpressionDefinition) {
this.baseDefinitionBinder.bind(def);
def.text = this.getText();
def._text = this.getText();
}
}
5 changes: 4 additions & 1 deletion src/build/generateDefinitionFile.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as path from "path";
import * as fs from "fs";
import {getInfoFromFiles, GlobalDefinition, ModuledDefinition, Options} from "./../main";
import {getInfoFromFiles, GlobalDefinition, ModuledDefinition, Options, BaseExpressionDefinition} from "./../main";

export function generateDefinitionFile() {
const result = getInfoFromFiles([
Expand Down Expand Up @@ -36,6 +36,9 @@ function removeInternalProperties(result: GlobalDefinition) {

const optionsInterface = result.getFile("options.ts")!.getInterface(nameof<Options>())!;
optionsInterface.properties = optionsInterface.properties.filter(p => p.name !== nameof<Options>(o => o.getTypesFromTypeNodes));

const baseExpressionDefinition = result.getFile("BaseExpressionDefinition.ts")!.getInterface(nameof(BaseExpressionDefinition))!;
baseExpressionDefinition.properties = baseExpressionDefinition.properties.filter(p => p.name !== nameof<BaseExpressionDefinition>(t => t._text));
}

function fixSetType(result: GlobalDefinition) {
Expand Down
16 changes: 8 additions & 8 deletions src/cloneFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export function cloneTypeDefinition(definition: TypeDefinition) {
export function fillTypeDefinition(from: TypeDefinition, to: TypeDefinition) {
fillBaseTypeDefinition(from, to);

to.callSignatures = []
to.callSignatures = [];
from.callSignatures.forEach(val => {
to.callSignatures.push(val);
});
Expand All @@ -40,27 +40,27 @@ export function fillBaseTypeDefinition(from: BaseTypeDefinition, to: BaseTypeDef
else {
to.arrayElementType = cloneTypeDefinition(from.arrayElementType);
}
to.intersectionTypes = []
to.intersectionTypes = [];
from.intersectionTypes.forEach(val => {
to.intersectionTypes.push(cloneTypeDefinition(val));
});
to.unionTypes = []
to.unionTypes = [];
from.unionTypes.forEach(val => {
to.unionTypes.push(cloneTypeDefinition(val));
});
to.definitions = []
to.definitions = [];
from.definitions.forEach(val => {
to.definitions.push(val);
});
to.properties = []
to.properties = [];
from.properties.forEach(val => {
to.properties.push(val);
});
to.typeArguments = []
to.typeArguments = [];
from.typeArguments.forEach(val => {
to.typeArguments.push(cloneTypeDefinition(val));
});
to.text = from.text;
to._text = from._text;
}

export function cloneBaseExpressionDefinition(definition: BaseExpressionDefinition) {
Expand All @@ -73,7 +73,7 @@ export function cloneBaseExpressionDefinition(definition: BaseExpressionDefiniti
export function fillBaseExpressionDefinition(from: BaseExpressionDefinition, to: BaseExpressionDefinition) {
fillBaseDefinition(from, to);

to.text = from.text;
to._text = from._text;
}

export function cloneBaseDefinition(definition: BaseDefinition) {
Expand Down
2 changes: 1 addition & 1 deletion src/definitions/base/BaseDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export abstract class BaseDefinition {
Object.defineProperty(this, nameof<BaseDefinition>(d => d.__uniqueID), {
configurable: false,
enumerable: false,
writable: false,
writable: true,
value: ++BaseDefinition._uniqueID
});
}
Expand Down
2 changes: 2 additions & 0 deletions src/definitions/base/BaseFunctionDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export abstract class BaseFunctionDefinition<ParameterType extends BaseParameter

setUserDefinedTypeGuard(structure: UserDefinedTypeGuardStructure) {
this.userDefinedTypeGuard = new MainFactory().createStructureFactory().getUserDefinedTypeGuard(structure);
this.returnType = new MainFactory().createStructureFactory().getTypeFromText("any");
this.returnType._text = (this.userDefinedTypeGuard!.parameterName || "this") + " is " + this.userDefinedTypeGuard!.type.text;
return this;
}

Expand Down
15 changes: 13 additions & 2 deletions src/definitions/expression/TypeDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {DefinitionUtils} from "./../../utils";
import {cloneTypeDefinition} from "./../../cloneFunctions";
import {MainFactory} from "./../../factories";
import {DefinitionUtils} from "./../../utils";
import {cloneTypeDefinition, fillTypeDefinition} from "./../../cloneFunctions";
import {CallSignatureDefinition} from "./../general";
import {BaseTypeDefinition} from "./base";
import {TypeNodeDefinition} from "./TypeNodeDefinition";
Expand All @@ -8,6 +9,16 @@ export class TypeDefinition extends BaseTypeDefinition {
callSignatures: CallSignatureDefinition[] = [];
node: TypeNodeDefinition | null;

get text() {
return this._text;
}

set text(text: string) {
const structureFactory = new MainFactory().createStructureFactory();
const newDefinition = structureFactory.getTypeFromText(text);
fillTypeDefinition(newDefinition, this);
}

getCallSignature(searchFunction: (typeDefinition: CallSignatureDefinition) => boolean) {
return DefinitionUtils.getDefinitionFromListByFunc(this.callSignatures, searchFunction);
}
Expand Down
12 changes: 11 additions & 1 deletion src/definitions/expression/base/BaseExpressionDefinition.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import {BaseDefinition} from "./../../base";

export class BaseExpressionDefinition extends BaseDefinition {
text: string;
// ReSharper disable once InconsistentNaming
_text: string; // internal

// this is done because of how text is used in TypeDefinition
get text() {
return this._text;
}

set text(value: string) {
this._text = value;
}
}
11 changes: 1 addition & 10 deletions src/factories/TsFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,7 @@ export class TsFactory {
}

getTypeFromTypeNode(node: TsTypeNode) {
const definition = bindToDefinition(new binders.TsTypeBinder(this, node.getType(), node), new definitions.TypeDefinition());
this.createdTypesWithDefinition.push({
type: node.getType(),
definition
});
return definition;
}

getTypeFromOnlyTypeNode(node: TsTypeNode) {
return ;
return this.getType(node.getType(), node);
}

getType(type: TsType, node: TsTypeNode | null) {
Expand Down
8 changes: 7 additions & 1 deletion src/tests/definitions/base/baseFunctionDefinitionTests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as assert from "assert";
import {expect} from "chai";
import {FunctionDefinition} from "./../../../definitions";
import {runCallSignatureDefinitionTests} from "./../../testHelpers";
import {runCallSignatureDefinitionTests, runTypeDefinitionTests} from "./../../testHelpers";

describe("BaseFunctionDefinition", () => {
describe("#addCallSignature()", () => {
Expand Down Expand Up @@ -49,6 +49,9 @@ describe("BaseFunctionDefinition", () => {
});
expect(f.userDefinedTypeGuard!.parameterName).to.equal("paramname");
expect(f.userDefinedTypeGuard!.type.text).to.equal("BaseDefinition");
runTypeDefinitionTests(f.returnType, {
text: "paramname is BaseDefinition"
});
});

it("should create a user defined type guard without a parameter name that has the this type", () => {
Expand All @@ -58,6 +61,9 @@ describe("BaseFunctionDefinition", () => {
});
expect(f.userDefinedTypeGuard!.parameterName).to.equal("this");
expect(f.userDefinedTypeGuard!.type.text).to.equal("BaseDefinition");
runTypeDefinitionTests(f.returnType, {
text: "this is BaseDefinition"
});
});
});
});
14 changes: 14 additions & 0 deletions src/tests/definitions/expression/typeDefinitionTests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as assert from "assert";
import {TypeDefinition} from "./../../../definitions";
import {runTypeDefinitionTests} from "./../../testHelpers";

describe("TypeDefinition", () => {
describe("#getCallSignature()", () => {
Expand All @@ -10,4 +11,17 @@ describe("TypeDefinition", () => {
assert.equal(def.getCallSignature(c => c.parameters.length === 1), def.callSignatures[1]);
});
});

describe("text", () => {
const def = new TypeDefinition();
def.text = "MyClass | MyOtherClass";
runTypeDefinitionTests(def, {
text: "MyClass | MyOtherClass",
unionTypes: [{
text: "MyClass"
}, {
text: "MyOtherClass"
}]
});
});
});

0 comments on commit 05d1086

Please sign in to comment.