Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 76a8ab7

Browse files
committed
Changed enable / disable switch implementation
The switches for enabling / disabling common rule implementations is now changed over to use the rule disabling mechanism.
1 parent 6a82211 commit 76a8ab7

File tree

9 files changed

+13
-39
lines changed

9 files changed

+13
-39
lines changed

src/CodeFormatter/Program.cs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,9 @@ Use ConvertTests to convert MSTest tests to xUnit.
6969

7070
var fileNamesBuilder = ImmutableArray.CreateBuilder<string>();
7171
var configBuilder = ImmutableArray.CreateBuilder<string[]>();
72-
var copyrightHeader = FormattingConstants.DefaultCopyrightHeader;
72+
var copyrightHeader = FormattingDefaults.DefaultCopyrightHeader;
7373
var ruleMap = ImmutableDictionary<string, bool>.Empty;
7474
var language = LanguageNames.CSharp;
75-
var convertUnicode = true;
7675
var allowTables = false;
7776
var verbose = false;
7877

@@ -111,11 +110,11 @@ Use ConvertTests to convert MSTest tests to xUnit.
111110
}
112111
else if (comparer.Equals(arg, "/nocopyright"))
113112
{
114-
copyrightHeader = ImmutableArray<string>.Empty;
113+
ruleMap = ruleMap.SetItem(FormattingDefaults.CopyrightRuleName, false);
115114
}
116115
else if (comparer.Equals(arg, "/nounicode"))
117116
{
118-
convertUnicode = false;
117+
ruleMap = ruleMap.SetItem(FormattingDefaults.UnicodeLiteralsRuleName, false);
119118
}
120119
else if (comparer.Equals(arg, "/verbose"))
121120
{
@@ -152,7 +151,6 @@ Use ConvertTests to convert MSTest tests to xUnit.
152151
ruleMap,
153152
language,
154153
allowTables,
155-
convertUnicode,
156154
verbose);
157155
}
158156

