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

Commit

Permalink
Adding support for partial classes
Browse files Browse the repository at this point in the history
Updating examples and output with the results of the partial class.
Renaming CustomType to InterfaceType - it is either an "interface" (that is, a class with the interface attribute), or an "unknown" type, ie. not built nor a class with the interface attribute.
  • Loading branch information
cskeppstedt committed Aug 27, 2013
1 parent f5e65d6 commit 47b42c5
Show file tree
Hide file tree
Showing 15 changed files with 131 additions and 84 deletions.
9 changes: 9 additions & 0 deletions T4TS.Example/Models/Partial.First.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System;

namespace T4TS.Example.Models
{
partial class Partial
{
public string FromFirstClass { get; set; }
}
}
11 changes: 11 additions & 0 deletions T4TS.Example/Models/Partial.Second.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace T4TS.Example.Models
{
[TypeScriptInterface]
partial class Partial
{
public string FromSecondClass { get; set; }
public bool? AlsoSecondClass { get; set; }
}
}
16 changes: 12 additions & 4 deletions T4TS.Example/Scripts/App/Test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@

module App {
export class Test {

constructor () {
private partialClass: T4TS.Partial;

constructor() {

this.partialClass = {
FromFirstClass: '',
FromSecondClass: '',
AlsoSecondClass: true
};

// Make an AJAX post and get some data from the server.
// In the callback, you can specify that the data is of a certain type:
$.post('./example', {}, (data: Fooz.Foobar) => {
$.post('./example', {}, (data: Fooz.IFoobar) => {

// Intellisense support for the properties:
alert(data.NestedObjectArr[0].Name);
alert(data.Recursive.OverrideAll ? "1" : "0");
Expand Down
2 changes: 2 additions & 0 deletions T4TS.Example/T4TS.Example.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
<Compile Include="Models\InheritanceTest3.cs" />
<Compile Include="Models\InheritanceTest4.cs" />
<Compile Include="Models\Inherited.cs" />
<Compile Include="Models\Partial.First.cs" />
<Compile Include="Models\Partial.Second.cs" />
<Compile Include="Models\TestClass.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Content Include="T4TS.d.ts">
Expand Down
9 changes: 9 additions & 0 deletions T4TS.Example/T4TS.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ module T4TS {
TwoDimList: number[][];
[index: number]: Barfoo;
}
/** Generated from T4TS.Example.Models.Partial **/
export interface Partial {
FromFirstClass: string;
}
/** Generated from T4TS.Example.Models.Partial **/
export interface Partial {
FromSecondClass: string;
AlsoSecondClass?: any;
}
}
12 changes: 7 additions & 5 deletions T4TS.Example/T4TS.tt
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@
<#@ assembly name="System.Core" #>
<#@ assembly name="Microsoft.VisualStudio.Shell.Interop.8.0" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="EnvDTE80" #>
<#@ assembly name="$(SolutionDir)\\T4TS\\bin\\Debug\\T4TS.Dll" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="T4TS" #>
<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ assembly name="$(SolutionDir)\\T4TS\\bin\\Debug\\T4TS.Dll" #>
<#@ Include File="T4TS.tt.settings.t4" #>
<#=
T4TS.OutputFormatter.GetOutput(GetDataToRender()) #><#+

List<T4TS.TypeScriptModule> GetDataToRender() {
List<TypeScriptModule> GetDataToRender() {
DTE dte = null;

// Get the DTE service from the host
Expand All @@ -31,15 +34,15 @@ List<T4TS.TypeScriptModule> GetDataToRender() {
throw new Exception("Could not find the VS project containing the T4TS file.");

// Read settings from T4TS.tt.settings.tt
var settings = new T4TS.Settings
var settings = new Settings
{
DefaultModule = DefaultModule,
DefaultOptional = DefaultOptional,
DefaultCamelCaseMemberNames = DefaultCamelCaseMemberNames,
DefaultInterfaceNamePrefix = DefaultInterfaceNamePrefix
};

var generator = new T4TS.CodeTraverser(project, settings);
var generator = new CodeTraverser(project, settings);

return generator.GetAllInterfaces().ToList();
}
Expand All @@ -60,5 +63,4 @@ Project GetProjectContainingT4File(DTE dte) {

return projectItem.ContainingProject;
}

#>
24 changes: 10 additions & 14 deletions T4TS/CodeTraverser.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
using EnvDTE;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace T4TS
{
Expand All @@ -32,6 +28,7 @@ public CodeTraverser(Project project, Settings settings)
public TypeContext BuildContext()
{
var typeContext = new TypeContext();
var partialClasses = new Dictionary<string, CodeClass>();

new ProjectTraverser(this.Project, (ns) =>
{
Expand All @@ -42,9 +39,10 @@ public TypeContext BuildContext()
return;

var values = GetInterfaceValues(codeClass, attribute);
var customType = new CustomType(GetInterfaceName(values), values.Module);
var interfaceType = new InterfaceType(values);

typeContext.AddCustomType(codeClass.FullName, customType);
if (!typeContext.ContainsInterfaceType(codeClass.FullName))
typeContext.AddInterfaceType(codeClass.FullName, interfaceType);
});
});

Expand All @@ -61,15 +59,12 @@ public IEnumerable<TypeScriptModule> GetAllInterfaces()
{
new NamespaceTraverser(ns, (codeClass) =>
{
if (codeClass.Attributes == null || codeClass.Attributes.Count == 0)
InterfaceType interfaceType;
if (!typeContext.TryGetInterfaceType(codeClass.FullName, out interfaceType))
return;

CodeAttribute attribute;
if (!TryGetAttribute(codeClass.Attributes, InterfaceAttributeFullName, out attribute))
return;

var values = GetInterfaceValues(codeClass, attribute);

var values = interfaceType.AttributeValues;

TypeScriptModule module;
if (!byModuleName.TryGetValue(values.Module, out module))
{
Expand Down Expand Up @@ -123,6 +118,7 @@ private TypeScriptInterface BuildInterface(CodeClass codeClass, TypeScriptInterf
if (TryGetMember(property, typeContext, out member))
tsInterface.Members.Add(member);
});

return tsInterface;
}

Expand Down Expand Up @@ -190,7 +186,7 @@ private bool TryGetMember(CodeProperty property, TypeContext typeContext, out Ty
Optional = values.Optional,
Type = (string.IsNullOrWhiteSpace(values.Type))
? typeContext.GetTypeScriptType(getter.Type)
: new CustomType(values.Type)
: new InterfaceType(values.Type)
};

if (values.CamelCase && values.Name == null)
Expand Down
5 changes: 0 additions & 5 deletions T4TS/Outputs/TypeScriptInterfaceAttributeValues.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EnvDTE;

namespace T4TS
{
Expand Down
2 changes: 1 addition & 1 deletion T4TS/T4TS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<Compile Include="Outputs\TypeScriptModule.cs" />
<Compile Include="Types\ArrayType.cs" />
<Compile Include="Types\BoolType.cs" />
<Compile Include="Types\CustomType.cs" />
<Compile Include="Types\InterfaceType.cs" />
<Compile Include="Types\NullableType.cs" />
<Compile Include="Types\NumberType.cs" />
<Compile Include="Types\StringType.cs" />
Expand Down
3 changes: 0 additions & 3 deletions T4TS/Traversal/ClassTraverser.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using EnvDTE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T4TS
{
Expand Down
3 changes: 0 additions & 3 deletions T4TS/Traversal/NamespaceTraverser.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using EnvDTE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T4TS
{
Expand Down
3 changes: 0 additions & 3 deletions T4TS/Traversal/ProjectTraverser.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using EnvDTE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T4TS
{
Expand Down
26 changes: 14 additions & 12 deletions T4TS/TypeContext.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using EnvDTE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T4TS
{
Expand All @@ -18,18 +15,23 @@ public class TypeContext
private static readonly string nullableTypeStart = "System.Nullable<";

/// <summary>
/// Lookup table for "custom types", ie. non-builtin types. Keyed on the FullName of the type.
/// Lookup table for "interface types", ie. non-builtin types (typically classes or unknown types). Keyed on the FullName of the type.
/// </summary>
private Dictionary<string, CustomType> customTypes = new Dictionary<string, CustomType>();
private Dictionary<string, InterfaceType> interfaceTypes = new Dictionary<string, InterfaceType>();

public void AddCustomType(string typeFullName, CustomType customType)
public void AddInterfaceType(string typeFullName, InterfaceType interfaceType)
{
customTypes.Add(typeFullName, customType);
interfaceTypes.Add(typeFullName, interfaceType);
}

public bool TryGetCustomType(string typeFullName, out CustomType customType)
public bool TryGetInterfaceType(string typeFullName, out InterfaceType interfaceType)
{
return customTypes.TryGetValue(typeFullName, out customType);
return interfaceTypes.TryGetValue(typeFullName, out interfaceType);
}

public bool ContainsInterfaceType(string typeFullName)
{
return interfaceTypes.ContainsKey(typeFullName);
}

public TypescriptType GetTypeScriptType(CodeTypeRef codeType)
Expand Down Expand Up @@ -80,9 +82,9 @@ private ArrayType TryResolveEnumerableType(string typeFullName)

public TypescriptType GetTypeScriptType(string typeFullName)
{
CustomType customType;
if (customTypes.TryGetValue(typeFullName, out customType))
return customType;
InterfaceType interfaceType;
if (interfaceTypes.TryGetValue(typeFullName, out interfaceType))
return interfaceType;

if (IsGenericEnumerable(typeFullName))
{
Expand Down
34 changes: 0 additions & 34 deletions T4TS/Types/CustomType.cs

This file was deleted.

56 changes: 56 additions & 0 deletions T4TS/Types/InterfaceType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace T4TS
{
public class InterfaceType : TypescriptType
{
public TypeScriptInterfaceAttributeValues AttributeValues { get; private set; }

public string QualifedModule
{
get
{
if (AttributeValues == null)
return null;

return AttributeValues.Module;
}
}

public override string Name
{
get
{
if (!string.IsNullOrEmpty(AttributeValues.NamePrefix))
return AttributeValues.NamePrefix + AttributeValues.Name;

return AttributeValues.Name;
}
}

public InterfaceType(TypeScriptInterfaceAttributeValues values)
{
AttributeValues = values;
}

public InterfaceType(string name)
{
AttributeValues = new TypeScriptInterfaceAttributeValues
{
Name = name
};
}

public override string ToString()
{
if (string.IsNullOrWhiteSpace(QualifedModule))
return base.ToString();

return QualifedModule + "." + base.ToString();
}
}
}

0 comments on commit 47b42c5

Please sign in to comment.