Skip to content

Commit d4d025e

Browse files
author
Denis Peshkov
committed
- refactoring
- removed Export Option (useless, because not needed for modules and required without them) - renamed GlobalScope Option into DeclareModule, because it is more clear and transparent name
1 parent 81e76df commit d4d025e

8 files changed

Lines changed: 69 additions & 88 deletions

File tree

src/TypeScriptDefinitionGenerator/Constants.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
{
33
public static class Constants
44
{
5-
public const string FileExtension = ".generated.d.ts";
65
public static string[] SupportedSourceExtensions { get; } = { ".cs" };
76
}
87
}

src/TypeScriptDefinitionGenerator/Generator/DtsGenerator.cs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Runtime.InteropServices;
66
using System.Text;
7+
using TypeScriptDefinitionGenerator.Helpers;
78

89
namespace TypeScriptDefinitionGenerator
910
{
@@ -17,14 +18,7 @@ public sealed class DtsGenerator : BaseCodeGeneratorWithSite
1718

1819
public override string GetDefaultExtension()
1920
{
20-
if (Options.WebEssentials2015)
21-
{
22-
return this.originalExt + Constants.FileExtension;
23-
}
24-
else
25-
{
26-
return Constants.FileExtension;
27-
}
21+
return Utility.GetDefaultExtension(this.originalExt);
2822
}
2923

3024
protected override byte[] GenerateCode(string inputFileName, string inputFileContent)

src/TypeScriptDefinitionGenerator/Generator/IntellisenseType.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,13 @@ public class IntellisenseType
3333
/// </summary>
3434
public IEnumerable<IntellisenseProperty> Shape { get; set; }
3535

36-
public bool IsKnownType
37-
{
38-
get { return TypeScriptName != "any"; }
39-
}
36+
public bool IsKnownType { get { return TypeScriptName != "any"; } }
4037

4138
public string TypeScriptName
4239
{
4340
get
4441
{
45-
if (IsDictionary)
46-
return GetKVPTypes();
47-
return GetTargetName(CodeName, false);
42+
return IsDictionary ? GetKVPTypes() : GetTargetName(CodeName, false);
4843
}
4944
}
5045

src/TypeScriptDefinitionGenerator/Generator/IntellisenseWriter.cs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,42 +22,32 @@ public static string WriteTypeScript(IEnumerable<IntellisenseObject> objects)
2222

2323
foreach (var ns in objects.GroupBy(o => o.Namespace))
2424
{
25-
if (!Options.GlobalScope)
25+
if (Options.DeclareModule)
2626
{
2727
sb.AppendFormat("declare module {0} {{\r\n", ns.Key);
2828
}
2929

30+
string export = !Options.DeclareModule ? "export " : string.Empty;
31+
string prefixModule = Options.DeclareModule ? "\t" : String.Empty;
32+
3033
foreach (IntellisenseObject io in ns)
3134
{
3235
if (!string.IsNullOrEmpty(io.Summary))
33-
sb.AppendLine("\t/** " + _whitespaceTrimmer.Replace(io.Summary, "") + " */");
36+
sb.Append(prefixModule).AppendLine("/** " + _whitespaceTrimmer.Replace(io.Summary, "") + " */");
3437

3538
if (io.IsEnum)
3639
{
37-
string export = Options.Export ? "export " : string.Empty;
38-
sb.AppendLine("\t" + export + "const enum " + Utility.CamelCaseClassName(io.Name) + " {");
40+
string type = "enum ";
41+
sb.Append(prefixModule).Append(export).Append(type).Append(Utility.CamelCaseClassName(io.Name)).Append(" ");
3942

40-
foreach (var p in io.Properties)
41-
{
42-
WriteTypeScriptComment(p, sb);
43-
44-
if (p.InitExpression != null)
45-
{
46-
sb.AppendLine("\t\t" + Utility.CamelCaseEnumValue(p.Name) + " = " + CleanEnumInitValue(p.InitExpression) + ",");
47-
}
48-
else
49-
{
50-
sb.AppendLine("\t\t" + Utility.CamelCaseEnumValue(p.Name) + ",");
51-
}
52-
}
53-
54-
sb.AppendLine("\t}");
43+
sb.AppendLine("{");
44+
WriteTSEnumDefinition(sb, prefixModule + "\t", io.Properties);
45+
sb.Append(prefixModule).AppendLine("}");
5546
}
5647
else
5748
{
5849
string type = Options.ClassInsteadOfInterface ? "class " : "interface ";
59-
string export = Options.Export ? "export " : string.Empty;
60-
sb.Append("\t").Append(export).Append(type).Append(Utility.CamelCaseClassName(io.Name)).Append(" ");
50+
sb.Append(prefixModule).Append(export).Append(type).Append(Utility.CamelCaseClassName(io.Name)).Append(" ");
6151

6252
if (!string.IsNullOrEmpty(io.BaseName))
6353
{
@@ -69,12 +59,13 @@ public static string WriteTypeScript(IEnumerable<IntellisenseObject> objects)
6959
sb.Append(Utility.CamelCaseClassName(io.BaseName)).Append(" ");
7060
}
7161

72-
WriteTSInterfaceDefinition(sb, "\t", io.Properties);
73-
sb.AppendLine();
62+
sb.AppendLine("{");
63+
WriteTSInterfaceDefinition(sb, prefixModule + "\t", io.Properties);
64+
sb.Append(prefixModule).AppendLine("}");
7465
}
7566
}
7667

77-
if (!Options.GlobalScope)
68+
if (Options.DeclareModule)
7869
{
7970
sb.AppendLine("}");
8071
}
@@ -93,34 +84,47 @@ private static string CleanEnumInitValue(string value)
9384
}
9485

9586

96-
private static void WriteTypeScriptComment(IntellisenseProperty p, StringBuilder sb)
87+
private static void WriteTypeScriptComment(IntellisenseProperty p, StringBuilder sb, string prefix)
9788
{
9889
if (string.IsNullOrEmpty(p.Summary)) return;
99-
sb.AppendLine("\t\t/** " + _whitespaceTrimmer.Replace(p.Summary, "") + " */");
90+
sb.Append(prefix).AppendLine("/** " + _whitespaceTrimmer.Replace(p.Summary, "") + " */");
10091
}
10192

102-
private static void WriteTSInterfaceDefinition(StringBuilder sb, string prefix,
103-
IEnumerable<IntellisenseProperty> props)
93+
private static void WriteTSEnumDefinition(StringBuilder sb, string prefix, IEnumerable<IntellisenseProperty> props)
10494
{
105-
sb.AppendLine("{");
95+
foreach (var p in props)
96+
{
97+
WriteTypeScriptComment(p, sb, prefix);
10698

99+
if (p.InitExpression != null)
100+
{
101+
sb.AppendLine(prefix + Utility.CamelCaseEnumValue(p.Name) + " = " + CleanEnumInitValue(p.InitExpression) + ",");
102+
}
103+
else
104+
{
105+
sb.AppendLine(prefix + Utility.CamelCaseEnumValue(p.Name) + ",");
106+
}
107+
}
108+
109+
}
110+
111+
private static void WriteTSInterfaceDefinition(StringBuilder sb, string prefix, IEnumerable<IntellisenseProperty> props)
112+
{
107113
foreach (var p in props)
108114
{
109-
WriteTypeScriptComment(p, sb);
110-
sb.AppendFormat("{0}\t{1}: ", prefix, Utility.CamelCasePropertyName(p.NameWithOption));
115+
WriteTypeScriptComment(p, sb, prefix);
116+
sb.AppendFormat("{0}{1}: ", prefix, Utility.CamelCasePropertyName(p.NameWithOption));
111117

112118
if (p.Type.IsKnownType) sb.Append(p.Type.TypeScriptName);
113119
else
114120
{
115121
if (p.Type.Shape == null) sb.Append("any");
116-
else WriteTSInterfaceDefinition(sb, prefix + "\t", p.Type.Shape);
122+
else WriteTSInterfaceDefinition(sb, prefix, p.Type.Shape);
117123
}
118124
if (p.Type.IsArray) sb.Append("[]");
119125

120126
sb.AppendLine(";");
121127
}
122-
123-
sb.Append(prefix).Append("}");
124128
}
125129
}
126130
}

