Esempio n. 1
0
        public void GlobalMethodsAttributeWithModuleNameCausesModuleGlobalMethodsToBeGenerated()
        {
            AssertCorrect(
                @"////////////////////////////////////////////////////////////////////////////////
// SomeNamespace.InnerNamespace.MyClass
exports.s1 = function(s) {
	S;
};
exports.s2 = function(t) {
	T;
};
Q;
R;
", new MockScriptSharpMetadataImporter()
            {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(t.Name == "MyClass" ? "" : t.FullName), GetModuleName = t => "my-module"
            },
                new JsClass(CreateMockTypeDefinition("SomeNamespace.InnerNamespace.MyClass"), JsClass.ClassTypeEnum.Class, null, null, new JsExpression[0])
            {
                StaticMethods = { new JsMethod(CreateMockMethod("S1"), "s1", null, CreateFunction("s")),
                                  new JsMethod(CreateMockMethod("S2"), "s2", null, CreateFunction("t")) },
                StaticInitStatements = { new JsExpressionStatement(JsExpression.Identifier("Q")),
                                         new JsExpressionStatement(JsExpression.Identifier("R")), }
            });
        }
Esempio n. 2
0
        public void ForeachStatementWithStrucElementType()
        {
            AssertCorrect(
                @"
class MyEnumerable {
	public MyEnumerator GetEnumerator() { return null; }
}
sealed class MyEnumerator {
	public int Current { get { return 0; } }
	public bool MoveNext() {}
}

public void M() {
	MyEnumerable list = null;
	// BEGIN
	foreach (var item in list) {
		int x = 0;
	}
	// END
}",
                @"	var $tmp1 = $list.GetEnumerator();
	while ($tmp1.MoveNext()) {
		var $item = $Clone($tmp1.Current, {to_Int32});
		var $x = $Clone(0, {to_Int32});
	}
", metadataImporter: new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.MutableValueType(t.Name), GetPropertySemantics = p => PropertyScriptSemantics.Field(p.Name)
            });
        }
        public void CannotUseNotUsableTypeAsATypeArgument()
        {
            var nc = new MockMetadataImporter {
                GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.NotUsableFromScript() : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"class C1 {}
class C {
	public void M() {
		var c = new C1();
	}
}"
            }, metadataImporter: nc, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("instance") && er.AllMessagesText[0].Contains("C1"));

            er = new MockErrorReporter(false);
            Compile(new[] {
                @"class C1 {}
class C2<T> {}
class C {
	public void M() {
		var x = new C2<C2<C1>>();
	}
}"
            }, metadataImporter: nc, errorReporter: er);
            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("type argument") && er.AllMessagesText[0].Contains("C1") && er.AllMessagesText[0].Contains("C2"));
        }
        public void CannotUseMutableValueTypeAsTypeArgument()
        {
            var md = new MockMetadataImporter {
                GetTypeSemantics = t => t.Kind == TypeKind.Struct ? TypeScriptSemantics.MutableValueType(t.Name) : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"struct S1 {}
class C1<T> {}
class C {
	public void M() {
		var c = new C1<S1>();
	}
}"
            }, metadataImporter: md, errorReporter: er);

            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("S1"));

            er = new MockErrorReporter(false);

            Compile(new[] {
                @"struct S1 {}
class C1<T> {}
class C {
	public void M() {
		var c = new C1<C1<S1>>();
	}
}"
            }, metadataImporter: md, errorReporter: er);

            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("S1"));
        }
Esempio n. 5
0
        public void CannotUseNotUsableTypeAsAGenericArgument()
        {
            var nc = new MockMetadataImporter {
                GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.NotUsableFromScript() : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"class C1 {}
class C {
	public void F1<T>() {}
	public void M() {
		F1<C1>();
	}
}"
            }, metadataImporter: nc, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("generic argument") && er.AllMessagesText[0].Contains("C1") && er.AllMessagesText[0].Contains("F1"));

            er = new MockErrorReporter(false);
            Compile(new[] {
                @"class C1 {}
interface I1<T> {}
class C {
	public void F1<T>() {}
	public void M() {
		F1<I1<I1<C1>>>();
	}
}"
            }, metadataImporter: nc, errorReporter: er);
            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("generic argument") && er.AllMessagesText[0].Contains("C1") && er.AllMessagesText[0].Contains("F1"));
        }
