public void AsyncModuleWithReferencesWorks() { var asm = Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new AsyncModuleAttribute() }); var t1 = Common.CreateMockTypeDefinition("SomeNamespace.InnerNamespace.Type1", Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("module1") })); var t2 = Common.CreateMockTypeDefinition("SomeNamespace.InnerNamespace.Type2", Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("module2") })); var t3 = Common.CreateMockTypeDefinition("SomeNamespace.Type3", Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("module1") })); var t4 = Common.CreateMockTypeDefinition("Type4", Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("module3") })); var actual = Process(new JsStatement[] { JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t1), "a"), JsExpression.Member(new JsTypeReferenceExpression(t2), "b")), JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t3), "c"), JsExpression.Member(new JsTypeReferenceExpression(t4), "d")), JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t1), "e"), JsExpression.Member(new JsTypeReferenceExpression(t4), "f")), }, new[] { asm, t1.ParentAssembly, t2.ParentAssembly, t3.ParentAssembly, t4.ParentAssembly }); AssertCorrect(actual, @"define(['mscorlib', 'module1', 'module2', 'module3'], function($_, $module1, $module2, $module3) { 'use strict'; var exports = {}; $module1.SomeNamespace.InnerNamespace.Type1.a + $module2.SomeNamespace.InnerNamespace.Type2.b; $module1.SomeNamespace.Type3.c + $module3.Type4.d; $module1.SomeNamespace.InnerNamespace.Type1.e + $module3.Type4.f; return exports; }); "); }
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; }); } })(); "); }
public void ImportingTypesFromModulesWorks() { var t1 = Common.CreateMockTypeDefinition("SomeNamespace.InnerNamespace.Type1", Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("module1") })); var t2 = Common.CreateMockTypeDefinition("SomeNamespace.InnerNamespace.Type2", Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("module2") })); var t3 = Common.CreateMockTypeDefinition("SomeNamespace.Type3", Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("module1") })); var t4 = Common.CreateMockTypeDefinition("Type4", Common.CreateMockAssembly(new Expression <Func <Attribute> >[] { () => new ModuleNameAttribute("module3") })); var actual = Process(new JsStatement[] { JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t1), "a"), JsExpression.Member(new JsTypeReferenceExpression(t2), "b")), JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t3), "c"), JsExpression.Member(new JsTypeReferenceExpression(t4), "d")), JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t1), "e"), JsExpression.Member(new JsTypeReferenceExpression(t4), "f")), }, new[] { Common.CreateMockAssembly(), t1.ParentAssembly, t2.ParentAssembly, t3.ParentAssembly, t4.ParentAssembly }); AssertCorrect(actual, @"'use strict'; require('mscorlib'); var $module1 = require('module1'); var $module2 = require('module2'); var $module3 = require('module3'); var $asm = {}; $module1.SomeNamespace.InnerNamespace.Type1.a + $module2.SomeNamespace.InnerNamespace.Type2.b; $module1.SomeNamespace.Type3.c + $module3.Type4.d; $module1.SomeNamespace.InnerNamespace.Type1.e + $module3.Type4.f; "); }
public void AsyncModuleWithReferencesWorks() { var asm = CreateMockAssembly(); var t1 = CreateMockType("SomeNamespace.InnerNamespace.Type1", asm); var t2 = CreateMockType("SomeNamespace.InnerNamespace.Type2", asm); var t3 = CreateMockType("SomeNamespace.Type3", asm); var t4 = CreateMockType("Type4", asm); var md = new MockScriptSharpMetadataImporter { GetModuleName = t => t.Name == "Type1" || t.Name == "Type3" ? "module1" : (t.Name == "Type2" ? "module2" : "module3"), IsAsyncModule = true }; var actual = Process(new JsStatement[] { new JsExpressionStatement(JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t1), "a"), JsExpression.Member(new JsTypeReferenceExpression(t2), "b"))), new JsExpressionStatement(JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t3), "c"), JsExpression.Member(new JsTypeReferenceExpression(t4), "d"))), new JsExpressionStatement(JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t1), "e"), JsExpression.Member(new JsTypeReferenceExpression(t4), "f"))), }, metadata: md); AssertCorrect(actual, @"define(['mscorlib', 'module1', 'module2', 'module3'], function($_, $module1, $module2, $module3) { var exports = {}; $module1.SomeNamespace.InnerNamespace.Type1.a + $module2.SomeNamespace.InnerNamespace.Type2.b; $module1.SomeNamespace.Type3.c + $module3.Type4.d; $module1.SomeNamespace.InnerNamespace.Type1.e + $module3.Type4.f; return exports; }); "); }
public void ImportingTypesFromModulesWorks() { var asm = CreateMockAssembly(); var t1 = CreateMockType("SomeNamespace.InnerNamespace.Type1", asm); var t2 = CreateMockType("SomeNamespace.InnerNamespace.Type2", asm); var t3 = CreateMockType("SomeNamespace.Type3", asm); var t4 = CreateMockType("Type4", asm); var md = new MockScriptSharpMetadataImporter { GetModuleName = t => t.Name == "Type1" || t.Name == "Type3" ? "module1" : (t.Name == "Type2" ? "module2" : "module3") }; var actual = Process(new JsStatement[] { new JsExpressionStatement(JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t1), "a"), JsExpression.Member(new JsTypeReferenceExpression(t2), "b"))), new JsExpressionStatement(JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t3), "c"), JsExpression.Member(new JsTypeReferenceExpression(t4), "d"))), new JsExpressionStatement(JsExpression.Add(JsExpression.Member(new JsTypeReferenceExpression(t1), "e"), JsExpression.Member(new JsTypeReferenceExpression(t4), "f"))), }, metadata: md); AssertCorrect(actual, @"require('mscorlib'); var $module1 = require('module1'); var $module2 = require('module2'); var $module3 = require('module3'); $module1.SomeNamespace.InnerNamespace.Type1.a + $module2.SomeNamespace.InnerNamespace.Type2.b; $module1.SomeNamespace.Type3.c + $module3.Type4.d; $module1.SomeNamespace.InnerNamespace.Type1.e + $module3.Type4.f; "); }
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)); }