src/TypeScriptDefinitionGenerator/Helpers/Utility.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,19 @@ internal static class Utility
77
{
88
public static string GenerateFileName(string sourceFile)
99
{
10+
return Path.ChangeExtension(sourceFile, GetDefaultExtension(Path.GetExtension(sourceFile)));
11+
}
12+
13+
public static string GetDefaultExtension(string originalExt)
14+
{
15+
string declaredExt = Options.DeclareModule ? ".d" : string.Empty;
16+
string ext = $".generated{declaredExt}.ts";
17+
1018
if (Options.WebEssentials2015)
1119
{
12-
return sourceFile + Constants.FileExtension;
13-
}
14-
else
15-
{
16-
return Path.ChangeExtension(sourceFile, Constants.FileExtension);
20+
return originalExt + ext;
1721
}
22+
return ext;
1823
}
1924

2025
public static string CamelCaseClassName(string name)

src/TypeScriptDefinitionGenerator/Options.cs

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ public class OptionsDialogPage : DialogPage
1717

1818
internal const bool _defClassInsteadOfInterface = false;
1919
internal const string _defModuleName = "Server.Dtos";
20-
internal const bool _defExport = false;
21-
internal const bool _defGlobalScope = false;
20+
internal const bool _defDeclareModule = true;
2221
internal const bool _defIgnoreIntellisense = true;
2322

2423
[Category("Casing")]
@@ -35,6 +34,12 @@ public class OptionsDialogPage : DialogPage
3534
[DisplayName("Camel case type names")]
3635
[DefaultValue(_defCamelCaseTypeNames)]
3736
public bool CamelCaseTypeNames { get; set; } = _defCamelCaseTypeNames;
37+
38+
[Category("Compatibilty")]
39+
[DisplayName("Web Essentials 2015 file names")]
40+
[Description("Web Essentials 2015 format is <filename>.cs.d.ts instead of <filename>.d.ts")]
41+
[DefaultValue(_defWebEssentials2015)]
42+
public bool WebEssentials2015 { get; set; } = _defWebEssentials2015;
3843

3944
[Category("Settings")]
4045
[DisplayName("Default Module name")]
@@ -48,28 +53,16 @@ public class OptionsDialogPage : DialogPage
4853
public bool ClassInsteadOfInterface { get; set; } = _defClassInsteadOfInterface;
4954

5055
[Category("Settings")]
51-
[DisplayName("Generate in global scope")]
52-
[Description("Controls whether to generate types in Global scope or wrapped in a module")]
53-
[DefaultValue(_defGlobalScope)]
54-
public bool GlobalScope { get; set; } = _defGlobalScope;
55-
56-
[Category("Settings")]
57-
[DisplayName("Export elements")]
58-
[Description("Controls whether to do export elements")]
59-
[DefaultValue(_defExport)]
60-
public bool Export { get; set; } = _defExport;
56+
[DisplayName("Declare module")]
57+
[Description("Controls whether to generate types in declared module or without one, but with export")]
58+
[DefaultValue(_defDeclareModule)]
59+
public bool DeclareModule { get; set; } = _defDeclareModule;
6160

6261
[Category("Settings")]
6362
[DisplayName("Ignore intellisense")]
6463
[Description("Ignore intellisense for client side reference names")]
6564
[DefaultValue(_defIgnoreIntellisense)]
6665
public bool IgnoreIntellisense { get; set; } = _defIgnoreIntellisense;
67-
68-
[Category("Compatibilty")]
69-
[DisplayName("Web Esentials 2015 file names")]
70-
[Description("Web Essentials 2015 format is <filename>.cs.d.ts instead of <filename>.d.ts")]
71-
[DefaultValue(_defWebEssentials2015)]
72-
public bool WebEssentials2015 { get; set; } = _defWebEssentials2015;
7366
}
7467

