Skip to content

Commit

Permalink
fix: handle vb syntax when doing file analyze (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
cslong authored Jul 1, 2022
1 parent 6b1f227 commit 154f897
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 18 deletions.
74 changes: 56 additions & 18 deletions src/Analysis/Codelyzer.Analysis.Build/FileBuildHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.VisualBasic;

namespace Codelyzer.Analysis.Build
{
Expand Down Expand Up @@ -51,27 +52,14 @@ public async Task<List<SourceFileBuildResult>> Build()

await Task.Run(() =>
{
foreach (var file in _fileInfo)
if (_projectPath.EndsWith(".vbproj", StringComparison.OrdinalIgnoreCase))
{
var fileContent = file.Value;
var syntaxTree = CSharpSyntaxTree.ParseText(fileContent, path: file.Key);
trees.Add(syntaxTree);
BuildVisualBasic(trees);
}
if (trees.Count != 0)
else
{
var projectName = Path.GetFileNameWithoutExtension(_projectPath);

if (_frameworkMetaReferences?.Any() == true)
{
_prePortCompilation = CSharpCompilation.Create(projectName, trees, _frameworkMetaReferences);
}
if (_coreMetaReferences?.Any() == true)
{
_compilation = CSharpCompilation.Create(projectName, trees, _coreMetaReferences);
}
BuildCsharp(trees);
}


_fileInfo.Keys.ToList().ForEach(file =>
{
var sourceFilePath = Path.GetRelativePath(_projectPath, file);
Expand All @@ -96,7 +84,57 @@ await Task.Run(() =>
});
});
return results;
}
}

private void BuildCsharp(List<SyntaxTree> trees)
{
foreach (var file in _fileInfo)
{
var fileContent = file.Value;
var syntaxTree = CSharpSyntaxTree.ParseText(fileContent, path: file.Key);
trees.Add(syntaxTree);
}

if (trees.Count != 0)
{
var projectName = Path.GetFileNameWithoutExtension(_projectPath);

if (_frameworkMetaReferences?.Any() == true)
{
_prePortCompilation = CSharpCompilation.Create(projectName, trees, _frameworkMetaReferences);
}

if (_coreMetaReferences?.Any() == true)
{
_compilation = CSharpCompilation.Create(projectName, trees, _coreMetaReferences);
}
}
}

private void BuildVisualBasic(List<SyntaxTree> trees)
{
foreach (var file in _fileInfo)
{
var fileContent = file.Value;
var syntaxTree = VisualBasicSyntaxTree.ParseText(fileContent, path: file.Key);
trees.Add(syntaxTree);
}

if (trees.Count != 0)
{
var projectName = Path.GetFileNameWithoutExtension(_projectPath);

if (_frameworkMetaReferences?.Any() == true)
{
_prePortCompilation = VisualBasicCompilation.Create(projectName, trees, _frameworkMetaReferences);
}

if (_coreMetaReferences?.Any() == true)
{
_compilation = VisualBasicCompilation.Create(projectName, trees, _coreMetaReferences);
}
}
}

public void Dispose()
{
Expand Down
54 changes: 54 additions & 0 deletions tst/Codelyzer.Analysis.Tests/FileBuildHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Collections.Generic;
using System.Linq;
using Codelyzer.Analysis.Build;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.VisualBasic;
using Microsoft.Extensions.Logging.Abstractions;
using NUnit.Framework;

namespace Codelyzer.Analysis.Tests
{
public class FileBuildHandlerTests
{
[Test]
public void TestCsharpFileBuildHandler()
{
var projectPath = "test.csproj";
var testFile = $@"
using System;
namespace Test
{{
public class TestClass {{}}
}}";
var fileInfo = new Dictionary<string, string>
{
{"TestFile.cs", testFile}
};
var fileBuildHandler = new FileBuildHandler(NullLogger.Instance, projectPath, fileInfo, new List<string>(), new List<string>());
var result= fileBuildHandler.Build().Result;
Assert.IsTrue(result.Count == 1);
Assert.IsTrue(result.First().SyntaxTree is CSharpSyntaxTree);
}

[Test]
public void TestVisualBasicFileBuildHandler()
{
var projectPath = "test.vbproj";
var testFile = $@"
Class TestClass
Public Sub Main()
Console.WriteLine(1)
End Sub
End Class";
var fileInfo = new Dictionary<string, string>
{
{"TestFile.vb", testFile}
};
var fileBuildHandler = new FileBuildHandler(NullLogger.Instance, projectPath, fileInfo, new List<string>(), new List<string>());
var result= fileBuildHandler.Build().Result;
Assert.IsTrue(result.Count == 1);
Assert.IsTrue(result.First().SyntaxTree is VisualBasicSyntaxTree);
}

}
}

0 comments on commit 154f897

Please sign in to comment.