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

Commit 59b7e82

Browse files
committed
Update command line parsing
The command line parsing code didn't reflect the current state of the tool. The <rule types> switch for instance was only used to add in the xUnit conversion rules which are no longer a part of the code style tool. This and some other dead code was removed and it now reflects the real feature set of the tool.
1 parent 0f924a6 commit 59b7e82

File tree

9 files changed

+37
-60
lines changed

9 files changed

+37
-60
lines changed

src/CodeFormatter/Program.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@ private static int Main(string[] args)
2424
{
2525
if (args.Length < 1)
2626
{
27-
Console.WriteLine("CodeFormatter <project or solution> [<rule types>] [/file:<filename>] [/nocopyright] [/tables] [/c:<config1,config2> [/copyright:file]");
28-
Console.WriteLine(" <rule types> - Rule types to use in addition to the default ones.");
29-
Console.WriteLine(" Use ConvertTests to convert MSTest tests to xUnit.");
27+
Console.WriteLine("CodeFormatter <project or solution> [/file:<filename>] [/nocopyright] [/nounicode] [/tables] [/c:<config1,config2> [/copyright:file]");
3028
Console.WriteLine(" <filename> - Only apply changes to files with specified name.");
3129
Console.WriteLine(" <configs> - Additional preprocessor configurations the formatter");
3230
Console.WriteLine(" should run under.");
3331
Console.WriteLine(" <copyright> - Specifies file containing copyright header.");
32+
Console.WriteLine(" Use ConvertTests to convert MSTest tests to xUnit.");
3433
Console.WriteLine(" <tables> - Let tables opt out of formatting by defining DOTNET_FORMATTER");
34+
Console.WriteLine(" <nounicode> - Do not convert unicode strings to escape sequences");
35+
Console.WriteLine(" <nocopyright>- Do not update the copyright message.");
3536
return -1;
3637
}
3738

@@ -46,6 +47,7 @@ private static int Main(string[] args)
4647
var ruleTypeBuilder = ImmutableArray.CreateBuilder<string>();
4748
var configBuilder = ImmutableArray.CreateBuilder<string[]>();
4849
var copyrightHeader = FormattingConstants.DefaultCopyrightHeader;
50+
var convertUnicode = true;
4951
var allowTables = false;
5052
var comparer = StringComparer.OrdinalIgnoreCase;
5153

@@ -82,6 +84,10 @@ private static int Main(string[] args)
8284
{
8385
copyrightHeader = ImmutableArray<string>.Empty;
8486
}
87+
else if (comparer.Equals(arg, "/nounicode"))
88+
{
89+
convertUnicode = false;
90+
}
8591
else if (comparer.Equals(arg, "/tables"))
8692
{
8793
allowTables = true;
@@ -106,6 +112,7 @@ private static int Main(string[] args)
106112
configBuilder.ToImmutableArray(),
107113
copyrightHeader,
108114
allowTables,
115+
convertUnicode,
109116
ct).Wait(ct);
110117
Console.WriteLine("Completed formatting.");
111118
return 0;
@@ -132,6 +139,7 @@ private static async Task RunAsync(
132139
ImmutableArray<string[]> preprocessorConfigurations,
133140
ImmutableArray<string> copyrightHeader,
134141
bool allowTables,
142+
bool convertUnicode,
135143
CancellationToken cancellationToken)
136144
{
137145
var workspace = MSBuildWorkspace.Create();
@@ -140,6 +148,7 @@ private static async Task RunAsync(
140148
engine.FileNames = fileNames;
141149
engine.CopyrightHeader = copyrightHeader;
142150
engine.AllowTables = allowTables;
151+
engine.ConvertUnicodeCharacters = convertUnicode;
143152

144153
string extension = Path.GetExtension(projectOrSolutionPath);
145154
if (StringComparer.OrdinalIgnoreCase.Equals(extension, ".sln"))

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(); }
13+
get { return new Rules.NonAsciiCharactersAreEscapedInLiterals(new Options() { ConvertUnicodeCharacters = true }); }
1414
}
1515

1616
[Fact]

src/Microsoft.DotNet.CodeFormatting/FormattingEngine.cs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,9 @@ public static class FormattingEngine
1717
public static IFormattingEngine Create(ImmutableArray<string> ruleTypes)
1818
{
1919
var catalog = new AssemblyCatalog(typeof(FormattingEngine).Assembly);
20-
21-
var ruleTypesHash = new HashSet<string>(ruleTypes, StringComparer.InvariantCultureIgnoreCase);
22-
var notFoundRuleTypes = new HashSet<string>(ruleTypes, StringComparer.InvariantCultureIgnoreCase);
23-
24-
var filteredCatalog = new FilteredCatalog(catalog, cpd =>
25-
{
26-
if (cpd.ExportDefinitions.Any(em =>
27-
em.ContractName == AttributedModelServices.GetContractName(typeof(ISyntaxFormattingRule)) ||
28-
em.ContractName == AttributedModelServices.GetContractName(typeof(ILocalSemanticFormattingRule)) ||
29-
em.ContractName == AttributedModelServices.GetContractName(typeof(IGlobalSemanticFormattingRule)) ||
30-
em.ContractName == AttributedModelServices.GetContractName(typeof(IFormattingFilter))))
31-
{
32-
object ruleType;
33-
if (cpd.Metadata.TryGetValue(RuleTypeConstants.PartMetadataKey, out ruleType))
34-
{
35-
if (ruleType is string)
36-
{
37-
notFoundRuleTypes.Remove((string)ruleType);
38-
if (!ruleTypesHash.Contains((string)ruleType))
39-
{
40-
return false;
41-
}
42-
}
43-
}
44-
}
45-
46-
return true;
47-
});
48-
49-
var container = new CompositionContainer(filteredCatalog);
20+
var container = new CompositionContainer(catalog);
5021
var engine = container.GetExportedValue<IFormattingEngine>();
5122
var consoleFormatLogger = new ConsoleFormatLogger();
52-
53-
// Need to do this after the catalog is queried, otherwise the lambda won't have been run
54-
foreach (var notFoundRuleType in notFoundRuleTypes)
55-
{
56-
consoleFormatLogger.WriteErrorLine("The specified rule type was not found: {0}", notFoundRuleType);
57-
}
58-
5923
return engine;
6024
}
6125
}

