Skip to content

Commit

Permalink
Merge pull request #95 from NeVeSpl/correctionOfPR82
Browse files Browse the repository at this point in the history
correction Of #82
  • Loading branch information
BenMorris authored May 18, 2021
2 parents 80cfe15 + 4cd0883 commit e780e99
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 12 deletions.
14 changes: 7 additions & 7 deletions src/NetArchTest.Rules/Dependencies/DependencySearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal class DependencySearch
/// <returns>A list of found types.</returns>
public IReadOnlyList<TypeDefinition> FindTypesThatHaveDependencyOnAny(IEnumerable<TypeDefinition> input, IEnumerable<string> dependencies)
{
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.HaveDependencyOnAny, dependencies);
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.HaveDependencyOnAny, dependencies, true);
}

/// <summary>
Expand All @@ -31,7 +31,7 @@ public IReadOnlyList<TypeDefinition> FindTypesThatHaveDependencyOnAny(IEnumerabl
/// <returns>A list of found types.</returns>
public IReadOnlyList<TypeDefinition> FindTypesThatHaveDependencyOnAll(IEnumerable<TypeDefinition> input, IEnumerable<string> dependencies)
{
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.HaveDependencyOnAll, dependencies);
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.HaveDependencyOnAll, dependencies, true);
}

/// <summary>
Expand All @@ -42,7 +42,7 @@ public IReadOnlyList<TypeDefinition> FindTypesThatHaveDependencyOnAll(IEnumerabl
/// <returns>A list of found types.</returns>
public IReadOnlyList<TypeDefinition> FindTypesThatOnlyHaveDependenciesOnAnyOrNone(IEnumerable<TypeDefinition> input, IEnumerable<string> dependencies)
{
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.OnlyHaveDependenciesOnAnyOrNone, dependencies);
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.OnlyHaveDependenciesOnAnyOrNone, dependencies, false);
}

/// <summary>
Expand All @@ -53,7 +53,7 @@ public IReadOnlyList<TypeDefinition> FindTypesThatOnlyHaveDependenciesOnAnyOrNon
/// <returns>A list of found types.</returns>
public IReadOnlyList<TypeDefinition> FindTypesThatOnlyHaveDependenciesOnAny(IEnumerable<TypeDefinition> input, IEnumerable<string> dependencies)
{
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.OnlyHaveDependenciesOnAny, dependencies);
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.OnlyHaveDependenciesOnAny, dependencies, false);
}

/// <summary>
Expand All @@ -64,17 +64,17 @@ public IReadOnlyList<TypeDefinition> FindTypesThatOnlyHaveDependenciesOnAny(IEnu
/// <returns>A list of found types.</returns>
public IReadOnlyList<TypeDefinition> FindTypesThatOnlyOnlyHaveDependenciesOnAll(IEnumerable<TypeDefinition> input, IEnumerable<string> dependencies)
{
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.OnlyHaveDependenciesOnAll, dependencies);
return FindTypes(input, TypeDefinitionCheckingResult.SearchType.OnlyHaveDependenciesOnAll, dependencies, false);
}

private List<TypeDefinition> FindTypes(IEnumerable<TypeDefinition> input, TypeDefinitionCheckingResult.SearchType searchType, IEnumerable<string> dependencies)
private List<TypeDefinition> FindTypes(IEnumerable<TypeDefinition> input, TypeDefinitionCheckingResult.SearchType searchType, IEnumerable<string> dependencies, bool serachForDependencyInFieldConstant)
{
var output = new List<TypeDefinition>();
var searchTree = new CachedNamespaceTree(dependencies);

foreach (var type in input)
{
var context = new TypeDefinitionCheckingContext(type, searchType, searchTree);
var context = new TypeDefinitionCheckingContext(type, searchType, searchTree, serachForDependencyInFieldConstant);
if (context.IsTypeFound())
{
output.Add(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
internal class TypeDefinitionCheckingContext
{
private readonly TypeDefinition _typeToCheck;
private readonly TypeDefinitionCheckingResult _result;

private readonly TypeDefinitionCheckingResult _result;
private readonly bool _serachForDependencyInFieldConstant;

public TypeDefinitionCheckingContext(TypeDefinition typeToCheck, TypeDefinitionCheckingResult.SearchType searchType, ISearchTree searchTree)
public TypeDefinitionCheckingContext(TypeDefinition typeToCheck, TypeDefinitionCheckingResult.SearchType searchType, ISearchTree searchTree, bool serachForDependencyInFieldConstant = false)
{
_typeToCheck = typeToCheck;
_result = new TypeDefinitionCheckingResult(searchType, searchTree);
_result = new TypeDefinitionCheckingResult(searchType, searchTree);
_serachForDependencyInFieldConstant = serachForDependencyInFieldConstant;
}

public bool IsTypeFound()
Expand Down Expand Up @@ -96,7 +97,7 @@ private void CheckFields(TypeDefinition typeToCheck)
{
CheckCustomAttributes(field);
CheckTypeReference(field.FieldType);
if (field.HasConstant && field.FieldType.FullName == typeof(string).FullName)
if (_serachForDependencyInFieldConstant && field.HasConstant && field.FieldType.FullName == typeof(string).FullName)
{
_result.CheckDependency(field.Constant.ToString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

public class Class_A
{
string stringField = "I am not a dependency!";

public static void LetUsCreateSomeAnonymousTypes()
{
var numbers = Enumerable.Range(0, 1);
Expand Down

0 comments on commit e780e99

Please sign in to comment.