private JsExpression GetScriptType(IType type, TypeContext context, Func <ITypeParameter, JsExpression> resolveTypeParameter) { string contextName = GetTypeContextShortName(context); if (type is ParameterizedType) { var pt = (ParameterizedType)type; return(JsExpression.Invocation(JsExpression.Identifier(contextName + "_$InstantiateGenericType"), new[] { new JsTypeReferenceExpression(Common.CreateMockTypeDefinition(type.Name, Common.CreateMockAssembly())) }.Concat(pt.TypeArguments.Select(a => GetScriptType(a, TypeContext.GenericArgument, resolveTypeParameter))))); } else if (type.TypeParameterCount > 0) { // This handles open generic types ( typeof(C<,>) ) return(new JsTypeReferenceExpression(Common.CreateMockTypeDefinition(contextName + "_" + type.GetDefinition().Name, Common.CreateMockAssembly()))); } else if (type.Kind == TypeKind.Array) { return(JsExpression.Invocation(JsExpression.Identifier(contextName + "_$Array"), GetScriptType(((ArrayType)type).ElementType, TypeContext.GenericArgument, resolveTypeParameter))); } else if (type.Kind == TypeKind.Anonymous) { return(JsExpression.Identifier(contextName + "_$Anonymous")); } else if (type is ITypeDefinition) { return(new JsTypeReferenceExpression(Common.CreateMockTypeDefinition(contextName + "_" + type.Name, Common.CreateMockAssembly()))); } else if (type is ITypeParameter) { return(resolveTypeParameter((ITypeParameter)type)); } else { throw new ArgumentException("Unsupported type + " + type); } }
public void CanAnalyzeExpression() { var asm = Common.CreateMockAssembly(); var t1 = Common.CreateMockTypeDefinition("Type", asm); var t2 = Common.CreateMockTypeDefinition("Type", asm); var t3 = Common.CreateMockTypeDefinition("Type", asm); var expr = JsExpression.Add(new JsTypeReferenceExpression(t1), JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t1), "X"), JsExpression.Add(new JsTypeReferenceExpression(t2), new JsTypeReferenceExpression(t3)))); var refs = TypeReferenceFinder.Analyze(expr); Assert.That(refs, Has.Count.EqualTo(3)); Assert.That(refs.Contains(t1)); Assert.That(refs.Contains(t2)); Assert.That(refs.Contains(t3)); }
public void CanAnalyzeStatements() { var asm = Common.CreateMockAssembly(); var t1 = Common.CreateMockTypeDefinition("Type", asm); var t2 = Common.CreateMockTypeDefinition("Type", asm); var t3 = Common.CreateMockTypeDefinition("Type", asm); var ast = new JsStatement[] { JsStatement.If(JsExpression.Member(new JsTypeReferenceExpression(t1), "X"), JsStatement.Block( JsExpression.Add(new JsTypeReferenceExpression(t2), new JsTypeReferenceExpression(t3)) ), null), JsExpression.Add(JsExpression.Number(1), new JsTypeReferenceExpression(t1)), }; var refs = TypeReferenceFinder.Analyze(ast); Assert.That(refs, Has.Count.EqualTo(3)); Assert.That(refs.Contains(t1)); Assert.That(refs.Contains(t2)); Assert.That(refs.Contains(t3)); }