Example #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="OrderingSettings"/> class.
        /// </summary>
        /// <param name="orderingSettingsObject">The JSON object containing the settings.</param>
        protected internal OrderingSettings(JsonObject orderingSettingsObject)
            : this()
        {
            foreach (var kvp in orderingSettingsObject)
            {
                switch (kvp.Key)
                {
                case "elementOrder":
                    kvp.AssertIsArray();
                    foreach (var value in kvp.Value.AsJsonArray)
                    {
                        this.elementOrder.Add(value.ToEnumValue <OrderingTrait>(kvp.Key));
                    }

                    break;

                case "systemUsingDirectivesFirst":
                    this.systemUsingDirectivesFirst = kvp.ToBooleanValue();
                    break;

                case "usingDirectivesPlacement":
                    this.usingDirectivesPlacement = kvp.ToEnumValue <UsingDirectivesPlacement>();
                    break;

                case "blankLinesBetweenUsingGroups":
                    this.blankLinesBetweenUsingGroups = kvp.ToEnumValue <OptionSetting>();
                    break;

                default:
                    break;
                }
            }
        }
Example #2
0
 protected internal OrderingSettings()
 {
     this.elementOrder = ImmutableArray <OrderingTrait> .Empty;
     this.systemUsingDirectivesFirst   = true;
     this.usingDirectivesPlacement     = UsingDirectivesPlacement.InsideNamespace;
     this.blankLinesBetweenUsingGroups = OptionSetting.Allow;
 }
        public async Task VerifyUsingReorderingWithoutMovingWithFileHeaderAsync()
        {
            var testCode = @"// This is a file header.

using Microsoft.CodeAnalysis;
using System;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"// This is a file header.

using System;
using Microsoft.CodeAnalysis;

namespace Foo
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);

            await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
        }
