flutter_hooks_lint_plugin is a dart analyzer plugin for the flutter hooks, inspired by eslint-plugin-react-hooks.
Lint to detect missing, unnecessary keys in the useEffect
calling.
It finds build variables (class fields, local variables, any other build-related variables) in the HookWidget, then compares references of the build variables and specified keys in the useEffect
calling, and reports a lint error if there are any differences.
final variable1 = callSomething();
final variable2 = callSomething();
useEffect(() {
print(variable1);
}, [variable2]); // <= missing key 'variable1', unnecessary key 'variable2'
Lint to detect nested using of hooks which is one of the bad practices.
It reports a lint error if there are any using of hooks under control flow syntax (if
, for
, while
, ... ).
if (flag) {
final variable = useState('hello'); // <= avoid nested hooks
}
Add flutter_hooks_lint_plugin
dependency to your pubspec.yaml
:
dev_dependencies:
flutter_hooks_lint_plugin: ^0.6.1
Add flutter_hooks_lint_plugin
plugin directive to your analyzer_options.yaml
:
analyzer:
plugins:
- flutter_hooks_lint_plugin
Then, run flutter pub get
and restart your IDE/Editor.
You can customize plugin's behavior by the analysis_options.yaml
:
flutter_hooks_lint_plugin:
exhaustive_keys:
# hooks do not change over the state's lifecycle
constant_hooks:
# default values
- useRef
- useIsMounted
- useFocusNode
- useContext
# your custom hooks here
- useConstantValue
There are several ways to suppress lints:
- add
// ignore_for_file: exhaustive_keys, nested_hooks
to suppress lints in the entire file - add
// ignore: exhaustive_keys, nested_hooks
to suppress lints at the next or current line - add
// ignore_keys: foo, bar
to suppress lints for the specific keys at the next or current line
You can also use this plugin by CLI command:
$ dart pub run flutter_hooks_lint_plugin:flutter_hooks_lint analyze ./
$ flutter pub run flutter_hooks_lint_plugin:flutter_hooks_lint analyze ./
- support
Fix
(suggestion)
Welcome PRs!
You can develop locally by modifying plugin's dependency to absolute path in tools/analyzer_plugin/pubspec.yaml
:
dependencies:
flutter_hooks_lint_plugin:
path: /home/mjhd/flutter_hooks_lint_plugin # <= absolute path to the cloned directory