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

Commit 47b42c5

Browse files
committed
Adding support for partial classes
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.
1 parent f5e65d6 commit 47b42c5

15 files changed

+131
-84
lines changed

T4TS.Example/Models/Partial.First.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace T4TS.Example.Models
4+
{
5+
partial class Partial
6+
{
7+
public string FromFirstClass { get; set; }
8+
}
9+
}

T4TS.Example/Models/Partial.Second.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System;
2+
3+
namespace T4TS.Example.Models
4+
{
5+
[TypeScriptInterface]
6+
partial class Partial
7+
{
8+
public string FromSecondClass { get; set; }
9+
public bool? AlsoSecondClass { get; set; }
10+
}
11+
}

T4TS.Example/Scripts/App/Test.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33

44
module App {
55
export class Test {
6-
7-
constructor () {
6+
private partialClass: T4TS.Partial;
7+
8+
constructor() {
9+
10+
this.partialClass = {
11+
FromFirstClass: '',
12+
FromSecondClass: '',
13+
AlsoSecondClass: true
14+
};
15+
816
// Make an AJAX post and get some data from the server.
917
// In the callback, you can specify that the data is of a certain type:
10-
$.post('./example', {}, (data: Fooz.Foobar) => {
11-
18+
$.post('./example', {}, (data: Fooz.IFoobar) => {
19+
1220
// Intellisense support for the properties:
1321
alert(data.NestedObjectArr[0].Name);
1422
alert(data.Recursive.OverrideAll ? "1" : "0");

T4TS.Example/T4TS.Example.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
<Compile Include="Models\InheritanceTest3.cs" />
4747
<Compile Include="Models\InheritanceTest4.cs" />
4848
<Compile Include="Models\Inherited.cs" />
49+
<Compile Include="Models\Partial.First.cs" />
50+
<Compile Include="Models\Partial.Second.cs" />
4951
<Compile Include="Models\TestClass.cs" />
5052
<Compile Include="Properties\AssemblyInfo.cs" />
5153
<Content Include="T4TS.d.ts">

T4TS.Example/T4TS.d.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,13 @@ module T4TS {
5656
TwoDimList: number[][];
5757
[index: number]: Barfoo;
5858
}
59+
/** Generated from T4TS.Example.Models.Partial **/
60+
export interface Partial {
61+
FromFirstClass: string;
62+
}
63+
/** Generated from T4TS.Example.Models.Partial **/
64+
export interface Partial {
65+
FromSecondClass: string;
66+
AlsoSecondClass?: any;
67+
}
5968
}

T4TS.Example/T4TS.tt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
<#@ assembly name="System.Core" #>
44
<#@ assembly name="Microsoft.VisualStudio.Shell.Interop.8.0" #>
55
<#@ assembly name="EnvDTE" #>
6+
<#@ assembly name="EnvDTE80" #>
7+
<#@ assembly name="$(SolutionDir)\\T4TS\\bin\\Debug\\T4TS.Dll" #>
68
<#@ import namespace="System.Collections.Generic" #>
79
<#@ import namespace="System.Linq" #>
10+
<#@ import namespace="System.Text" #>
811
<#@ import namespace="EnvDTE" #>
12+
<#@ import namespace="T4TS" #>
913
<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>
1014
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
11-
<#@ assembly name="$(SolutionDir)\\T4TS\\bin\\Debug\\T4TS.Dll" #>
1215
<#@ Include File="T4TS.tt.settings.t4" #>
1316
<#=
1417
T4TS.OutputFormatter.GetOutput(GetDataToRender()) #><#+
1518

16-
List<T4TS.TypeScriptModule> GetDataToRender() {
19+
List<TypeScriptModule> GetDataToRender() {
1720
DTE dte = null;
1821

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

3336
// Read settings from T4TS.tt.settings.tt
34-
var settings = new T4TS.Settings
37+
var settings = new Settings
3538
{
3639
DefaultModule = DefaultModule,
3740
DefaultOptional = DefaultOptional,
3841
DefaultCamelCaseMemberNames = DefaultCamelCaseMemberNames,
3942
DefaultInterfaceNamePrefix = DefaultInterfaceNamePrefix
4043
};
4144

42-
var generator = new T4TS.CodeTraverser(project, settings);
45+
var generator = new CodeTraverser(project, settings);
4346

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

6164
return projectItem.ContainingProject;
6265
}
63-
6466
#>

T4TS/CodeTraverser.cs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
using EnvDTE;
22
using System;
33
using System.Collections.Generic;
4-
using System.Diagnostics;
54
using System.Linq;
6-
using System.Reflection;
7-
using System.Text;
8-
using System.Threading.Tasks;
95

106
namespace T4TS
117
{
@@ -32,6 +28,7 @@ public CodeTraverser(Project project, Settings settings)
3228
public TypeContext BuildContext()
3329
{
3430
var typeContext = new TypeContext();
31+
var partialClasses = new Dictionary<string, CodeClass>();
3532

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

4441
var values = GetInterfaceValues(codeClass, attribute);
45-
var customType = new CustomType(GetInterfaceName(values), values.Module);
42+
var interfaceType = new InterfaceType(values);
4643

47-
typeContext.AddCustomType(codeClass.FullName, customType);
44+
if (!typeContext.ContainsInterfaceType(codeClass.FullName))
45+
typeContext.AddInterfaceType(codeClass.FullName, interfaceType);
4846
});
4947
});
5048

@@ -61,15 +59,12 @@ public IEnumerable<TypeScriptModule> GetAllInterfaces()
6159
{
6260
new NamespaceTraverser(ns, (codeClass) =>
6361
{
64-
if (codeClass.Attributes == null || codeClass.Attributes.Count == 0)
62+
InterfaceType interfaceType;
63+
if (!typeContext.TryGetInterfaceType(codeClass.FullName, out interfaceType))
6564
return;
6665

67-
CodeAttribute attribute;
68-
if (!TryGetAttribute(codeClass.Attributes, InterfaceAttributeFullName, out attribute))
69-
return;
70-
71-
var values = GetInterfaceValues(codeClass, attribute);
72-
66+
var values = interfaceType.AttributeValues;
67+
7368
TypeScriptModule module;
7469
if (!byModuleName.TryGetValue(values.Module, out module))
7570
{
@@ -123,6 +118,7 @@ private TypeScriptInterface BuildInterface(CodeClass codeClass, TypeScriptInterf
123118
if (TryGetMember(property, typeContext, out member))
124119
tsInterface.Members.Add(member);
125120
});
121+
126122
return tsInterface;
127123
}
128124

@@ -190,7 +186,7 @@ private bool TryGetMember(CodeProperty property, TypeContext typeContext, out Ty
190186
Optional = values.Optional,
191187
Type = (string.IsNullOrWhiteSpace(values.Type))
192188
? typeContext.GetTypeScriptType(getter.Type)
193-
: new CustomType(values.Type)
189+
: new InterfaceType(values.Type)
194190
};
195191

196192
if (values.CamelCase && values.Name == null)

T4TS/Outputs/TypeScriptInterfaceAttributeValues.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
6-
using EnvDTE;
72

83
namespace T4TS
94
{

T4TS/T4TS.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<Compile Include="Outputs\TypeScriptModule.cs" />
6262
<Compile Include="Types\ArrayType.cs" />
6363
<Compile Include="Types\BoolType.cs" />
64-
<Compile Include="Types\CustomType.cs" />
64+
<Compile Include="Types\InterfaceType.cs" />
6565
<Compile Include="Types\NullableType.cs" />
6666
<Compile Include="Types\NumberType.cs" />
6767
<Compile Include="Types\StringType.cs" />

T4TS/Traversal/ClassTraverser.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using EnvDTE;
22
using System;
3-
using System.Collections.Generic;
43
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
74

85
namespace T4TS
96
{

0 commit comments

Comments
 (0)