Skip to content

Commit

Permalink
Fixes #14
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitz committed Feb 8, 2018
1 parent f646ec1 commit bbffd70
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 15 deletions.
15 changes: 9 additions & 6 deletions common/Classes/TSBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private void writeEnum(KeyValuePair<string, TSEnumDescription> x, int indentatio
}

private void writeMember(TSMemberDescription m, string ns, int indentationLevel, string memberName, bool isClass) {
bool isConstructor = memberName.IsNullOrEmpty();
bool isConstructor = memberName == "<ctor>";

writeJsDoc(m.JsDoc, indentationLevel, true);

Expand Down Expand Up @@ -123,7 +123,10 @@ private void writeMember(TSMemberDescription m, string ns, int indentationLevel,
private string parametersString(TSMemberDescription m) => m.Parameters?.JoinedKVP((name, prm) => $"{name}: {GetTypeString(prm.Type, "")}");

private void writeTSLintRuleDisable(string ruleName, int indentationLevel) => writeTSLintRuleDisable(new[] { ruleName }, indentationLevel);
private void writeTSLintRuleDisable(IEnumerable<string> ruleNames, int indentationLevel) => $"// tslint:disable-next-line {ruleNames.Joined(" ")}".AppendLineTo(sb, indentationLevel);
private void writeTSLintRuleDisable(IEnumerable<string> ruleNames, int indentationLevel) {
if (ruleNames.None()) { return; }
$"// tslint:disable-next-line {ruleNames.Joined(" ")}".AppendLineTo(sb, indentationLevel);
}

private void writeInterface(KeyValuePair<string, TSInterfaceDescription> x, string ns, int indentationLevel) {
var name = SplitName(x.Key).name;
Expand Down Expand Up @@ -154,14 +157,14 @@ private void writeInterface(KeyValuePair<string, TSInterfaceDescription> x, stri
$"{typeDefiner} {name}{genericParameters} {extends}{{".AppendLineTo(sb, indentationLevel);

@interface.Members
.Concat(@interface.Constructors.Select(y=>KVP("",y)))
.OrderByDescending(y=>y.Value.Private)
.ThenBy(y=>y.Key.IsNullOrEmpty())
.Concat(@interface.Constructors.Select(y => KVP("<ctor>", y)))
.OrderByDescending(y => y.Value.Private)
.ThenBy(y => y.Key.IsNullOrEmpty())
.ThenBy(y => y.Key)
.ThenByDescending(y => y.Value.Parameters?.Count ?? -1)
.ThenByDescending(y => y.Value.GenericParameters.Any())
.ThenBy(y => parametersString(y.Value))
.ForEach(y => writeMember(y.Value, ns, indentationLevel + 1,y.Key, @interface.IsClass));
.ForEach(y => writeMember(y.Value, ns, indentationLevel + 1, y.Key, @interface.IsClass));

"}".AppendWithNewSection(sb, indentationLevel);
}
Expand Down
1 change: 1 addition & 0 deletions common/Extensions/IEnumerableKVP.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public static IEnumerable<KeyValuePair<TKey, TValue>> ForEachKVP<TKey, TValue>(t
public static ILookup<TKey, TValue> ToLookup<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> src) => src.ToLookup(x => x.Key, x => x.Value);

public static bool AnyKVP<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> src, Func<TKey, TValue, bool> predicate) => src.WhereKVP(predicate).Any();
public static bool NoneKVP<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> src, Func<TKey, TValue, bool> predicate) => src.WhereKVP(predicate).None();

public static TValue Get<TKey, TValue>(this IEnumerable<KeyValuePair<TKey, TValue>> src, TKey key) => src.WhereKVP((k, v) => key.Equals(k)).SingleOrDefault().Value;
}
Expand Down
7 changes: 7 additions & 0 deletions common/Extensions/IEnumerableT.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,12 @@ public static long Product<T>(this IEnumerable<T> src, Func<T, long> selector) {
public static IEnumerable<(T1, T2)> Zip<T1, T2>(this IEnumerable<T1> first, IEnumerable<T2> second) => first.Zip(second, (a, b) => (a, b));

public static bool All<T1, T2>(this IEnumerable<(T1, T2)> src, Func<T1, T2, bool> predicate) => src.All(x => predicate(x.Item1, x.Item2));

public static T OnlyOrDefault<T>(this IEnumerable<T> src, Func<T, bool> predicate=null) {
if (predicate != null) { src = src.Where(predicate); }
var firstTwo = src.Take(2).ToList();
if (firstTwo.Count == 1) { return firstTwo[0]; }
return default(T);
}
}
}
34 changes: 29 additions & 5 deletions tlibuilder/TlbInf32Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,32 @@ private TSMemberDescription GetMemberDescriptionForName(IEnumerable<MemberInfo>
}

