Site icon Flutter Packages | Pub dev Packages – Flutter Mobile App World

linter for dart code

Cool linter

This is a custom linter package for dart/flutter code. It can set linter for exclude some of words. This words you can set in analysis_options.yaml by example below

Usage

1. Add dependency to pubspec.yaml

dev_dependencies:
  cool_linter: ^1.2.0 # last version of plugin

2. Add configuration to analysis_options.yaml

analyzer:
  plugins:
    - cool_linter

cool_linter:
  extended_rules:
    - always_specify_stream_subscription
    - prefer_trailing_comma
  always_specify_types:
    - typed_literal
    - declared_identifier
    - set_or_map_literal
    - simple_formal_parameter
    - type_name
    - variable_declaration_list
  regexp_exclude:
    -
      pattern: Colors
      hint: Use colors from design system instead!
      severity: WARNING
    -
      pattern: Test123{1}
      severity: ERROR
  exclude_folders:
    - test/**
    - lib/ku/**
  1. always_specify_types linter:

always_specify_types This rule is like dart core linter rule, but you can choose which of this subrules want to use:

Also you can choose exclude folders for this rule. See exclude_folders

  1. regexp_exclude linter:
'.dart_tool/**',
'.vscode/**',
'packages/**',
'ios/**',
'macos/**',
'web/**',
'linux/**',
'windows/**',
'go/**',
  1. extended_rules. always_specify_stream_subscription linter:

Always use StreamSubscription for Stream.listen();

CORRECT:

final Stream<String> stream2 = Stream<String>.value('value');
final StreamSubscription<String> sub = stream2.listen((_) {}); // OK

WARNING:

final Stream<String> stream1 = Stream<String>.value('value');
stream1.listen((String ttt) {}); // LINT

Attention!!!

You must restart your IDE for starting plugin

3. CLI

You can use linter as command line tool dart bin/cool_linter_cli.dart analyze -tsc -d test/fix/result --regexp_path test/regexp/regexp_settings_cli.yaml

or using dart pub run

Available options:

Also you must specify --regexp_path parameter if you want to regexp analyzer. Example: --regexp_path <path to regexp settings .yaml file>

regexp_exclude:
  -
    pattern: Colors
    hint: Use colors from design system instead!
    severity: WARNING
  -
    pattern: \sTestClass\s
    hint: Dont use TestClass
    severity: ERROR
  -
    pattern: \sTestClass2\s
    hint: Dont use TestClass2
    severity: INFO

4. Result

Example of analysis_options.yaml

analyzer:
  plugins:
    - cool_linter

cool_linter:
  extended_rules:
    - always_specify_stream_subscription
    - prefer_trailing_comma
  always_specify_types:
    - typed_literal
    - declared_identifier
    - set_or_map_literal
    - simple_formal_parameter
    - type_name
    - variable_declaration_list
  regexp_exclude:
    -
      pattern: Colors
      hint: Use colors from design system instead!
      severity: WARNING
    -
      pattern: Test123{1}
      severity: ERROR
  exclude_folders:
    - test/**
    - lib/ku/**

Contribute to linter source code at GitHub

https://github.com/jWinterDay/cool_linter
Exit mobile version