Example #4
0
        public async Task VerifyUsingReorderingWithoutMovingWithFileHeaderAsync()
        {
            var testCode = @"// This is a file header.

using Microsoft.CodeAnalysis;
using System;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"// This is a file header.

using System;
using Microsoft.CodeAnalysis;

namespace Foo
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            var expected = StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(4, 1).WithArguments("System", "Microsoft.CodeAnalysis");

            await this.VerifyCSharpFixAsync(testCode, new[] { expected }, fixedTestCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
Example #5
0
        public async Task VerifyUsingReorderingWithoutMovingAsync()
        {
            var testCode = @"using Microsoft.CodeAnalysis;
using SystemAction = System.Action;
using static System.Math;
using System;

using static System.String;
using MyFunc = System.Func<int, bool>;

using System.Collections.Generic;
using System.Collections;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using static System.Math;
using static System.String;
using MyFunc = System.Func<int, bool>;
using SystemAction = System.Action;

namespace Foo
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            DiagnosticResult[] expected =
            {
                StyleCopDiagnosticVerifier <SA1216UsingStaticDirectivesMustBePlacedAtTheCorrectLocation> .Diagnostic().WithLocation(3,                                                   1),
                StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(4,                      1).WithArguments("System","System.Math"),
                StyleCopDiagnosticVerifier <SA1209UsingAliasDirectivesMustBePlacedAfterOtherUsingDirectives> .Diagnostic().WithLocation(7,                                               1),
                StyleCopDiagnosticVerifier <SA1211UsingAliasDirectivesMustBeOrderedAlphabeticallyByAliasName> .Diagnostic().WithLocation(7,                       1).WithArguments("MyFunc","SystemAction"),
                StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(9,  1).WithArguments("System.Collections.Generic","System.Math"),
                StyleCopDiagnosticVerifier <SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace> .Diagnostic().WithLocation(9,                                                   1),
                StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(10,         1).WithArguments("System.Collections","System.Math"),
            };
            await this.VerifyCSharpFixAsync(testCode, expected, fixedTestCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
Example #6
0
        protected internal OrderingSettings(JsonObject orderingSettingsObject, AnalyzerConfigOptionsWrapper analyzerConfigOptions)
        {
            ImmutableArray <OrderingTrait> .Builder elementOrder = null;
            bool?systemUsingDirectivesFirst = null;
            UsingDirectivesPlacement?usingDirectivesPlacement     = null;
            OptionSetting?           blankLinesBetweenUsingGroups = null;

            foreach (var kvp in orderingSettingsObject)
            {
                switch (kvp.Key)
                {
                case "elementOrder":
                    kvp.AssertIsArray();
                    elementOrder = ImmutableArray.CreateBuilder <OrderingTrait>();
                    foreach (var value in kvp.Value.AsJsonArray)
                    {
                        elementOrder.Add(value.ToEnumValue <OrderingTrait>(kvp.Key));
                    }

                    break;

                case "systemUsingDirectivesFirst":
                    systemUsingDirectivesFirst = kvp.ToBooleanValue();
                    break;

                case "usingDirectivesPlacement":
                    usingDirectivesPlacement = kvp.ToEnumValue <UsingDirectivesPlacement>();
                    break;

                case "blankLinesBetweenUsingGroups":
                    blankLinesBetweenUsingGroups = kvp.ToEnumValue <OptionSetting>();
                    break;

                default:
                    break;
                }
            }

            systemUsingDirectivesFirst ??= AnalyzerConfigHelper.TryGetBooleanValue(analyzerConfigOptions, "dotnet_sort_system_directives_first");
            usingDirectivesPlacement ??= AnalyzerConfigHelper.TryGetStringValueAndNotification(analyzerConfigOptions, "csharp_using_directive_placement") switch
            {
                ("inside_namespace", _) => UsingDirectivesPlacement.InsideNamespace,
                ("outside_namespace", _) => UsingDirectivesPlacement.OutsideNamespace,
                _ => null,
            };
        public async Task VerifyUsingReorderingWithoutMovingAsync()
        {
            var testCode = @"using Microsoft.CodeAnalysis;
using SystemAction = System.Action;
using static System.Math;
using System;

using static System.String;
using MyFunc = System.Func<int, bool>;

using System.Collections.Generic;
using System.Collections;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.CodeAnalysis;
using static System.Math;
using static System.String;
using MyFunc = System.Func<int, bool>;
using SystemAction = System.Action;

namespace Foo
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);

            await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
        }
        public async Task VerifyUsingReorderingWithoutMovingWithMultiLineFileHeaderAsync()
        {
            var testCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
//   Copyright (c) FooCorp. All rights reserved.
// </copyright>

using Microsoft.CodeAnalysis;
using System;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
//   Copyright (c) FooCorp. All rights reserved.
// </copyright>

using System;
using Microsoft.CodeAnalysis;

namespace Foo
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);

            await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
        }
Example #9
0
        public async Task VerifyUsingReorderingWithoutMovingWithMultiLineCommentAsync()
        {
            var testCode = @"/*
 * Copyright by FooCorp Inc.
 */

using System;
using Microsoft.CodeAnalysis;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"/*
 * Copyright by FooCorp Inc.
 */

using Microsoft.CodeAnalysis;
using System;

namespace Foo
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            var expected = StyleCopDiagnosticVerifier <SA1210UsingDirectivesMustBeOrderedAlphabeticallyByNamespace> .Diagnostic().WithLocation(5, 1);

            await this.VerifyCSharpFixAsync(testCode, new[] { expected }, fixedTestCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
Example #10
0
        public async Task VerifyUsingReorderingWithoutMovingWithMultiLineFileHeaderAsync()
        {
            var testCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
//   Copyright (c) FooCorp. All rights reserved.
// </copyright>

using Microsoft.CodeAnalysis;
using System;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
//   Copyright (c) FooCorp. All rights reserved.
// </copyright>

using System;
using Microsoft.CodeAnalysis;

namespace Foo
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            var expected = StyleCopDiagnosticVerifier <SA1208SystemUsingDirectivesMustBePlacedBeforeOtherUsingDirectives> .Diagnostic().WithLocation(6, 1).WithArguments("System", "Microsoft.CodeAnalysis");

            await this.VerifyCSharpFixAsync(testCode, new[] { expected }, fixedTestCode, DiagnosticResult.EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
        }
        public async Task VerifyUsingReorderingWithoutMovingWithMultiLineCommentAsync()
        {
            var testCode = @"/*
 * Copyright by FooCorp Inc.
 */

using System;
using Microsoft.CodeAnalysis;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"/*
 * Copyright by FooCorp Inc.
 */

using Microsoft.CodeAnalysis;
using System;

namespace Foo
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
        }
        public async Task VerifyUsingReorderingWithoutMovingAsync()
        {
            var testCode = @"using Microsoft.CodeAnalysis;
using SystemAction = System.Action;
using static System.Math;
using System;

using static System.String;
using MyFunc = System.Func<int,bool>;

using System.Collections.Generic;
using System.Collections;

namespace NamespaceName
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"using Microsoft.CodeAnalysis;
using System;
using System.Collections;
using System.Collections.Generic;
using static System.Math;
using static System.String;
using MyFunc = System.Func<int, bool>;
using SystemAction = System.Action;

namespace NamespaceName
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
        }
        private static string DetermineIndentation(CompilationUnitSyntax compilationUnit, IndentationSettings indentationSettings, UsingDirectivesPlacement usingDirectivesPlacement)
        {
            string usingsIndentation;

            if (usingDirectivesPlacement == UsingDirectivesPlacement.InsideNamespace)
            {
                var rootNamespace    = compilationUnit.Members.First(member => BaseNamespaceDeclarationSyntaxWrapper.IsInstance(member));
                var indentationLevel = IndentationHelper.GetIndentationSteps(indentationSettings, rootNamespace);
                if (!rootNamespace.IsKind(SyntaxKindEx.FileScopedNamespaceDeclaration))
                {
                    indentationLevel++;
                }

                usingsIndentation = IndentationHelper.GenerateIndentationString(indentationSettings, indentationLevel);
            }
            else
            {
                usingsIndentation = string.Empty;
            }

            return(usingsIndentation);
        }
 protected internal OrderingSettings()
 {
     this.elementOrder = ImmutableArray.CreateBuilder <OrderingTrait>();
     this.systemUsingDirectivesFirst = true;
     this.usingDirectivesPlacement   = UsingDirectivesPlacement.InsideNamespace;
 }
        public async Task VerifyUsingReorderingWithoutMovingWithMultiLineFileHeaderAsync()
        {
            var testCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
//   Copyright (c) FooCorp. All rights reserved.
// </copyright>

using System;
using Microsoft.CodeAnalysis;

namespace Foo
{
    public class Bar
    {
    }
}
";

            var fixedTestCode = @"// <copyright file=""Test0.cs"" company=""FooCorp"">
//   Copyright (c) FooCorp. All rights reserved.
// </copyright>

using Microsoft.CodeAnalysis;
using System;

namespace Foo
{
    public class Bar
    {
    }
}
";

            this.usingDirectivesPlacement = UsingDirectivesPlacement.OutsideNamespace;
            await this.VerifyCSharpDiagnosticAsync(fixedTestCode, EmptyDiagnosticResults, CancellationToken.None).ConfigureAwait(false);
            await this.VerifyCSharpFixAsync(testCode, fixedTestCode).ConfigureAwait(false);
        }
Example #16
0
        private static string DetermineIndentation(CompilationUnitSyntax compilationUnit, IndentationSettings indentationSettings, UsingDirectivesPlacement usingDirectivesPlacement)
        {
            string usingsIndentation;

            if (usingDirectivesPlacement == UsingDirectivesPlacement.InsideNamespace)
            {
                var rootNamespace    = compilationUnit.Members.OfType <NamespaceDeclarationSyntax>().First();
                var indentationLevel = IndentationHelper.GetIndentationSteps(indentationSettings, rootNamespace);
                usingsIndentation = IndentationHelper.GenerateIndentationString(indentationSettings, indentationLevel + 1);
            }
            else
            {
                usingsIndentation = string.Empty;
            }

            return(usingsIndentation);
        }
 protected internal OrderingSettings()
 {
     this.elementOrder = ImmutableArray.CreateBuilder<OrderingTrait>();
     this.systemUsingDirectivesFirst = true;
     this.usingDirectivesPlacement = UsingDirectivesPlacement.InsideNamespace;
 }