Esempio n. 6
0
        public void ClassWithSameNameAsVariable2()
        {
            var someAsm = Common.CreateMockAssembly();
            var actual  = Process(new JsStatement[] {
                JsStatement.Function("f", new[] { "x" }, JsStatement.Block(
                                         JsStatement.Var("y", JsExpression.Number(0)),
                                         JsExpression.FunctionDefinition(new string[0],
                                                                         JsExpression.Binary(ExpressionNodeType.Add, JsExpression.Member(new JsTypeReferenceExpression(Common.CreateMockTypeDefinition("x", someAsm)), "a"), JsExpression.Member(new JsTypeReferenceExpression(Common.CreateMockTypeDefinition("y", someAsm)), "a"))
                                                                         ),
                                         JsExpression.FunctionDefinition(new string[0], JsStatement.Block(
                                                                             JsStatement.Var("z", JsExpression.Binary(ExpressionNodeType.Add, JsExpression.Identifier("x"), JsExpression.Identifier("y")))
                                                                             ))
                                         ))
            }, new[] { Common.CreateMockAssembly(), someAsm }, new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(t.Name)
            }, namer: new Namer());

            AssertCorrect(actual,
                          @"(function() {
	'use strict';
	var $asm = {};
	function f(x1) {
		var y1 = 0;
		(function() {
			x.a + y.a;
		});
		(function() {
			var z = x1 + y1;
		});
	}
})();
");
        }
Esempio n. 7
0
        public void ModuleNameOnAssemblyCanBeOverriddenOnType()
        {
            var asm = Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("my-module") });
            var c1  = Common.CreateMockTypeDefinition("C1", asm, attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("other-module") });
            var c2  = Common.CreateMockTypeDefinition("C2", asm, attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("") });
            var c3  = Common.CreateMockTypeDefinition("C3", asm, attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute(null) });

            var actual = Process(new JsStatement[] {
                JsExpression.Member(new JsTypeReferenceExpression(c1), "a"),
                JsExpression.Member(new JsTypeReferenceExpression(c2), "b"),
                JsExpression.Member(new JsTypeReferenceExpression(c3), "c"),
            }, new[] { Common.CreateMockAssembly(), asm }, metadata: new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(t.Name.Replace("+", "_"))
            });

            AssertCorrect(actual,
                          @"'use strict';
require('mscorlib');
var $othermodule = require('other-module');
var $asm = {};
$othermodule.C1.a;
C2.b;
C3.c;
");
        }
Esempio n. 8
0
        public void RenamedVariableClashWithImplicitGlobal()
        {
            var someAsm = Common.CreateMockAssembly();
            var actual  = Process(new JsStatement[] {
                JsStatement.Function("f", new[] { "x" }, JsStatement.Block(
                                         JsExpression.FunctionDefinition(new string[0],
                                                                         JsExpression.Member(new JsTypeReferenceExpression(Common.CreateMockTypeDefinition("x", someAsm)), "a")
                                                                         ),
                                         JsExpression.FunctionDefinition(new string[0], JsStatement.Block(
                                                                             JsExpression.Add(JsExpression.Identifier("x"), JsExpression.Identifier("x1"))
                                                                             ))
                                         ))
            }, new[] { Common.CreateMockAssembly(), someAsm }, new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(t.Name)
            }, namer: new Namer());

            AssertCorrect(actual,
                          @"(function() {
	'use strict';
	var $asm = {};
	function f(x2) {
		(function() {
			x.a;
		});
		(function() {
			x2 + x1;
		});
	}
})();
");
        }
Esempio n. 9
0
        public void CannotUseMutableValueTypeAsATypeArgumentInATypeOfExpression()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.MutableValueType(t.Name) : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"struct C1 {}
