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

Port FxCop Code Metrics rules #1740

Merged
merged 3 commits into from
Jul 20, 2018
Merged

Port FxCop Code Metrics rules #1740

merged 3 commits into from
Jul 20, 2018

Conversation

mavasani
Copy link
Contributor

@mavasani mavasani commented Jul 19, 2018

  1. CA1501: Avoid excessive inheritance
  2. CA1502: Avoid excessive complexity
  3. CA1505: Avoid unmaintainable code
  4. CA1506: Avoid excessive class coupling

Fixes #433
Fixes #434
Fixes #436
Fixes #437

Customer requests:

  1. https://developercommunity.visualstudio.com/content/problem/142328/code-metrics-powertool-missing-for-vs2017.html
  2. https://visualstudio.uservoice.com/forums/121579-visual-studio-ide/suggestions/33459643--netcore-code-metrics

Future work items (I will file issues for these soon#1741):

  1. Refactor the core code metrics computation into a separate library.
  2. A command line tool, such as Metrics.exe, that uses the above library and dumps out the metrics tree onto console or a log file (Metrics.exe shipped prior to VS2017).
  3. Share code with VS implementation of Code Metrics - currently we have a clone of the code that computes the metrics. Once we start inserting packages from this repo to VS repo, we can delete the clone and reference that package (which will need to happen when we move CodeStyle rules to NuGet packages).

1. [CA1501](https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1501-avoid-excessive-inheritance): Avoid excessive inheritance
2. [CA1502](https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1502-avoid-excessive-complexity): Avoid excessive complexity
3. [CA1505](https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1505-avoid-unmaintainable-code): Avoid unmaintainable code
4. [CA1506](https://docs.microsoft.com/en-us/visualstudio/code-quality/ca1506-avoid-excessive-class-coupling): Avoid excessive class coupling

Fixes dotnet#433
Fixes dotnet#434
Fixes dotnet#436
Fixes dotnet#437

Future work items (I will file issues for these soon):
1. Refactor the core code metrics computation into a separate library.
2 A command line tool, such as Metrics.exe, that uses the above library and dumps out the metrics tree onto console or a log file (Metrics.exe shipped prior to VS2017).
3. Share code with VS implementation of Code Metrics - currently we have a clone of the code that computes the metrics. Once we start inserting packages from this repo to VS repo, we can delete the clone and reference that package (which will need to happen when we move CodeStyle rules to NuGet packages).
@mavasani mavasani requested a review from a team July 19, 2018 20:49
@mavasani mavasani mentioned this pull request Jul 19, 2018
3 tasks
@mavasani
Copy link
Contributor Author

Tagging @heejaechang as the core code metrics computation code/tests are a clone from the VS side pull request. #1741 tracks refactoring/packaging this further.

@heejaechang
Copy link
Contributor

is analyzer only new code and all others are just fork from vs side?

@mavasani
Copy link
Contributor Author

is analyzer only new code and all others are just fork from vs side?

Yes, analyzer and analyzer tests are the only new piece of code.

/// CA1501: 10
/// See CA1508 unit tests for more examples.
/// </summary>
private const string CodeMetricsConfigurationFile = "CodeMetricsConfig.txt";
Copy link
Contributor

Choose a reason for hiding this comment

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

is this existing format? or new? if it is new, can we use new editorconfig support?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Talked offline, the editorconfig options passed down to the compiler/analyzers is still not checked in, and will not work against 2.6.1 (which the current branch targets)

{
compilationContext.ReportDiagnostic(diagnostic);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

is it intentionally keep going? or forgot to return?>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good idea, let me change analyzer to bail out on configuration errors.

}

// Compute code metrics.
var computeTask = Task.Run(() => CodeAnalysisMetricData.ComputeAsync(compilationContext.Compilation, compilationContext.CancellationToken));
Copy link
Contributor

Choose a reason for hiding this comment

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

is there a reason who run it on Task.Run and then wait on that? rather than directly wait on task returned by ComputAsync?

private static bool TryParseCodeMetricsConfigurationFile(
AdditionalText additionalText,
CancellationToken cancellationToken,
out ImmutableDictionary<string, List<(SymbolKind?, uint)>> ruleIdToThresholdMap,
Copy link
Contributor

Choose a reason for hiding this comment

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

reason it is List rather than dictionary? I guess it is expected to be small most of time?

Copy link
Contributor

Choose a reason for hiding this comment

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

IReadOnlyList?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, only one rule realistically needs separate values for Types and Methods. I can change it to IReadOnlyList.

@mavasani
Copy link
Contributor Author

@dotnet-bot retest prtest/Windows/NT/Debug please

1 similar comment
@mavasani
Copy link
Contributor Author

@dotnet-bot retest prtest/Windows/NT/Debug please


";

var expectedMetricsText = @"
Copy link
Contributor

Choose a reason for hiding this comment

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

are these new test or ported from VS side?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ported from VS side. Hopefully we will delete from VS side soon.

@@ -0,0 +1,103 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Copy link
Contributor

Choose a reason for hiding this comment

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

is this right copyright header?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used the same header as other files in the repo, for example https://github.com/dotnet/roslyn-analyzers/blob/master/src/Utilities/DiagnosticHelpers.cs#L1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants