Skip to content

Commit

Permalink
Merge pull request dotnet#69329 from DoctorKrolic/forbid-void-parameter
Browse files Browse the repository at this point in the history
Forbid declaring parameters of `void` type in 'Change Signature'->"Add parameter" dialog
  • Loading branch information
CyrusNajmabadi authored Aug 4, 2023
2 parents 0602129 + 454478c commit fdf726c
Show file tree
Hide file tree
Showing 17 changed files with 129 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
AutomationProperties.LabeledBy="{Binding ElementName=TypeNameLabelName}"/>
<StackPanel Orientation="Horizontal" Grid.Row="2" Margin="0,5,0,5">
<imaging:CrispImage Moniker="{x:Static imagecatalog:KnownMonikers.StatusInformation}" Visibility="{Binding TypeIsEmptyImage, Mode=OneWay}"/>
<imaging:CrispImage Moniker="{x:Static imagecatalog:KnownMonikers.StatusError}" Visibility="{Binding TypeDoesNotParseImage, Mode=OneWay}"/>
<imaging:CrispImage Moniker="{x:Static imagecatalog:KnownMonikers.StatusError}" Visibility="{Binding TypeDoesNotParseOrInvalidTypeImage, Mode=OneWay}"/>
<imaging:CrispImage Moniker="{x:Static imagecatalog:KnownMonikers.StatusOK}" Visibility="{Binding TypeBindsImage, Mode=OneWay}"/>
<imaging:CrispImage Moniker="{x:Static imagecatalog:KnownMonikers.StatusWarning}" Visibility="{Binding TypeDoesNotBindImage, Mode=OneWay}"/>
<vs:LiveTextBlock Padding="5 0 0 0" Grid.Row="2" x:Name="TypeBindsTextBlock"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public AddParameterDialogViewModel(Document document, int positionForTypeBinding

TypeIsEmptyImage = Visibility.Visible;
TypeBindsImage = Visibility.Collapsed;
TypeDoesNotParseImage = Visibility.Collapsed;
TypeDoesNotParseOrInvalidTypeImage = Visibility.Collapsed;
TypeDoesNotBindImage = Visibility.Collapsed;
TypeBindsDynamicStatus = ServicesVSResources.Please_enter_a_type_name;

Expand Down Expand Up @@ -104,7 +104,7 @@ public bool IsOptional
public string TypeBindsDynamicStatus { get; set; }
public Visibility TypeBindsImage { get; set; }
public Visibility TypeDoesNotBindImage { get; set; }
public Visibility TypeDoesNotParseImage { get; set; }
public Visibility TypeDoesNotParseOrInvalidTypeImage { get; set; }
public Visibility TypeIsEmptyImage { get; set; }

private string _verbatimTypeName = string.Empty;
Expand All @@ -120,6 +120,8 @@ public string VerbatimTypeName
}
}

private bool _isVoidParameterType;

internal bool CanSubmit([NotNullWhen(false)] out string? message)
{
if (string.IsNullOrEmpty(VerbatimTypeName) || string.IsNullOrEmpty(ParameterName))
Expand All @@ -128,6 +130,12 @@ internal bool CanSubmit([NotNullWhen(false)] out string? message)
return false;
}

if (_isVoidParameterType)
{
message = ServicesVSResources.SystemVoid_is_not_a_valid_type_for_a_parameter;
return false;
}

if (TypeSymbol == null || !IsParameterTypeSyntacticallyValid(VerbatimTypeName))
{
message = ServicesVSResources.Parameter_type_contains_invalid_characters;
Expand Down Expand Up @@ -175,11 +183,12 @@ private void SendFailureNotification(string message)
private void SetCurrentTypeTextAndUpdateBindingStatus(string typeName)
{
VerbatimTypeName = typeName;
_isVoidParameterType = false;

if (string.IsNullOrWhiteSpace(typeName))
{
TypeIsEmptyImage = Visibility.Visible;
TypeDoesNotParseImage = Visibility.Collapsed;
TypeDoesNotParseOrInvalidTypeImage = Visibility.Collapsed;
TypeDoesNotBindImage = Visibility.Collapsed;
TypeBindsImage = Visibility.Collapsed;
TypeBindsDynamicStatus = ServicesVSResources.Please_enter_a_type_name;
Expand All @@ -193,18 +202,25 @@ private void SetCurrentTypeTextAndUpdateBindingStatus(string typeName)
var languageService = Document.GetRequiredLanguageService<IChangeSignatureViewModelFactoryService>();
TypeSymbol = _semanticModel.GetSpeculativeTypeInfo(PositionForTypeBinding, languageService.GetTypeNode(typeName), SpeculativeBindingOption.BindAsTypeOrNamespace).Type;

var typeParses = IsParameterTypeSyntacticallyValid(typeName);
if (!typeParses || TypeSymbol == null)
if (TypeSymbol is { SpecialType: SpecialType.System_Void })
{
_isVoidParameterType = true;
TypeDoesNotParseOrInvalidTypeImage = Visibility.Visible;
TypeDoesNotBindImage = Visibility.Collapsed;
TypeBindsImage = Visibility.Collapsed;
TypeBindsDynamicStatus = ServicesVSResources.SystemVoid_is_not_a_valid_type_for_a_parameter;
}
else if (!IsParameterTypeSyntacticallyValid(typeName) || TypeSymbol == null)
{
TypeDoesNotParseImage = Visibility.Visible;
TypeDoesNotParseOrInvalidTypeImage = Visibility.Visible;
TypeDoesNotBindImage = Visibility.Collapsed;
TypeBindsImage = Visibility.Collapsed;
TypeBindsDynamicStatus = ServicesVSResources.Type_name_has_a_syntax_error;
}
else
{
var parameterTypeBinds = DoesTypeFullyBind(TypeSymbol);
TypeDoesNotParseImage = Visibility.Collapsed;
TypeDoesNotParseOrInvalidTypeImage = Visibility.Collapsed;

TypeBindsImage = parameterTypeBinds ? Visibility.Visible : Visibility.Collapsed;
TypeDoesNotBindImage = !parameterTypeBinds ? Visibility.Visible : Visibility.Collapsed;
Expand All @@ -217,7 +233,7 @@ private void SetCurrentTypeTextAndUpdateBindingStatus(string typeName)
NotifyPropertyChanged(nameof(TypeBindsDynamicStatus));
NotifyPropertyChanged(nameof(TypeBindsImage));
NotifyPropertyChanged(nameof(TypeDoesNotBindImage));
NotifyPropertyChanged(nameof(TypeDoesNotParseImage));
NotifyPropertyChanged(nameof(TypeDoesNotParseOrInvalidTypeImage));
NotifyPropertyChanged(nameof(TypeIsEmptyImage));
}

Expand Down
4 changes: 4 additions & 0 deletions src/VisualStudio/Core/Def/ServicesVSResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -2025,4 +2025,8 @@ Additional information: {1}</value>
<data name="Prefer_collection_expression" xml:space="preserve">
<value>Prefer collection expression</value>
</data>
<data name="SystemVoid_is_not_a_valid_type_for_a_parameter" xml:space="preserve">
<value>'System.Void' is not a valid type for a parameter</value>
<comment>{Locked="System.Void"} "System.Void" represents CLR's type and should not be localized</comment>
</data>
</root>
5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.cs.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.de.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.es.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.fr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.it.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.ja.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.ko.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.pl.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.pt-BR.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.ru.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.tr.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.zh-Hans.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions src/VisualStudio/Core/Def/xlf/ServicesVSResources.zh-Hant.xlf

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit fdf726c

Please sign in to comment.