예제 #1
0
        private void ReformatDirectory(string inp, string output) {
            FormattingOptions options = new FormattingOptions() { SpacesPerIndent = 2, SpaceAfterFunctionInAnonymousFunctions = false };
            foreach (var file in Directory.GetFiles(inp, "*.js", SearchOption.AllDirectories)) {
                var newCode = FormatCode(File.ReadAllText(file), options);
                var outFile = Path.Combine(output, file.Substring(inp.Length));
                Directory.CreateDirectory(Path.GetDirectoryName(outFile));

                File.WriteAllText(outFile, newCode);
            }
        }
 public static Edit[] GetEditsAfterKeystroke(string code, int position, char ch, FormattingOptions options = null) {
     using (new DebugTimer("FormatKeyStroke")) {
         if (ch == ';' || ch == '}') {
             var ast = new JSParser(code).Parse(new CodeSettings() { AllowShebangLine = true });
             var visitor = new RangeVisitor(ch, position, ast);
             ast.Walk(visitor);
             if (visitor.Span != default(IndexSpan)) {
                 return FilterRange(
                     visitor.Span.Start,
                     visitor.Span.End,
                     GetEdits(code, options, ast)
                 );
             }
         }
         return new Edit[0];
     }
 }
        public void TypeScriptPortedTests() {
            dynamic testCases = _serializer.DeserializeObject("[" + File.ReadAllText("ruleFormattingTests.json") + "]");
            
            var options = new FormattingOptions();
            int testCaseId = 0;
            foreach (var testCase in testCases) {
                string filename = "fob" + testCaseId++ + ".js";
                Console.WriteLine(testCase["input"]);
                var buffer = new MockTextBuffer(testCase["input"], "test.js", "Node.js");
                if (!ApplyEdits(filename, testCase, buffer)) {
                    continue;
                }

                Assert.AreEqual(
                    ((string)testCase["expected"]).Replace("\r\n", "\n").Replace("\n", "\r\n"),
                    buffer.CurrentSnapshot.GetText().Replace("\r\n", "\n").Replace("\n", "\r\n")
                );
            }
        }
예제 #4
0
        public void TestInsertSpaces() {
            var options = new FormattingOptions() { SpacesPerIndent = 2 };
            TestCode(
"switch (abc) {\r\n\tcase 42: break;\r\n}",
"switch (abc) {\r\n  case 42: break;\r\n}",
                options
            );

            TestCode(
                "switch (abc) {\r\n\t\tcase 42: break;\r\n}",
                "switch (abc) {\r\n  case 42: break;\r\n}",
                options
            );

            options = new FormattingOptions() { SpacesPerIndent = 6 };
            TestCode(
                "switch (abc) {\r\n\tcase 42: break;\r\n}",
                "switch (abc) {\r\n      case 42: break;\r\n}",
                options
            );

            TestCode(
                "switch (abc) {\r\n    case 42: break;\r\n}",
                "switch (abc) {\r\n      case 42: break;\r\n}",
                options
            );
        }
예제 #5
0
        public void TestComments() {
            // comments in weird spots can result in some slightly odd 
            // insertions or missing insertions.  These aren't set in stone
            // necessarily but these test cases make sure we're not doing
            // anything particularly horrible.  The current behavior is
            // mostly driven by whether or not we're scanning forwards or
            // backwards to replace a particular piece of white space.
            var options = new FormattingOptions() { SpaceAfterOpeningAndBeforeClosingNonEmptyParenthesis = true };
            TestCode(
@"if (/*comment*/true/*comment*/) {
}",
@"if (/*comment*/true /*comment*/) {
}",
  options);

            TestCode(
@"switch (abc) /*comment*/ {
    case 'abc': break;
}",
@"switch (abc) /*comment*/ {
    case 'abc': break;
}");

            TestCode(
@"switch (abc) /*comment*/
{
    case 'abc': break;
}",
@"switch (abc) /*comment*/ {
    case 'abc': break;
}");

            TestCode(
@"var x = 1, /* comment */
    y = 2;",
@"var x = 1, /* comment */
    y = 2;"
);

            TestCode(
@"var x = 1, /* comment */y = 2;",
@"var x = 1, /* comment */ y = 2;"
);

            TestCode(
@"x = a/*comment*/+/*comment*/b;",
@"x = a /*comment*/+/*comment*/ b;"
);

            TestCode(
@"x = a/*comment*/+/*comment*/
      b;",
@"x = a /*comment*/+/*comment*/
      b;"
);

            TestCode(
@"x = a/*comment*/+
      /*comment*/b;",
@"x = a /*comment*/+
      /*comment*/ b;"
);
        }
예제 #6
0
 public FormattingVisitor(string code, JsAst tree, FormattingOptions options = null, bool onEnter = false) {
     _code = code;
     _options = options ?? new FormattingOptions();
     _onEnter = onEnter;
     _tree = tree;
 }
