Skip to content

Commit e4b458b

Browse files
committed
Cleanup null checks
1 parent 4cbb927 commit e4b458b

File tree

8 files changed

+85
-56
lines changed

8 files changed

+85
-56
lines changed

Disassembler/AssemblyLoader.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,15 @@ internal class AssemblyLoader
3737

3838
public AssemblyLoader(string assemblyPath) {
3939
this.assemblyPath = assemblyPath;
40-
context = new LocalFolderLoadContext(Path.GetDirectoryName(assemblyPath));
40+
var assemDir = Path.GetDirectoryName(assemblyPath);
41+
if (assemDir != null)
42+
{
43+
context = new LocalFolderLoadContext(assemDir);
44+
}
45+
else
46+
{
47+
throw new Exception("Unexpected null getting assembly directory.");
48+
}
4149
}
4250

4351
public Assembly? Load()

Disassembler/CompiledFileLocator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ private List<CompilationInfo> ReadCompiledFile(string file)
5353
}
5454
}
5555
}
56-
catch (Exception ex)
56+
catch (Exception)
5757
{
58-
58+
//ignore
5959
}
6060
return result;
6161
}

DotNETDepends/Dependencies.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public List<SourceType> FindSourceSymbolReferences(SourceType symbol)
100100
{
101101
foreach (var typeRef in type.TypeReferences)
102102
{
103-
if (typeRef.Name.Equals(symbol.Name) && typeRef.Namespace.Equals(symbol.Namespace))
103+
if (typeRef.Name.Equals(symbol.Name) && symbol.Namespace.Equals(typeRef.Namespace))
104104
{
105105
result.Add(type);
106106
break;

DotNETDepends/PublishedWebProject.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,14 @@ private static void AddFilesToList(List<string> paths, FileInfo[] files)
4040
private List<string> GetSourceFiles()
4141
{
4242
var result = new List<string>();
43-
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(project.FilePath));
44-
AddFilesToList(result, dirInfo.GetFiles("*.cshtml", SearchOption.AllDirectories));
45-
AddFilesToList(result, dirInfo.GetFiles("*.vbhtml", SearchOption.AllDirectories));
46-
AddFilesToList(result, dirInfo.GetFiles("*.razor", SearchOption.AllDirectories));
43+
var dir = Path.GetDirectoryName(project.FilePath);
44+
if (dir != null)
45+
{
46+
var dirInfo = new DirectoryInfo(dir);
47+
AddFilesToList(result, dirInfo.GetFiles("*.cshtml", SearchOption.AllDirectories));
48+
AddFilesToList(result, dirInfo.GetFiles("*.vbhtml", SearchOption.AllDirectories));
49+
AddFilesToList(result, dirInfo.GetFiles("*.razor", SearchOption.AllDirectories));
50+
}
4751
return result;
4852
}
4953

@@ -105,7 +109,7 @@ private void ReadProjectFile(IErrorReporter errorReporter)
105109
{
106110
if (project.FilePath != null && assemblyName != null)
107111
{
108-
var binRoot = Path.Combine(new string[] { Path.GetDirectoryName(project.FilePath), "bin", config });
112+
var binRoot = Path.Combine(new string[] { Path.GetDirectoryName(project.FilePath)?? "", "bin", config });
109113
DirectoryInfo dirInfo = new(binRoot);
110114
var dirs = dirInfo.GetDirectories();
111115
string? assemPath = null;

DotNETDepends/RoslynProject.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,20 @@ internal class RoslynProject
4141
protected readonly Project project;
4242
private readonly IErrorReporter errorReporter;
4343
private readonly Dependencies dependencies;
44-
private readonly string solutionRoot;
44+
private readonly string? solutionRoot;
4545
public RoslynProject(Project project, Dependencies dependencies, IErrorReporter errorReporter)
4646
{
4747
this.project = project;
4848
this.dependencies = dependencies;
4949
this.errorReporter = errorReporter;
5050
solutionRoot = Path.GetDirectoryName(project.Solution.FilePath);
51+
5152
}
5253

5354
public async Task Analyze()
5455
{
5556
var compilation = await project.GetCompilationAsync().ConfigureAwait(false);
56-
if (compilation != null)
57+
if (compilation != null && solutionRoot != null)
5758
{
5859

5960
foreach (var tree in compilation.SyntaxTrees)

DotNETDepends/SolutionReader.cs

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,25 @@ private static bool ProjectContainsNETWebFiles(Project project)
211211
{
212212
if (project.FilePath != null)
213213
{
214-
var dirInfo = new DirectoryInfo(Path.GetDirectoryName(project.FilePath));
215-
var cshtml = dirInfo.GetFiles("*.cshtml", SearchOption.AllDirectories);
216-
if (cshtml.Length > 0)
214+
var projDir = Path.GetDirectoryName(project.FilePath);
215+
if (projDir != null)
217216
{
218-
return true;
219-
}
220-
var vbhtml = dirInfo.GetFiles("*.vbhtml", SearchOption.AllDirectories);
221-
if (vbhtml.Length > 0)
222-
{
223-
return true;
224-
}
225-
var razor = dirInfo.GetFiles("*.razor", SearchOption.AllDirectories);
226-
if (razor.Length > 0)
227-
{
228-
return true;
217+
var dirInfo = new DirectoryInfo(projDir);
218+
var cshtml = dirInfo.GetFiles("*.cshtml", SearchOption.AllDirectories);
219+
if (cshtml.Length > 0)
220+
{
221+
return true;
222+
}
223+
var vbhtml = dirInfo.GetFiles("*.vbhtml", SearchOption.AllDirectories);
224+
if (vbhtml.Length > 0)
225+
{
226+
return true;
227+
}
228+
var razor = dirInfo.GetFiles("*.razor", SearchOption.AllDirectories);
229+
if (razor.Length > 0)
230+
{
231+
return true;
232+
}
229233
}
230234
}
231235
return false;

DotNETDepends/SourceTypeBuilderFactory.cs

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ internal abstract class SourceTypeBuilderBase : SourceTypeBuilder
7979
{
8080
return null;
8181
}
82-
return FindNamespaceFromViewImports(Path.GetDirectoryName(startDir), projectDir, importFileName, importFileExtension, out typeRoot);
82+
return FindNamespaceFromViewImports(Path.GetDirectoryName(startDir) ?? "", projectDir, importFileName, importFileExtension, out typeRoot);
8383

8484
}
8585
/*
@@ -89,28 +89,30 @@ protected string DetermineNamespaceFromPath(string path, string? rootNamespace,
8989
{
9090
var relativePath = Path.GetRelativePath(projectDir, path);
9191
relativePath = Path.GetDirectoryName(relativePath);
92-
if (relativePath.StartsWith(Path.DirectorySeparatorChar))
92+
if (relativePath != null)
9393
{
94-
relativePath = relativePath.Substring(1);
95-
}
96-
if (relativePath.EndsWith(Path.DirectorySeparatorChar))
97-
{
98-
relativePath = relativePath[..^1];
99-
}
100-
relativePath = relativePath.Replace(Path.DirectorySeparatorChar, '.');
101-
if (rootNamespace != null && rootNamespace.Length != 0)
102-
{
103-
if (relativePath.Length > 0)
94+
if (relativePath.StartsWith(Path.DirectorySeparatorChar))
10495
{
105-
return rootNamespace + "." + relativePath;
96+
relativePath = relativePath.Substring(1);
10697
}
107-
else
98+
if (relativePath.EndsWith(Path.DirectorySeparatorChar))
10899
{
109-
return rootNamespace;
100+
relativePath = relativePath[..^1];
101+
}
102+
relativePath = relativePath.Replace(Path.DirectorySeparatorChar, '.');
103+
if (rootNamespace != null && rootNamespace.Length != 0)
104+
{
105+
if (relativePath.Length > 0)
106+
{
107+
return rootNamespace + "." + relativePath;
108+
}
109+
else
110+
{
111+
return rootNamespace;
112+
}
110113
}
111114
}
112-
113-
return relativePath;
115+
return relativePath ?? "";
114116
}
115117
}
116118

@@ -124,7 +126,7 @@ public override SourceType Build(string? rootNamespace, string filePath, string
124126
var ns = ReadNamespaceFromFile(filePath);
125127
if (ns == null)
126128
{
127-
ns = FindNamespaceFromViewImports(Path.GetDirectoryName(filePath), projectDir, "_Imports", ".razor", out string? typeRoot);
129+
ns = FindNamespaceFromViewImports(Path.GetDirectoryName(filePath) ?? "", projectDir, "_Imports", ".razor", out string? typeRoot);
128130
ns = DetermineNamespaceFromPath(filePath, ns ?? rootNamespace, typeRoot ?? projectDir);
129131
}
130132
ns ??= "";
@@ -145,7 +147,7 @@ public override SourceType Build(string? rootNamespace, string filePath, string
145147
var ns = ReadNamespaceFromFile(filePath);
146148
if (ns == null)
147149
{
148-
ns = FindNamespaceFromViewImports(Path.GetDirectoryName(filePath), projectDir, "_ViewImports", Path.GetExtension(filePath), out string? typeRoot);
150+
ns = FindNamespaceFromViewImports(Path.GetDirectoryName(filePath) ?? "", projectDir, "_ViewImports", Path.GetExtension(filePath), out string? typeRoot);
149151
ns = DetermineNamespaceFromPath(filePath, ns ?? rootNamespace, typeRoot ?? projectDir);
150152
}
151153
ns ??= "";
@@ -160,15 +162,19 @@ private string GetTypeName(string filePath, string projectDir)
160162
{
161163
var relativeFile = Path.GetRelativePath(projectDir, filePath);
162164
var relativeDir = Path.GetDirectoryName(relativeFile);
163-
if (relativeDir.StartsWith(Path.DirectorySeparatorChar))
164-
{
165-
relativeDir = relativeDir.Substring(1);
166-
}
167-
if (relativeDir.EndsWith(Path.DirectorySeparatorChar))
165+
if (relativeDir != null)
168166
{
169-
relativeDir = relativeDir[..^1];
167+
if (relativeDir.StartsWith(Path.DirectorySeparatorChar))
168+
{
169+
relativeDir = relativeDir.Substring(1);
170+
}
171+
if (relativeDir.EndsWith(Path.DirectorySeparatorChar))
172+
{
173+
relativeDir = relativeDir[..^1];
174+
}
175+
return relativeDir.Replace(Path.DirectorySeparatorChar, '_') + "_" + Path.GetFileNameWithoutExtension(filePath);
170176
}
171-
return relativeDir.Replace(Path.DirectorySeparatorChar, '_') + "_" + Path.GetFileNameWithoutExtension(filePath);
177+
return "";
172178
}
173179
}
174180
internal class SourceTypeBuilderFactory

TestDependencyReading/TestUtils.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ namespace TestDependencyReading
88
{
99
internal class TestUtils
1010
{
11-
public static string? GetFixturesPath()
11+
public static string GetFixturesPath()
1212
{
13-
string workingDir = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
14-
while(workingDir != null && !workingDir.EndsWith("TestDependencyReading")) {
15-
workingDir= Path.GetDirectoryName(workingDir);
13+
var assembly = System.Reflection.Assembly.GetExecutingAssembly();
14+
if (assembly != null && assembly.Location != null)
15+
{
16+
string? workingDir = Path.GetDirectoryName(assembly.Location);
17+
while (workingDir != null && !workingDir.EndsWith("TestDependencyReading"))
18+
{
19+
workingDir = Path.GetDirectoryName(workingDir);
20+
}
21+
return Path.Combine(workingDir ?? "", "Fixtures");
1622
}
17-
return Path.Combine(workingDir, "Fixtures");
23+
throw new Exception("Unable to determine fixture directory.");
1824
}
1925
}
2026
}

0 commit comments

Comments
 (0)