-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Visual Basic support #15
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<ExportCodeRefactoringProvider(NullCheck_CodeRefactoringCodeRefactoringProvider.RefactoringId, LanguageNames.VisualBasic), [Shared]> | ||
Friend Class NullCheck_CodeRefactoringCodeRefactoringProvider | ||
Inherits CodeRefactoringProvider | ||
|
||
Public Const RefactoringId As String = "TR0001" | ||
|
||
Public NotOverridable Overrides Async Function ComputeRefactoringsAsync(context As CodeRefactoringContext) As Task | ||
Dim root = Await context.Document.GetSyntaxRootAsync(context.CancellationToken).ConfigureAwait(False) | ||
' Find the node at the selection. | ||
Dim node = root.FindNode(context.Span) | ||
' Only offer a refactoring if the selected node is a type statement node. | ||
Dim _parmeter_ = TryCast(node, ParameterSyntax) | ||
If _parmeter_ Is Nothing Then Return | ||
Dim _method_ = TryCast(_parmeter_.Parent.Parent.Parent, MethodBlockSyntax) | ||
If _method_ Is Nothing Then Exit Function | ||
Dim _Model_ = Await context.Document.GetSemanticModelAsync(context.CancellationToken) | ||
Dim ifStatements = _method_.Statements.Where(Function(s) (TypeOf s Is MultiLineIfBlockSyntax) OrElse (TypeOf s Is SingleLineIfStatementSyntax)) | ||
Dim pinfo = _Model_.GetTypeInfo(_parmeter_.AsClause.Type, context.CancellationToken) | ||
If pinfo.ConvertedType.IsReferenceType = False Then Return | ||
Dim IsNullCheckAlreadyPresent = ifStatements.Any(NullChecks(_parmeter_)) | ||
If Not IsNullCheckAlreadyPresent Then context.RegisterRefactoring(CodeAction.Create("Check Parameter for null", Function(ct As CancellationToken) AddParameterNullCheckAsync(context.Document, _parmeter_, _method_, ct))) | ||
End Function | ||
|
||
Private Shared Function NullChecks(_parmeter_ As ParameterSyntax) As Func(Of StatementSyntax, Boolean) | ||
Return Function(s) | ||
If TypeOf s Is SingleLineIfStatementSyntax Then | ||
Dim isExpr = TryCast(DirectCast(s, SingleLineIfStatementSyntax).Condition, BinaryExpressionSyntax) | ||
If (isExpr Is Nothing) OrElse (Not isExpr.IsKind(SyntaxKind.IsExpression)) Then Return False | ||
Return CheckIfCondition(_parmeter_, isExpr) | ||
ElseIf TypeOf s Is MultiLineIfBlockSyntax Then | ||
Dim isExpr = TryCast(DirectCast(s, MultiLineIfBlockSyntax).IfStatement.Condition, BinaryExpressionSyntax) | ||
If (isExpr Is Nothing) OrElse (Not isExpr.IsKind(SyntaxKind.IsExpression)) Then Return False | ||
Return CheckIfCondition(_parmeter_, isExpr) | ||
End If | ||
Return False | ||
End Function | ||
End Function | ||
|
||
Private Shared Function CheckIfCondition(paramSyntax As ParameterSyntax, isExpr As BinaryExpressionSyntax) As Boolean | ||
Dim l = TryCast(isExpr.Left, IdentifierNameSyntax) | ||
Dim r = TryCast(isExpr.Right, LiteralExpressionSyntax) | ||
If l Is Nothing OrElse r Is Nothing Then Return False | ||
If r.IsKind(SyntaxKind.NothingLiteralExpression) = False Then Return False | ||
Return String.Compare(l.Identifier.Text, paramSyntax.Identifier.Identifier.Text, StringComparison.Ordinal) = 0 | ||
End Function | ||
|
||
Private Async Function AddParameterNullCheckAsync(document As Document, | ||
parameterStmt As ParameterSyntax, | ||
method As MethodBlockSyntax, | ||
cancellationToken As CancellationToken) As Task(Of Document) | ||
Dim _null_ = SyntaxFactory.NothingLiteralExpression(SyntaxFactory.Token(SyntaxKind.NothingKeyword)) | ||
Dim _IsExpr_ = SyntaxFactory.IsExpression( | ||
SyntaxFactory.IdentifierName(parameterStmt.Identifier.Identifier.Text), | ||
_null_).WithAdditionalAnnotations(Formatting.Formatter.Annotation) | ||
' Note: If I can find the nameof feature in VB.net, then I'll change this line to reflect that | ||
Dim _paramname_ = SyntaxFactory.StringLiteralExpression( | ||
SyntaxFactory.StringLiteralToken("""" & parameterStmt.Identifier.Identifier.Text & """", | ||
parameterStmt.Identifier.Identifier.Text)) | ||
Dim st = SyntaxFactory.ObjectCreationExpression(Nothing, | ||
SyntaxFactory.ParseTypeName(GetType(ArgumentNullException).FullName), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need Module Module1
Sub Main()
End Sub
Sub Foo(foo As String)
If foo Is Nothing Then Throw New ArgumentNullException("foo")
End Sub
End Module There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest making this a follow-up issue. Including the full name might be suboptimal, but isn't incorrect. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sharwell VS grays out the namespace name which means it's not needed. I think we should omit the namespace whenever we can. That's fine, we can add it as an enhancement issue later. |
||
SyntaxFactory.ArgumentList().AddArguments( | ||
SyntaxFactory.SimpleArgument(_paramname_) | ||
), | ||
Nothing) | ||
|
||
Dim throwExpr = SyntaxFactory.ThrowStatement(st) | ||
|
||
Dim if_ = SyntaxFactory.SingleLineIfStatement( | ||
SyntaxFactory.Token(SyntaxKind.IfKeyword), | ||
_IsExpr_, | ||
SyntaxFactory.Token(SyntaxKind.ThenKeyword), | ||
New SyntaxList(Of StatementSyntax)().Add(throwExpr), | ||
Nothing).WithAdditionalAnnotations(Formatting.Formatter.Annotation) | ||
|
||
Dim newStatements = method.Statements.Insert(0, if_) | ||
Dim newBlock = method.WithStatements(newStatements) | ||
Return document.WithSyntaxRoot((Await document.GetSyntaxRootAsync(cancellationToken)).ReplaceNode(method, newBlock)) | ||
End Function | ||
End Class |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Imports System.Reflection | ||
Imports System.Runtime.InteropServices | ||
|
||
' General Information about an assembly is controlled through the following | ||
' set of attributes. Change these attribute values to modify the information | ||
' associated with an assembly. | ||
|
||
' Review the values of the assembly attributes | ||
|
||
<Assembly: AssemblyTitle("NullParameterCheckRefactoring.VB")> | ||
<Assembly: AssemblyDescription("")> | ||
<Assembly: AssemblyCompany("")> | ||
<Assembly: AssemblyProduct("NullParameterCheckRefactoring.VB")> | ||
<Assembly: AssemblyCopyright("Copyright © 2014")> | ||
<Assembly: AssemblyTrademark("")> | ||
|
||
<Assembly: ComVisible(False)> | ||
|
||
' Version information for an assembly consists of the following four values: | ||
' | ||
' Major Version | ||
' Minor Version | ||
' Build Number | ||
' Revision | ||
' | ||
' You can specify all the values or you can default the Build and Revision Numbers | ||
' by using the '*' as shown below: | ||
<Assembly: AssemblyVersion("1.0.*")> | ||
<Assembly: AssemblyFileVersion("1.0.0.0")> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
<PropertyGroup> | ||
<MinimumVisualStudioVersion>11.0</MinimumVisualStudioVersion> | ||
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> | ||
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> | ||
<SchemaVersion>2.0</SchemaVersion> | ||
<ProjectTypeGuids>{14182A97-F7F0-4C62-8B27-98AA8AE2109A};{F184B08F-C81C-45F6-A57F-5ABD9991F28F}</ProjectTypeGuids> | ||
<ProjectGuid>{5C5C6CFE-22CB-4E7A-9916-B6F28785B5C0}</ProjectGuid> | ||
<OutputType>Library</OutputType> | ||
<RootNamespace>NullParameterCheckRefactoring</RootNamespace> | ||
<AssemblyName>NullParameterCheckRefactoring.VB</AssemblyName> | ||
<TargetFrameworkProfile>Profile7</TargetFrameworkProfile> | ||
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion> | ||
<FileAlignment>512</FileAlignment> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> | ||
<DebugSymbols>true</DebugSymbols> | ||
<DebugType>full</DebugType> | ||
<Optimize>false</Optimize> | ||
<OutputPath>bin\Debug\</OutputPath> | ||
<DefineDebug>true</DefineDebug> | ||
<DefineTrace>true</DefineTrace> | ||
<ErrorReport>prompt</ErrorReport> | ||
<NoWarn>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036</NoWarn> | ||
</PropertyGroup> | ||
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> | ||
<DebugType>pdbonly</DebugType> | ||
<Optimize>true</Optimize> | ||
<OutputPath>bin\Release\</OutputPath> | ||
<DefineDebug>false</DefineDebug> | ||
<DefineTrace>true</DefineTrace> | ||
<ErrorReport>prompt</ErrorReport> | ||
<NoWarn>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036</NoWarn> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<OptionExplicit>On</OptionExplicit> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<OptionCompare>Binary</OptionCompare> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<OptionStrict>On</OptionStrict> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<OptionInfer>On</OptionInfer> | ||
</PropertyGroup> | ||
<PropertyGroup> | ||
<StartAction>Program</StartAction> | ||
<StartProgram>$(DevEnvDir)devenv.exe</StartProgram> | ||
<StartArguments>/rootsuffix Roslyn</StartArguments> | ||
</PropertyGroup> | ||
<ItemGroup> | ||
<Import Include="Microsoft.CodeAnalysis" /> | ||
<Import Include="Microsoft.CodeAnalysis.CodeActions" /> | ||
<Import Include="Microsoft.CodeAnalysis.CodeRefactorings" /> | ||
<Import Include="Microsoft.CodeAnalysis.Rename" /> | ||
<Import Include="Microsoft.CodeAnalysis.Text" /> | ||
<Import Include="Microsoft.CodeAnalysis.VisualBasic" /> | ||
<Import Include="Microsoft.CodeAnalysis.VisualBasic.Syntax" /> | ||
<Import Include="Microsoft.VisualBasic" /> | ||
<Import Include="System" /> | ||
<Import Include="System.Collections" /> | ||
<Import Include="System.Collections.Generic" /> | ||
<Import Include="System.Composition" /> | ||
<Import Include="System.Data" /> | ||
<Import Include="System.Diagnostics" /> | ||
<Import Include="System.Linq" /> | ||
<Import Include="System.Threading" /> | ||
<Import Include="System.Threading.Tasks" /> | ||
<Import Include="System.Xml.Linq" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Compile Include="CodeRefactoringProvider.vb" /> | ||
<Compile Include="My Project\AssemblyInfo.vb" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<Reference Include="Microsoft.CodeAnalysis"> | ||
<HintPath>..\..\packages\Microsoft.CodeAnalysis.Common.1.0.0-beta1-20141031-01\lib\portable-net45+win8\Microsoft.CodeAnalysis.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="Microsoft.CodeAnalysis.VisualBasic"> | ||
<HintPath>..\..\packages\Microsoft.CodeAnalysis.VisualBasic.1.0.0-beta1-20141031-01\lib\portable-net45+win8\Microsoft.CodeAnalysis.VisualBasic.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="Microsoft.CodeAnalysis.VisualBasic.Workspaces"> | ||
<HintPath>..\..\packages\Microsoft.CodeAnalysis.VisualBasic.Workspaces.1.0.0-beta1-20141031-01\lib\portable-net45+win8\Microsoft.CodeAnalysis.VisualBasic.Workspaces.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="Microsoft.CodeAnalysis.Workspaces"> | ||
<HintPath>..\..\packages\Microsoft.CodeAnalysis.Workspaces.Common.1.0.0-beta1-20141031-01\lib\portable-net45+win8\Microsoft.CodeAnalysis.Workspaces.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="System.Collections.Immutable"> | ||
<HintPath>..\..\packages\System.Collections.Immutable.1.1.32-beta\lib\portable-net45+win8+wp8+wpa81\System.Collections.Immutable.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="System.Composition.AttributedModel"> | ||
<HintPath>..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.AttributedModel.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="System.Composition.Convention"> | ||
<HintPath>..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Convention.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="System.Composition.Hosting"> | ||
<HintPath>..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Hosting.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="System.Composition.Runtime"> | ||
<HintPath>..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.Runtime.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="System.Composition.TypedParts"> | ||
<HintPath>..\..\packages\Microsoft.Composition.1.0.27\lib\portable-net45+win8+wp8+wpa81\System.Composition.TypedParts.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
<Reference Include="System.Reflection.Metadata"> | ||
<HintPath>..\..\packages\System.Reflection.Metadata.1.0.17-beta\lib\portable-net45+win8\System.Reflection.Metadata.dll</HintPath> | ||
<Private>False</Private> | ||
</Reference> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="packages.config" /> | ||
</ItemGroup> | ||
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\Portable\$(TargetFrameworkVersion)\Microsoft.Portable.VisualBasic.targets" /> | ||
<Import Project="$(VSToolsPath)\VSSDK\Microsoft.VsSDK.targets" Condition="'$(VSToolsPath)' != ''" /> | ||
<!-- To modify your build process, add your task inside one of the targets below and uncomment it. | ||
Other similar extension points exist, see Microsoft.Common.targets. | ||
<Target Name="BeforeBuild"> | ||
</Target> | ||
<Target Name="AfterBuild"> | ||
</Target> | ||
--> | ||
</Project> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<packages> | ||
<package id="Microsoft.CodeAnalysis.Common" version="1.0.0-beta1-20141031-01" targetFramework="portable-net45+win" /> | ||
<package id="Microsoft.CodeAnalysis.VisualBasic" version="1.0.0-beta1-20141031-01" targetFramework="portable-net45+win" /> | ||
<package id="Microsoft.CodeAnalysis.VisualBasic.Workspaces" version="1.0.0-beta1-20141031-01" targetFramework="portable-net45+win" /> | ||
<package id="Microsoft.CodeAnalysis.Workspaces.Common" version="1.0.0-beta1-20141031-01" targetFramework="portable-net45+win" /> | ||
<package id="Microsoft.Composition" version="1.0.27" targetFramework="portable-net45+win" /> | ||
<package id="System.Collections.Immutable" version="1.1.32-beta" targetFramework="portable-net45+win" /> | ||
<package id="System.Reflection.Metadata" version="1.0.17-beta" targetFramework="portable-net45+win" /> | ||
</packages> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we have
nameof
operator in VB?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest making this a follow-up issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