예제 #7
0
        public void TestControlFlowBraceCombo() {
            var options = new FormattingOptions() { OpenBracesOnNewLineForControl = false, SpaceAfterKeywordsInControlFlowStatements = false };

            TestCode(
@"do {
} while (true);",
@"do {
} while (true);",
                options
            );

            TestCode(
@"try {
} finally {
}",
@"try {
} finally {
}",
                options
            );
        }
예제 #8
0
 private static string FormatEnter(string code, int start, int end, FormattingOptions options) {
     var ast = new JSParser(code).Parse(new CodeSettings());
     var edits = Formatter.GetEditsAfterEnter(code, start, end, options);
     return ApplyEdits(code, edits);
 }
예제 #9
0
 private static string FormatCode(string code, FormattingOptions options) {
     var ast = new JSParser(code).Parse(new CodeSettings());
     var edits = Formatter.GetEditsForDocument(code, options);
     return ApplyEdits(code, edits);
 }
예제 #10
0
 private static void TestCode(int position, char ch, string code, string expected, FormattingOptions options = null) {
     Assert.AreEqual(expected, FormatCode(code, position, ch, options));
 }
 private static void FormatSelectionTest(string input, string expected, int start, int end, FormattingOptions options = null) {
     string pathToFile = "fob.py";
     var buffer = new MockTextBuffer(input, pathToFile, "Node.js");
     var edits = Formatter.GetEditsForRange(
         buffer.CurrentSnapshot.GetText(),
         start,
         end,
         options ?? new FormattingOptions()
     );
     EditFilter.ApplyEdits(buffer, edits);
     Assert.AreEqual(expected, buffer.CurrentSnapshot.GetText());
 }
 public static Edit[] GetEditsForRange(string code, int start, int end, FormattingOptions options = null) {
     using (new DebugTimer("FormatRange")) {
         var edits = GetEdits(code, options);
         return FilterRange(start, end, edits);
     }
 }
 /// <summary>
 /// Returns a list of edits which should be applied to the specified code.
 /// </summary>
 public static Edit[] GetEditsForDocument(string code, FormattingOptions options = null) {
     using (new DebugTimer("FormatDocument")) {
         var edits = GetEdits(code, options);
         return edits.ToArray();
     }
 }
 private static List<Edit> GetEdits(string code, FormattingOptions options, JsAst ast, bool onEnter = false) {
     var visitor = new FormattingVisitor(code, ast, options, onEnter);
     visitor.Format(ast);
     return visitor.Edits;
 }
 private static List<Edit> GetEdits(string code, FormattingOptions options, bool onEnter = false) {
     var ast = new JSParser(code).Parse(new CodeSettings() { AllowShebangLine = true });
     return GetEdits(code, options, ast, onEnter);
 }
예제 #16
0
        public void TestNewLineBracesForFlowControl2() {
            var options = new FormattingOptions() { OpenBracesOnNewLineForControl = false };
            TestCode(
            @"switch (abc)
{
    case 42: break;
}",
            @"switch (abc) {
    case 42: break;
}",
                    options);

            TestCode(
@"do
{
} while(true);",
@"do {
} while(true);",
               options);

            TestCode(
@"while (true)
{
}",
@"while (true) {
}",
               options);

            TestCode(
@"with (true)
{
}",
@"with (true) {
}",
               options);

            TestCode(
@"for (var i = 0; i < 10; i++)
{
}",
@"for (var i = 0; i < 10; i++) {
}",
               options);

            TestCode(
@"for (var x in [])
{
}",
@"for (var x in []) {
}",
                options);

            TestCode(
@"if (true)
{
}",
@"if (true) {
}",
        options);

            TestCode(
@"if (true)
{
} else
{
}",
@"if (true) {
} else {
}",
        options);

            TestCode(
@"try
{
} finally
{
}",
@"try {
} finally {
}",
        options);

            TestCode(
@"try
{
} catch(abc)
{
}",
@"try {
} catch (abc) {
}",
        options);
        }
예제 #17
0
        private static void TestCode(string code, string expected, FormattingOptions options = null) {
            var firstFormat = FormatCode(code, options);
            Assert.AreEqual(expected, firstFormat);

            // TODO: We should reenable this once we get this to work.  At the time of removing this TestInvalidTrailingQuote 
            // failed due to this...

            // a second call to format on a formatted code should have no changes
            //var secondFormat = FormatCode(firstFormat, options);
            //Assert.AreEqual(firstFormat, secondFormat, "First and Second call to format had different results...");

        }
예제 #18
0
        public void TestFor() {
            var options = new FormattingOptions() { SpaceAfterSemiColonInFor = true };
            TestCode(
@"for (var i = 0;i < 10;i++) {
}",
@"for (var i = 0; i < 10; i++) {
}",
  options);

            options = new FormattingOptions() { SpaceAfterSemiColonInFor = false };
            TestCode(
@"for (var i = 0; i < 10; i++) {
}",
@"for (var i = 0;i < 10;i++) {
}",
  options);
        }
예제 #19
0
 private static void TestEnter(int start, int end, string code, string expected, FormattingOptions options = null) {
     Assert.AreEqual(expected, FormatEnter(code, start, end, options));
 }
