Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,34 @@ accessor.Expression is IdentifierNameSyntax identifierName &&
{

var argument = accessor.ArgumentList.Arguments.FirstOrDefault();
if (argument != null && argument.Expression is LiteralExpressionSyntax literal && literal.IsKind(SyntaxKind.StringLiteralExpression))
if (argument != null && TryGetString(argument.Expression, out var value))
{
result = CreateLocalizedString(literal.Token.ValueText, null, node);
result = CreateLocalizedString(value, null, node);
return true;
}
}

return false;
}

private static bool TryGetString(ExpressionSyntax expression, out string value)
{
if (expression is LiteralExpressionSyntax literal && literal.IsKind(SyntaxKind.StringLiteralExpression))
{
value = literal.Token.ValueText;
return true;
Comment thread
hishamco marked this conversation as resolved.
}

if (expression is BinaryExpressionSyntax binary &&
binary.IsKind(SyntaxKind.AddExpression) &&
TryGetString(binary.Left, out var left) &&
TryGetString(binary.Right, out var right))
{
value = left + right;
return true;
Comment thread
hishamco marked this conversation as resolved.
}

value = null;
return false;
Comment thread
hishamco marked this conversation as resolved.
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void ExtractString()
var extractor = new SingularStringExtractor(metadataProvider);

var syntaxTree = CSharpSyntaxTree.ParseText($"S[\"{text}\"];", path: "DummyPath");

var node = syntaxTree
.GetRoot()
.DescendantNodes()
Expand All @@ -27,4 +27,34 @@ public void ExtractString()
Assert.True(extracted);
Assert.Equal(text, result.Text);
}
}

[Theory]
[InlineData("""S["my " + "text"];""", "my text")]
[InlineData("""S["a " + "long " + "text"];""", "a long text")]
Comment thread
ArturDorochowicz marked this conversation as resolved.
[InlineData(
"""
S["This is a long piece of text " +
"continued on another line."];
""",
"This is a long piece of text continued on another line.")]
public void ExtractConcatenatedString(string source, string expected)
{
// Arrange
var metadataProvider = new CSharpMetadataProvider("DummyBasePath");
var extractor = new SingularStringExtractor(metadataProvider);

var syntaxTree = CSharpSyntaxTree.ParseText(source, path: "DummyPath");

var node = syntaxTree
.GetRoot()
.DescendantNodes()
.ElementAt(2);

// Act
var extracted = extractor.TryExtract(node, out var result);

// Assert
Assert.True(extracted);
Assert.Equal(expected, result.Text);
}
}
Loading