Example #1
0
        public void TestDivide()
        {
            var typescript = @"
                if (pageSize > 0)
                {
                    page = skippedSoFar / pageSize;
                }

                var pageCount = 0;
                var i = totalCount;
                while (i > 0)
                {
                    pageCount++;
                    i -= pageSize;
                }

                dbList.PagingInfo = new PagingInfo(skippedSoFar, totalCount, pageSize, page, pageCount);";
            var beautifier = new TsBeautifier();
            var result     = beautifier.Beautify(typescript);

            Assert.AreEqual(@"if (pageSize > 0) {
    page = skippedSoFar / pageSize;
}
var pageCount = 0;
var i = totalCount;
while (i > 0) {
    pageCount++;
    i -= pageSize;
}
dbList.PagingInfo = new PagingInfo(skippedSoFar, totalCount, pageSize, page, pageCount);", result);
        }
Example #2
0
        protected static string Beautify(string typescript)
        {
            var beautifier = new TsBeautifier();
            var result     = beautifier.Beautify(typescript);

            return(result);
        }
Example #3
0
        public void TestLargeFile2()
        {
            var typescript = LargeFile.Input2;
            var beautifier = new TsBeautifier();
            var result     = beautifier.Beautify(typescript);

            Assert.AreEqual(LargeFile.Input2Beautified, result);
        }
Example #4
0
        public void TestImports()
        {
            var typescript = @"import   { PropertyInfo ,   Type, Interface, TypeInfo} from ""@brandless/tsutility"";";
            var beautifier = new TsBeautifier();
            var result     = beautifier.Beautify(typescript);

            Assert.AreEqual(@"import { PropertyInfo, Type, Interface, TypeInfo } from ""@brandless/tsutility"";", result);
        }
Example #5
0
        public void TestGenerics()
        {
            var typescript = @"f<Array<string>>(p => { });";
            var beautifier = new TsBeautifier();
            var result     = beautifier.Beautify(typescript);

            Assert.AreEqual(@"f<Array<string>>(p => {});", result);
        }
Example #6
0
        public void TestSingleLineWhitespaceCleaning()
        {
            var typescript = "let   x: string    = `something` ;";
            var beautifier = new TsBeautifier();
            var result     = beautifier.Beautify(typescript);

            Assert.AreEqual(@"let x: string = `something`;", result);
        }
Example #7
0
        public void TestBug1()
        {
            var jsBeautifyOptions = new TsBeautifyOptions();

            jsBeautifyOptions.PreserveNewlines = false;
            var typescript = Bug1.TypeScript;

            typescript = new TsBeautifier(jsBeautifyOptions).Beautify(typescript);
        }
Example #8
0
        public void TestMultipleGenerics()
        {
            var code =
                @"    private _propertiesMap: Dictionary<string, IProperty> = new Dictionary<string, IProperty>();";
            var beautifier = new TsBeautifier();
            var result     = beautifier.Beautify(code);

            Assert.AreEqual(@"private _propertiesMap: Dictionary<string, IProperty> = new Dictionary<string, IProperty>();", result);
        }
Example #9
0
        public void TestHugeConversion()
        {
            var typescript = TsBeautifyTs.Code;

            for (var i = 0; i < 1000; i++)
            {
                var beautifier = new TsBeautifier();
                var result     = beautifier.Beautify(typescript);
            }
        }
Example #10
0
        public void Test123()
        {
            var typescript =
                @"r=//
`^$`;";
            var beautifier = new TsBeautifier();
            var result     = beautifier.Beautify(typescript);

            Assert.AreEqual(@"r = //
`^$`;", result);
        }
Example #11
0
        public void TestTest()
        {
            var code =
                @"@test public async""Test1"" () {
}@test public async""Test2"" () {
}";
            var beautifier = new TsBeautifier();
            var result     = beautifier.Beautify(code);

            Assert.AreEqual(@"@test public async ""Test1"" () {}
@test public async ""Test2"" () {}", result);
        }
Example #12
0
        public void Test456()
        {
            var typescript =
                @"let set = (<ITrackingSet><any>MethodInfoExtensions.InvokeGeneric(Enumerable.First(TypeInfo.GetRuntimeMethods(TrackingSetCollection )

, m => m.Name == <any>`TrackingSet`)

, this, new Array<Object>(), type));";
            var beautifier = new TsBeautifier();
            var result     = beautifier.Beautify(typescript);

            Assert.AreEqual(@"let set = (<ITrackingSet><any>MethodInfoExtensions.InvokeGeneric(Enumerable.First(TypeInfo.GetRuntimeMethods(TrackingSetCollection), m => m.Name == <any>`TrackingSet`), this, new Array<Object>(), type));", result);
        }
