Skip to content
Open
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
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,24 @@ To double check which configuration is being used, invoke
dotnet document config
```

If you wish to exclude private members, properties, or methods modify your configuration file like:

```yaml
class:
...
exclude_private: true
method:
...
exclude_private: true
property:
...
exclude_private: true
default_member:
...
exclude_private: true

```

> 👉 Folder based configuration discovery is not yet supported

## Acknowledgments
Expand Down
41 changes: 29 additions & 12 deletions src/DotnetDocument.Tools/Handlers/ApplyDocumentHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,17 @@ public Result Apply(string? path, bool isDryRun)

// Replace the
var changedSyntaxTree = root.ReplaceNodes(_walker.NodesWithoutXmlDoc,
(node, syntaxNode) => _serviceResolver
.Resolve(syntaxNode.Kind().ToString())
?
.Apply(syntaxNode) ?? syntaxNode);
(node, syntaxNode) =>
{
var strategy = _serviceResolver.Resolve(syntaxNode.Kind().ToString());

if (strategy != null && strategy.ShouldDocument(syntaxNode))
{
return strategy.Apply(syntaxNode);
}

return syntaxNode;
});

// TODO: Don't write if no changes

Expand Down Expand Up @@ -164,14 +171,24 @@ private IEnumerable<MemberDocumentationStatus> GetFileDocumentationStatus(string

foreach (var node in _walker.NodesWithoutXmlDoc)
{
var nodeWithDoc = _serviceResolver
.Resolve(node.Kind().ToString())
?
.Apply(node);

yield return new MemberDocumentationStatus(filePath, SyntaxUtils.FindMemberIdentifier(node),
node.Kind(), false, node, nodeWithDoc,
node.GetLocation().GetLineSpan().StartLinePosition.ToString());
var docStrategy = _serviceResolver.Resolve(node.Kind().ToString());
var shouldDocument = docStrategy?.ShouldDocument(node);

// Check if we should document the node
if (shouldDocument.HasValue && shouldDocument.Value)
{
var nodeWithDoc = docStrategy?.Apply(node);

yield return new MemberDocumentationStatus(filePath, SyntaxUtils.FindMemberIdentifier(node),
node.Kind(), false, node, nodeWithDoc,
node.GetLocation().GetLineSpan().StartLinePosition.ToString());
}
else
{
yield return new MemberDocumentationStatus(filePath, SyntaxUtils.FindMemberIdentifier(node),
node.Kind(), true, null, node,
node.GetLocation().GetLineSpan().StartLinePosition.ToString());
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/DotnetDocument/Configuration/DocumentationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ public abstract class MemberDocumentationOptionsBase
/// </summary>
public bool Required { get; init; } = true;

/// <summary>
/// Gets or inits the value of the exclude private
/// </summary>
public IEnumerable<string> AccessModifiers { get; init; } = new List<string>
{
"public",
"internal",
"private",
"protected",
"protected private",
"protected internal",
};

/// <summary>
/// Gets the syntax kind
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Linq;
using DotnetDocument.Configuration;
using DotnetDocument.Syntax;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace DotnetDocument.Extensions
{
public static class MemberDeclarationSyntaxExtensions
{
public static bool ShouldDocument(this MemberDeclarationSyntax node, MemberDocumentationOptionsBase options)
{
var shouldDocument = false;

foreach (var modifier in options.AccessModifiers)
{
var modifierArray = modifier.Split(" ");
Copy link

Choose a reason for hiding this comment

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

@tmonck I cloned your repo to try this out and this logic doesn't seem to work. For example, when I just had "public" in the access modifiers list in the config, the if (modifierArray.Length > 1) statement gets skipped over entirely

Copy link

@gmkado gmkado Apr 24, 2024

Choose a reason for hiding this comment

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

This is my workaround

private static readonly List<string> _accessorKeyWords =
[
    "public",
    "internal",
    "private",
    "protected"
];

public static bool ShouldDocument(this MemberDeclarationSyntax node, MemberDocumentationOptionsBase options)
{
    var nodeModifiers = node.Modifiers.Select(m => m.Text)
        .Where(mText=> _accessorKeyWords.Contains(mText));
    return options.AccessModifiers.Any(m=>
    {
        var optionModifier = m.Split(' ');
        return nodeModifiers.All(nodeModifier => optionModifier.Contains(nodeModifier));
    });
}

This doesn't allow the user to specify other valid modifiers besides what's in _accessorKeyWords (e.g. readonly) so not sure if this works for your implementation

Copy link
Author

Choose a reason for hiding this comment

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

I'm getting back into dotnet development day to day now. I'll look back at this and see if I can fix the issue.

Copy link
Author

Choose a reason for hiding this comment

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

I've updated the logic which should allow for public to be in the list by itself. It does ensure if you use a single word accessor like public that it's the first item in the modifiers for the node.


if (node != null)
{
if (modifierArray.Length > 1)
{
if (node.Modifiers.Any(m => m.Text.Equals(modifierArray[0], StringComparison.Ordinal)) &&
node.Modifiers.Any(m => m.Text.Equals(modifierArray[1], StringComparison.Ordinal)) &&
!SyntaxUtils.IsDocumented(node))
{
shouldDocument = true;

break;
}

}
else
{
if (node.Modifiers.All(m => m.Text.Equals(modifier, StringComparison.Ordinal)) &&
!SyntaxUtils.IsDocumented(node))
{
shouldDocument = true;

break;
}

if (node.Modifiers.Any() && node.Modifiers[0].Text.Equals(modifier, StringComparison.Ordinal) &&
!SyntaxUtils.IsDocumented(node))
{
shouldDocument = true;

break;
}
}
}
}

return shouldDocument;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using DotnetDocument.Syntax;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

namespace DotnetDocument.Strategies.Abstractions
{
Expand All @@ -18,6 +19,11 @@ public abstract class DocumentationStrategyBase<T> : IDocumentationStrategy wher
/// <returns>An enumerable of syntax kind</returns>
public abstract IEnumerable<SyntaxKind> GetSupportedKinds();

public bool ShouldDocument(SyntaxNode node) =>
ShouldDocument(node as T ?? throw new InvalidOperationException());

public abstract bool ShouldDocument(T node);

/// <summary>
/// Applies the node
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace DotnetDocument.Strategies.Abstractions
/// </summary>
public interface IDocumentationStrategy
{
bool ShouldDocument(SyntaxNode node);
/// <summary>
/// Gets the supported kinds
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/DotnetDocument/Strategies/ClassDocumentationStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using DotnetDocument.Configuration;
using DotnetDocument.Extensions;
using DotnetDocument.Format;
using DotnetDocument.Strategies.Abstractions;
using DotnetDocument.Syntax;
Expand Down Expand Up @@ -32,6 +33,7 @@ public class ClassDocumentationStrategy : DocumentationStrategyBase<ClassDeclara
/// </summary>
private readonly ClassDocumentationOptions _options;

public override bool ShouldDocument(ClassDeclarationSyntax node) => node.ShouldDocument(_options);
/// <summary>
/// Initializes a new instance of the <see cref="ClassDocumentationStrategy" /> class
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using DotnetDocument.Configuration;
using DotnetDocument.Extensions;
using DotnetDocument.Format;
using DotnetDocument.Strategies.Abstractions;
using DotnetDocument.Syntax;
Expand Down Expand Up @@ -32,6 +33,7 @@ public class ConstructorDocumentationStrategy : DocumentationStrategyBase<Constr
/// </summary>
private readonly CtorDocumentationOptions _options;

public override bool ShouldDocument(ConstructorDeclarationSyntax node) => node.ShouldDocument(_options);
/// <summary>
/// Initializes a new instance of the <see cref="ConstructorDocumentationStrategy" /> class
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using DotnetDocument.Configuration;
using DotnetDocument.Extensions;
using DotnetDocument.Format;
using DotnetDocument.Strategies.Abstractions;
using DotnetDocument.Syntax;
Expand Down Expand Up @@ -36,6 +38,7 @@ public class DefaultDocumentationStrategy : DocumentationStrategyBase<MemberDecl
/// </summary>
private readonly DefaultMemberDocumentationOptions _options;

public override bool ShouldDocument(MemberDeclarationSyntax node) => node.ShouldDocument(_options);
/// <summary>
/// Initializes a new instance of the <see cref="DefaultDocumentationStrategy" /> class
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/DotnetDocument/Strategies/EnumDocumentationStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using DotnetDocument.Configuration;
using DotnetDocument.Extensions;
using DotnetDocument.Format;
using DotnetDocument.Strategies.Abstractions;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -30,6 +31,7 @@ public class EnumDocumentationStrategy : DocumentationStrategyBase<EnumDeclarati
/// </summary>
private readonly EnumDocumentationOptions _options;

public override bool ShouldDocument(EnumDeclarationSyntax node) => node.ShouldDocument(_options);
/// <summary>
/// Initializes a new instance of the <see cref="EnumDocumentationStrategy" /> class
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using DotnetDocument.Configuration;
using DotnetDocument.Extensions;
using DotnetDocument.Format;
using DotnetDocument.Strategies.Abstractions;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -30,6 +31,13 @@ public class EnumMemberDocumentationStrategy : DocumentationStrategyBase<EnumMem
/// </summary>
private readonly EnumMemberDocumentationOptions _options;

/// <summary>
///
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public override bool ShouldDocument(EnumMemberDeclarationSyntax node) => node.ShouldDocument(_options);

/// <summary>
/// Initializes a new instance of the <see cref="EnumMemberDocumentationStrategy" /> class
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using DotnetDocument.Configuration;
using DotnetDocument.Extensions;
using DotnetDocument.Format;
using DotnetDocument.Strategies.Abstractions;
using DotnetDocument.Syntax;
Expand Down Expand Up @@ -32,6 +33,7 @@ public class InterfaceDocumentationStrategy : DocumentationStrategyBase<Interfac
/// </summary>
private readonly InterfaceDocumentationOptions _options;

public override bool ShouldDocument(InterfaceDeclarationSyntax node) => node.ShouldDocument(_options);
/// <summary>
/// Initializes a new instance of the <see cref="InterfaceDocumentationStrategy" /> class
/// </summary>
Expand All @@ -51,6 +53,7 @@ public override IEnumerable<SyntaxKind> GetSupportedKinds() => new[]
SyntaxKind.InterfaceDeclaration
};


/// <summary>
/// Applies the node
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using DotnetDocument.Configuration;
using DotnetDocument.Extensions;
using DotnetDocument.Format;
using DotnetDocument.Strategies.Abstractions;
using DotnetDocument.Syntax;
Expand Down Expand Up @@ -52,6 +53,8 @@ public override IEnumerable<SyntaxKind> GetSupportedKinds() => new[]
SyntaxKind.MethodDeclaration
};

public override bool ShouldDocument(MethodDeclarationSyntax node) => node.ShouldDocument(_options);

/// <summary>
/// Applies the node
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using DotnetDocument.Configuration;
using DotnetDocument.Extensions;
using DotnetDocument.Format;
using DotnetDocument.Strategies.Abstractions;
using Humanizer;
Expand Down Expand Up @@ -32,6 +33,13 @@ public class PropertyDocumentationStrategy : DocumentationStrategyBase<PropertyD
/// </summary>
private readonly PropertyDocumentationOptions _options;

/// <summary>
///
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
public override bool ShouldDocument(PropertyDeclarationSyntax node) => node.ShouldDocument(_options);

/// <summary>
/// Initializes a new instance of the <see cref="PropertyDocumentationStrategy" /> class
/// </summary>
Expand Down
5 changes: 5 additions & 0 deletions src/DotnetDocument/Syntax/DocumentationSyntaxWalker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ public class DocumentationSyntaxWalker : CSharpSyntaxWalker
/// Gets the value of the nodes with xml doc
/// </summary>
public IList<SyntaxNode> NodesWithXmlDoc { get; } = new List<SyntaxNode>();

/// <summary>
/// Gets the value of all nodes
/// </summary>
public IList<SyntaxNode> AllNodes { get; } = new List<SyntaxNode>();

/// <summary>
/// Gets the value of the nodes without xml doc
Expand Down