Giter Site home page Giter Site logo

dart-code-checker / dart-code-metrics Goto Github PK

View Code? Open in Web Editor NEW
858.0 12.0 254.0 26.48 MB

Software analytics tool that helps developers analyse and improve software quality.

Home Page: https://dcm.dev

License: Other

Dart 99.16% CSS 0.84%
dart dartlang codeclimate analysis analyzer cli tool flutter antipatterns analyzer-plugin

dart-code-metrics's Introduction

DCM logo

Dart Code Metrics

This package has been discontinued and is no longer maintained.

To continue using Dart Code Metrics, visit our website to purchase a license https://dcm.dev/pricing/.

If you were a Dart Code Metrics contributor, you can receive a special license. Feel free to contact [email protected].

dart-code-metrics's People

Contributors

akaiser avatar andrewst avatar anrock avatar ascenio avatar aschyolokov avatar bigdaddys avatar creativecreatorormaybenot avatar csepanda avatar dependabot[bot] avatar desdaemon avatar dkrutskikh avatar furaiev avatar fzyzcjy avatar grafovdenis avatar hfabi avatar incendial avatar lsaudon avatar mariamelnik avatar nxtswitch avatar ookami-kb avatar orevial avatar rmortes avatar roman-petrov avatar san-smith avatar sinegovsky-ivan avatar tkreuder avatar valentinvignal avatar vlkonoshenko avatar xsahil03x avatar zmeggyesi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

dart-code-metrics's Issues

Allow to ignore metrics warning

It would be nice to have // ignore support for number-of-methods, number-of-arguments, and other metrics.

Sometimes, metrics provide false-positive warning for cases, when large amount of arguments & methods is necessary. Example of large amount of arguments is Angular DI factory providers, which serves particular technical role, but might require large amount of arguments, because those arguments should be injected into constructor.

Implement no-magic-number rule

Issue a warning when there is a number literal in expression, except -1/+1/0. Recommend to extract magic number to named constant.

Bad:

final foo = bar + 4; // no-magic-number warning here

Fix:

const baz = 4; // 4 is extracted as constant
final foo = bar + baz;  // No more warning

No warnings for exceptional literals:

final lastIndex = myList.length - 1;
final quotedContentStart = 'blah-blah "quoted" blah'.indexOf('"') + 1;
final isReservedId = (x) => x < 0;

Inspired by TSLint rule

Make MetricsAnalysisRecorder exception safe

MetricsAnalysisRecorder has unsafe API that can lead to runtime exceptions if startRecordFile() and endRecordFile() calls are unpaired.

This can be improved using Dispose pattern.

Suggested API changes:

  • Create two new interfaces
    • MetricsRecordsStore with two methods:
      • records with same signature as MetricsAnalysisRecorder.records
      • void recordFile(String filePath, String rootDirectory, void f(MetricsRecordsBuilder))
    • MetricsRecordsBuilder that should expose methods recordComponent, recordFunction, recordIssues with same signatures as corresponding methods in MetricsAnalysisRecorder.records.
  • In MetricsAnalysisRecorder create private copies of startRecordFile, endRecordFile, recordComponent, recordFunction, recordIssues and make public versions call private versions
  • Make MetricsAnalysisRecorder implement both of this interfaces
    • recordFile should implement Dispose pattern - call private _startRecordFile, then pass this to supplied lambda and execute _endRecordFile at the end.
        void recordFile(String filePath, String rootDirectory, void f(MetricsRecordsBuilder) {
          _startRecordFile(filePath, rootDirectory);
          f(this);
          _endRecordFile()
        }
  • Mark startRecordFile, endRecordFile, recordComponent, recordFunction, recordIssues as deprecated in favour of MetricsRecordsStore.recordFile method

[New rule] avoid export from another package

Please describe what the rule should do:
avoid export from another package
If your rule is inspired by other please provide link to it:

What category of rule is this? (place an "X" next to just one item)

[ X] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

For example current package has name current_package.

export 'package:another_package/src/math.dart';  //<--- Bad
export 'package:current_package/src/some_lib_1.dart';
export 'package:current_package/src/some_lib_2.dart';

Are you willing to submit a pull request to implement this rule?
may be

[New rule] avoid void arrow function

For dart code is better to avoid use void arrow function, for several reasons:

  • after compilation return is added to such function:
void funcA() => a += 55;
function(){return $.a=$.a+55}
  • if in such function we add more action in git diff it looks better

Please describe what the rule should do:
it will ask not to use void arrow function

If your rule is inspired by other please provide link to it:

What category of rule is this? (place an "X" next to just one item)

[ ] Warns about a potential error (problem)
[X] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

 //bad
void funcA() => a += 55;

//good
void funcA() {
  a += 55;
}

Are you willing to submit a pull request to implement this rule?
yes

[Question] Line of codes are not equal to the file's line of code?

What do you want to discuss?
I'm trying to know why the line of code reported by the tool isn't exactly the line of code of the file itself. I assume this is intended but I just need to know the reason behind and how the tool does it technically.

image
Line of code reported by the tool: 18
Line of code inside the file: 37

[New rule] Potential null dereference

Please describe what the rule should do:
Check for potential null dereference in conditional expressions and blocks.

If your rule is inspired by other please provide link to it:
https://www.viva64.com/en/w/v6008/

What category of rule is this? (place an "X" next to just one item)

[X] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

if (obj != null || obj.foo()) { ... } // in obj.foo invocation obj could be null

if (obj == null && obj.foo()) { ... } // in obj.foo invocation obj is always null

if (obj == null) {
  ...
  var bar = obj.foo(); // in obj.foo invocation obj is always null
  ...
}

Are you willing to submit a pull request to implement this rule?
Yes

Implement no-object-declaration

A rule that issues a warning when class member is declared with Object type. Must not issue a warning when function/method argument has type Object or when type argument is Object.

[Rule change] Add rules for static field/method in member ordering rule

Add rules for static field/method in member-ordering rule

What rule do you want to change?
member-ordering

How will the change be implemented? (New option, new default behavior, etc.)?
new option, just we will be able to make order for static methods/fields too

Please provide some example code that this change will affect:

class RescheduleUpdateComponent {
  @HostBinding('class')
  static const String hostClass = 'reschedule-update';

  final DateTimeFormatter _dateTimeFormatter;
 
  @HostBinding(‘class.active’)
  bool get isActive() => _viewState.isActive;
}

What does the rule currently do for this code?

  • member-ordering:
    order:
    - static_fields
    - private_fields
    - public_fields
    - angular_host_bindings
    in such configuration we can change order for static methods

What will the rule do after it's changed?
can make particular order for static field/method

Are you willing to submit a pull request to implement this change?
I

Improve no-magic-number rule

Some field cases with false-positivish warnings:

final someDay = DateTime(2006, 12, 1); // DateTime has no const constructor, so there is always a warning or each int should be defined as const separately
Future.delayed(const Duration(seconds: 5)); // Triggered despite const constructor.
...
const delay = Duration(seconds: 5));
Future.delayed(delay); // This is fine. Not sure if should be fixed
... Intl.message(..., example: const <String, int>{ 'Assigneed': 3 } // Warning

So far it looks like rule should be altered to not trigger if magic number is inside const constructors and number literals in other const literals.
DateTime should be made an exception, since it doesn't have a const constructor and putting every argument in separate const declaration is inconvenient.

[New rule] No equal arguments

Please describe what the rule should do:
Check for equal arguments passed to functions or method invocations.

If your rule is inspired by other please provide link to it:

What category of rule is this? (place an "X" next to just one item)

[X] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

class User {
  final String firstName;
  final String lastName;

  const User(this.firstName, this.lastName);
}

User createUser(String firstName, String lastName)  {
  return User(
    firstName,
    firstName, // <-- 
  );
}

void getUserData(User user) {
  String getFullName(String firstName, String lastName) {
    return firstName + ' ' + lastName;
  }

  final fullName = getFullName(
    user.firstName,
    user.firstName, // <-- 
  );
}

Are you willing to submit a pull request to implement this rule?
Yes

Simplify usage as library

Right now it's neccessary to create a bunch of internal classes and orchestrate them to create a report.

There should be a simpler way to perform common scenarios, preferably with a single function.

Exit code for CI

Hello,

It could be very useful to add an option like --set-exit-on-alarm and --set-exit-on-warning to allow this library to make a pipeline fail.

I'm talking about GitLab here, but I think that exit code are used in other CI tools to made them fail.

[Question] "--ignore-files" does not work?

What do you want to discuss?
When I use the command line tool, "--ignore-files" does not seem to work. And only the absolute path can be used, the path "/**.dart" cannot be found.
1.
image
2.
image

[New rule] Redundant async

Please describe what the rule should do:
Checks for redundant async.
Cases where async is useful include:

  • You are using await.
  • You are returning an error asynchronously. async and then throw is shorter than return Future.error(...).
  • You are returning a value and you want it implicitly wrapped in a future. async is shorter than Future.value(...).

What category of rule is this? (place an "X" next to just one item)

[X] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

Bad:

Future<void> afterTwoThings(Future<void> first, Future<void> second) async {
  return Future.wait([first, second]);
}

Good:

Future<void> usesAwait(Future<String> later) async {
  print(await later);
}

Future<void> asyncError() async {
  throw 'Error!';
}

Future<void> asyncValue() async => 'value';

Are you willing to submit a pull request to implement this rule?
Yes

Improve member-ordering

We should add ability to sort members alphabetically inside one group.
Also should add support for Angular view children/content children annotations.

[New rule] Prefer trailing comma

Please describe what the rule should do:
Should check for trailing comma for arguments, parameters, enum values and collections.
By default should warn in cases when items aren't on a single line.
Additionally should accept items count breakpoint config. For example, if breakpoint count is equal 2, then the rule should warn when items count is greater than or equal to the count or items aren't on a single line.

What category of rule is this? (place an "X" next to just one item)

[ ] Warns about a potential error (problem)
[X] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

Without config:
Bad:

void firstFunction(String arg1, String arg2,
  String arg3) {
  return;
}

Good:

void firstFunction(
  String arg1,
  String arg2,
  String arg3,
) {
  return;
}

With given config:

- break_on: 2

Bad:

void firstFunction(String arg1, String arg2,
  String arg3) {
  return;
}

void secondFunction(String arg1, String arg2, String arg3) {
  return;
}

Good:

void firstFunction(
  String arg1,
  String arg2,
  String arg3,
) {
  return;
}

void secondFunction(
  String arg1,
  String arg2,
  String arg3,
) {
  return;
}

enum FirstEnum { firstItem }

const instance = FirstClass(
  0,
  0,
  0,
);

Are you willing to submit a pull request to implement this rule?
Yes

[New rule] Avoid return FutureOr

Please describe what the rule should do:
Warns when return type is FutureOr<T>, because end users need to check whether get back a T or a Future before they can do anything useful.

What category of rule is this? (place an "X" next to just one item)

[X] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

Bad:

FutureOr<int> triple(FutureOr<int> value) {
  if (value is int) return value * 3;
  return (value as Future<int>).then((v) => v * 3);
}

Good:

Future<int> triple(FutureOr<int> value) async => (await value) * 3;

Are you willing to submit a pull request to implement this rule?
Yes

[New rule] validate settings

Please describe what the rule should do:

Validate settings block in analysis_options.yaml

If your rule is inspired by other please provide link to it:

What category of rule is this? (place an "X" next to just one item)

[ ] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[X] Other (please specify:) Warns about problems in plugin setting block

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

Are you willing to submit a pull request to implement this rule?

[New rule] Then statement is equal to the else statement

Please describe what the rule should do:
Check for equal then and else statements.

If your rule is inspired by other please provide link to it:
https://www.viva64.com/en/w/v6004/

What category of rule is this? (place an "X" next to just one item)

[X] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

if (condition) {
  result = value;
} else {
  result = value;
}

result = condition ? value : value;

Are you willing to submit a pull request to implement this rule?
Yes

Promoting

It is an awesome tool
I didn't know it is there before so I wonder if you could promote it on the dart linter team or make pull requests to merge it to the main tool?
dart-lang/linter#2302

[New rule] Prefer future void

Please describe what the rule should do:
Warns when return type is Future or Future<Null> instead of a Future<void>.

What category of rule is this? (place an "X" next to just one item)

[X] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

Bad:

Future schedule(void Function callback) => _scheduler.add(callback);

Future<Null> (void Function callback) => _scheduler.add(callback);

Good:

Future<void> (void Function callback) => _scheduler.add(callback);

Are you willing to submit a pull request to implement this rule?
Yes

[New rule] avoid named from keywords list

Please describe what the rule should do:
avoid named from keywords list https://dart.dev/guides/language/language-tour#keywords
If your rule is inspired by other please provide link to it:

What category of rule is this? (place an "X" next to just one item)

[ X] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

void main() {

  const abstract = 123;
  const import = 123;
  const as = 123;
  const export = 123;
  const interface = 123;
  const sync = 123;
  const async = 123;
  const await = 123;
  const extension = 123;
  const library = 123;
  const external = 123;
  const mixin = 123;
  const factory = 123;
  const typedef = 123;
  const on = 123;
  const operator = 123;
  const part = 123;
  const covariant = 123;
  const Function = 123;
  const get = 123;
  const yield = 123;
  const deferred = 123;
  const hide = 123;
  const set = 123;
  const show = 123;
  const dynamic = 123;
  const implements = 123;
  const static = 123;
}

Are you willing to submit a pull request to implement this rule?
may be

Implement number-of-methods metric

The number of methods is the total number of methods or functions in a class. High number of methods indicates a high complexity of the class.

[New anti-pattern] Large Class

Please describe what the rule should do:

Detect a classes contains many fields/methods/lines of code.

If your rule is inspired by other please provide link to it:

What category of rule is this? (place an "X" next to just one item)

[ ] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[x] Other (please specify:) anti-pattern

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):

