Skip to content

Commit 808641e

Browse files
committed
tests tests tests tests
1 parent 3cdda1e commit 808641e

36 files changed

+2111
-192
lines changed

Discord.Net.ComponentDesigner.sln.DotSettings.user

Lines changed: 52 additions & 0 deletions
Large diffs are not rendered by default.

src/Discord.Net.ComponentDesigner.Generator/Constants.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static class Constants
1616
public const int SELECT_MIN_VALUES = 0;
1717
public const int SELECT_MAX_VALUES = 25;
1818

19-
public const int MAX_MEDIA_ITEMS = 25;
19+
public const int MAX_MEDIA_ITEMS = 10;
2020
public const int MAX_MEDIA_ITEM_DESCRIPTION_LENGTH = 1024;
2121

2222
public const int MAX_SECTION_CHILDREN = 3;

src/Discord.Net.ComponentDesigner.Generator/Diagnostics.cs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -360,15 +360,6 @@ public static Diagnostic CreateParsingDiagnostic(CXDiagnostic diagnostic, Locati
360360
true
361361
);
362362

363-
public static readonly DiagnosticDescriptor ButtonCannotHaveALabelAndEmoji = new(
364-
"DC0039",
365-
"Buttons can't have a label and emoji",
366-
"Buttons can't have both a label and an emoji",
367-
"Components",
368-
DiagnosticSeverity.Error,
369-
true
370-
);
371-
372363
public static readonly DiagnosticDescriptor CardinalityForcedToRuntime = new(
373364
"DC0040",
374365
"Cardinality forced to runtime check",
@@ -386,4 +377,31 @@ public static Diagnostic CreateParsingDiagnostic(CXDiagnostic diagnostic, Locati
386377
DiagnosticSeverity.Error,
387378
true
388379
);
380+
381+
public static readonly DiagnosticDescriptor ComponentDoesntAllowChildren = new(
382+
"DC0042",
383+
"Component doesn't allow children",
384+
"'{0}' doesn't allow children",
385+
"Components",
386+
DiagnosticSeverity.Error,
387+
true
388+
);
389+
390+
public static readonly DiagnosticDescriptor MediaGalleryIsEmpty = new(
391+
"DC0043",
392+
"Empty media gallery",
393+
"A media gallery must have at least one 'media-gallery-item'",
394+
"Components",
395+
DiagnosticSeverity.Error,
396+
true
397+
);
398+
399+
public static readonly DiagnosticDescriptor TooManyItemsInMediaGallery = new(
400+
"DC0043",
401+
"Too many items in media gallery",
402+
$"A media gallery can have at most {Constants.MAX_MEDIA_ITEMS} 'media-gallery-item's",
403+
"Components",
404+
DiagnosticSeverity.Error,
405+
true
406+
);
389407
}

src/Discord.Net.ComponentDesigner.Generator/Discord.Net.ComponentDesigner.Generator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
<IncludeBuildOutput>false</IncludeBuildOutput>
1313
<PublishSingleFile>true</PublishSingleFile>
14+
<NoWarn>RS1035</NoWarn>
1415
</PropertyGroup>
1516

1617
<ItemGroup>

src/Discord.Net.ComponentDesigner.Generator/Graph/CXGraph.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ namespace Discord.CX;
1212

1313
public readonly struct CXGraph
1414
{
15-
1615
public bool HasErrors => Diagnostics.Any(x => x.Severity is DiagnosticSeverity.Error);
1716
public IReadOnlyList<Diagnostic> Diagnostics
1817
=> [.._diagnostics, ..RootNodes.SelectMany(x => x.Diagnostics)];

src/Discord.Net.ComponentDesigner.Generator/Graph/CXGraphManager.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,14 @@ private RenderedInterceptor CreateRender(CancellationToken token = default)
167167
UsesDesigner
168168
);
169169
}
170-
170+
171171
var context = new ComponentContext(Graph);
172172

173+
foreach (var node in Graph.RootNodes)
174+
{
175+
node.UpdateState(context);
176+
}
177+
173178
Graph.Validate(context);
174179

175180
if (context.HasErrors || Graph.HasErrors)

src/Discord.Net.ComponentDesigner.Generator/Nodes/ComponentNode.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public sealed override string Render(ComponentState state, ComponentContext cont
3232

3333
public virtual void Validate(TState state, ComponentContext context)
3434
{
35+
base.Validate(state, context);
3536
}
3637

3738
public sealed override void Validate(ComponentState state, ComponentContext context)
@@ -47,6 +48,8 @@ public abstract class ComponentNode
4748

4849
public virtual IReadOnlyList<ComponentProperty> Properties { get; } = [];
4950

51+
protected virtual bool AllowChildrenInCX => HasChildren;
52+
5053
public virtual void Validate(ComponentState state, ComponentContext context)
5154
{
5255
// validate properties
@@ -62,9 +65,9 @@ public virtual void Validate(ComponentState state, ComponentContext context)
6265
}
6366
}
6467

