public async Task<IEnumerable<IMemberMetric>> Calculate(TypeDeclarationSyntaxInfo typeNode)
		{
			var walker = new MemberCollector();
			var members = walker.GetMembers(typeNode).AsArray();
			if ((typeNode.Syntax is ClassDeclarationSyntax
				|| typeNode.Syntax is StructDeclarationSyntax)
				&& members.All(m => m.Kind() != SyntaxKind.ConstructorDeclaration))
			{
				var defaultConstructor = SyntaxFactory.ConstructorDeclaration(typeNode.Name)
											   .WithModifiers(SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.PublicKeyword)))
											   .WithBody(SyntaxFactory.Block());
				members = members.Concat(new[] { defaultConstructor }).AsArray();
			}

			var metrics = await CalculateMemberMetrics(members).ConfigureAwait(false);
			return metrics.AsArray();
		}
		private static async Task<Tuple<Compilation, SemanticModel, TypeDeclarationSyntaxInfo>> VerifyCompilation(Compilation compilation, TypeDeclarationSyntaxInfo typeNode)
		{
			var tree = typeNode.Syntax.SyntaxTree;

			if (tree == null)
			{
				var cu = CSharpSyntaxTree.Create(
					SyntaxFactory
					.CompilationUnit()
					.WithMembers(SyntaxFactory.List(new[] { (MemberDeclarationSyntax)typeNode.Syntax })));
				var root = await cu.GetRootAsync().ConfigureAwait(false);
				typeNode.Syntax = (TypeDeclarationSyntax)root.ChildNodes().First();
				var newCompilation = compilation.AddSyntaxTrees(cu);
				var semanticModel = newCompilation.GetSemanticModel(cu);
				return new Tuple<Compilation, SemanticModel, TypeDeclarationSyntaxInfo>(newCompilation, semanticModel, typeNode);
			}

			var result = AddToCompilation(compilation, tree);
			var childNodes = result.Item2.GetRoot().DescendantNodesAndSelf();
			typeNode.Syntax = childNodes.OfType<TypeDeclarationSyntax>().First();
			return new Tuple<Compilation, SemanticModel, TypeDeclarationSyntaxInfo>(
				result.Item1,
				result.Item1.GetSemanticModel(result.Item2),
				typeNode);
		}
Exemplo n.º 3
0
		public IEnumerable<SyntaxNode> GetMembers(TypeDeclarationSyntaxInfo type)
		{
			Visit(type.Syntax);
			return _members.ToList();
		}