Skip to content

Commit da88128

Browse files
Red: Refactor and change back to old broken way to check test fails
1 parent 2c955f2 commit da88128

File tree

5 files changed

+62
-47
lines changed

5 files changed

+62
-47
lines changed

ICSharpCode.CodeConverter/CSharp/VBToCSConversion.cs

+18-13
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using ICSharpCode.CodeConverter.Shared;
55
using ICSharpCode.CodeConverter.Util;
6+
using ICSharpCode.CodeConverter.VB;
67
using Microsoft.CodeAnalysis;
78
using Microsoft.CodeAnalysis.CSharp;
89
using Microsoft.CodeAnalysis.Text;
@@ -17,24 +18,21 @@ namespace ICSharpCode.CodeConverter.CSharp
1718
public class VBToCSConversion : ILanguageConversion
1819
{
1920
private Compilation _sourceCompilation;
20-
private readonly List<SyntaxTree> _firstPassResults = new List<SyntaxTree>();
2121
private readonly List<SyntaxTree> _secondPassResults = new List<SyntaxTree>();
22-
private Lazy<CSharpCompilation> _targetCompilation;
23-
24-
private CSharpCompilation CreateCompilation(List<SyntaxTree> csTrees)
22+
private CSharpCompilation _convertedCompilation;
23+
24+
25+
public void Initialize(Compilation convertedCompilation)
2526
{
26-
var references = _sourceCompilation.References.Select(ReferenceConverter.ConvertReference);
27-
return CSharpCompilation.Create("Conversion", csTrees, references,
28-
new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
27+
_convertedCompilation = (CSharpCompilation) convertedCompilation;
2928
}
3029

3130
public SyntaxTree SingleFirstPass(Compilation sourceCompilation, SyntaxTree tree)
3231
{
3332
_sourceCompilation = sourceCompilation;
3433
var converted = VisualBasicConverter.ConvertCompilationTree((VisualBasicCompilation)sourceCompilation, (VisualBasicSyntaxTree)tree);
3534
var convertedTree = SyntaxFactory.SyntaxTree(converted);
36-
_firstPassResults.Add(convertedTree);
37-
_targetCompilation = new Lazy<CSharpCompilation>(() => CreateCompilation(_firstPassResults));
35+
_convertedCompilation = _convertedCompilation.AddSyntaxTrees(convertedTree);
3836
return convertedTree;
3937
}
4038

@@ -60,6 +58,8 @@ public SyntaxNode GetSurroundedNode(IEnumerable<SyntaxNode> descendantNodes,
6058
};
6159
}
6260

61+
public string TargetLanguage { get; } = LanguageNames.CSharp;
62+
6363
public bool CanBeContainedByMethod(SyntaxNode node)
6464
{
6565
return node is VBSyntax.IncompleteMemberSyntax ||
@@ -116,15 +116,14 @@ public List<SyntaxNode> FindSingleImportantChild(SyntaxNode annotatedNode)
116116

117117
public SyntaxNode SingleSecondPass(KeyValuePair<string, SyntaxTree> cs)
118118
{
119-
var cSharpSyntaxNode = new CompilationErrorFixer(_targetCompilation.Value, (CSharpSyntaxTree)cs.Value).Fix();
119+
var cSharpSyntaxNode = new CompilationErrorFixer(_convertedCompilation, (CSharpSyntaxTree)cs.Value).Fix();
120120
_secondPassResults.Add(cSharpSyntaxNode.SyntaxTree);
121121
return cSharpSyntaxNode;
122122
}
123123

124124
public string GetWarningsOrNull()
125125
{
126-
var finalCompilation = CreateCompilation(_firstPassResults);
127-
return CompilationWarnings.WarningsForCompilation(_sourceCompilation, "source") + CompilationWarnings.WarningsForCompilation(finalCompilation, "target");
126+
return CompilationWarnings.WarningsForCompilation(_sourceCompilation, "source") + CompilationWarnings.WarningsForCompilation(_convertedCompilation, "target");
128127
}
129128

130129
public SyntaxTree CreateTree(string text)
@@ -133,6 +132,12 @@ public SyntaxTree CreateTree(string text)
133132
}
134133

135134
public Compilation CreateCompilationFromTree(SyntaxTree tree, IEnumerable<MetadataReference> references)
135+
{
136+
var compilation = CreateVisualBasicCompilation(references);
137+
return compilation.AddSyntaxTrees(tree);
138+
}
139+
140+
public static VisualBasicCompilation CreateVisualBasicCompilation(IEnumerable<MetadataReference> references)
136141
{
137142
var compilationOptions = new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary)
138143
.WithGlobalImports(GlobalImport.Parse(
@@ -148,7 +153,7 @@ public Compilation CreateCompilationFromTree(SyntaxTree tree, IEnumerable<Metada
148153
"System.Text",
149154
"System.Threading.Tasks",
150155
"Microsoft.VisualBasic"));
151-
var compilation = VisualBasicCompilation.Create("Conversion", new[] {tree}, references)
156+
var compilation = VisualBasicCompilation.Create("Conversion", references: references)
152157
.WithOptions(compilationOptions);
153158
return compilation;
154159
}

ICSharpCode.CodeConverter/ILanguageConversion.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using Microsoft.CodeAnalysis;
34

45
namespace ICSharpCode.CodeConverter.CSharp
@@ -20,5 +21,7 @@ SyntaxNode GetSurroundedNode(IEnumerable<SyntaxNode> descendantNodes,
2021
bool surroundedWithMethod);
2122
IReadOnlyCollection<(string, string)> GetProjectTypeGuidMappings();
2223
IEnumerable<(string, string)> GetProjectFileReplacementRegexes();
24+
string TargetLanguage { get; }
25+
void Initialize(Compilation convertedCompilation);
2326
}
2427
}

