Skip to content

Commit ad1b9d5

Browse files
committed
Use collection expressions
1 parent 39cce5b commit ad1b9d5

File tree

17 files changed

+64
-65
lines changed

17 files changed

+64
-65
lines changed

src/DocoptNet.Playground/Pages/Index.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,5 +196,5 @@ Options:
196196
}
197197
}
198198

199-
static readonly char[] NewLineChars = { '\r', '\n' };
199+
static readonly char[] NewLineChars = ['\r', '\n'];
200200
}

src/DocoptNet/CodeGeneration/CSharpSourceBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ public CSharpSourceBuilder this[IEnumerable<CSharpSourceBuilder> codes]
9494
public CSharpSourceBuilder NewLine { get { AppendLine(); return this; } }
9595

9696
static readonly char[] ForbiddenRegularStringLiteralChars =
97-
{
97+
[
9898
'"', '\\', '\r', '\n',
9999
'\u0085', // Next line character
100100
'\u2028', // Line separator character
101101
'\u2029', // Paragraph separator character
102-
};
102+
];
103103

104104
const string Quote = "\"";
105105

src/DocoptNet/CodeGeneration/SourceGenerator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ sealed class SyntaxReceiver : ISyntaxContextReceiver
4343
{
4444
ModelSymbols? _modelSymbols;
4545

46-
public List<(ClassDeclarationSyntax Class, AttributeData Attributes)> ClassAttributes { get; } = new();
46+
public List<(ClassDeclarationSyntax Class, AttributeData Attributes)> ClassAttributes { get; } = [];
4747

4848
sealed class ModelSymbols
4949
{
@@ -203,15 +203,15 @@ enum GenerationOptions
203203
}
204204

205205
public static SourceText Generate(string? ns, string name, SourceText text) =>
206-
Generate(ns, name, Enumerable.Empty<string>(), null, text, GenerationOptions.None);
206+
Generate(ns, name, [], null, text, GenerationOptions.None);
207207

208208
static SourceText Generate(string? ns, string name, IEnumerable<string> parents, string? helpConstName,
209209
SourceText text, GenerationOptions generationOptions) =>
210210
Generate(ns, name, parents, helpConstName, text, null, generationOptions);
211211

212212
public static SourceText Generate(string? ns, string name,
213213
SourceText text, Encoding? outputEncoding) =>
214-
Generate(ns, name, Enumerable.Empty<string>(), null, text, outputEncoding, GenerationOptions.None);
214+
Generate(ns, name, [], null, text, outputEncoding, GenerationOptions.None);
215215

216216
static SourceText Generate(string? ns, string name, IEnumerable<string> parents, string? helpConstName,
217217
SourceText text, Encoding? outputEncoding, GenerationOptions options)

src/DocoptNet/Docopt.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ static IEnumerable<Pattern> ParseExpr(Tokens tokens, ICollection<Option> options
314314
}
315315
result = result.Distinct().ToList();
316316
if (result.Count > 1)
317-
return new[] {new Either(result.ToArray())};
317+
return [new Either(result.ToArray())];
318318
return result;
319319
}
320320

@@ -327,7 +327,7 @@ static ICollection<Pattern> ParseSeq(Tokens tokens, ICollection<Option> options)
327327
var atom = ParseAtom(tokens, options);
328328
if (tokens.Current() == "...")
329329
{
330-
atom = new[] { new OneOrMore(atom.ToArray()) };
330+
atom = [new OneOrMore(atom.ToArray())];
331331
tokens.Move();
332332
}
333333
result.AddRange(atom);
@@ -396,7 +396,7 @@ static IEnumerable<Option> ParseShorts(Tokens tokens, ICollection<Option> option
396396

397397
var token = tokens.Move() ?? throw new NullReferenceException();
398398
Debug.Assert(token.StartsWith("-") && !token.StartsWith("--"));
399-
var left = token.TrimStart(new[] {'-'});
399+
var left = token.TrimStart(['-']);
400400
var parsed = new List<Option>();
401401
while (left != "")
402402
{
@@ -500,7 +500,7 @@ static IEnumerable<Option> ParseLong(Tokens tokens, ICollection<Option> options)
500500
if (tokens.ThrowsInputError)
501501
option.Value = value is { } v ? v : ArgValue.True;
502502
}
503-
return new[] {option};
503+
return [option];
504504
}
505505

