more_visibility 0.1.3
more_visibility: ^0.1.3 copied to clipboard
Custom lint and builder bringing directory-scoped visibility via @mprotected and @mdefault annotations.
more_visibility #
Custom lint + post-process builder that brings Java-style “protected” and “default” visibility to Dart projects using the annotations @mprotected and @mdefault.
What it does #
@mprotected: declaration/file is usable from the same directory and any subdirectories.@mdefault: declaration/file is usable only from the same directory.- Lint powered by
custom_lintcatches violations at analysis time. - Post-process builder automatically stamps generated files (Riverpod, Freezed, etc.) with a file-level annotation so they obey the same visibility rules.
Quick start #
- Add dependencies:
dependencies:
more_visibility_annotation: ^0.1.0
dev_dependencies:
more_visibility: ^0.1.0 # custom_lint plugin
custom_lint: any
build_runner: any # if you want the auto-annotation builder
- Enable the plugin in
analysis_options.yaml:
analyzer:
plugins:
- custom_lint
- Annotate code:
import 'package:more_visibility_annotation/more_visibility_annotation.dart';
@mprotected // usable from lib/ and lib/**
final shared = 1;
@mdefault // usable only inside this directory
final local = 2;
- Run the lints:
dart run custom_lint
File-level annotations #
Annotate an entire file to give every declaration the same visibility:
import 'package:more_visibility_annotation/more_visibility_annotation.dart';
@mprotected
library feature_auth;
// Everything in this file inherits the @mprotected rule.
Auto-annotating generated files #
Add the post-process builder so generated files copy the declaration-level visibility from their source part:
# build.yaml in your app/repo
targets:
$default:
builders:
more_visibility:auto_annotate:
enabled: true
post_process_builders:
more_visibility:auto_annotate:
options:
visibility: mprotected # fallback if source has no annotated declaration
Then run:
dart run build_runner build --delete-conflicting-outputs
The builder inserts @mprotected (or @mdefault) at the top of matching generated files (*.g.dart, *.freezed.dart, *.riverpod.dart) unless they are already annotated.
The builder copies the first declaration-level @mprotected/@mdefault from the source part file into the generated file (after part of), so generated declarations share the same visibility. File-level annotations are not copied because parts share library metadata automatically.
Example project #
See example/ for a minimal project showing allowed/blocked usages and how the lint reports violations.
Testing #
Run the package tests, which include an end-to-end lint invocation and builder coverage:
dart test
More info #
docs/usage.md: setup steps and idiomsdocs/visibility_rules.md: rule details and edge casesdocs/auto_annotation.md: builder options and integration notes