Skip to content

Commit

Permalink
Workarounds for #25
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitz committed Jan 31, 2017
1 parent 4bcca3d commit 083cebd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion core/Classes/DataClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public class TSMemberDescription {
}

public class TSInterfaceDescription {
//TODO This functionality is specific to ActiveX definition creation, and should really be in the TlbInf32Generator class
//TODO https://github.com/zspitz/ts-activex-gen/issues/22
public bool IsActiveXCreateable { get; set; }
public string EnumerableType { get; set; }
public Dictionary<string, TSMemberDescription> Members { get; } = new Dictionary<string, TSMemberDescription>();
Expand Down
56 changes: 28 additions & 28 deletions core/Classes/TSBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using TsActivexGen.Util;
Expand All @@ -10,28 +11,20 @@ namespace TsActivexGen {
public class TSBuilder {
private StringBuilder sb;

[Obsolete("Depends on literal types -- https://github.com/Microsoft/TypeScript/pull/9407")]
public bool WriteValueOnlyNamespaces { get; set; } = false;

private void WriteEnum(KeyValuePair<string, TSEnumDescription> x) {
var name = NameOnly(x.Key);
var @enum = x.Value;
var members = @enum.Members.OrderBy(y => y.Key);
switch (@enum.Typename.FullName) {
case "number":
$"const enum {name} {{".AppendLineTo(sb, 1);
members.AppendLinesTo(sb, (memberName, value) => $"{memberName} = {value}", 2, ",");
"}".AppendWithNewSection(sb, 1);
break;
case "string":
//TODO this works for all values, not just for string
//TODO all types can be better represented using literal types, pending https://github.com/Microsoft/TypeScript/pull/9407
$"type {name} = ".AppendLineTo(sb, 1);
members.AppendLinesTo(sb, (memberName, value) => $"\"{value}\" //{memberName}", 2, null, "| ");
sb.AppendLine();
break;
default:
throw new InvalidOperationException("Unable to emit declarations for enum type which is not numeric or string");

//https://github.com/zspitz/ts-activex-gen/issues/25
if (@enum.Typename.FullName == "number") {
$"const enum {name} {{".AppendLineTo(sb, 1);
members.AppendLinesTo(sb, (memberName, value) => $"{memberName} = {value}", 2, ",");
"}".AppendWithNewSection(sb, 1);
} else {
$"type {name} = ".AppendLineTo(sb, 1);
members.AppendLinesTo(sb, (memberName, value) => $"\"{value}\" //{memberName}", 2, null, "| ");
sb.AppendLine();
}
}

Expand Down Expand Up @@ -94,14 +87,23 @@ public string GetTypescript(TSNamespace ns) {
ns.Aliases.OrderBy(x => x.Key).ForEach(x => WriteAlias(x, ns.Name));
}

if (ns.Enums.Any()) {
"//Enums".AppendLineTo(sb, 1);
ns.Enums.OrderBy(x => x.Key).ForEach(WriteEnum);
var numericEnums = ns.Enums.Where(x => x.Value.Typename.FullName == "number");
if (numericEnums.Any()) {
"//Numeric enums".AppendLineTo(sb, 1);
numericEnums.OrderBy(x => x.Key).ForEach(WriteEnum);
}

var nonnumericEnums = ns.Enums.Where(x => x.Value.Typename.FullName != "number");
if (nonnumericEnums.Any()) {
"//Nonnumeric enums".AppendLineTo(sb, 1);
numericEnums.OrderBy(x => x.Key).ForEach(WriteEnum);

//TODO add these to runtime file https://github.com/zspitz/ts-activex-gen/issues/25
}

if (WriteValueOnlyNamespaces && ns.Namespaces.Any()) {
"//Modules with constant values".AppendLineTo(sb, 1);
ns.Namespaces.OrderBy(x => x.Key).ForEach(WriteNamespace);
if (ns.Namespaces.Any() && Debugger.IsAttached) {
//TODO add these to runtime file, not .d.ts -- https://github.com/zspitz/ts-activex-gen/issues/25
//use the WriteNamespace method
}

if (ns.Interfaces.Any()) {
Expand Down Expand Up @@ -132,8 +134,6 @@ public string GetTypescript(TSNamespace ns) {
return sb.ToString();
}

public List<KeyValuePair<string,string>> GetTypescript(TSNamespaceSet namespaceSet) => namespaceSet.Namespaces.SelectKVP((name, ns) => KVP(name, GetTypescript(ns))).ToList();
public List<KeyValuePair<string, string>> GetTypescript(TSNamespaceSet namespaceSet) => namespaceSet.Namespaces.SelectKVP((name, ns) => KVP(name, GetTypescript(ns))).ToList();
}
}

//TODO use const keyword instead of readonly keyword?
}
7 changes: 3 additions & 4 deletions core/Classes/TlbInf32Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,7 @@ private TSTypeName GetTypeName(VarTypeInfo vti, object value = null) {
return ret;
}

//We're assuming all members are constant
//In any case, in JS there is no way to access module members
//https://github.com/zspitz/ts-activex-gen/issues/25
private KeyValuePair<string, TSNamespaceDescription> ToTSNamespaceDescription(ConstantInfo c) {
var ret = new TSNamespaceDescription();
c.Members.Cast().Select(x => KVP(x.Name, AsString((object)x.Value))).AddRangeTo(ret.Members);
Expand All @@ -108,8 +107,8 @@ private KeyValuePair<string, TSEnumDescription> ToTSEnumDescription(ConstantInfo
var typename = GetTypeName(x.ReturnType, oValue);
if (ret.Typename == null) {
ret.Typename = typename;
} else if (ret.Typename != typename) {
throw new InvalidOperationException("Multiple types in enum"); //this should really be handled as a module with constants; but it's irrelevant because Javascript has no way to access module members
} else if (ret.Typename != TSTypeName.Any && ret.Typename != typename) {
ret.Typename = TSTypeName.Any;
}
return KVP(x.Name, AsString(oValue));
}).AddRangeTo(ret.Members);
Expand Down
1 change: 1 addition & 0 deletions wpf/Classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class OutputFileDetails {
public bool WriteOutput { get; set; }
public string OutputText { get; set; }
public string FullPath => Path.Combine(OutputFolder, FileName.ForceEndsWith(".d.ts"));
public bool EmitModuleConstants { get; set; }
}

public class DefinitionTypesComboBox : ComboBox {
Expand Down

0 comments on commit 083cebd

Please sign in to comment.