506506
internal static ICollection<Option> ParseDefaults(string doc)

src/DocoptNet/Internals/BranchPatterns.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override ICollection<Pattern> Flat(params Type[] types)
3636
if (types == null) throw new ArgumentNullException(nameof(types));
3737
if (types.Contains(GetType()))
3838
{
39-
return new Pattern[] { this };
39+
return [this];
4040
}
4141
return Children.SelectMany(child => child.Flat(types)).ToList();
4242
}

src/DocoptNet/Internals/LeafPattern.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public override ICollection<Pattern> Flat(params Type[] types)
2727
if (types == null) throw new ArgumentNullException(nameof(types));
2828
if (types.Length == 0 || types.Contains(GetType()))
2929
{
30-
return new Pattern[] { this };
30+
return [this];
3131
}
3232
return Array.Empty<Pattern>();
3333
}

src/DocoptNet/Internals/Option.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public override string ToString()
5050

5151
const string DescSeparator = " ";
5252

53-
static readonly char[] OptionDelimiters = { ' ', '\t', ',', '=' };
53+
static readonly char[] OptionDelimiters = [' ', '\t', ',', '='];
5454

5555
public static Option Parse(string optionDescription)
5656
{

src/DocoptNet/Internals/Pattern.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public static Either Transform(Pattern pattern)
9797
/// </summary>
9898
public IEnumerable<Pattern> Flat()
9999
{
100-
return Flat(new Type[0]);
100+
return Flat([]);
101101
}
102102
}
103103
}