ICSharpCode.CodeConverter/Shared/ProjectConversion.cs

+27-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,12 @@
66
using System.Threading.Tasks;
77
using ICSharpCode.CodeConverter.CSharp;
88
using ICSharpCode.CodeConverter.Util;
9+
using ICSharpCode.CodeConverter.VB;
910
using Microsoft.CodeAnalysis;
11+
using Microsoft.CodeAnalysis.CSharp;
1012
using Microsoft.CodeAnalysis.Formatting;
1113
using Microsoft.CodeAnalysis.Text;
14+
using Microsoft.CodeAnalysis.VisualBasic;
1215

1316
namespace ICSharpCode.CodeConverter.Shared
1417
{
@@ -24,18 +27,19 @@ public class ProjectConversion
2427
private readonly ILanguageConversion _languageConversion;
2528
private readonly bool _handlePartialConversion;
2629

27-
private ProjectConversion(Compilation sourceCompilation, string solutionDir, ILanguageConversion languageConversion)
28-
: this(sourceCompilation, sourceCompilation.SyntaxTrees.Where(t => t.FilePath.StartsWith(solutionDir)), languageConversion)
30+
private ProjectConversion(Compilation sourceCompilation, string solutionDir, ILanguageConversion languageConversion, Compilation convertedCompilation)
31+
: this(sourceCompilation, sourceCompilation.SyntaxTrees.Where(t => t.FilePath.StartsWith(solutionDir)), languageConversion, convertedCompilation)
2932
{
3033
_solutionDir = solutionDir;
3134
}
3235

33-
private ProjectConversion(Compilation sourceCompilation, IEnumerable<SyntaxTree> syntaxTreesToConvert, ILanguageConversion languageConversion)
36+
private ProjectConversion(Compilation sourceCompilation, IEnumerable<SyntaxTree> syntaxTreesToConvert, ILanguageConversion languageConversion, Compilation convertedCompilation)
3437
{
3538
_languageConversion = languageConversion;
3639
this._sourceCompilation = sourceCompilation;
3740
_syntaxTreesToConvert = syntaxTreesToConvert.ToList();
3841
_handlePartialConversion = _syntaxTreesToConvert.Count() == 1;
42+
languageConversion.Initialize(convertedCompilation.RemoveAllSyntaxTrees());
3943
}
4044

4145
public static ConversionResult ConvertText<TLanguageConversion>(string text, IReadOnlyCollection<MetadataReference> references) where TLanguageConversion : ILanguageConversion, new()
@@ -54,7 +58,7 @@ public static async Task<ConversionResult> ConvertSingle(Compilation compilation
5458
syntaxTree = annotatedSyntaxTree;
5559
}
5660

57-
var conversion = new ProjectConversion(compilation, new [] {syntaxTree}, languageConversion);
61+
var conversion = new ProjectConversion(compilation, new [] {syntaxTree}, languageConversion, GetConvertedCompilation(compilation, languageConversion));
5862
var conversionResults = ConvertProjectContents(conversion).ToList();
5963
var codeResult = conversionResults.SingleOrDefault(x => !string.IsNullOrWhiteSpace(x.ConvertedCode))
6064
?? conversionResults.First();
@@ -68,10 +72,28 @@ public static IEnumerable<ConversionResult> ConvertProjectContents(Project proje
6872
var solutionFilePath = project.Solution.FilePath;
6973
var solutionDir = Path.GetDirectoryName(solutionFilePath);
7074
var compilation = project.GetCompilationAsync().GetAwaiter().GetResult();
71-
var projectConversion = new ProjectConversion(compilation, solutionDir, languageConversion);
75+
var projectConversion = new ProjectConversion(compilation, solutionDir, languageConversion, GetConvertedCompilation(project.GetCompilationAsync().GetAwaiter().GetResult(), languageConversion));
7276
foreach (var conversionResult in ConvertProjectContents(projectConversion)) yield return conversionResult;
7377
}
7478

79+
/// <summary>
80+
/// If the source compilation has project references to a compilation of the same language, this will fail with an argument exception.
81+
/// Use <see cref="GetConvertedCompilationWithProjectReferences"/> wherever this is possible.
82+
/// </summary>
83+
private static Compilation GetConvertedCompilation(Compilation compilation, ILanguageConversion languageConversion)
84+
{
85+
return languageConversion is VBToCSConversion ? CSToVBConversion.CreateCSharpCompilation(compilation.References) : (Compilation) VBToCSConversion.CreateVisualBasicCompilation(compilation.References);
86+
}
87+
88+
private static Compilation GetConvertedCompilationWithProjectReferences(Project project, ILanguageConversion languageConversion)
89+
{
90+
return project.Solution.RemoveProject(project.Id)
91+
.AddProject(project.Id, project.Name, project.AssemblyName, languageConversion.TargetLanguage)
92+
.GetProject(project.Id)
93+
.WithProjectReferences(project.AllProjectReferences).WithMetadataReferences(project.MetadataReferences)
94+
.GetCompilationAsync().GetAwaiter().GetResult();
95+
}
96+
7597
private static IEnumerable<ConversionResult> ConvertProjectContents(ProjectConversion projectConversion)
7698
{
7799
foreach (var pathNodePair in projectConversion.Convert())

ICSharpCode.CodeConverter/Util/ReferenceConverter.cs

-19
This file was deleted.

ICSharpCode.CodeConverter/VB/CSToVBConversion.cs

+13-9
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,20 @@ namespace ICSharpCode.CodeConverter.VB
1919
{
2020
public class CSToVBConversion : ILanguageConversion
2121
{
22-
private readonly List<SyntaxTree> _firstPassResults = new List<SyntaxTree>();
2322
private Compilation _sourceCompilation;
23+
private VisualBasicCompilation _convertedCompilation;
2424

25-
private VisualBasicCompilation CreateCompilation(List<SyntaxTree> vbTrees)
25+
public void Initialize(Compilation convertedCompilation)
2626
{
27-
var references = _sourceCompilation.References.Select(ReferenceConverter.ConvertReference);
28-
return VisualBasicCompilation.Create("Conversion", vbTrees, references,
29-
new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
27+
_convertedCompilation = (VisualBasicCompilation) convertedCompilation;
3028
}
3129

3230
public SyntaxTree SingleFirstPass(Compilation sourceCompilation, SyntaxTree tree)
3331
{
3432
_sourceCompilation = sourceCompilation;
3533
var converted = CSharpConverter.ConvertCompilationTree((CSharpCompilation)sourceCompilation, (CSharpSyntaxTree)tree);
3634
var convertedTree = VBSyntaxFactory.SyntaxTree(converted);
37-
_firstPassResults.Add(convertedTree);
35+
_convertedCompilation = _convertedCompilation.AddSyntaxTrees(convertedTree);
3836
return convertedTree;
3937
}
4038

@@ -60,6 +58,8 @@ public SyntaxNode GetSurroundedNode(IEnumerable<SyntaxNode> descendantNodes,
6058
};
6159
}
6260

61+
public string TargetLanguage { get; } = LanguageNames.VisualBasic;
62+
6363
public bool CanBeContainedByMethod(SyntaxNode node)
6464
{
6565
return node is CSSyntax.IncompleteMemberSyntax ||
@@ -115,8 +115,7 @@ public SyntaxNode SingleSecondPass(KeyValuePair<string, SyntaxTree> cs)
115115

116116
public string GetWarningsOrNull()
117117
{
118-
var finalCompilation = CreateCompilation(_firstPassResults);
119-
return CompilationWarnings.WarningsForCompilation(_sourceCompilation, "source") + CompilationWarnings.WarningsForCompilation(finalCompilation, "target");
118+
return CompilationWarnings.WarningsForCompilation(_sourceCompilation, "source") + CompilationWarnings.WarningsForCompilation(_convertedCompilation, "target");
120119
}
121120

122121
public SyntaxTree CreateTree(string text)
@@ -126,7 +125,12 @@ public SyntaxTree CreateTree(string text)
126125

127126
public Compilation CreateCompilationFromTree(SyntaxTree tree, IEnumerable<MetadataReference> references)
128127
{
129-
return CSharpCompilation.Create("Conversion", new[] { tree }, references, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
128+
return CreateCSharpCompilation(references).AddSyntaxTrees(tree);
129+
}
130+
131+
public static CSharpCompilation CreateCSharpCompilation(IEnumerable<MetadataReference> references)
132+
{
133+
return CSharpCompilation.Create("Conversion", references: references, options: new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary));
130134
}
131135
}
132136
}

0 commit comments

Comments
 (0)