more_visibility 0.1.4
more_visibility: ^0.1.4 copied to clipboard
Analysis server plugin and builder bringing directory-scoped visibility via @mprotected and @mdefault annotations.
more_visibility #
Analysis server plugin + 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
analysis_server_plugincatches violations at analysis time in IDEs anddart analyze. - Post-process builder automatically stamps generated files (Riverpod, Freezed, etc.) with a file-level annotation so they obey the same visibility rules.
Requirements #
- Dart SDK 3.10.0 or later (Flutter SDK 3.38.0 or later)
Quick start #
- Add dependencies:
dependencies:
more_visibility_annotation: ^0.1.0
dev_dependencies:
more_visibility: ^0.1.3 # analysis server plugin
build_runner: any # if you want the auto-annotation builder
- Enable the plugin in
analysis_options.yaml:
plugins:
more_visibility: ^0.1.3
Or for local development:
plugins:
more_visibility:
path: path/to/more_visibility
- 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 analyze
Or your IDE will automatically show violations as you code.
Configuring severity levels #
By default, visibility violations are reported as errors. You can change the severity level in your analysis_options.yaml:
analyzer:
errors:
# Change all visibility violations to warnings
more_visibility_protected: warning
more_visibility_module_default: warning
# Or set to info (won't fail CI)
more_visibility_protected: info
more_visibility_module_default: info
# Or ignore completely
more_visibility_protected: ignore
more_visibility_module_default: ignore
Available severity levels:
error(default) - Causes analysis to failwarning- Shown as warning, doesn't fail analysis by defaultinfo- Informational onlyignore- Suppresses the diagnostic
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