Skip to content

Commit 13b4e07

Browse files
committed
diagnostic fixes
1 parent 6e072b3 commit 13b4e07

File tree

7 files changed

+256
-43
lines changed

7 files changed

+256
-43
lines changed

YarnSpinner.Compiler/Compiler.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public static CompilationResult Compile(CompilationJob compilationJob)
331331
// diagnostics for the affected expressions.
332332
foreach (var constraint in failingConstraints)
333333
{
334-
diagnostics.Add(new Yarn.Compiler.Diagnostic(constraint.SourceFileName, constraint.SourceContext, $"Expression failed to resolve in a reasonable time ({TypeSolverTimeLimit}). Try simplifying this expression."));
334+
diagnostics.Add(DiagnosticDescriptor.TypeSolverTimeout.Create(constraint.SourceFileName, constraint.SourceContext, TypeSolverTimeLimit.ToString()));
335335
}
336336
break;
337337
}
@@ -356,7 +356,7 @@ public static CompilationResult Compile(CompilationJob compilationJob)
356356
{
357357
foreach (var failureMessage in constraint.GetFailureMessages(typeSolution))
358358
{
359-
diagnostics.Add(new Yarn.Compiler.Diagnostic(constraint.SourceFileName, constraint.SourceRange, failureMessage) { Code = "YS0010" });
359+
diagnostics.Add(new Yarn.Compiler.Diagnostic(constraint.SourceFileName, constraint.SourceRange, failureMessage) { Code = DiagnosticDescriptor.TypeMismatch.Code });
360360
}
361361
}
362362
watchdog.Stop();
@@ -416,7 +416,7 @@ public static CompilationResult Compile(CompilationJob compilationJob)
416416
{
417417
var suggestion = decl.Name.StartsWith("$") ? $" For example: <<declare {decl.Name} = (initial value) >>" : string.Empty;
418418

419-
diagnostics.Add(new Diagnostic(decl.SourceFileName, decl.Range, $"Can't determine type of {decl.Name} given its usage. Manually specify its type with a declare statement.{suggestion}"));
419+
diagnostics.Add(new Diagnostic(decl.SourceFileName, decl.Range, $"Can't determine type of {decl.Name} given its usage. Manually specify its type with a declare statement.{suggestion}") { Code = DiagnosticDescriptor.TypeInferenceFailure.Code });
420420

421421
decl.Type = Types.Error;
422422
}
@@ -452,7 +452,7 @@ public static CompilationResult Compile(CompilationJob compilationJob)
452452
// Compile error!
453453
if (typedContext is ParserRuleContext parserRuleContext)
454454
{
455-
diagnostics.Add(new Diagnostic(parsedFile.Name, parserRuleContext, $"Can't determine the type of this expression."));
455+
diagnostics.Add(DiagnosticDescriptor.ExpressionTypeUndetermined.Create(parsedFile.Name, parserRuleContext));
456456
}
457457
else
458458
{
@@ -637,7 +637,7 @@ public static CompilationResult Compile(CompilationJob compilationJob)
637637

638638
if (declaration.DefaultValue == null)
639639
{
640-
diagnostics.Add(new Diagnostic($"Variable declaration {declaration.Name} (type {declaration.Type?.Name ?? "undefined"}) has a null default value. This is not allowed."));
640+
diagnostics.Add(DiagnosticDescriptor.NullDefaultValue.Create("(unknown)", declaration.Name, declaration.Type?.Name ?? "undefined"));
641641
continue;
642642
}
643643

@@ -743,7 +743,8 @@ private static void AddErrorsForSettingReadonlyVariables(List<FileParseResult> p
743743
file.Name,
744744
setStatement.variable(),
745745
$"{variableName} cannot be modified (it's a smart variable and is always equal to " +
746-
$"{smartVariables[variableName]?.InitialValueParserContext?.GetTextWithWhitespace() ?? "(unknown)"})"));
746+
$"{smartVariables[variableName]?.InitialValueParserContext?.GetTextWithWhitespace() ?? "(unknown)"})")
747+
{ Code = DiagnosticDescriptor.SmartVariableReadOnly.Code });
747748
}
748749
}
749750
});
@@ -820,7 +821,7 @@ private static void AddErrorsForInvalidNodeNames(List<FileParseResult> parseResu
820821

821822
foreach (var node in nodesWithIllegalTitleCharacters)
822823
{
823-
diagnostics.Add(new Diagnostic(node.File.Name, node.TitleHeader, $"The node '{node.Name}' contains illegal characters."));
824+
diagnostics.Add(DiagnosticDescriptor.InvalidNodeName.Create(node.File.Name, node.TitleHeader, "title", node.Name));
824825
}
825826

826827
var nodesByTitle = nodesWithNames.GroupBy(n => n.Name);
@@ -852,7 +853,7 @@ bool HasWhenHeader(YarnSpinnerParser.NodeContext nodeContext)
852853
// don't. Create errors for these others.
853854
foreach (var entry in group.Where(n => n.Node.GetWhenHeaders().Any() == false))
854855
{
855-
var d = new Diagnostic(entry.File.Name, entry.TitleHeader, $"All nodes in the group '{entry.Node.NodeTitle}' must have a 'when' clause (use 'when: always' if you want this node to not have any conditions)");
856+
var d = DiagnosticDescriptor.NodeGroupMissingWhen.Create(entry.File.Name, entry.TitleHeader, entry.Node.NodeTitle);
856857
diagnostics.Add(d);
857858
}
858859
}
@@ -878,15 +879,15 @@ bool HasWhenHeader(YarnSpinnerParser.NodeContext nodeContext)
878879
{
879880
foreach (var entry in group)
880881
{
881-
var d = new Diagnostic(entry.File.Name, entry.Node.GetHeader("subtitle"), $"More than one node in group {entry.Name} has subtitle {subtitle}");
882+
var d = DiagnosticDescriptor.DuplicateSubtitle.Create(entry.File.Name, entry.Node.GetHeader("subtitle"), entry.Name, subtitle);
882883
diagnostics.Add(d);
883884
}
884885
}
885886
if (invalidTitleCharacters.IsMatch(subtitle))
886887
{
887888
foreach (var entry in group)
888889
{
889-
diagnostics.Add(new Diagnostic(entry.File.Name, entry.Node.GetHeader("subtitle"), $"The node subtitle '{subtitle}' contains illegal characters."));
890+
diagnostics.Add(DiagnosticDescriptor.InvalidNodeName.Create(entry.File.Name, entry.Node.GetHeader("subtitle"), "subtitle", subtitle));
890891
}
891892
}
892893
}
@@ -897,7 +898,7 @@ bool HasWhenHeader(YarnSpinnerParser.NodeContext nodeContext)
897898
// More than one node has this name! Report an error on both.
898899
foreach (var entry in group)
899900
{
900-
var d = new Diagnostic(entry.File.Name, entry.TitleHeader, $"More than one node is named {entry.Name}") { Code = "YS0003" };
901+
var d = new Diagnostic(entry.File.Name, entry.TitleHeader, $"More than one node is named {entry.Name}") { Code = DiagnosticDescriptor.DuplicateNodeTitle.Code };
901902
diagnostics.Add(d);
902903
}
903904
}
@@ -907,11 +908,11 @@ bool HasWhenHeader(YarnSpinnerParser.NodeContext nodeContext)
907908
{
908909
if (node.Node.NodeTitle == null)
909910
{
910-
diagnostics.Add(new Diagnostic(node.File.Name, node.Node.body(), $"Nodes must have a title") { Code = "YS0011" });
911+
diagnostics.Add(new Diagnostic(node.File.Name, node.Node.body(), $"Nodes must have a title") { Code = DiagnosticDescriptor.DuplicateNodeTitle.Code });
911912
}
912913
if (node.Node.title_header().Length > 1)
913914
{
914-
diagnostics.Add(new Diagnostic(node.File.Name, node.Node.title_header()[1], $"Nodes must have a single title node") { Code = "YS0011" });
915+
diagnostics.Add(new Diagnostic(node.File.Name, node.Node.title_header()[1], $"Nodes must have a single title node") { Code = DiagnosticDescriptor.DuplicateNodeTitle.Code });
915916
}
916917
}
917918
}
@@ -933,7 +934,7 @@ private static HashSet<string> AddDiagnosticsForEmptyNodes(List<FileParseResult>
933934
foreach (var entry in empties)
934935
{
935936
var title = entry.Node.NodeTitle;
936-
var d = new Diagnostic(entry.File.Name, entry.Node, $"Node \"{title ?? "(missing title)"}\" is empty and will not be included in the compiled output.", Diagnostic.DiagnosticSeverity.Warning);
937+
var d = DiagnosticDescriptor.EmptyNode.Create(entry.File.Name, entry.Node, title ?? "(missing title)");
937938
diagnostics.Add(d);
938939
if (title != null)
939940
{
@@ -1000,7 +1001,7 @@ internal static (IEnumerable<Declaration>, IEnumerable<Diagnostic>) GetDeclarati
10001001
// that Yarn Spinner can use?
10011002
if (Types.TypeMappings.TryGetValue(method.ReturnType, out var yarnReturnType) == false)
10021003
{
1003-
diagnostics.Add(new Diagnostic($"Function {function.Key} cannot be used in Yarn Spinner scripts: {method.ReturnType} is not a valid return type."));
1004+
diagnostics.Add(DiagnosticDescriptor.InvalidLibraryFunction.Create("(unknown)", function.Key, $"{method.ReturnType} is not a valid return type."));
10041005
continue;
10051006
}
10061007

@@ -1024,13 +1025,13 @@ internal static (IEnumerable<Declaration>, IEnumerable<Diagnostic>) GetDeclarati
10241025

10251026
if (paramInfo.IsOptional)
10261027
{
1027-
diagnostics.Add(new Diagnostic($"Function {function.Key} cannot be used in Yarn Spinner scripts: parameter {paramInfo.Name} is optional, which isn't supported."));
1028+
diagnostics.Add(DiagnosticDescriptor.InvalidLibraryFunction.Create("(unknown)", function.Key, $"parameter {paramInfo.Name} is optional, which isn't supported."));
10281029
continue;
10291030
}
10301031

10311032
if (paramInfo.IsOut)
10321033
{
1033-
diagnostics.Add(new Diagnostic($"Function {function.Key} cannot be used in Yarn Spinner scripts: parameter {paramInfo.Name} is an out parameter, which isn't supported."));
1034+
diagnostics.Add(DiagnosticDescriptor.InvalidLibraryFunction.Create("(unknown)", function.Key, $"parameter {paramInfo.Name} is an out parameter, which isn't supported."));
10341035
continue;
10351036
}
10361037

@@ -1051,12 +1052,12 @@ internal static (IEnumerable<Declaration>, IEnumerable<Diagnostic>) GetDeclarati
10511052
}
10521053
else
10531054
{
1054-
diagnostics.Add(new Diagnostic($"Function {function.Key} cannot be used in Yarn Spinner scripts: params array {paramInfo.Name}'s type ({paramInfo.ParameterType}) cannot be used in Yarn functions"));
1055+
diagnostics.Add(DiagnosticDescriptor.InvalidLibraryFunction.Create("(unknown)", function.Key, $"params array {paramInfo.Name}'s type ({paramInfo.ParameterType}) cannot be used in Yarn functions"));
10551056
}
10561057
}
10571058
else if (Types.TypeMappings.TryGetValue(paramInfo.ParameterType, out var yarnParameterType) == false)
10581059
{
1059-
diagnostics.Add(new Diagnostic($"Function {function.Key} cannot be used in Yarn Spinner scripts: parameter {paramInfo.Name}'s type ({paramInfo.ParameterType}) cannot be used in Yarn functions"));
1060+
diagnostics.Add(DiagnosticDescriptor.InvalidLibraryFunction.Create("(unknown)", function.Key, $"parameter {paramInfo.Name}'s type ({paramInfo.ParameterType}) cannot be used in Yarn functions"));
10601061
}
10611062
else
10621063
{

0 commit comments

Comments
 (0)