예제 #20
0
        public void TestSpaceAfterComma() {
            var options = new FormattingOptions() { SpaceAfterComma = true };
            TestCode(
@"
1,2,3
x(1,2,3)
function x(a,b,c) {
}",
@"
1, 2, 3
x(1, 2, 3)
function x(a, b, c) {
}",
  options);

            options = new FormattingOptions() { SpaceAfterComma = false };
            TestCode(
@"
1, 2, 3
x(1, 2, 3)
function x(a, b, c) {
}",
@"
1,2,3
x(1,2,3)
function x(a,b,c) {
}",
                options);
        }
예제 #21
0
 private static string FormatCode(string code, int position, char ch, FormattingOptions options) {
     var ast = new JSParser(code).Parse(new CodeSettings());
     var edits = Formatter.GetEditsAfterKeystroke(code, position, ch, options);
     return ApplyEdits(code, edits);
 }
예제 #22
0
        public void TestSpaceAfterFunctionInAnonymousFunctions() {
            var options = new FormattingOptions() { SpaceAfterFunctionInAnonymousFunctions = true };
            TestCode(
@"
x = function() {
}",
@"
x = function () {
}",
  options);

            options = new FormattingOptions() { SpaceAfterFunctionInAnonymousFunctions = false };
            TestCode(
@"
x = function () {
}",
@"
x = function() {
}",

                options);
        }
예제 #23
0
        public void TestNewLineBracesForFunctions() {
            var options = new FormattingOptions() { OpenBracesOnNewLineForFunctions = true };

            TestCode(
@"function x() {
}",
@"function x()
{
}",
               options);

            options = new FormattingOptions() { OpenBracesOnNewLineForFunctions = false };
            TestCode(
@"function x()
{
}",
@"function x() {
}",
               options);
        }
예제 #24
0
        public void TestInsertTabs() {
            var options = new FormattingOptions() { SpacesPerIndent = null };
            TestCode(
@"switch (abc) {
    case 42: break;
}",
"switch (abc) {\r\n\tcase 42: break;\r\n}",
                options
            );

            TestCode(
                "switch (abc) {\r\n\tcase 42: break;\r\n}",
                "switch (abc) {\r\n\tcase 42: break;\r\n}",
                options
            );

            TestCode(
@"switch (abc) {
  case 42: break;
}",
"switch (abc) {\r\n\tcase 42: break;\r\n}",
                options
            );
        }
예제 #25
0
        public void TestSpaceAfterOpeningAndBeforeClosingNonEmptyParenthesis() {
            var options = new FormattingOptions() { SpaceAfterOpeningAndBeforeClosingNonEmptyParenthesis = true };

            TestCode(
@"for (var x in abc) {
}",
@"for ( var x in abc ) {
}",
                options
            );


            TestCode(
@"for (var i = 0; i < 10; i++) {
}",
@"for ( var i = 0; i < 10; i++ ) {
}",
                options
            );

            TestCode(
@"if (true) {
}",
@"if ( true ) {
}",
                options
            );

            TestCode(
@"while (true) {
}",
@"while ( true ) {
}",
                options
            );

            TestCode(
@"do {
} while (true);",
@"do {
} while ( true );",
                options
            );

            TestCode(
@"try {
} catch (foo) {
}",
@"try {
} catch ( foo ) {
}",
                options
            );

            TestCode(
@"function () {
}",
@"function () {
}",
                options
            );

            TestCode(
@"function (  ) {
}",
@"function () {
}",
                options
            );

            TestCode(
@"function (a) {
}",
@"function ( a ) {
}",
                options
            );

            TestCode(
@"function (a, b) {
}",
@"function ( a, b ) {
}",
        options
    );

            TestCode(
@"(a)",
@"( a )",
        options
    );

            TestCode(
@"f(a)",
@"f( a )",
        options
    );

            TestCode(
@"f(a, b)",
@"f( a, b )",
        options
    );

            TestCode(
@"new f(a)",
@"new f( a )",
       options
   );

            TestCode(
@"new f(a, b)",
@"new f( a, b )",
        options
    );

            TestCode(
@"f[a]",
@"f[a]",
        options
    );

            TestCode(
@"f[a, b]",
@"f[a, b]",
        options
    );

            TestCode(
@"switch (abc) {
    case 42: break;
}",
@"switch ( abc ) {
    case 42: break;
}",
        options
    );

            TestCode(
@"(x)",
@"( x )",
        options
    );
        }
예제 #26
0
 public static Edit[] GetEditsAfterKeystroke(string code, int position, char ch, FormattingOptions options = null)
 {
     using (new DebugTimer("FormatKeyStroke")) {
         if (ch == ';' || ch == '}')
         {
             var ast = new JSParser(code).Parse(new CodeSettings()
             {
                 AllowShebangLine = true
             });
             var visitor = new RangeVisitor(ch, position, ast);
             ast.Walk(visitor);
             if (visitor.Span != default(IndexSpan))
             {
                 return(FilterRange(
                            visitor.Span.Start,
                            visitor.Span.End,
                            GetEdits(code, options, ast)
                            ));
             }
         }
         return(new Edit[0]);
     }
 }