Are you willing to submit a pull request to implement this rule?

Implement unnecessary-type-assertion

Issue a warning for unnecessary type assertions.

Bad:

final myList = <int>[1,2,3];
...
myList.whereType<int> ... // myList is known to contain ints, therefore type assertion is redundant
...
if (myList is List) ... // myList is known to be a List, therefore type check is redundant

Inspired by TSLint rule

[New rule] Component annotation arguments ordering

Please describe what the rule should do:
Enforces ordering for @Component Angular annotation arguments. Order should be configurable.
Available options:

  • selector
  • template(includes templateUrl and template)
  • directives (includes directives and directiveTypes)
  • pipes
  • encapsulation
  • styles (includes styles and styleUrls)
  • exports (includes exports and exportAs)
  • changeDetection
  • visibility
  • providers (includes providers and viewProviders)

What category of rule is this? (place an "X" next to just one item)

[ ] Warns about a potential error (problem)
[ ] Suggests an alternate way of doing something (suggestion)
[x] Other (please specify: enforces specific order)

Provide 2-3 code examples that this rule will warn about (it will be better if you can provide both good and bad examples):
With given config:

- selector
- providers
- change-detection

Bad:

@Component(
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: 'component-selector',
)

Good:

@Component(
  selector: 'component-selector',
  changeDetection: ChangeDetectionStrategy.OnPush,
)

Are you willing to submit a pull request to implement this rule?
Yes

About analyzer_plugin

Hello!

This is more of a question on analyzer_plugin and not directly related to this package – but the resources on analyzer_plugin are scarce.
I would love if you were able to answer this question for me:

How to debug custom analyzer plugins?

I've forked this repository and added both a print and File(...).writeAsString in tools/analyzer_plugin/bin/plugin.dart, but neither of them do anything (while the warnings do appear).

Ironically, I even removed the call to start, but still get the warnings implemented by this package to appear – which is illogical.

It's as if the analyzer is using the version of dart_code_metrics on pub rather than the local one – when I've changed the dependency on dart_code_metrics in tools/analyzer_plugin/pubspec.yaml to use the local version instead of the remote version.

I would appreciate some guidance in that area. Thanks in advance!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.