Skip to content

Commit

Permalink
Prevent comments confusing parser when sanitizing curly braces
Browse files Browse the repository at this point in the history
Fix failing tests
  • Loading branch information
bcssov committed Dec 4, 2022
1 parent 882ad24 commit 37845c9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/IronyModManager.Parser.Tests/ModParserTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,12 @@ public void Should_parse_mod_file_with_invalid_quotations()
sb.AppendLine(@" """);
sb.AppendLine(@"}");

// Behavior changes, parsed as the game would see it (I think)
var parser = new ModParser(new Logger(), new CodeParser(new Logger()));
var result = parser.Parse(sb.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries), Common.DescriptorModType.DescriptorMod);
result.Dependencies.Should().BeNull();
result.Dependencies.Should().NotBeNull();
result.Dependencies.Count().Should().Be(1);
result.Dependencies.FirstOrDefault().Should().Be("\r\n}");
result.FileName.Should().Be("path");
result.Name.Should().Be("AI Species \"Limit\"");
result.Picture.Should().Be("thumbnail.png");
Expand Down
5 changes: 2 additions & 3 deletions src/IronyModManager.Parser/CodeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ public CodeParser(ILogger logger)
public virtual IEnumerable<string> CleanCode(string file, IEnumerable<string> lines)
{
var commentId = IsLua(file) ? Common.Constants.Scripts.LuaScriptCommentId : Common.Constants.Scripts.ScriptCommentId.ToString();
var cleaned = FormatCurlyBraces(string.Join(Environment.NewLine, lines)).Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
return cleaned.Where(p => !string.IsNullOrWhiteSpace(p) && !p.Trim().StartsWith(commentId))
.Select(p => FormatCodeTerminators(RemoveInlineComments(commentId, p)));
var validCodeLines = lines.Where(p => !string.IsNullOrWhiteSpace(p) && !p.Trim().StartsWith(commentId)).Select(p => RemoveInlineComments(commentId, p));
return FormatCurlyBraces(string.Join(Environment.NewLine, validCodeLines)).Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries).Select(FormatCodeTerminators);
}

/// <summary>
Expand Down

0 comments on commit 37845c9

Please sign in to comment.