Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undocumented public API metric should count only documentation comments #2148

Merged
merged 2 commits into from
Dec 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion its/projects/MetricsTest/bar/Class1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

namespace CSLib.bar
{
// Commented public API
/// <summary>
/// Documented public API
/// </summary>
public class MyClazz
{
public int MyProperty
Expand Down
4 changes: 3 additions & 1 deletion its/projects/MetricsTest/foo/Class1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

namespace CSLib.foo
{
// Commented public API
public class MyClazz
{
/// <summary>
/// Documented public API
/// </summary>
public int MyProperty
{
get
Expand Down
6 changes: 4 additions & 2 deletions its/projects/MetricsTest/foo/Class1_copy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

namespace CSLib.foo2
{
// Commented public API
public class MyClazz
{
/// <summary>
/// Documented public API
/// </summary>
public int MyProperty
{
get
Expand All @@ -25,7 +27,7 @@ class Class1
public void Main()
{
MyClazz myClazz = new MyClazz();
Console.WriteLine("Hello, world! " + myClazz.MyProperty);
Console.WriteLine(myClazz.MyProperty);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Forgot to revert this change I made while debugging...

Console.ReadLine();
}
}
Expand Down
3 changes: 3 additions & 0 deletions its/projects/VbMetricsTest/bar/Module1.vb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
REM This is a comment!

Public Module bar
''' <summary>
''' Documented public API
''' </summary>
Public Class MyClazz
Public ReadOnly Property MyProperty As Integer
Get
Expand Down
3 changes: 3 additions & 0 deletions its/projects/VbMetricsTest/foo/Module1.vb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
REM This is a comment!

Public Module foo
''' <summary>
''' Documented public API
''' </summary>
Public Class MyClazz
Public ReadOnly Property MyProperty As Integer
Get
Expand Down
3 changes: 3 additions & 0 deletions its/projects/VbMetricsTest/foo/Module1_copy.vb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
REM This is a comment!

Public Module foo2
''' <summary>
''' Documented public API
''' </summary>
Public Class MyClazz
Public ReadOnly Property MyProperty As Integer
Get
Expand Down
30 changes: 15 additions & 15 deletions its/src/test/java/com/sonar/it/csharp/MetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,17 +163,17 @@ public void complexityInFunctionsAtFileLevel() {

@Test
public void linesAtProjectLevel() {
assertThat(getProjectMeasureAsInt("lines")).isEqualTo(99);
assertThat(getProjectMeasureAsInt("lines")).isEqualTo(105);
}

@Test
public void linesAtDirectoryLevel() {
assertThat(getDirectoryMeasureAsInt("lines")).isEqualTo(66);
assertThat(getDirectoryMeasureAsInt("lines")).isEqualTo(70);
}

@Test
public void linesAtFileLevel() {
assertThat(getFileMeasureAsInt("lines")).isEqualTo(33);
assertThat(getFileMeasureAsInt("lines")).isEqualTo(35);
}

/* Lines of code */
Expand All @@ -197,17 +197,17 @@ public void linesOfCodeAtFileLevel() {

@Test
public void commentLinesAtProjectLevel() {
assertThat(getProjectMeasureAsInt("comment_lines")).isEqualTo(6);
assertThat(getProjectMeasureAsInt("comment_lines")).isEqualTo(12);
}

@Test
public void commentLinesAtDirectoryLevel() {
assertThat(getDirectoryMeasureAsInt("comment_lines")).isEqualTo(4);
assertThat(getDirectoryMeasureAsInt("comment_lines")).isEqualTo(8);
}

@Test
public void commentLinesAtFileLevel() {
assertThat(getFileMeasureAsInt("comment_lines")).isEqualTo(2);
assertThat(getFileMeasureAsInt("comment_lines")).isEqualTo(4);
}

/* Functions */
Expand Down Expand Up @@ -296,20 +296,18 @@ public void linesOfCodeByLine() {

assertThat(value).contains("9=1");
assertThat(value).contains("10=1");

assertThat(value).contains("11=1");
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lines of code shifted because of the new comments I added

assertThat(value).contains("12=1");
assertThat(value).contains("13=1");
assertThat(value).contains("14=1");
assertThat(value).contains("15=1");

assertThat(value).contains("16=1");
assertThat(value).contains("17=1");
assertThat(value).contains("18=1");
assertThat(value).contains("19=1");
assertThat(value).contains("20=1");
assertThat(value).contains("21=1");

assertThat(value).contains("22=1");
assertThat(value).contains("23=1");
assertThat(value).contains("24=1");

assertThat(value).contains("25=1");
assertThat(value).contains("26=1");
assertThat(value).contains("27=1");
Expand All @@ -318,6 +316,8 @@ public void linesOfCodeByLine() {
assertThat(value).contains("30=1");
assertThat(value).contains("31=1");
assertThat(value).contains("32=1");
assertThat(value).contains("33=1");
assertThat(value).contains("34=1");

assertThat(value.length()).isEqualTo(128); // No other line
}
Expand All @@ -329,9 +329,9 @@ public void executableLines() {

String value = getFileMeasure("executable_lines_data").getValue();

assertThat(value).contains("18=1");
assertThat(value).contains("28=1");
assertThat(value).contains("29=1");
assertThat(value).contains("20=1");
assertThat(value).contains("30=1");
assertThat(value).contains("31=1");

assertThat(value.length()).isEqualTo(14); // No other lines
}
Expand Down
22 changes: 11 additions & 11 deletions its/src/test/java/com/sonar/it/vbnet/MetricsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -160,17 +160,17 @@ public void complexityInFunctionsAtFileLevel() {

@Test
public void linesAtProjectLevel() {
assertThat(getProjectMeasureAsInt("lines")).isEqualTo(60);
assertThat(getProjectMeasureAsInt("lines")).isEqualTo(69);
}

@Test
public void linesAtDirectoryLevel() {
assertThat(getDirectoryMeasureAsInt("lines")).isEqualTo(40);
assertThat(getDirectoryMeasureAsInt("lines")).isEqualTo(46);
}

@Test
public void linesAtFileLevel() {
assertThat(getFileMeasureAsInt("lines")).isEqualTo(20);
assertThat(getFileMeasureAsInt("lines")).isEqualTo(23);
}

/* Comment lines */
Expand All @@ -194,17 +194,17 @@ public void linesOfCodeAtFileLevel() {

@Test
public void commentLinesAtProjectLevel() {
assertThat(getProjectMeasureAsInt("comment_lines")).isEqualTo(3);
assertThat(getProjectMeasureAsInt("comment_lines")).isEqualTo(12);
}

@Test
public void commentLinesAtDirectoryLevel() {
assertThat(getDirectoryMeasureAsInt("comment_lines")).isEqualTo(2);
assertThat(getDirectoryMeasureAsInt("comment_lines")).isEqualTo(8);
}

@Test
public void commentLinesAtFileLevel() {
assertThat(getFileMeasureAsInt("comment_lines")).isEqualTo(1);
assertThat(getFileMeasureAsInt("comment_lines")).isEqualTo(4);
}

/* Classes */
Expand Down Expand Up @@ -295,22 +295,22 @@ public void linesOfCodeByLine() {

assertThat(value).contains("1=1");
assertThat(value).contains("5=1");
assertThat(value).contains("6=1");
assertThat(value).contains("7=1");
assertThat(value).contains("8=1");
assertThat(value).contains("9=1");
assertThat(value).contains("10=1");
assertThat(value).contains("11=1");
assertThat(value).contains("12=1");
assertThat(value).contains("13=1");
assertThat(value).contains("14=1");
assertThat(value).contains("15=1");
assertThat(value).contains("16=1");
assertThat(value).contains("17=1");
assertThat(value).contains("18=1");
assertThat(value).contains("19=1");
assertThat(value).contains("20=1");
assertThat(value).contains("21=1");
assertThat(value).contains("22=1");


assertThat(value.length()).isEqualTo(68); // No other line
assertThat(value.length()).isEqualTo(71); // No other line
}

private Measure getFileMeasure(String metricKey) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* SonarAnalyzer for .NET
* Copyright (C) 2015-2018 SonarSource SA
* mailto: contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using SonarAnalyzer.Helpers;

namespace SonarAnalyzer.Metrics
{
public static class CSharpPublicApiMetric
{
public static ImmutableArray<SyntaxNode> GetMembers(SyntaxTree syntaxTree)
{
var root = syntaxTree.GetRoot();
var publicNodes = ImmutableArray.CreateBuilder<SyntaxNode>();
var toVisit = new Stack<SyntaxNode>();

PushChildMembers(root, toVisit);

while (toVisit.Any())
{
var member = toVisit.Pop();

var isPublic = member.ChildTokens().AnyOfKind(SyntaxKind.PublicKeyword);
if (isPublic)
{
publicNodes.Add(member);
}
else if (!member.IsKind(SyntaxKind.NamespaceDeclaration))
{
continue;
}

PushChildMembers(member, toVisit);
}

return publicNodes.ToImmutable();
}

private static void PushChildMembers(SyntaxNode syntaxNode, Stack<SyntaxNode> stack)
{
var members = syntaxNode.ChildNodes().OfType<MemberDeclarationSyntax>();
foreach (var member in members)
{
stack.Push(member);
}
}
}
}
Loading