Skip to content

Commit c455949

Browse files
committed
Consolidate file-level directive separators
1 parent 746f86e commit c455949

16 files changed

+165
-85
lines changed

src/Cli/dotnet/Commands/CliCommandStrings.resx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,9 +1506,13 @@ Tool '{1}' (version '{2}') was successfully installed. Entry is added to the man
15061506
<comment>{0} is the file path and line number. {1} is an inner exception message.</comment>
15071507
</data>
15081508
<data name="PropertyDirectiveMissingParts" xml:space="preserve">
1509-
<value>The property directive needs to have two parts separated by a space like 'PropertyName PropertyValue': {0}</value>
1509+
<value>The property directive needs to have two parts separated by '=' like '#:property PropertyName=PropertyValue': {0}</value>
15101510
<comment>{0} is the file path and line number.</comment>
15111511
</data>
1512+
<data name="InvalidDirectiveName" xml:space="preserve">
1513+
<value>The directive at '{2}' should contain a name without special characters and an optional version separated by '{1}' like '#:{0} Abc{1}Xyz'.</value>
1514+
<comment>{0} is the directive type like 'package' or 'sdk'. {1} is the expected separator like '@' or '='. {2} is the file path and line number. </comment>
1515+
</data>
15121516
<data name="CannotConvertDirective" xml:space="preserve">
15131517
<value>Some directives cannot be converted: the first error is at {0}. Run the file to see all compilation errors. Specify '--force' to convert anyway.</value>
15141518
<comment>{Locked="--force"}. {0} is the file path and line number.</comment>

src/Cli/dotnet/Commands/Run/VirtualProjectBuildingCommand.cs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,9 @@ internal static partial class Patterns
869869
{
870870
[GeneratedRegex("""\s+""")]
871871
public static partial Regex Whitespace { get; }
872+
873+
[GeneratedRegex("""[\s@=/]""")]
874+
public static partial Regex DisallowedNameCharacters { get; }
872875
}
873876

874877
/// <summary>
@@ -907,25 +910,29 @@ private CSharpDirective() { }
907910
}
908911
}
909912

910-
private static (string, string?)? ParseOptionalTwoParts(ImmutableArray<SimpleDiagnostic>.Builder? errors, SourceFile sourceFile, TextSpan span, string directiveKind, string directiveText, SearchValues<char>? separators = null)
913+
private static (string, string?)? ParseOptionalTwoParts(ImmutableArray<SimpleDiagnostic>.Builder? errors, SourceFile sourceFile, TextSpan span, string directiveKind, string directiveText, char separator)
911914
{
912-
var i = separators != null
913-
? directiveText.AsSpan().IndexOfAny(separators)
914-
: directiveText.IndexOf(' ', StringComparison.Ordinal);
915-
var firstPart = i < 0 ? directiveText : directiveText[..i];
915+
var i = directiveText.IndexOf(separator, StringComparison.Ordinal);
916+
var firstPart = (i < 0 ? directiveText : directiveText.AsSpan(..i)).Trim();
916917

917-
if (string.IsNullOrWhiteSpace(firstPart))
918+
if (firstPart.IsWhiteSpace())
918919
{
919920
return ReportError<(string, string?)?>(errors, sourceFile, span, string.Format(CliCommandStrings.MissingDirectiveName, directiveKind, sourceFile.GetLocationString(span)));
920921
}
921922

923+
// If the name contains characters that resemble separators, report an error to avoid any confusion.
924+
if (Patterns.DisallowedNameCharacters.IsMatch(firstPart))
925+
{
926+
return ReportError<(string, string?)?>(errors, sourceFile, span, string.Format(CliCommandStrings.InvalidDirectiveName, directiveKind, separator, sourceFile.GetLocationString(span)));
927+
}
928+
922929
var secondPart = i < 0 ? [] : directiveText.AsSpan((i + 1)..).TrimStart();
923930
if (i < 0 || secondPart.IsWhiteSpace())
924931
{
925-
return (firstPart, null);
932+
return (firstPart.ToString(), null);
926933
}
927934

928-
return (firstPart, secondPart.ToString());
935+
return (firstPart.ToString(), secondPart.ToString());
929936
}
930937

931938
/// <summary>
@@ -945,7 +952,7 @@ private Sdk() { }
945952

946953
public static new Sdk? Parse(ImmutableArray<SimpleDiagnostic>.Builder? errors, SourceFile sourceFile, TextSpan span, string directiveKind, string directiveText)
947954
{
948-
if (ParseOptionalTwoParts(errors, sourceFile, span, directiveKind, directiveText) is not var (sdkName, sdkVersion))
955+
if (ParseOptionalTwoParts(errors, sourceFile, span, directiveKind, directiveText, separator: '@') is not var (sdkName, sdkVersion))
949956
{
950957
return null;
951958
}
@@ -976,7 +983,7 @@ private Property() { }
976983

977984
public static new Property? Parse(ImmutableArray<SimpleDiagnostic>.Builder? errors, SourceFile sourceFile, TextSpan span, string directiveKind, string directiveText)
978985
{
979-
if (ParseOptionalTwoParts(errors, sourceFile, span, directiveKind, directiveText) is not var (propertyName, propertyValue))
986+
if (ParseOptionalTwoParts(errors, sourceFile, span, directiveKind, directiveText, separator: '=') is not var (propertyName, propertyValue))
980987
{
981988
return null;
982989
}
@@ -1009,16 +1016,14 @@ private Property() { }
10091016
/// </summary>
10101017
public sealed class Package : CSharpDirective
10111018
{
1012-
private static readonly SearchValues<char> s_separators = SearchValues.Create(' ', '@');
1013-
10141019
private Package() { }
10151020

10161021
public required string Name { get; init; }
10171022
public string? Version { get; init; }
10181023

10191024
public static new Package? Parse(ImmutableArray<SimpleDiagnostic>.Builder? errors, SourceFile sourceFile, TextSpan span, string directiveKind, string directiveText)
10201025
{
1021-
if (ParseOptionalTwoParts(errors, sourceFile, span, directiveKind, directiveText, s_separators) is not var (packageName, packageVersion))
1026+
if (ParseOptionalTwoParts(errors, sourceFile, span, directiveKind, directiveText, separator: '@') is not var (packageName, packageVersion))
10221027
{
10231028
return null;
10241029
}

src/Cli/dotnet/Commands/xlf/CliCommandStrings.cs.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.de.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.es.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.fr.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.it.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.ja.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/Commands/xlf/CliCommandStrings.ko.xlf

Lines changed: 7 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)