class C {
	public void M() {
		var t = typeof(C1);
	}
}"
            }, metadataImporter: metadataImporter, errorReporter: er);

            Assert.That(er.AllMessages.Count, Is.EqualTo(0));

            er = new MockErrorReporter(false);
            Compile(new[] {
                @"struct C1 {}
interface I1<T> {}
class C {
	public void M() {
		var t= typeof(I1<I1<C1>>);
	}
}"
            }, metadataImporter: metadataImporter, errorReporter: er);
            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].Code == 7539 && er.AllMessages[0].FormattedMessage.Contains("mutable value type") && er.AllMessages[0].FormattedMessage.Contains("C1"));
        }
        public void CannotUseNotUsableTypeInATypeOfExpression()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = t => t.Name == "C1" ? TypeScriptSemantics.NotUsableFromScript() : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] {
                @"class C1 {}
class C {
	public void M() {
		var t = typeof(C1);
	}
}"
            }, metadataImporter: metadataImporter, errorReporter: er);

            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("typeof") && er.AllMessagesText[0].Contains("C1"));

            er = new MockErrorReporter(false);
            Compile(new[] {
                @"class C1 {}
interface I1<T> {}
class C {
	public void M() {
		var t= typeof(I1<I1<C1>>);
	}
}"
            }, metadataImporter: metadataImporter, errorReporter: er);
            Assert.That(er.AllMessagesText.Count, Is.EqualTo(1));
            Assert.That(er.AllMessagesText[0].Contains("not usable from script") && er.AllMessagesText[0].Contains("typeof") && er.AllMessagesText[0].Contains("C1"));
        }
        public void UsingUnusableClassAsABaseClassForAnotherUnusableClassIsNotAnError()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NotUsableFromScript()
            };

            Compile(new[] { "class B {} class D : B {}" }, metadataImporter: metadataImporter);
            // No errors is good enough
        }
Esempio n. 12
0
        public void IgnoreGenericArgumentsInTheTypeSemanticsRemovesGenericArgumentsFromTheType()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(t.Name, ignoreGenericArguments: true)
            };

            Compile(new[] { "class C1<T1, T2> {}" }, metadataImporter: metadataImporter);
            FindClass("C1").TypeArgumentNames.Should().BeEmpty();
        }
Esempio n. 13
0
        public void AccessingMemberOnTypeWithEmptyScriptNameResultsInGlobalAccess()
        {
            var actual = Process(new JsStatement[] {
                new JsExpressionStatement(new JsMemberAccessExpression(new JsTypeReferenceExpression(CreateMockType("GlobalType", CreateMockAssembly())), "x")),
            }, metadata: new MockScriptSharpMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType("")
            });

            AssertCorrect(actual, "x;\n");
        }
Esempio n. 14
0
        public void AccessingMemberOnTypeWithEmptyScriptNameInOwnAssemblyWithModuleNameUsesExports()
        {
            var asm    = CreateMockAssembly();
            var actual = Process(new JsStatement[] {
                new JsExpressionStatement(new JsMemberAccessExpression(new JsTypeReferenceExpression(CreateMockType("GlobalType", asm)), "x")),
            }, mainAssembly: asm, metadata: new MockScriptSharpMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(""), GetModuleName = t => "my-module", MainModuleName = "my-module"
            });

            AssertCorrect(actual, "exports.x;\n");
        }
        public void InterfaceThatIsNotUsableFromScriptCannotBeImplementedByAUsableClass()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = t => t.Name == "I1" ? TypeScriptSemantics.NotUsableFromScript() : TypeScriptSemantics.NormalType(t.Name)
            };
            var er = new MockErrorReporter(false);

            Compile(new[] { "interface I1 {} class C1 : I1 {}" }, metadataImporter: metadataImporter, errorReporter: er);
            Assert.That(er.AllMessages.Count, Is.EqualTo(1));
            Assert.That(er.AllMessages[0].FormattedMessage.Contains("not usable from script") && er.AllMessages[0].FormattedMessage.Contains("inheritance list") && er.AllMessages[0].FormattedMessage.Contains("I1") && er.AllMessages[0].FormattedMessage.Contains("C1"));
        }
