Skip to content

Commit bca4490

Browse files
committed
Add extension methods for new properties of existing types
1 parent 9473a4c commit bca4490

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Lightup
5+
{
6+
using System;
7+
using Microsoft.CodeAnalysis;
8+
using Microsoft.CodeAnalysis.CSharp.Syntax;
9+
10+
internal static class MemberDeclarationSyntaxExtensions
11+
{
12+
private static readonly Func<MemberDeclarationSyntax, SyntaxList<AttributeListSyntax>> AttributeListsAccessor;
13+
private static readonly Func<MemberDeclarationSyntax, SyntaxTokenList> ModifiersAccessor;
14+
private static readonly Func<MemberDeclarationSyntax, SyntaxList<AttributeListSyntax>, MemberDeclarationSyntax> WithAttributeListsAccessor;
15+
private static readonly Func<MemberDeclarationSyntax, SyntaxTokenList, MemberDeclarationSyntax> WithModifiersAccessor;
16+
17+
static MemberDeclarationSyntaxExtensions()
18+
{
19+
AttributeListsAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<MemberDeclarationSyntax, SyntaxList<AttributeListSyntax>>(typeof(MemberDeclarationSyntax), nameof(AttributeLists));
20+
ModifiersAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<MemberDeclarationSyntax, SyntaxTokenList>(typeof(MemberDeclarationSyntax), nameof(Modifiers));
21+
WithAttributeListsAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<MemberDeclarationSyntax, SyntaxList<AttributeListSyntax>>(typeof(MemberDeclarationSyntax), nameof(AttributeLists));
22+
WithModifiersAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<MemberDeclarationSyntax, SyntaxTokenList>(typeof(MemberDeclarationSyntax), nameof(Modifiers));
23+
}
24+
25+
public static SyntaxList<AttributeListSyntax> AttributeLists(this MemberDeclarationSyntax syntax)
26+
{
27+
return AttributeListsAccessor(syntax);
28+
}
29+
30+
public static SyntaxTokenList Modifiers(this MemberDeclarationSyntax syntax)
31+
{
32+
return ModifiersAccessor(syntax);
33+
}
34+
35+
public static MemberDeclarationSyntax WithAttributeLists(this MemberDeclarationSyntax syntax, SyntaxList<AttributeListSyntax> attributeLists)
36+
{
37+
return WithAttributeListsAccessor(syntax, attributeLists);
38+
}
39+
40+
public static MemberDeclarationSyntax WithModifiers(this MemberDeclarationSyntax syntax, SyntaxTokenList modifiers)
41+
{
42+
return WithModifiersAccessor(syntax, modifiers);
43+
}
44+
45+
public static MemberDeclarationSyntax AddAttributeLists(this MemberDeclarationSyntax syntax, params AttributeListSyntax[] items)
46+
{
47+
return syntax.WithAttributeLists(syntax.AttributeLists().AddRange(items));
48+
}
49+
50+
public static MemberDeclarationSyntax AddModifiers(this MemberDeclarationSyntax syntax, params SyntaxToken[] items)
51+
{
52+
return syntax.WithModifiers(syntax.Modifiers().AddRange(items));
53+
}
54+
}
55+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright (c) Tunnel Vision Laboratories, LLC. All Rights Reserved.
2+
// Licensed under the MIT License. See LICENSE in the project root for license information.
3+
4+
namespace StyleCop.Analyzers.Lightup
5+
{
6+
using System;
7+
using Microsoft.CodeAnalysis;
8+
using Microsoft.CodeAnalysis.CSharp.Syntax;
9+
10+
internal static class StatementSyntaxExtensions
11+
{
12+
private static readonly Func<StatementSyntax, SyntaxList<AttributeListSyntax>> AttributeListsAccessor;
13+
private static readonly Func<StatementSyntax, SyntaxList<AttributeListSyntax>, StatementSyntax> WithAttributeListsAccessor;
14+
15+
static StatementSyntaxExtensions()
16+
{
17+
AttributeListsAccessor = LightupHelpers.CreateSyntaxPropertyAccessor<StatementSyntax, SyntaxList<AttributeListSyntax>>(typeof(StatementSyntax), nameof(AttributeLists));
18+
WithAttributeListsAccessor = LightupHelpers.CreateSyntaxWithPropertyAccessor<StatementSyntax, SyntaxList<AttributeListSyntax>>(typeof(StatementSyntax), nameof(AttributeLists));
19+
}
20+
21+
public static SyntaxList<AttributeListSyntax> AttributeLists(this StatementSyntax syntax)
22+
{
23+
return AttributeListsAccessor(syntax);
24+
}
25+
26+
public static StatementSyntax WithAttributeLists(this StatementSyntax syntax, SyntaxList<AttributeListSyntax> attributeLists)
27+
{
28+
return WithAttributeListsAccessor(syntax, attributeLists);
29+
}
30+
31+
public static StatementSyntax AddAttributeLists(this StatementSyntax syntax, params AttributeListSyntax[] items)
32+
{
33+
return syntax.WithAttributeLists(syntax.AttributeLists().AddRange(items));
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)