Code Monkey home page Code Monkey logo

subpackage_lint's Introduction

subpackage_lint

pub package

ko-fi

A set of lint rules to enforce best practices for subpackages.

Philosophy

Subpackages or "lightweight packages" are a way of splitting up your code into separate modules without introducing the overhead of adding extra build tools or having to manage code in several places.

The idea is to group of one feature together and treat it like it would be a separate library, but without moving to another project and maintaining the flexibility of overriding the rule if necessary.

/my_subpackage
├── my_subpackage.dart
└── /src
    ├── /a_folder
    │   └── another_private_file.dart
    ├── a_private_file.dart
    └── some_public_file.dart

Within the code, for example in the /lib directory of your Dart app, you create a new directory which will contain everything of your subpackage. Let's name this my_subpackage in this example. Top-level within that directory you'll create a library file that contains all the exports of this package. Exports are the files and classes you want to make publicly available. This library file should have the same name as your directory, so you'll end up with the following naming convention: /my_subpackage/my_subpackage.dart

Considering the example above, this could be the content of the library file:

// my_subpackage/my_subpackage.dart


/// Here's a description of what this subpackage does
/// This comment will be visible in your IDE when hovering over the import of this library file.
library my_subpackage;

export 'src/some_public_file.dart'; // only export the public files or classes

Imports

The golden rule is to avoid /src/ anywhere in your imports.

Within the subpackage

Use relative imports within the src directory of the subpackage. If you see src somewhere in a relative import, it means you went too far.

// Good
import 'a_private_file.dart';

// Good
import '../some_public_file.dart';

// Bad
import '../src/a_private_file.dart';

// Bad
import 'package:my_app/my_subpackage/my_subpackage.dart';

// Bad
import 'package:my_app/my_subpackage/src/a_private_file.dart';

// Bad
import '../my_subpackage.dart';

Importing the subpackage

When using this subpackage somewhere else in your code, you should import the library file:

// Good: absolute import of library file
import 'package:my_app/my_subpackage/my_subpackage.dart';

// Bad: absolute import of src file
import 'package:my_app/my_subpackage/src/some_public_file.dart'

// Bad: relative import of src file
import '../../my_subpackage/src/some_public_file.dart';

// Not recommended: relative import of library file
import '../../my_subpackage/my_subpackage.dart';

This way of organizing your imports is not enforced by the framework and is a guideline that the developers should agree to. Luckily, there are these lint rules to help you.

Getting started

Add the following to your pubspec.yaml file:

dev_dependencies:
  custom_lint:
  subpackage_lint:

Add the following to the analysis_options.yaml file:

analyzer:
  plugins:
    - custom_lint

Exclude files

To exclude files from being linted, add the following to the analysis_options.yaml file:

custom_lint:
  rules:
    - avoid_src_import_from_other_subpackage:
      exclude:
        - "lib/my/excluded/file.dart"
        - "**_test.dart"
        - "**.g.dart"
    - avoid_src_import_from_same_package:
      exclude:
        - "lib/my/excluded/directory/**"
        - "**_test.dart"
    - avoid_package_import_for_same_package:
      exclude:
        - "**_test.dart"

subpackage_lint's People

Contributors

dumazy avatar

Stargazers

Pau Picas avatar  avatar Andres Araujo avatar Seiya Kokushi avatar Sylvain Autran avatar Alex Isaienko avatar

Watchers

 avatar

subpackage_lint's Issues

Allow excluding for specific directories

When slowly migrating a codebase that uses a different structure to subpackages, it might be useful to exclude parts of the existing source code to keep a clear overview.

We should be able to exclude directories like /lib/my_directory/** or something similar.

Thank you! Please share an option for donation

Thank you, mate! This is the most important set of lints I have in some of my projects now.

This library enforces exactly the import rules I wanted to be enforced by the built-in linter out-of-the-box ever since I started using Dart.

Now I can finally ditch the cumbersome monorepo structure for some of my projects and simply rely on the linter for enforcing the import rules I consider sane for a single-package application.

Writing this to let you know that your work is appreciated and to give you some motivation for continuing to support this lib.

I'd love to buy you a coffee/beer/cola/whatever, so please let me (and others) know how I can donate to you.

Add includes to configuration

Now that it's possible to exclude certain directories or files, we should make it possible to include only a smaller group of files.

The lint should first check if there are any includes, if there are, only continue with the ones available.
After that, this subset will be filtered out more by looking at the excludes.

The following example might make this clear:

includes:
  - lib/foo/**
excludes
  - **.g.dart

This should only use the lint rule in files under lib/foo but exclude the .g.dart files in there. The lint should not run for files in lib/bar, or any .g.dart files in the project.

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.