tests/DocoptNet.Tests/CodeGeneration/LanguageAgnosticTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class LanguageAgnosticTests
1919
[Test]
2020
[TestCaseSource(typeof(Tests.LanguageAgnosticTests),
2121
nameof(Tests.LanguageAgnosticTests.Docopt),
22-
new object[] { "testcases.docopt" })]
22+
["testcases.docopt"])]
2323
public void Test(string doc, string cmdLine, string expected)
2424
{
2525
const string main = @"

tests/DocoptNet.Tests/CodeGeneration/SourceGeneratorTests.cs

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ naval_fate.exe mine (set|remove) <x> <y> [--moored | --drifting]
4848
[Test]
4949
public void Generate_with_usage_in_external_file()
5050
{
51-
AssertMatchesSnapshot(new[]
52-
{
51+
AssertMatchesSnapshot(
52+
[
5353
("Program.docopt.txt", SourceText.From(NavalFateUsage))
54-
});
54+
]);
5555
}
5656

5757
[Test]
@@ -68,38 +68,38 @@ public void Generate_with_empty_root_namespace()
6868
[Test]
6969
public void Generate_with_inline_usage()
7070
{
71-
AssertMatchesSnapshot(new[]
72-
{
71+
AssertMatchesSnapshot(
72+
[
7373
("Program.cs", SourceText.From($@"
7474
[DocoptNet.DocoptArguments]
7575
partial class Arguments
7676
{{
7777
public const string Help = @""{NavalFateUsage}"";
7878
}}"))
79-
});
79+
]);
8080
}
8181

8282
[Test]
8383
public void Generate_with_inline_usage_with_custom_const_name()
8484
{
85-
AssertMatchesSnapshot(new[]
86-
{
85+
AssertMatchesSnapshot(
86+
[
8787
("Program.cs", SourceText.From($@"
8888
[DocoptNet.DocoptArguments(HelpConstName = nameof(HelpText))]
8989
partial class Arguments
9090
{{
9191
public const string HelpText = @""{NavalFateUsage}"";
9292
}}"))
93-
});
93+
]);
9494
}
9595

9696
[Test]
9797
public void Generate_with_multiple_inline_usages()
9898
{
9999
const string help = "Usage: my_program (run [--fast] | jump [--high])";
100100

101-
AssertMatchesSnapshot(new[]
102-
{
101+
AssertMatchesSnapshot(
102+
[
103103
("Program.cs", SourceText.From(@"
104104
using System;
105105
using DocoptNet;
@@ -133,14 +133,14 @@ partial class Arguments8 { public const string Help = @""" + help + @"""; }
133133
134134
[Arguments]
135135
partial class Arguments8 { }"))
136-
});
136+
]);
137137
}
138138

139139
[Test]
140140
public void Generate_with_inline_usage_missing_help_const()
141141
{
142-
AssertMatchesSnapshot(new[]
143-
{
142+
AssertMatchesSnapshot(
143+
[
144144
("Program.cs", SourceText.From(@"
145145
[DocoptNet.DocoptArguments]
146146
partial class Arguments1 { }
@@ -150,14 +150,14 @@ partial class Arguments2 { public const string Help = @""Usage: program""; }
150150
151151
[DocoptNet.DocoptArguments]
152152
partial class Arguments3 { public string Help => @""Usage: program""; }"))
153-
});
153+
]);
154154
}
155155

156156
[Test]
157157
public void Generate_with_nested_namespace()
158158
{
159-
AssertMatchesSnapshot(new[]
160-
{
159+
AssertMatchesSnapshot(
160+
[
161161
("Program.cs", SourceText.From(@"
162162
namespace Outer
163163
{
@@ -167,14 +167,14 @@ namespace Inner
167167
partial class Arguments { const string Help = @""Usage: program""; }
168168
}
169169
}"))
170-
});
170+
]);
171171
}
172172

173173
[Test]
174174
public void Generate_with_classes_from_different_namespaces_share_the_same_name()
175175
{
176-
AssertMatchesSnapshot(new[]
177-
{
176+
AssertMatchesSnapshot(
177+
[
178178
("Program.cs", SourceText.From(@"
179179
[DocoptNet.DocoptArguments]
180180
sealed partial class ProgramArguments
@@ -199,14 +199,14 @@ sealed partial class ProgramArguments
199199
const string Help = ""Usage: program"";
200200
}
201201
}"))
202-
});
202+
]);
203203
}
204204

205205
[Test]
206206
public void Generate_with_nested_args_class()
207207
{
208-
AssertMatchesSnapshot(new[]
209-
{
208+
AssertMatchesSnapshot(
209+
[
210210
("Program.cs", SourceText.From(@"
211211
static partial class Program
212212
{
@@ -237,14 +237,14 @@ sealed partial class Arguments
237237
}
238238
}
239239
}"))
240-
});
240+
]);
241241
}
242242

243243
[Test]
244244
public void Generate_with_classes_in_separate_files()
245245
{
246-
AssertMatchesSnapshot(new[]
247-
{
246+
AssertMatchesSnapshot(
247+
[
248248
("File1.cs", SourceText.From(@"
249249
namespace Namespace1
250250
{
@@ -263,7 +263,7 @@ sealed partial class ProgramArguments
263263
const string Help = ""Usage: program"";
264264
}
265265
}")),
266-
});
266+
]);
267267
}
268268

269269
void AssertMatchesSnapshot((string Path, SourceText Text)[] sources,
@@ -365,7 +365,7 @@ orderby af
365365

366366
var expectedFiles = Directory.Exists(expectedSourcesPath)
367367
? EnumerateFiles(expectedSourcesPath)
368-
: Enumerable.Empty<string>();
368+
: [];
369369

370370
var results =
371371
expectedFiles.FullJoin(actualFiles,
@@ -539,7 +539,7 @@ public T Run<T>(DocoptOptions options, Func<IDictionary, T> selector, params str
539539
var method = _type.GetMethod(parserFactoryMethodName, BindingFlags.Public | BindingFlags.Static);
540540
if (method == null)
541541
throw new MissingMethodException(_type.Name, parserFactoryMethodName);
542-
var parser = (IBaselineParser<object>)method.Invoke(null, Array.Empty<object>())!;
542+
var parser = (IBaselineParser<object>)method.Invoke(null, [])!;
543543
parser = parser.WithOptions(parser.Options.WithOptionsFirst(options.OptionsFirst));
544544
var result = options.Help
545545
? (IParser<object>.IResult)parser.EnableHelp().Parse(argv)
@@ -611,7 +611,7 @@ internal static (CSharpGeneratorDriver, CSharpCompilation)
611611
additionalTexts.Select(at => KeyValuePair.Create(at, DocoptSourceItemTypeConfigOption))
612612
.ToImmutableDictionary());
613613

614-
var driver = CSharpGeneratorDriver.Create(new[] { generator },
614+
var driver = CSharpGeneratorDriver.Create([generator],
615615
additionalTexts,
616616
optionsProvider: optionsProvider);
617617

0 commit comments

Comments
 (0)