Esempio n. 16
0
        public void ImportingImportedTypeFromOwnAssemblyWorks()
        {
            var asm    = CreateMockAssembly();
            var type   = CreateMockType("MyImportedType", asm);
            var actual = Process(new JsStatement[] {
                new JsReturnStatement(JsExpression.Binary(ExpressionNodeType.Add, JsExpression.Member(new JsTypeReferenceExpression(type), "x"), JsExpression.Number(1)))
            }, mainAssembly: asm, metadata: new MockScriptSharpMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType("$" + t.FullName), IsImported = t => ReferenceEquals(t, type)
            });

            AssertCorrect(actual, "return $MyImportedType.x + 1;\n");
        }
Esempio n. 17
0
        public void AccessingMemberOnTypeWithEmptyScriptNameResultsInGlobalAccess()
        {
            var actual = Process(new JsStatement[] {
                new JsExpressionStatement(JsExpression.MemberAccess(new JsTypeReferenceExpression(Common.CreateMockTypeDefinition("GlobalType", Common.CreateMockAssembly())), "x")),
            }, Common.CreateMockAssembly(), new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType("")
            });

            AssertCorrect(actual,
                          @"(function() {
	x;
})();
");
        }
Esempio n. 18
0
        public void ModuleNameAttributeIsInheritedToInnerTypesButCanBeOverridden()
        {
            var asm  = Common.CreateMockAssembly();
            var c1   = Common.CreateMockTypeDefinition("C1", asm, attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("my-module") });
            var c1d1 = Common.CreateMockTypeDefinition("C1+D1", asm, declaringType: c1);
            var c1d2 = Common.CreateMockTypeDefinition("C1+D2", asm, declaringType: c1, attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("other-module") });
            var c1d3 = Common.CreateMockTypeDefinition("C1+D3", asm, declaringType: c1, attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute(null) });
            var c1d4 = Common.CreateMockTypeDefinition("C1+D4", asm, declaringType: c1, attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("") });

            var c2   = Common.CreateMockTypeDefinition("C2", asm);
            var c2d1 = Common.CreateMockTypeDefinition("C2+D1", asm, declaringType: c2);
            var c2d2 = Common.CreateMockTypeDefinition("C2+D2", asm, declaringType: c2, attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("third-module") });
            var c2d3 = Common.CreateMockTypeDefinition("C2+D3", asm, declaringType: c2, attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute(null) });
            var c2d4 = Common.CreateMockTypeDefinition("C2+D4", asm, declaringType: c2, attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("") });

            var actual = Process(new JsStatement[] {
                JsExpression.Member(new JsTypeReferenceExpression(c1), "a"),
                JsExpression.Member(new JsTypeReferenceExpression(c1d1), "b"),
                JsExpression.Member(new JsTypeReferenceExpression(c1d2), "c"),
                JsExpression.Member(new JsTypeReferenceExpression(c1d3), "d"),
                JsExpression.Member(new JsTypeReferenceExpression(c1d4), "e"),
                JsExpression.Member(new JsTypeReferenceExpression(c2), "f"),
                JsExpression.Member(new JsTypeReferenceExpression(c2d1), "g"),
                JsExpression.Member(new JsTypeReferenceExpression(c2d2), "h"),
                JsExpression.Member(new JsTypeReferenceExpression(c2d3), "i"),
                JsExpression.Member(new JsTypeReferenceExpression(c2d4), "j"),
            }, new[] { Common.CreateMockAssembly(), asm }, metadata: new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(t.Name.Replace("+", "_"))
            });

            AssertCorrect(actual,
                          @"'use strict';
require('mscorlib');
var $mymodule = require('my-module');
var $othermodule = require('other-module');
var $thirdmodule = require('third-module');
var $asm = {};
$mymodule.C1.a;
C1_D1.b;
$othermodule.C1_D2.c;
C1_D3.d;
C1_D4.e;
C2.f;
C2_D1.g;
$thirdmodule.C2_D2.h;
C2_D3.i;
C2_D4.j;
");
        }
Esempio n. 19
0
        public void CurrentAssemblyReferenceWorksInModule()
        {
            var actual = Process(new JsStatement[] {
                JsExpression.Member(Linker.CurrentAssemblyExpressionStatic, "a")
            }, new[] { Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("my-module") }) }, new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(t.Name)
            }, namer: new Namer());

            AssertCorrect(actual,
                          @"(function() {
	'use strict';
	exports.a;
})();
");
        }
Esempio n. 20
0
        public void ImportingTypesFromGlobalNamespaceWorks()
        {
            var otherAsm = CreateMockAssembly();
            var actual   = Process(new JsStatement[] {
                new JsExpressionStatement(new JsTypeReferenceExpression(CreateMockType("GlobalType", otherAsm))),
                new JsReturnStatement(JsExpression.Binary(ExpressionNodeType.Add, JsExpression.Member(new JsTypeReferenceExpression(CreateMockType("Global.NestedNamespace.InnerNamespace.Type", otherAsm)), "x"), JsExpression.Number(1)))
            }, metadata: new MockScriptSharpMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(string.Join(".", t.FullName.Split('.').Select(x => "$" + x)))
            });

            AssertCorrect(actual,
                          @"$GlobalType;
return $Global.$NestedNamespace.$InnerNamespace.$Type.x + 1;
");
        }
Esempio n. 21
0
        public void AccessingMemberOnTypeWithEmptyScriptNameInOwnAssemblyWithModuleNameUsesExports()
        {
            var asm    = Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("main-module") });
            var actual = Process(new JsStatement[] {
                new JsExpressionStatement(JsExpression.MemberAccess(new JsTypeReferenceExpression(Common.CreateMockTypeDefinition("GlobalType", asm)), "x")),
            }, asm, metadata: new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType("")
            });

            AssertCorrect(actual,
                          @"(function() {
	exports.x;
})();
");
        }
Esempio n. 22
0
        public void AccessingMemberOnTypeWithEmptyScriptNameInOwnAssemblyButWithDifferentModuleNameResultsInARequire()
        {
            var asm    = Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("main-module") });
            var actual = Process(new JsStatement[] {
                new JsExpressionStatement(JsExpression.MemberAccess(new JsTypeReferenceExpression(Common.CreateMockTypeDefinition("GlobalType", asm, attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("my-module") })), "x")),
            }, asm, metadata: new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType("")
            });

            AssertCorrect(actual,
                          @"require('mscorlib');
var $mymodule = require('my-module');
$mymodule.x;
");
        }
        public void ClassesWithGenerateCodeSetToFalseAndTheirNestedClassesAreNotInTheOutput()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = type => TypeScriptSemantics.NormalType(type.Name, generateCode: type.Name != "C2")
            };

            Compile(new[] { "class C1 {} class C2 { class C3 {} }" }, metadataImporter: metadataImporter);
            CompiledTypes.Select(t => t.CSharpTypeDefinition.Name).Should().BeEquivalentTo(new[] { "C1", "C3" });

            metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = type => type.Name != "C2" ? TypeScriptSemantics.NormalType(type.Name) : TypeScriptSemantics.NotUsableFromScript()
            };
            Compile(new[] { "class C1 {} class C2 { class C3 {} }" }, metadataImporter: metadataImporter);
            CompiledTypes.Select(t => t.CSharpTypeDefinition.Name).Should().BeEquivalentTo(new[] { "C1", "C3" });
        }
Esempio n. 24
0
        public void AccessingMemberOnTypeWithEmptyScriptNameInOwnAssemblyButWithDifferentModuleNameResultsInARequire()
        {
            var asm    = CreateMockAssembly();
            var actual = Process(new JsStatement[] {
                new JsExpressionStatement(new JsMemberAccessExpression(new JsTypeReferenceExpression(CreateMockType("GlobalType", asm)), "x")),
            }, mainAssembly: asm, metadata: new MockScriptSharpMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(""), GetModuleName = t => "my-module", MainModuleName = "main-module"
            });

            AssertCorrect(actual,
                          @"require('mscorlib');
var $mymodule = require('my-module');
$mymodule.x;
");
        }
