Giter Site home page Giter Site logo

dart-custom-analyzer-plugin's Introduction

Dart Custom Analyzer Plugin

A simple rules analysis logic library for a dart analyzer plugin to remotely reference. Heavily inspired by (and often borrowed from) dart code metrics https://github.com/dart-code-checker/dart-code-metrics.

Usage (plugin or library)

Whether this repo is used as a plugin or a library, files that it analyzes must be .dart files within a dart package containing an analysis_options.yaml file.

Include the following entry in the analysis_options.yaml file(s) with your specified set of rules. Rules may be simple strings, or maps with rule options.

target packages' analysis_options.yaml
custom_linter:
  rules:
    - <rule id>
    - <rule id>:
        enabled: false
        exclude:
         - /test/**
         - /lib/generated**
        <rule specific option>: <val>
    - <rule id>
    - ...

Each rule may have its own options, but all rules may include the following optional keys:

  • enabled - controls whether the rule is run or not.
  • exclude - list if file paths to exclude from analysis.
    • ex: /test/**

Library

Simply call the analyze function. Returned will be a list of AnalyzerErrorFixes identical to those provided to the analyzer server when used as a plugin.

import 'package:dart_custom_analyzer_plugin/analyzer.dart';

void main(List<String> arguments) async {
  final errors = await analyze(<List of directory and/or file paths>);
  return;
}

VS Code Plugin

Create a dart analyzer plugin loader package, and a plugin package. The plugin package itself will be subpackage of the loader package located in /tools/analyzer_plugin/ Note: match the exact names of the directories and plugin.dart file.

The plugin loader package's directory structure:
<Plugin Loader Package Directory>
    tools/
        analyzer_plugin/
            bin/
                plugin.dart
            pubspec.yaml
    pubspec.yaml

Reference this project in the plugin subpackage's pubspec.yaml Note: the dependency path must either be remote or an absolute (not relative) path.

/tools/analyzer_plugin/pubspec.yaml
...
dependencies:
  dart_custom_analyzer_plugin:
    git: https://github.com/aikalant/dart-custom-analyzer-plugin
    #path: <Absolute path to cloned directory>

Inside of plugin.dart, call the start function.

/tools/analyzer_plugin/bin/plugin.dart
import 'dart:isolate';

import 'package:dart_custom_analyzer_plugin/analyzer_plugin.dart';

void main(List<String> args, SendPort sendPort) {
  start(args, sendPort);
}

Ensure that the plugin loader is a dev dependency for any of the target packages it will be analyzing

Target packages' /pubspec.yaml
dev_dependencies:
  <Plugin Loader package name (parent package, not subpackage)>:
    path: <path>

Finally, add the plugin loader to the analysis_options.yaml of the target packages

Target packages' /analysis_options.yaml
analyzer:
  plugins:
    - <Plugin Loader package name (parent package, not subpackage)>

Don't forget to restart the analyzer server (ctrl + shift + p)

If you have trouble, try clearing the analysis server plugin cache and then restarting the server:

  • ~/.dartServer/.plugin_manager/ for linux/mac
  • %LOCALAPPDATA%/.dartServer/.plugin_manager/ for windows

Sources

https://github.com/dart-code-checker/dart-code-metrics

dart-custom-analyzer-plugin's People

Contributors

aikalant avatar definitelyokay avatar kseino avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.