7568
public class Options
@@ -116,19 +109,11 @@ public static bool ClassInsteadOfInterface
116109
}
117110
}
118111

119-
public static bool GlobalScope
120-
{
121-
get
122-
{
123-
return overrides != null ? overrides.GlobalScope : DtsPackage.Options.GlobalScope;
124-
}
125-
}
126-
127-
public static bool Export
112+
public static bool DeclareModule
128113
{
129114
get
130115
{
131-
return overrides != null ? overrides.Export : DtsPackage.Options.Export;
116+
return overrides != null ? overrides.DeclareModule : DtsPackage.Options.DeclareModule;
132117
}
133118
}
134119

@@ -224,10 +209,7 @@ internal class OptionsOverride
224209
public bool ClassInsteadOfInterface { get; set; } = OptionsDialogPage._defClassInsteadOfInterface;
225210

226211
// [JsonRequired]
227-
public bool GlobalScope { get; set; } = OptionsDialogPage._defGlobalScope;
228-
229-
// [JsonRequired]
230-
public bool Export { get; set; } = OptionsDialogPage._defExport;
212+
public bool DeclareModule { get; set; } = OptionsDialogPage._defDeclareModule;
231213

232214
// [JsonRequired]
233215
public bool IgnoreIntellisense { get; set; } = OptionsDialogPage._defIgnoreIntellisense;

tests/ClassLibrary1/SomeClass.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
public class SomeClass
44
{
55
public int Inc1 { get; set; }
6+
7+
public SomeEnum Some { get; set; }
68
}
79
}

tests/TypeScriptDefinitionGenerator.Tests/IntellisenseParserTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void _ShouldWorkProperly()
6161
ClassInsteadOfInterface = false,
6262
DefaultModuleName = "Server.Dtos",
6363
Export = false,
64-
GlobalScope = false,
64+
DeclareModule = true,
6565
IgnoreIntellisense = true,
6666
});
6767
var list = IntellisenseParser.ProcessFile(item).ToList();

0 commit comments

Comments
 (0)