Esempio n. 25
0
        public void CurrentAssemblyReferenceWorksInNonModule()
        {
            var actual = Process(new JsStatement[] {
                JsExpression.Member(Linker.CurrentAssemblyExpressionStatic, "a")
            }, new[] { Common.CreateMockAssembly() }, new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(t.Name)
            }, namer: new Namer());

            AssertCorrect(actual,
                          @"(function() {
	'use strict';
	var $asm = {};
	$asm.a;
})();
");
        }
Esempio n. 26
0
        public void ImportingImportedTypeFromOwnAssemblyWorks()
        {
            var asm    = Common.CreateMockAssembly();
            var type   = Common.CreateMockTypeDefinition("MyImportedType", asm, attributes: new Expression <Func <Attribute> >[] { () => new ImportedAttribute() });
            var actual = Process(new JsStatement[] {
                new JsReturnStatement(JsExpression.Binary(ExpressionNodeType.Add, JsExpression.Member(new JsTypeReferenceExpression(type), "x"), JsExpression.Number(1)))
            }, asm, metadata: new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType("$" + t.FullName)
            });

            AssertCorrect(actual,
                          @"(function() {
	return $MyImportedType.x + 1;
})();
");
        }
Esempio n. 27
0
        public void CurrentAssemblyReferenceWorksInAsyncModule()
        {
            var actual = Process(new JsStatement[] {
                JsExpression.Member(Linker.CurrentAssemblyExpressionStatic, "a")
            }, new[] { Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new AsyncModuleAttribute() }) }, new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType(t.Name)
            }, namer: new Namer());

            AssertCorrect(actual,
                          @"define(['mscorlib'], function(_) {
	'use strict';
	var exports = {};
	exports.a;
	return exports;
});
");
        }
Esempio n. 28
0
        public void UsingTypeWithNoScriptNameInModuleReturnsTheModuleVariable()
        {
            var asm    = Common.CreateMockAssembly(attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("main-module") });
            var type   = Common.CreateMockTypeDefinition("GlobalType", asm, attributes: new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("some-module") });
            var actual = Process(new JsStatement[] {
                JsStatement.Return(new JsTypeReferenceExpression(type)),
            }, new[] { asm }, metadata: new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType("")
            });

            AssertCorrect(actual,
                          @"'use strict';
require('mscorlib');
var $somemodule = require('some-module');
return $somemodule;
");
        }
Esempio n. 29
0
        public void ImportingGlobalMethodsFromModulesWorks()
        {
            var t1 = Common.CreateMockTypeDefinition("Type1", Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("mymodule") }));
            var md = new MockMetadataImporter {
                GetTypeSemantics = t => TypeScriptSemantics.NormalType("")
            };

            var actual = Process(new JsStatement[] {
                new JsExpressionStatement(JsExpression.Member(new JsTypeReferenceExpression(t1), "a")),
            }, Common.CreateMockAssembly(), metadata: md);

            AssertCorrect(actual,
                          @"require('mscorlib');
var $mymodule = require('mymodule');
$mymodule.a;
");
        }
        public void EnumsWithGenerateCodeSetToFalseAreNotInTheOutput()
        {
            var metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = type => TypeScriptSemantics.NormalType(type.Name, generateCode: type.Name != "C2")
            };

            Compile(new[] { "enum C1 {} enum C2 {}" }, metadataImporter);
            CompiledTypes.Should().HaveCount(1);
            CompiledTypes[0].CSharpTypeDefinition.Name.Should().Be("C1");

            metadataImporter = new MockMetadataImporter {
                GetTypeSemantics = type => type.Name != "C2" ? TypeScriptSemantics.NormalType(type.Name) : TypeScriptSemantics.NotUsableFromScript()
            };
            Compile(new[] { "enum C1 {} enum C2 {}" }, metadataImporter);
            CompiledTypes.Should().HaveCount(1);
            CompiledTypes[0].CSharpTypeDefinition.Name.Should().Be("C1");
        }