Skip to content

Commit 3d17581

Browse files
author
Denis Peshkov
committed
#1 Merge branch 'bugfix/problem_with_types_defined_in_external_libs'
2 parents 3e543e8 + 494a898 commit 3d17581

22 files changed

Lines changed: 525 additions & 50 deletions

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ x64/
2020
# NCrunch
2121
*.ncrunchsolution
2222
*.ncrunchproject
23-
_NCrunch_WebCompiler
23+
_NCrunch_WebCompiler
24+
*.d.ts

TypeScriptDefinitionGenerator.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
1212
EndProject
1313
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TypeScriptDefinitionGenerator", "src\TypeScriptDefinitionGenerator\TypeScriptDefinitionGenerator.csproj", "{F8E2F6C6-880A-4C64-A9EE-0CF04F9C3CC6}"
1414
EndProject
15+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TypeScriptDefinitionGenerator.Tests", "tests\TypeScriptDefinitionGenerator.Tests\TypeScriptDefinitionGenerator.Tests.csproj", "{5BCC614E-E6DD-4624-A05C-43CB8897262F}"
16+
EndProject
17+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClassLibrary1", "tests\ClassLibrary1\ClassLibrary1.csproj", "{4699A92D-640D-4528-AD93-5905AD307F72}"
18+
EndProject
19+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{0D3148DE-5554-4F5A-A5D7-E5770FB9A174}"
20+
EndProject
1521
Global
1622
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1723
Debug|Any CPU = Debug|Any CPU
@@ -22,6 +28,14 @@ Global
2228
{F8E2F6C6-880A-4C64-A9EE-0CF04F9C3CC6}.Debug|Any CPU.Build.0 = Debug|Any CPU
2329
{F8E2F6C6-880A-4C64-A9EE-0CF04F9C3CC6}.Release|Any CPU.ActiveCfg = Release|Any CPU
2430
{F8E2F6C6-880A-4C64-A9EE-0CF04F9C3CC6}.Release|Any CPU.Build.0 = Release|Any CPU
31+
{5BCC614E-E6DD-4624-A05C-43CB8897262F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
32+
{5BCC614E-E6DD-4624-A05C-43CB8897262F}.Debug|Any CPU.Build.0 = Debug|Any CPU
33+
{5BCC614E-E6DD-4624-A05C-43CB8897262F}.Release|Any CPU.ActiveCfg = Release|Any CPU
34+
{5BCC614E-E6DD-4624-A05C-43CB8897262F}.Release|Any CPU.Build.0 = Release|Any CPU
35+
{4699A92D-640D-4528-AD93-5905AD307F72}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36+
{4699A92D-640D-4528-AD93-5905AD307F72}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{4699A92D-640D-4528-AD93-5905AD307F72}.Release|Any CPU.ActiveCfg = Release|Any CPU
38+
{4699A92D-640D-4528-AD93-5905AD307F72}.Release|Any CPU.Build.0 = Release|Any CPU
2539
EndGlobalSection
2640
GlobalSection(SolutionProperties) = preSolution
2741
HideSolutionNode = FALSE

src/TypeScriptDefinitionGenerator/Commands/ToggleCustomTool.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.ComponentModel.Design;
66
using System.IO;
77
using System.Linq;
8+
using TypeScriptDefinitionGenerator.Helpers;
89
using Tasks = System.Threading.Tasks;
910

1011
namespace TypeScriptDefinitionGenerator
@@ -66,7 +67,7 @@ private void BeforeQueryStatus(object sender, EventArgs e)
6667
{
6768
if (_item.ContainingProject.IsKind(ProjectTypes.DOTNET_Core, ProjectTypes.ASPNET_5, ProjectTypes.WEBSITE_PROJECT))
6869
{
69-
string dtsFile = GenerationService.GenerateFileName(_item.FileNames[1]);
70+
string dtsFile = Utility.GenerateFileName(_item.FileNames[1]);
7071
button.Checked = File.Exists(dtsFile);
7172
}
7273
else
@@ -84,7 +85,7 @@ private void Execute(object sender, EventArgs e)
8485
// .NET Core and Website projects
8586
if (_item.ContainingProject.IsKind(ProjectTypes.DOTNET_Core, ProjectTypes.ASPNET_5, ProjectTypes.WEBSITE_PROJECT))
8687
{
87-
string dtsFile = GenerationService.GenerateFileName(_item.FileNames[1]);
88+
string dtsFile = Utility.GenerateFileName(_item.FileNames[1]);
8889
bool synOn = File.Exists(dtsFile);
8990

9091
if (synOn)

src/TypeScriptDefinitionGenerator/Generator/GenerationService.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
using System.ComponentModel.Composition;
77
using System.IO;
88
using System.Windows.Threading;
9+
using Microsoft.Internal.VisualStudio.PlatformUI;
10+
using TypeScriptDefinitionGenerator.Helpers;
911

1012
namespace TypeScriptDefinitionGenerator
1113
{
@@ -40,7 +42,7 @@ private void FileActionOccurred(object sender, TextDocumentFileActionEventArgs e
4042
return;
4143
_item = VSHelpers.GetProjectItem(e.FilePath);
4244
Options.ReadOptionOverrides(_item, false);
43-
string fileName = GenerationService.GenerateFileName(e.FilePath);
45+
string fileName = Utility.GenerateFileName(e.FilePath);
4446

4547
if (File.Exists(fileName))
4648
{
@@ -67,22 +69,10 @@ public static string ConvertToTypeScript(ProjectItem sourceItem)
6769
}
6870
}
6971

70-
public static string GenerateFileName(string sourceFile)
71-
{
72-
if (Options.WebEssentials2015)
73-
{
74-
return sourceFile + Constants.FileExtension;
75-
}
76-
else
77-
{
78-
return Path.ChangeExtension(sourceFile, Constants.FileExtension);
79-
}
80-
}
81-
8272
public static void CreateDtsFile(ProjectItem sourceItem)
8373
{
8474
string sourceFile = sourceItem.FileNames[1];
85-
string dtsFile = GenerationService.GenerateFileName(sourceFile);
75+
string dtsFile = Utility.GenerateFileName(sourceFile);
8676
string dts = ConvertToTypeScript(sourceItem);
8777

8878
VSHelpers.CheckFileOutOfSourceControl(dtsFile);

src/TypeScriptDefinitionGenerator/Generator/IntellisenseParser.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,12 @@
1111

1212
namespace TypeScriptDefinitionGenerator
1313
{
14-
public static class IntellisenseParser
14+
internal static class IntellisenseParser
1515
{
16-
private static string DefaultModuleName = Options.DefaultModuleName;
1716
private const string ModuleNameAttributeName = "TypeScriptModule";
1817
private static readonly Regex IsNumber = new Regex("^[0-9a-fx]+[ul]{0,2}$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
1918
private static Project _project;
2019

21-
//internal static class Ext
22-
//{
23-
// public const string TypeScript = ".d.ts";
24-
//}
25-
2620
internal static IEnumerable<IntellisenseObject> ProcessFile(ProjectItem item, HashSet<CodeClass> underProcess = null)
2721
{
2822
if (item.FileCodeModel == null || item.ContainingProject == null)
@@ -239,7 +233,7 @@ private static string GetDataContractName(CodeClass cc, string attrName)
239233

240234
private static string GetNamespace(CodeElements attrs)
241235
{
242-
if (attrs == null) return DefaultModuleName;
236+
if (attrs == null) return Options.DefaultModuleName;
243237

244238
var namespaceFromAttr = from a in attrs.Cast<CodeAttribute2>()
245239
where a.Name.EndsWith(ModuleNameAttributeName, StringComparison.OrdinalIgnoreCase)
@@ -248,7 +242,7 @@ from arg in a.Arguments.Cast<CodeAttributeArgument>()
248242
where !string.IsNullOrWhiteSpace(v)
249243
select v;
250244

251-
return namespaceFromAttr.FirstOrDefault() ?? DefaultModuleName;
245+
return namespaceFromAttr.FirstOrDefault() ?? Options.DefaultModuleName;
252246
}
253247

254248
private static IntellisenseType GetType(CodeClass rootElement, CodeTypeRef codeTypeRef, HashSet<string> traversedTypes, HashSet<string> references)
@@ -270,25 +264,37 @@ private static IntellisenseType GetType(CodeClass rootElement, CodeTypeRef codeT
270264

271265
try
272266
{
273-
274267
var codeClass = effectiveTypeRef.CodeType as CodeClass2;
275268
var codeEnum = effectiveTypeRef.CodeType as CodeEnum;
276269
var isPrimitive = IsPrimitive(effectiveTypeRef);
270+
//VSHelpers.WriteOnBuildDebugWindow($"###{effectiveTypeRef.CodeType.GetType().FullName}");
277271

278272
var result = new IntellisenseType
279273
{
280274
IsArray = !isDictionary && (isArray || isCollection),
281275
IsDictionary = isDictionary,
282276
CodeName = effectiveTypeRef.AsString
283277
};
284-
if (effectiveTypeRef.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType && effectiveTypeRef.CodeType.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject)
278+
279+
//VSHelpers.WriteOnBuildDebugWindow($"#{result.CodeName}#{result.TypeScriptName}#{effectiveTypeRef.AsString}#{effectiveTypeRef.AsFullName}#{effectiveTypeRef.CodeType}");
280+
//VSHelpers.WriteOnBuildDebugWindow($"##{effectiveTypeRef.TypeKind}##{vsCMTypeRef.vsCMTypeRefCodeType}##{effectiveTypeRef.CodeType.InfoLocation}##{vsCMInfoLocation.vsCMInfoLocationProject}");
281+
if (effectiveTypeRef.TypeKind == vsCMTypeRef.vsCMTypeRefCodeType)
285282
{
286-
result.ClientSideReferenceName = (codeClass != null && HasIntellisense(codeClass.ProjectItem, references) ? (GetNamespace(codeClass) + "." + Utility.CamelCaseClassName(GetClassName(codeClass))) : null) ??
287-
(codeEnum != null && HasIntellisense(codeEnum.ProjectItem, references) ? (GetNamespace(codeEnum) + "." + Utility.CamelCaseClassName(codeEnum.Name)) : null);
283+
var hasIntellisense = Options.IgnoreIntellisense;
284+
if (effectiveTypeRef.CodeType.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject)
285+
{
286+
if (codeClass != null)
287+
hasIntellisense = HasIntellisense(codeClass.ProjectItem, references);
288+
if (codeEnum != null)
289+
hasIntellisense = HasIntellisense(codeEnum.ProjectItem, references);
290+
}
291+
292+
result.ClientSideReferenceName = (codeClass != null && hasIntellisense ? (GetNamespace(codeClass) + "." + Utility.CamelCaseClassName(GetClassName(codeClass))) : null) ??
293+
(codeEnum != null && hasIntellisense ? (GetNamespace(codeEnum) + "." + Utility.CamelCaseClassName(codeEnum.Name)) : null);
288294
}
289295
else result.ClientSideReferenceName = null;
290296

291-
if (!isPrimitive && codeClass != null && !traversedTypes.Contains(effectiveTypeRef.CodeType.FullName) && !isCollection)
297+
if (!isPrimitive && (codeClass != null || codeEnum != null) && !traversedTypes.Contains(effectiveTypeRef.CodeType.FullName) && !isCollection)
292298
{
293299
traversedTypes.Add(effectiveTypeRef.CodeType.FullName);
294300
result.Shape = GetProperties(effectiveTypeRef.CodeType.Members, traversedTypes, references).ToList();
@@ -373,7 +379,7 @@ private static bool HasIntellisense(ProjectItem projectItem, HashSet<string> ref
373379
{
374380
for (short i = 0; i < projectItem.FileCount; i++)
375381
{
376-
var fileName = GenerationService.GenerateFileName(projectItem.FileNames[i]);
382+
var fileName = Utility.GenerateFileName(projectItem.FileNames[i]);
377383

378384
references.Add(fileName);
379385
return true;

src/TypeScriptDefinitionGenerator/Generator/IntellisenseProperty.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ public IntellisenseProperty()
88
{
99

1010
}
11+
1112
public IntellisenseProperty(IntellisenseType type, string propertyName)
1213
{
1314
Type = type;
@@ -18,11 +19,11 @@ public IntellisenseProperty(IntellisenseType type, string propertyName)
1819

1920
public string NameWithOption { get { return (this.Type != null && this.Type.IsOptional) ? this.Name + "?" : this.Name; } }
2021

21-
[SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods",
22-
Justification = "Unambiguous in this context.")]
22+
[SuppressMessage("Microsoft.Naming", "CA1721:PropertyNamesShouldNotMatchGetMethods", Justification = "Unambiguous in this context.")]
2323
public IntellisenseType Type { get; set; }
2424

2525
public string Summary { get; set; }
26+
2627
public string InitExpression { get; set; }
2728
}
2829
}

src/TypeScriptDefinitionGenerator/Helpers/Utility.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
using System.Globalization;
2+
using System.IO;
23

34
namespace TypeScriptDefinitionGenerator.Helpers
45
{
56
internal static class Utility
67
{
8+
public static string GenerateFileName(string sourceFile)
9+
{
10+
if (Options.WebEssentials2015)
11+
{
12+
return sourceFile + Constants.FileExtension;
13+
}
14+
else
15+
{
16+
return Path.ChangeExtension(sourceFile, Constants.FileExtension);
17+
}
18+
}
19+
720
public static string CamelCaseClassName(string name)
821
{
922
if (Options.CamelCaseTypeNames)

src/TypeScriptDefinitionGenerator/Helpers/VSHelpers.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ internal static void WriteOnBuildOutputWindow(string text)
5454
WriteOnOutputWindow(text, Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.BuildOutputPane_guid);
5555
}
5656

57+
internal static void WriteOnBuildDebugWindow(string text)
58+
{
59+
#if DEBUG
60+
Console.WriteLine(text);
61+
WriteOnOutputWindow(text, Microsoft.VisualStudio.VSConstants.OutputWindowPaneGuid.DebugPane_guid);
62+
#endif
63+
}
64+
5765
internal static void WriteOnOutputWindow(string text, Guid guidBuildOutput)
5866
{
5967
if (!text.EndsWith(Environment.NewLine))

src/TypeScriptDefinitionGenerator/Options.cs

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@ public class OptionsDialogPage : DialogPage
1212
internal const bool _defCamelCaseEnumerationValues = false;
1313
internal const bool _defCamelCasePropertyNames = true;
1414
internal const bool _defCamelCaseTypeNames = false;
15+
16+
internal const bool _defWebEssentials2015 = false;
17+
1518
internal const bool _defClassInsteadOfInterface = false;
16-
internal const bool _defGlobalScope = false;
17-
internal const bool _defExport = false;
18-
internal const bool _defWebEssentials2015 = true;
1919
internal const string _defModuleName = "Server.Dtos";
20+
internal const bool _defExport = false;
21+
internal const bool _defGlobalScope = false;
22+
internal const bool _defIgnoreIntellisense = true;
2023

2124
[Category("Casing")]
2225
[DisplayName("Camel case enum values")]
@@ -56,7 +59,12 @@ public class OptionsDialogPage : DialogPage
5659
[DefaultValue(_defExport)]
5760
public bool Export { get; set; } = _defExport;
5861

59-
62+
[Category("Settings")]
63+
[DisplayName("Ignore intellisense")]
64+
[Description("Ignore intellisense for client side reference names")]
65+
[DefaultValue(_defIgnoreIntellisense)]
66+
public bool IgnoreIntellisense { get; set; } = _defIgnoreIntellisense;
67+
6068
[Category("Compatibilty")]
6169
[DisplayName("Web Esentials 2015 file names")]
6270
[Description("Web Essentials 2015 format is <filename>.cs.d.ts instead of <filename>.d.ts")]
@@ -68,63 +76,71 @@ public class Options
6876
{
6977
const string OVERRIDE_FILE_NAME = "tsdefgen.json";
7078
static OptionsOverride overrides { get; set; } = null;
71-
static public bool CamelCaseEnumerationValues
79+
public static bool CamelCaseEnumerationValues
7280
{
7381
get
7482
{
7583
return overrides != null ? overrides.CamelCaseEnumerationValues : DtsPackage.Options.CamelCaseEnumerationValues;
7684
}
7785
}
7886

79-
static public bool CamelCasePropertyNames
87+
public static bool CamelCasePropertyNames
8088
{
8189
get
8290
{
8391
return overrides != null ? overrides.CamelCasePropertyNames : DtsPackage.Options.CamelCasePropertyNames;
8492
}
8593
}
8694

87-
static public bool CamelCaseTypeNames
95+
public static bool CamelCaseTypeNames
8896
{
8997
get
9098
{
9199
return overrides != null ? overrides.CamelCaseTypeNames : DtsPackage.Options.CamelCaseTypeNames;
92100
}
93101
}
94-
//todo:设置为服务器命名空间
95-
static public string DefaultModuleName
102+
//todo: set to server namespace
103+
public static string DefaultModuleName
96104
{
97105
get
98106
{
99107
return overrides != null ? overrides.DefaultModuleName : DtsPackage.Options.DefaultModuleName;
100108
}
101109
}
102110

103-
static public bool ClassInsteadOfInterface
111+
public static bool ClassInsteadOfInterface
104112
{
105113
get
106114
{
107115
return overrides != null ? overrides.ClassInsteadOfInterface : DtsPackage.Options.ClassInsteadOfInterface;
108116
}
109117
}
110118

111-
static public bool GlobalScope
119+
public static bool GlobalScope
112120
{
113121
get
114122
{
115123
return overrides != null ? overrides.GlobalScope : DtsPackage.Options.GlobalScope;
116124
}
117125
}
118126

119-
static public bool Export
127+
public static bool Export
120128
{
121129
get
122130
{
123131
return overrides != null ? overrides.Export : DtsPackage.Options.Export;
124132
}
125133
}
126134

127-
static public bool WebEssentials2015
135+
public static bool IgnoreIntellisense
136+
{
137+
get
138+
{
139+
return overrides != null ? overrides.IgnoreIntellisense : DtsPackage.Options.IgnoreIntellisense;
140+
}
141+
}
142+
143+
public static bool WebEssentials2015
128144
{
129145
get
130146
{
@@ -184,6 +200,10 @@ public static void ReadOptionOverrides(ProjectItem sourceItem, bool display = tr
184200
}
185201
}
186202

203+
internal static void SetOptionsOverrides(OptionsOverride optionsOverride)
204+
{
205+
overrides = optionsOverride;
206+
}
187207
}
188208

189209
internal class OptionsOverride
@@ -209,6 +229,9 @@ internal class OptionsOverride
209229
// [JsonRequired]
210230
public bool Export { get; set; } = OptionsDialogPage._defExport;
211231

232+
// [JsonRequired]
233+
public bool IgnoreIntellisense { get; set; } = OptionsDialogPage._defIgnoreIntellisense;
234+
212235
// [JsonRequired]
213236
public bool WebEssentials2015 { get; set; } = OptionsDialogPage._defWebEssentials2015;
214237

0 commit comments

Comments
 (0)