src/Microsoft.DotNet.CodeFormatting/FormattingEngineImplementation.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ public bool AllowTables
6464
get { return _allowTables; }
6565
set { _allowTables = value; }
6666
}
67+
68+
public bool ConvertUnicodeCharacters
69+
{
70+
get { return _options.ConvertUnicodeCharacters; }
71+
set { _options.ConvertUnicodeCharacters = value; }
72+
}
6773

6874
[ImportingConstructor]
6975
internal FormattingEngineImplementation(

src/Microsoft.DotNet.CodeFormatting/IFormattingEngine.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public interface IFormattingEngine
1616
ImmutableArray<string[]> PreprocessorConfigurations { get; set; }
1717
ImmutableArray<string> FileNames { get; set; }
1818
bool AllowTables { get; set; }
19+
bool ConvertUnicodeCharacters { get; set; }
1920
Task FormatSolutionAsync(Solution solution, CancellationToken cancellationToken);
2021
Task FormatProjectAsync(Project porject, CancellationToken cancellationToken);
2122
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,6 @@
138138
<Compile Include="Rules\NonAsciiCharactersAreEscapedInLiteralsRule.cs" />
139139
<Compile Include="Rules\ExplicitThisRule.cs" />
140140
<Compile Include="Rules\RuleOrder.cs" />
141-
<Compile Include="RuleTypeConstants.cs" />
142141
</ItemGroup>
143142
<ItemGroup>
144143
<None Include="app.config" />

src/Microsoft.DotNet.CodeFormatting/Options.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ internal sealed class Options
2424

2525
internal IFormatLogger FormatLogger { get; set; }
2626

27+
internal bool ConvertUnicodeCharacters { get; set; }
28+
2729
[ImportingConstructor]
2830
internal Options()
2931
{
3032
CopyrightHeader = FormattingConstants.DefaultCopyrightHeader;
3133
FileNames = ImmutableArray<string>.Empty;
3234
PreprocessorConfigurations = ImmutableArray<string[]>.Empty;
3335
FormatLogger = new ConsoleFormatLogger();
36+
ConvertUnicodeCharacters = true;
3437
}
3538
}
3639
}

src/Microsoft.DotNet.CodeFormatting/RuleTypeConstants.cs

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,21 @@ namespace Microsoft.DotNet.CodeFormatting.Rules
1818
[SyntaxRuleOrder(SyntaxRuleOrder.NonAsciiChractersAreEscapedInLiterals)]
1919
internal sealed class NonAsciiCharactersAreEscapedInLiterals : CSharpOnlyFormattingRule, ISyntaxFormattingRule
2020
{
21+
private readonly Options _options;
22+
23+
[ImportingConstructor]
24+
internal NonAsciiCharactersAreEscapedInLiterals(Options options)
25+
{
26+
_options = options;
27+
}
28+
2129
public SyntaxNode Process(SyntaxNode root, string languageName)
2230
{
31+
if (!_options.ConvertUnicodeCharacters)
32+
{
33+
return root;
34+
}
35+
2336
return UnicodeCharacterEscapingSyntaxRewriter.Rewriter.Visit(root);
2437
}
2538

0 commit comments

Comments
 (0)