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"

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.