private Dictionary<string, TSMemberDescription> GetMembers(Members members, ref string enumerableType, string typename) {
var ret = members.Cast().Where(x => !x.IsRestricted() && x.Name != "_NewEnum").ToLookup(x => x.Name).Select(grp => KVP(grp.Key, GetMemberDescriptionForName(grp, typename))).ToDictionary();
var membersList = members.Cast().ToList();
TSMemberDescription defaultProperty = null;
var ret = membersList.Where(x => !x.IsRestricted() && x.Name != "_NewEnum").ToLookup(x => x.Name).Select(grp => {
var memberKVP = KVP(grp.Key, GetMemberDescriptionForName(grp, typename));
var defaultMember = grp.Where(x => x.MemberId == 0).ToList();
if (!memberKVP.Value.IsProperty && defaultMember.Any()) {
if (memberKVP.Value.Parameters.None()) {
var i = 5;
}
if (grp.Count() != defaultMember.Count) { throw new Exception("Default and non-default properties on the same name"); }
if (defaultProperty != null) { throw new Exception("Multiple default properties in 'members'"); }
defaultProperty = memberKVP.Value;
}
return memberKVP;
}).ToDictionary();

if (defaultProperty != null) { ret.Add("", defaultProperty); }

var enumerableType1 = enumerableType; //because ref parameters cannot be used within lambda expressions
members.Cast().ToLookup(x => x.Name).IfContainsKey("_NewEnum", mi => {
membersList.ToLookup(x => x.Name).IfContainsKey("_NewEnum", mi => {
ret.IfContainsKey("Item", itemMI => enumerableType1 = ((TSSimpleType)itemMI.ReturnType).FullName);
});

// there is an overload of EnumeratorConstructor that accepts anything with an Item member, and resolves the enumerator type to the return type of Item
var lookup = ret.WhereKVP((name, descr) => name == "Item").ToLookup(kvp => kvp.Value.ReturnType);
if (lookup.Count == 1) { enumerableType1 = null; }
enumerableType = enumerableType1;

return ret;
Expand All @@ -194,10 +214,14 @@ private KeyValuePair<string, TSInterfaceDescription> ToTSInterfaceDescriptionBas
GetMembers(m, ref enumerableType, typename).AddRangeTo(ret.Members);
if (!enumerableType.IsNullOrEmpty()) { enumerableCollectionItemMapping[new TSSimpleType(typename)] = enumerableType; }
ret.JsDoc.Add("", helpString);
ret.IsClass = true;
ret.MakeFinal();
if (ret.Members.NoneKVP((name, descr) => name == "")) {
ret.IsClass = true;
ret.MakeFinal();
} else {
ret.IsClass = false;
}
var kvp = KVP(typename, ret);
kvp.MakeNominal();
if (ret.IsClass) { kvp.MakeNominal(); }
return kvp;
}

Expand Down
3 changes: 0 additions & 3 deletions wpf/Classes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
using static TsActivexGen.Wpf.Misc;
using static TsActivexGen.Wpf.Functions;
using static System.Environment;
using System.Windows.Data;
using System;
using System.Globalization;

namespace TsActivexGen.Wpf {
[AddINotifyPropertyChangedInterface]
Expand Down
3 changes: 2 additions & 1 deletion wpf/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public static string GetTsConfig(string name) => @"
],
""types"": [],
""noEmit"": true,
""forceConsistentCasingInFileNames"": true
""forceConsistentCasingInFileNames"": true,
""esModuleInterop"": true
},
""files"": [
""index.d.ts"",
Expand Down

0 comments on commit bbffd70

Please sign in to comment.