Skip to content

Commit

Permalink
fix: #5: system types cannot be included as mixins
Browse files Browse the repository at this point in the history
All types that are stored in CLR module cannot be included as mixins
  • Loading branch information
pgenfer committed Feb 27, 2016
1 parent a550d0d commit 7ecfcb9
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 2 deletions.
14 changes: 14 additions & 0 deletions MixinRefactoring.Test/Integration/Dummys/Person.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,18 @@ public class PersonFromAbstractName : PersonWithAbstractName
{
private SimpleName _name;
}

/// <summary>
/// this class has only native data types
/// and therefore no mixins should be implemented
/// </summary>
public class PersonWithNativeTypes
{
private int _int;
private string _string;
private byte _byte;
private double _double;
private decimal _decimal;
private IEnumerable<int> _enumerable;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.CSharp;
using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory;

namespace MixinRefactoring.Test
{
Expand Down
31 changes: 31 additions & 0 deletions MixinRefactoring.Test/Integration/MixinCommandTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Microsoft.CodeAnalysis.CSharp.Syntax;
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;

namespace MixinRefactoring.Test
{
[TestFixture]
public class MixinCommandTest
{
[Test]
public void ClassWithNativeFields_CanExecuteMixinCommand_CannotExecute()
{
// arrange
// 1. load source files and get class and mixin declarations
var sourceCode = new SourceCode("Person.cs");
var personClass = sourceCode.Class("PersonWithNativeTypes");
var fieldDeclarations = personClass.DescendantNodes().OfType<FieldDeclarationSyntax>();

foreach(var fieldDeclaration in fieldDeclarations)
{
var mixinCommand = new MixinCommand(sourceCode.Semantic, fieldDeclaration);
Assert.IsFalse(mixinCommand.CanExecute());
}
}
}
}
1 change: 1 addition & 0 deletions MixinRefactoring.Test/MixinRefactoring.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<Compile Include="Integration\MethodSyntaxReaderTest.cs" />
<Compile Include="Integration\ParameterSyntaxReaderTest.cs" />
<Compile Include="Integration\PropertySyntaxReaderTest.cs" />
<Compile Include="Integration\MixinCommandTest.cs" />
<Compile Include="Unit\SemanticTypeReaderTest.cs" />
<Compile Include="Unit\MemberCompareTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
3 changes: 2 additions & 1 deletion MixinRefactoring.Vsix/source.extension.vsixmanifest
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<PackageManifest Version="2.0.0" xmlns="http://schemas.microsoft.com/developer/vsx-schema/2011" xmlns:d="http://schemas.microsoft.com/developer/vsx-schema-design/2011">
<Metadata>
<Identity Id="mixinSharp..6af25ee5-c001-4b3b-85f4-e7785ff466f0" Version="0.9.1" Language="en-US" Publisher="Patric Genfer"/>
<Identity Id="mixinSharp..6af25ee5-c001-4b3b-85f4-e7785ff466f0" Version="1.0.0" Language="en-US" Publisher="Patric Genfer"/>
<DisplayName>mixinSharp</DisplayName>
<Description xml:space="preserve">A code refactoring extension that adds mixin support to C# by auto generating the required code.</Description>
<MoreInfo>https://github.com/pgenfer/mixinSharp/wiki</MoreInfo>
<License>license.txt</License>
<GettingStartedGuide>https://github.com/pgenfer/mixinSharp/wiki/Tutorial</GettingStartedGuide>
<Icon>mixin_logo.png</Icon>
Expand Down
13 changes: 13 additions & 0 deletions MixinRefactoring/SyntaxReader/FieldSyntaxReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ namespace MixinRefactoring
{
public class FieldSyntaxReader : SyntaxWalkerWithSemantic
{
/// <summary>
/// string constant used to identify the CLR module by name
/// (the place where the system types are defined)
/// </summary>
private const string CommonLanguageRuntimeLibrary = "CommonLanguageRuntimeLibrary";

public MixinReference MixinReference { get; private set; }

public FieldSyntaxReader(SemanticModel semantic):base(semantic)
Expand All @@ -28,6 +34,13 @@ public override void VisitVariableDeclarator(VariableDeclaratorSyntax node)
// type could not be resolved => return here
if (typeOfField.TypeKind == TypeKind.Error)
return;

// also ignore native types (like int, string, double etc...)
// we identify them by checking if the types are declared in the runtime itself
// if yes, they are system types and we will skip them
var module = typeOfField.ContainingModule;
if (module != null && module.Name == CommonLanguageRuntimeLibrary)
return;

var classFactory = new ClassFactory(_semantic);
// create the mixin reference, that is the name of the field and the type the field references
Expand Down

0 comments on commit 7ecfcb9

Please sign in to comment.