@@ -183,7 +181,6 @@ private static int RunFormat(
183181
ImmutableDictionary<string, bool> ruleMap,
184182
string language,
185183
bool allowTables,
186-
bool convertUnicode,
187184
bool verbose)
188185
{
189186
var cts = new CancellationTokenSource();
@@ -201,7 +198,6 @@ private static int RunFormat(
201198
ruleMap,
202199
language,
203200
allowTables,
204-
convertUnicode,
205201
verbose,
206202
ct).Wait(ct);
207203
Console.WriteLine("Completed formatting.");
@@ -230,7 +226,6 @@ private static async Task<int> RunFormatAsync(
230226
ImmutableDictionary<string, bool> ruleMap,
231227
string language,
232228
bool allowTables,
233-
bool convertUnicode,
234229
bool verbose,
235230
CancellationToken cancellationToken)
236231
{
@@ -239,7 +234,6 @@ private static async Task<int> RunFormatAsync(
239234
engine.FileNames = fileNames;
240235
engine.CopyrightHeader = copyrightHeader;
241236
engine.AllowTables = allowTables;
242-
engine.ConvertUnicodeCharacters = convertUnicode;
243237
engine.Verbose = verbose;
244238

245239
if (!SetRuleMap(engine, ruleMap))

src/Microsoft.DotNet.CodeFormatting.Tests/Rules/NonAsciiCharactersAreEscapedInLiteralsRuleTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public class NonAsciiCharactersAreEscapedInLiteralsTests : SyntaxRuleTestBase
1010
{
1111
internal override ISyntaxFormattingRule Rule
1212
{
13-
get { return new Rules.NonAsciiCharactersAreEscapedInLiterals(new Options() { ConvertUnicodeCharacters = true }); }
13+
get { return new Rules.NonAsciiCharactersAreEscapedInLiterals(); }
1414
}
1515

1616
[Fact]

src/Microsoft.DotNet.CodeFormatting/FormattingConstants.cs renamed to src/Microsoft.DotNet.CodeFormatting/FormattingDefaults.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010

1111
namespace Microsoft.DotNet.CodeFormatting
1212
{
13-
public static class FormattingConstants
13+
public static class FormattingDefaults
1414
{
15+
public const string UnicodeLiteralsRuleName = "UnicodeLiterals";
16+
public const string CopyrightRuleName = "Copyright";
17+
1518
private static readonly string[] s_defaultCopyrightHeader =
1619
{
1720
"// Copyright (c) Microsoft. All rights reserved.",
@@ -20,7 +23,7 @@ public static class FormattingConstants
2023

2124
public static readonly ImmutableArray<string> DefaultCopyrightHeader;
2225

23-
static FormattingConstants()
26+
static FormattingDefaults()
2427
{
2528
DefaultCopyrightHeader = ImmutableArray.CreateRange(s_defaultCopyrightHeader);
2629
}

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,6 @@ public bool Verbose
7373
set { _verbose = value; }
7474
}
7575

76-
public bool ConvertUnicodeCharacters
77-
{
78-
get { return _options.ConvertUnicodeCharacters; }
79-
set { _options.ConvertUnicodeCharacters = value; }
80-
}
81-
8276
public ImmutableArray<IRuleMetadata> AllRules
8377
{
8478
get

src/Microsoft.DotNet.CodeFormatting/IFormattingEngine.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public interface IFormattingEngine
1717
ImmutableArray<string> FileNames { get; set; }
1818
ImmutableArray<IRuleMetadata> AllRules { get; }
1919
bool AllowTables { get; set; }
20-
bool ConvertUnicodeCharacters { get; set; }
2120
bool Verbose { get; set; }
2221
void ToggleRuleEnabled(IRuleMetadata ruleMetaData, bool enabled);
2322
Task FormatSolutionAsync(Solution solution, CancellationToken cancellationToken);

src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<Compile Include="SyntaxUtil.cs" />
9393
<Compile Include="Filters\FilenameFilter.cs" />
9494
<Compile Include="Filters\UsableFileFilter.cs" />
95-
<Compile Include="FormattingConstants.cs" />
95+
<Compile Include="FormattingDefaults.cs" />
9696
<Compile Include="FormattingEngine.cs" />
9797
<Compile Include="FormattingEngineImplementation.cs" />
9898
<Compile Include="IFormatLogger.cs" />

src/Microsoft.DotNet.CodeFormatting/Options.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,13 @@ internal sealed class Options
2727

2828
internal IFormatLogger FormatLogger { get; set; }
2929

30-
internal bool ConvertUnicodeCharacters { get; set; }
31-
3230
[ImportingConstructor]
3331
internal Options()
3432
{
35-
CopyrightHeader = FormattingConstants.DefaultCopyrightHeader;
33+
CopyrightHeader = FormattingDefaults.DefaultCopyrightHeader;
3634
FileNames = ImmutableArray<string>.Empty;
3735
PreprocessorConfigurations = ImmutableArray<string[]>.Empty;
3836
FormatLogger = new ConsoleFormatLogger();
39-
ConvertUnicodeCharacters = true;
4037
}
4138
}
4239
}

src/Microsoft.DotNet.CodeFormatting/Rules/CopyrightHeaderRule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.DotNet.CodeFormatting.Rules
1515
[SyntaxRule(CopyrightHeaderRule.Name, CopyrightHeaderRule.Description, SyntaxRuleOrder.CopyrightHeaderRule)]
1616
internal sealed partial class CopyrightHeaderRule : SyntaxFormattingRule, ISyntaxFormattingRule
1717
{
18-
internal const string Name = "Copyright";
18+
internal const string Name = FormattingDefaults.CopyrightRuleName;
1919
internal const string Description = "Insert the copyright header into every file";
2020

2121
private abstract class CommonRule

src/Microsoft.DotNet.CodeFormatting/Rules/NonAsciiCharactersAreEscapedInLiteralsRule.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,11 @@ namespace Microsoft.DotNet.CodeFormatting.Rules
1717
[SyntaxRule(NonAsciiCharactersAreEscapedInLiterals.Name, NonAsciiCharactersAreEscapedInLiterals.Description, SyntaxRuleOrder.NonAsciiChractersAreEscapedInLiterals)]
1818
internal sealed class NonAsciiCharactersAreEscapedInLiterals : CSharpOnlyFormattingRule, ISyntaxFormattingRule
1919
{
20-
internal const string Name = "UnicodeLiterals";
20+
internal const string Name = FormattingDefaults.UnicodeLiteralsRuleName;
2121
internal const string Description = "Use unicode escape sequence instead of unicode literals";
2222

23-
private readonly Options _options;
24-
25-
[ImportingConstructor]
26-
internal NonAsciiCharactersAreEscapedInLiterals(Options options)
27-
{
28-
_options = options;
29-
}
30-
3123
public SyntaxNode Process(SyntaxNode root, string languageName)
3224
{
33-
if (!_options.ConvertUnicodeCharacters)
34-
{
35-
return root;
36-
}
37-
3825
return UnicodeCharacterEscapingSyntaxRewriter.Rewriter.Visit(root);
3926
}
4027

0 commit comments

Comments
 (0)