-
-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add wrap with observer assist #949
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
932f113
feat: add wrap with observer assist
amondnet 2ddd25c
feat: add wrap with observer assist
amondnet e69f8b8
feat: add wrap with observer assist
amondnet a6f30a3
feat: add wrap with observer assist
amondnet 0b555ec
feat: add wrap with observer assist
amondnet ebadfd0
fix: `ObservableSet` and `ObservableMap` should not notify all listen…
amondnet 41ddfd1
feat: add mobx_lint
amondnet fddf6db
feat: add wrap with observer assist
amondnet 27a6bd1
feat: add wrap with observer assist
amondnet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
## 1.0.0 | ||
|
||
- add wrap with observer assist by [@amondnet](https://github.com/amondnet) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 MobX | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
mobx_lint is a developer tool for users of mobx, designed to help stop common issues and simplify repetitive tasks. | ||
|
||
mobx_lint adds various warnings with quick fixes and refactoring options, such as: | ||
|
||
- Refactor wrap a widget with Observer | ||
|
||
## Installing mobx_lint | ||
|
||
mobx_lint is implemented using [custom_lint]. As such, it uses custom_lint's installation logic. | ||
Long story short: | ||
|
||
- Add both mobx_lint and custom_lint to your `pubspec.yaml`: | ||
```yaml | ||
dev_dependencies: | ||
custom_lint: | ||
mobx_lint: | ||
``` | ||
- Enable `custom_lint`'s plugin in your `analysis_options.yaml`: | ||
|
||
```yaml | ||
analyzer: | ||
plugins: | ||
- custom_lint | ||
``` | ||
|
||
## Running mobx_lint in the terminal/CI | ||
|
||
Custom lint rules created by mobx_lint may not show-up in `dart analyze`. | ||
To fix this, you can run a custom command line: `custom_lint`. | ||
|
||
Since your project should already have custom_lint installed | ||
(cf [installing mobx_lint](#installing-mobx_lint)), then you should be | ||
able to run: | ||
|
||
```sh | ||
dart run custom_lint | ||
``` | ||
|
||
Alternatively, you can globally install `custom_lint`: | ||
|
||
```sh | ||
# Install custom_lint for all projects | ||
dart pub global activate custom_lint | ||
# run custom_lint's command line in a project | ||
custom_lint | ||
``` | ||
|
||
## All assists | ||
|
||
### Wrap widgets with a `Observer` | ||
|
||
![Wrap with Consumer sample](resources/wrap_with_observer.gif) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# This file configures the static analysis results for your project (errors, | ||
# warnings, and lints). | ||
# | ||
# This enables the 'recommended' set of lints from `package:lints`. | ||
# This set helps identify many issues that may lead to problems when running | ||
# or consuming Dart code, and enforces writing Dart using a single, idiomatic | ||
# style and format. | ||
# | ||
# If you want a smaller set of lints you can change this to specify | ||
# 'package:lints/core.yaml'. These are just the most critical lints | ||
# (the recommended set includes the core lints). | ||
# The core lints are also what is used by pub.dev for scoring packages. | ||
|
||
include: package:lints/recommended.yaml | ||
|
||
# Uncomment the following section to specify additional rules. | ||
|
||
# linter: | ||
# rules: | ||
# - camel_case_types | ||
|
||
# analyzer: | ||
# exclude: | ||
# - path/to/excluded/files/** | ||
|
||
# For more information about the core and recommended set of lints, see | ||
# https://dart.dev/go/core-lints | ||
|
||
# For additional information about configuring this file, see | ||
# https://dart.dev/guides/language/analysis-options |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
library mobx_lint; | ||
|
||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
import 'src/assists/wrap_with_observer.dart'; | ||
|
||
PluginBase createPlugin() => _MobxPlugin(); | ||
|
||
class _MobxPlugin extends PluginBase { | ||
@override | ||
List<LintRule> getLintRules(CustomLintConfigs configs) => []; | ||
|
||
@override | ||
List<Assist> getAssists() => [ | ||
WrapWithObserver(), | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'package:analyzer/source/source_range.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
import 'package:mobx_lint/src/mobx_custom_lint.dart'; | ||
|
||
import '../mobx_types.dart'; | ||
|
||
/// Right above "wrap in builder" | ||
const wrapPriority = 28; | ||
|
||
class WrapWithObserver extends MobxAssist { | ||
WrapWithObserver(); | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ChangeReporter reporter, | ||
CustomLintContext context, | ||
SourceRange target, | ||
) { | ||
context.registry.addInstanceCreationExpression((node) { | ||
// Select from "new" to the opening bracket | ||
if (!target.intersects(node.constructorName.sourceRange)) return; | ||
|
||
final createdType = node.constructorName.type.type; | ||
|
||
if (createdType == null || | ||
!widgetType.isAssignableFromType(createdType)) { | ||
return; | ||
} | ||
|
||
final changeBuilder = reporter.createChangeBuilder( | ||
message: 'Wrap with Observer', | ||
priority: wrapPriority, | ||
); | ||
|
||
changeBuilder.addDartFileEdit((builder) { | ||
builder.addSimpleInsertion( | ||
node.offset, | ||
'Observer(builder: (context) { return ', | ||
); | ||
builder.addSimpleInsertion(node.end, '; },)'); | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:analyzer/source/source_range.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
abstract class MobxAssist extends DartAssist { | ||
@override | ||
Future<void> startUp( | ||
CustomLintResolver resolver, | ||
CustomLintContext context, | ||
SourceRange target, | ||
) async { | ||
await super.startUp(resolver, context, target); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
/// [TypeChecker] from `Widget` from Flutter | ||
const widgetType = TypeChecker.fromName('Widget', packageName: 'flutter'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
name: mobx_lint | ||
amondnet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
description: mobx_lint is a developer tool for users of mobx, designed to help stop common issues and simplify repetitive tasks. | ||
version: 1.0.0 | ||
homepage: https://mobx.netlify.app/ | ||
repository: https://github.com/mobxjs/mobx.dart | ||
issue_tracker: https://github.com/mobxjs/mobx.dart/issues | ||
|
||
environment: | ||
sdk: ">=2.16.0 <3.0.0" | ||
|
||
dependencies: | ||
analyzer: ">=6.0.0 <7.0.0" | ||
analyzer_plugin: ^0.11.2 | ||
collection: ^1.16.0 | ||
custom_lint_builder: ^0.5.2 | ||
meta: ^1.7.0 | ||
path: ^1.8.1 | ||
mobx: ^2.2.0 | ||
dev_dependencies: | ||
lints: ^2.0.0 | ||
test: ^1.21.0 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: mobx_lint_flutter_test | ||
publish_to: none | ||
|
||
environment: | ||
sdk: ">=3.0.0 <4.0.0" | ||
flutter: ">=3.0.0" | ||
|
||
dependencies: | ||
flutter: | ||
sdk: flutter | ||
mobx: | ||
flutter_mobx: | ||
|
||
|
||
dev_dependencies: | ||
mobx_lint: | ||
path: ../mobx_lint | ||
test: ^1.15.0 | ||
|
||
dependency_overrides: | ||
mobx: | ||
path: ../mobx | ||
flutter_mobx: | ||
path: ../flutter_mobx | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import 'assists/wrap_widget/wrap_widget_test.dart' as wrap_widget_test; | ||
|
||
void main() { | ||
wrap_widget_test.main(); | ||
} |
14 changes: 14 additions & 0 deletions
14
mobx_lint_flutter_test/test/assists/wrap_widget/wrap_widget.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import 'package:flutter/material.dart'; | ||
|
||
class MyWidget extends StatelessWidget { | ||
const MyWidget({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
Map(); | ||
|
||
return Scaffold( | ||
body: Container(), | ||
); | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
mobx_lint_flutter_test/test/assists/wrap_widget/wrap_widget_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:analyzer/dart/analysis/results.dart'; | ||
import 'package:analyzer/dart/analysis/utilities.dart'; | ||
import 'package:analyzer/source/source_range.dart'; | ||
import 'package:mobx_lint/src/assists/wrap_with_observer.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
import '../../golden.dart'; | ||
|
||
void main() { | ||
testGolden( | ||
'Wrap with observer', | ||
'assists/wrap_widget/wrap_with_observer.json', | ||
() async { | ||
final assist = WrapWithObserver(); | ||
final file = File('test/assists/wrap_widget/wrap_widget.dart').absolute; | ||
|
||
final result = await resolveFile2(path: file.path); | ||
result as ResolvedUnitResult; | ||
|
||
var changes = [ | ||
// Map | ||
...await assist.testRun(result, const SourceRange(171, 0)), | ||
|
||
// Scaffold | ||
...await assist.testRun(result, const SourceRange(190, 0)), | ||
|
||
// Container | ||
...await assist.testRun(result, const SourceRange(212, 0)), | ||
|
||
// Between () | ||
...await assist.testRun(result, const SourceRange(222, 0)), | ||
]; | ||
|
||
changes.forEach((element) { | ||
print(element); | ||
}); | ||
|
||
expect(changes, hasLength(2)); | ||
|
||
return changes; | ||
}, | ||
); | ||
} |
1 change: 1 addition & 0 deletions
1
mobx_lint_flutter_test/test/assists/wrap_widget/wrap_with_observer.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[{"priority":28,"change":{"message":"Wrap with Observer","edits":[{"fileStamp":0,"edits":[{"offset":228,"length":0,"replacement":"; },)"},{"offset":188,"length":0,"replacement":"Observer(builder: (context) { return "}]}],"linkedEditGroups":[]}},{"priority":28,"change":{"message":"Wrap with Observer","edits":[{"fileStamp":0,"edits":[{"offset":221,"length":0,"replacement":"; },)"},{"offset":210,"length":0,"replacement":"Observer(builder: (context) { return "}]}],"linkedEditGroups":[]}}] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should mention this in the docs as well so folks can see it easily on the website ... perhaps it can be a separate PR