Skip to content

Commit

Permalink
[Rgen] Add helper properties to determine if a code change was for a …
Browse files Browse the repository at this point in the history
…static, abstract or partial class. (#21875)

We precalculate the values form the modifiers, this later makes is
simpler when writing the code generator to check those modifiers we are
interested in without falling in the trap of executing the same LINQ
query over an over.

---------

Co-authored-by: GitHub Actions Autoformatter <[email protected]>
  • Loading branch information
mandel-macaque and GitHub Actions Autoformatter authored Dec 31, 2024
1 parent 1e729bf commit 5974821
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/rgen/Microsoft.Macios.Generator/DataModel/CodeChanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,34 @@ readonly struct CodeChanges {
/// </summary>
public ImmutableArray<AttributeCodeChange> Attributes { get; init; } = [];

/// <summary>
/// True if the code changes are for a static symbol.
/// </summary>
public bool IsStatic { get; private init; }

/// <summary>
/// True if the code changes are for a partial symbol.
/// </summary>
public bool IsPartial { get; private init; }

/// <summary>
/// True if the code changes are for an abstract symbol.
/// </summary>
public bool IsAbstract { get; private init; }

readonly ImmutableArray<SyntaxToken> modifiers = [];
/// <summary>
/// Modifiers list.
/// </summary>
public ImmutableArray<SyntaxToken> Modifiers { get; init; } = [];
public ImmutableArray<SyntaxToken> Modifiers {
get => modifiers;
init {
modifiers = value;
IsStatic = value.Any (m => m.IsKind (SyntaxKind.StaticKeyword));
IsPartial = value.Any (m => m.IsKind (SyntaxKind.PartialKeyword));
IsAbstract = value.Any (m => m.IsKind (SyntaxKind.AbstractKeyword));
}
}

readonly ImmutableArray<EnumMember> enumMembers = [];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,4 +810,85 @@ void CodeChangesFromClassDeclaration (ApplePlatform platform, string inputText,
Assert.NotNull (changes);
Assert.Equal (expected, changes.Value, comparer);
}

[Fact]
public void IsStaticPropertyTest ()
{
var changes = new CodeChanges (
bindingType: BindingType.SmartEnum,
name: "name1",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.name1",
symbolAvailability: new ());

Assert.False (changes.IsStatic);

changes = new CodeChanges (
bindingType: BindingType.SmartEnum,
name: "name1",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.name1",
symbolAvailability: new ()) {
Modifiers = [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
SyntaxFactory.Token (SyntaxKind.StaticKeyword),
]
};

Assert.True (changes.IsStatic);
}

[Fact]
public void IsPartialPropertyTest ()
{
var changes = new CodeChanges (
bindingType: BindingType.SmartEnum,
name: "name1",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.name1",
symbolAvailability: new ());

Assert.False (changes.IsPartial);

changes = new CodeChanges (
bindingType: BindingType.SmartEnum,
name: "name1",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.name1",
symbolAvailability: new ()) {
Modifiers = [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
SyntaxFactory.Token (SyntaxKind.PartialKeyword),
]
};

Assert.True (changes.IsPartial);
}

[Fact]
public void IsAbstractPropertyTest ()
{
var changes = new CodeChanges (
bindingType: BindingType.SmartEnum,
name: "name1",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.name1",
symbolAvailability: new ());

Assert.False (changes.IsAbstract);

changes = new CodeChanges (
bindingType: BindingType.SmartEnum,
name: "name1",
@namespace: ["NS"],
fullyQualifiedSymbol: "NS.name1",
symbolAvailability: new ()) {
Modifiers = [
SyntaxFactory.Token (SyntaxKind.PublicKeyword),
SyntaxFactory.Token (SyntaxKind.AbstractKeyword),
]
};

Assert.True (changes.IsAbstract);
}
}

9 comments on commit 5974821

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.