Example #13
0
        public void TestKeepBracesOnNewLines()
        {
            var code       = @"if (true)
                {
                    var x = 1;
                }";
            var beautifier = new TsBeautifier().Configure(x => x.OpenBlockOnNewLine = true);
            var result     = beautifier.Beautify(code);

            Assert.AreEqual(@"if (true)
{
    var x = 1;
}", result);
        }
Example #14
0
        private static void Iterate(Stopwatch timer)
        {
            var typescript = TsBeautifyTs.Code;
            var iterations = 100;

            for (var i = 0; i < iterations; i++)
            {
                var beautifier = new TsBeautifier();
                var result     = beautifier.Beautify(typescript);
            }

            timer.Stop();
            Console.WriteLine($"Completed {iterations} iterations in {FormatTime(timer.ElapsedTicks)}");
            Console.WriteLine($"Each iteration took an average of {FormatTime(timer.ElapsedTicks / iterations)}");
        }
Example #15
0
        static void Main(string[] args)
        {
            Console.WriteLine("Preparing");
            var timer = new Stopwatch();
            var files = Directory.EnumerateFiles(@"D:\Code\Brandless\Iql.Npm.Unformatted", "*.ts",
                                                 SearchOption.AllDirectories)
                        .Select(f => new { File = f, Contents = File.ReadAllText(f) }).ToList();

            var options = new ProgressBarOptions
            {
                ProgressCharacter   = '─',
                ProgressBarOnBottom = false
            };

            //foreach (var text in texts)
            //{
            //    var beautifier = new TsBeautifier();
            //    var result = beautifier.Beautify(text);
            //}
            using (var pbar = new ProgressBar(files.Count, "Starting parallel", options))
            {
                object lastFile = null;
                timer.Start();
                //foreach (var file in files)
                //{
                //    pbar.Tick(Path.GetFileName(file.File));
                //    if (Path.GetFileName(file.File) == "MetadataSerializationJsonCache.ts")
                //    {
                //        continue;
                //    }
                //    var beautifier = new TsBeautifier();
                //    var result = beautifier.Beautify(file.Contents);
                //}
                Parallel.ForEach(files, file =>
                {
                    lastFile = file;
                    pbar.Tick(Path.GetFileName(file.File));
                    var beautifier = new TsBeautifier();
                    var result     = beautifier.Beautify(file.Contents);
                });
            }
            //var text = File.ReadAllText(
            //    @"D:\Code\Brandless\Iql.Npm.Unformatted\Iql.Tests\src\Tests\MetadataSerialization\MetadataSerializationJsonCache.ts");
            //var beautifier = new TsBeautifier();
            //var result = beautifier.Beautify(text);
            timer.Stop();
            Console.WriteLine($"Completed in {FormatTime(timer.ElapsedTicks)}");
        }
Example #16
0
        public void TestImportModule()
        {
            var typescript = @"import ""module-alias/register"";
import { suite, test, slow, timeout } from ""mocha-typescript"";
import { assert } from ""chai"";import { __Iql_Tests_Types_typeLoaded, __Iql_Tests_Types_defer } from ""../../Iql.Tests.Types.Defer"";
import { TestsBase } from ""../TestsBase"";
";
            var beautifier = new TsBeautifier();
            var result     = beautifier.Beautify(typescript);

            Assert.AreEqual(@"import ""module-alias/register"";
import { suite, test, slow, timeout } from ""mocha-typescript"";
import { assert } from ""chai"";
import { __Iql_Tests_Types_typeLoaded, __Iql_Tests_Types_defer } from ""../../Iql.Tests.Types.Defer"";
import { TestsBase } from ""../TestsBase"";", result);
        }
Example #17
0
        public void TestSingleLineCommentShouldBeOnNewLine()
        {
            var code       = @"if (true)
                {
                    // I am a comment
                    var x = 1; // An am an end-of-line comment
                    var y = /* I am an in-line comment */ 2;
                    // I am another comment
                }";
            var beautifier = new TsBeautifier().Configure(x => x.OpenBlockOnNewLine = true);
            var result     = beautifier.Beautify(code);

            Assert.AreEqual(@"if (true)
{
    // I am a comment
    var x = 1; // An am an end-of-line comment
    var y = /* I am an in-line comment */ 2;
    // I am another comment
}", result);
        }
Example #18
0
        public void ClassNameWithGenericsShouldHaveSpace()
        {
            var code       = @"class MyClass<T> extends MyOtherClass {
    SomeMethod(): string {
        let x = <string><any>"""";
        return <string><any>"""";
    }
}";
            var beautifier = new TsBeautifier().Configure(x => x.OpenBlockOnNewLine = true);
            var result     = beautifier.Beautify(code);

            Assert.AreEqual(
                @"class MyClass<T> extends MyOtherClass
{
    SomeMethod(): string
    {
        let x = <string><any>"""";
        return <string><any>"""";
    }
}",
                result);
        }
Example #19
0
        public void TestKeyWordInLiteral()
        {
            var typescript = @"        switch (type) {
            case ""number"":
                return """" + key;
            case ""string"":
                return <string><any>key;
            case ""function"": {
                let name = key[""ClassName""] || key[""Name""] || key[""name""];//TypeInfo.NameOf(key);
                if (!name) {
                    name = TypeInfo.NameOf(key);
                }
                if (name) {
                    return name;
                }
            }
                break;
}";
            var beautifier = new TsBeautifier();
            var result     = beautifier.Beautify(typescript);

            Assert.AreEqual(@"switch (type) {
case ""number"":
    return """" + key;
case ""string"":
    return <string><any>key;
case ""function"":
    {
        let name = key[""ClassName""] || key[""Name""] || key[""name""]; //TypeInfo.NameOf(key);
        if (!name) {
            name = TypeInfo.NameOf(key);
        }
        if (name) {
            return name;
        }
    }
    break;
}", result);
        }
Example #20
0
 public void TestHugeStringConversion()
 {
     var typescript = HugeString.TypeScript;
     var beautifier = new TsBeautifier();
     var result     = beautifier.Beautify(typescript);
 }