Elevate Your Dart Code with Custom Lints: Beginner’s Guide

Elevate Your Dart Code with Custom Lints: Beginner’s Guide

Hey Dart Developers,

Have you ever wondered how lint was created? 🤔 If not, Let’s checkout it.

Before we dig-in, let's understand what are lints?

What are Lints?
Lint, or a linter, is a static code analysis tool used to flag programming errors, bugs & stylistic errors.

If you have ever seen any yellow line under your code that asks you to fix it, then it might be a chance that It is a Lint Warning for the code you wrote.

In every project that you are working on, there are some pre-defined best practices to follow while coding. But, at a point in time you miss those best practices & missing these will eventually make your code more complex/messy. This can be the primary reason for code complexity in any project.

You can solve this problem with Custom Lint Package which is developed by Invertase.

What is custom_lint?

A powerful tool for building custom lint rules that allow package authors as well as Flutter and Dart developers to go beyond the standard lint rules. Custom Lint allows you to define your own rules that you can follow in your project/package.

What is the need for Custom Lint Rule?

More Custom Rules bring consistency to code, makes it more maintainable & improve the code quality.

Some key features make the custom_lint Package more beneficial than other packages.

  • Supports hot-reload/hot-restart: Updating the source code of a linter plugin will dynamically restart it, without having to restart your IDE/analyzer server.

  • Debugger support. Inspect your lints using the Dart debugger and place breakpoints.

  • Built-in testing mechanism using // expect_lint

Let’s Get Started

First, you need to create a dart package in your project for custom lints.

To create a plugin class, you need to extend PluginBase class & pass your Custom Lints in getLintRules method. To add this you need a follow these steps:

  • Create a class for your custom lint which will extend DartLintRule class & overrides run method.

  • To provide your lint name & problem message you need to define a const variable of LintCode & pass it to the super constructor of DartLintRule class.

This is the basic example of a custom Lint class:

Here are some new terminologies that you must know to understand the code.

  • DartLintRule — A base class for emitting warnings/errors/info inside Dart files.

  • DartFix — A base class for defining quick fixes inside Dart files.

  • CustomLintResolver — This class helps us to interact with files & point out lints in them.

  • Analysis Error — It will store error details that we report from our Lint Class

  • ErrorReporter — It reports the Analysis Error to Error Listener & shows up the error in Dart Analysis Tab

  • CustomLintContext — This class will be shared with Dart Lints & Quick Fixes in Project. You can access the Lint Search & Report-related data from it.

Creating a Quick Fix for Your Custom Lint

Create a custom class, extend DartFix Class & override run method.

You can access the Lint Data using context . You can use the object of ChangeBuilder Class to show up Quick Fix Message in User’s IDE.

To replace the dart code you have to use, addDartFileEditmethod from ChangeBuilder & use the builder to access some pre-defined methods for code replacement.

The above snippets hold the basic code for creating a Custom Lint & a Quick Fix for it. If you want to know more about Custom Lint, I have created an Example for Custom Lint.

In the example, I have tried creating a custom lint that compares the dart fileName & class Name defined inside it. Doing this will make your class Name & fileName the same, so that it can be identified easily & makes the code consistent.

You can check out the code from my GitHub.

I will be publishing my next article in which I will cover some of the advanced topics & examples of Custom Lint. Stay tuned for the update.

If I got something wrong? Let me know in the comments. I would love to improve. 🤘🏻

Clap 👏 If this article helps you learning something new 🙌🏻

Let’s connect on Twitter / LinkedIn 🤝