Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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,37 @@ 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 @@ -5,16 +5,33 @@ namespace OrchardCoreContrib.PoExtractor.DotNet.CS.Tests;

public class SingularStringExtractorTests
{
[Fact]
public void ExtractString()
[Theory]
[InlineData("""S["Thing"];""", "Thing")]
[InlineData(
"""
S[@"This is a multi-line
string."];
""",
"""
This is a multi-line

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No localizer here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but this is the output

string.
""")]
[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 ExtractString(string source, string expected)
{
// Arrange
var text = "Thing";
var metadataProvider = new CSharpMetadataProvider("DummyBasePath");
var extractor = new SingularStringExtractor(metadataProvider);

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

var node = syntaxTree
.GetRoot()
.DescendantNodes()
Expand All @@ -25,6 +42,6 @@ public void ExtractString()

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