65-
// report any unknown properties
6668
if (state.Source is CXElement element)
6769
{
70+
// report any unknown properties
6871
foreach (var attribute in element.Attributes)
6972
{
7073
if (!TryGetPropertyFromName(attribute.Identifier.Value, out _))
@@ -77,6 +80,16 @@ public virtual void Validate(ComponentState state, ComponentContext context)
7780
);
7881
}
7982
}
83+
84+
// report invalid children
85+
if (!AllowChildrenInCX && !HasChildren && element.Children.Count > 0)
86+
{
87+
context.AddDiagnostic(
88+
Diagnostics.ComponentDoesntAllowChildren,
89+
element.Children,
90+
Name
91+
);
92+
}
8093
}
8194
}
8295

src/Discord.Net.ComponentDesigner.Generator/Nodes/ComponentProperty.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public sealed class ComponentProperty
1919
public IReadOnlyList<string> Aliases { get; }
2020

2121
public bool IsOptional { get; }
22+
public bool RequiresValue { get; }
2223
public string DotnetPropertyName { get; }
2324
public string DotnetParameterName { get; }
2425
public PropertyRenderer Renderer { get; }
@@ -28,6 +29,7 @@ public sealed class ComponentProperty
2829
public ComponentProperty(
2930
string name,
3031
bool isOptional = false,
32+
bool requiresValue = true,
3133
IEnumerable<string>? aliases = null,
3234
IEnumerable<PropertyValidator>? validators = null,
3335
PropertyRenderer? renderer = null,
@@ -38,6 +40,7 @@ public ComponentProperty(
3840
Name = name;
3941
Aliases = [..aliases ?? []];
4042
IsOptional = isOptional;
43+
RequiresValue = requiresValue;
4144
DotnetPropertyName = dotnetPropertyName ?? name;
4245
DotnetParameterName = dotnetParameterName ?? name;
4346
Renderer = renderer ?? Renderers.CreateDefault(this);

src/Discord.Net.ComponentDesigner.Generator/Nodes/ComponentPropertyValue.cs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public CXValue? Value
2222

2323
public bool HasValue => Value is not null;
2424

25+
public bool CanOmitFromSource => Property.IsOptional && !IsSpecified;
26+
2527
public bool TryGetLiteralValue(out string value)
2628
{
2729
switch (Value)
@@ -58,17 +60,14 @@ public void ReportPropertyConfigurationDiagnostics(
5860
);
5961
}
6062

61-
if (requiresValue.HasValue)
63+
if (requiresValue.HasValue && Attribute is not null && Value is null)
6264
{
63-
if (Value is null or CXValue.Invalid && requiresValue.Value)
64-
{
65-
context.AddDiagnostic(
66-
Diagnostics.MissingRequiredProperty,
67-
Attribute ?? state.Source,
68-
state.OwningNode?.Inner.Name,
69-
Property.Name
70-
);
71-
}
65+
context.AddDiagnostic(
66+
Diagnostics.MissingRequiredProperty,
67+
Attribute ?? state.Source,
68+
state.OwningNode?.Inner.Name,
69+
Property.Name
70+
);
7271
}
7372
}
7473
}

src/Discord.Net.ComponentDesigner.Generator/Nodes/ComponentState.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void RequireOneOf(ComponentContext context, params ReadOnlySpan<Component
8484

8585
sb.Append(properties[i].Name);
8686

87-
if (sb.Length < properties.Length - 1) sb.Append('\'');
87+
if (i < properties.Length - 1) sb.Append('\'');
8888
}
8989

9090
context.AddDiagnostic(
@@ -135,17 +135,16 @@ public string RenderProperties(
135135

136136
var propertyValue = GetProperty(property);
137137

138-
if (propertyValue?.Value is null) continue;
139-
138+
if(propertyValue.CanOmitFromSource) continue;
139+
140140
var prefix = asInitializers
141141
? $"{property.DotnetPropertyName} = "
142142
: $"{property.DotnetParameterName}: ";
143143

144144
values.Add($"{prefix}{property.Renderer(context, propertyValue)}");
145145
}
146146

147-
var joiner = asInitializers ? string.Empty : ",";
148-
return string.Join($"{joiner}\n", values);
147+
return string.Join($",{Environment.NewLine}", values);
149148
}
150149

151150
public string RenderInitializer(
@@ -175,7 +174,7 @@ public string RenderChildren(ComponentContext context, Func<CXGraph.Node, bool>?
175174
if (predicate is not null) children = children.Where(predicate);
176175

177176
return string.Join(
178-
",\n",
177+
$",{Environment.NewLine}",
179178
children.Select(x => x.Render(context))
180179
);
181180
}

0 commit comments

Comments
 (0)