-
-
Notifications
You must be signed in to change notification settings - Fork 956
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b5e50b
commit 0a37ce6
Showing
8 changed files
with
179 additions
and
0 deletions.
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,90 @@ | ||
import 'package:analyzer/dart/element/element.dart'; | ||
import 'package:analyzer/error/error.dart'; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
import 'package:riverpod_analyzer_utils/riverpod_analyzer_utils.dart'; | ||
|
||
import '../riverpod_custom_lint.dart'; | ||
|
||
const _buildMethodName = 'build'; | ||
|
||
class NotifierBuild extends RiverpodLintRule { | ||
const NotifierBuild() : super(code: _code); | ||
|
||
static const _code = LintCode( | ||
name: 'notifier_build', | ||
problemMessage: | ||
'Classes annotated by `@riverpod` must have the `build` method', | ||
); | ||
|
||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ErrorReporter reporter, | ||
CustomLintContext context, | ||
) { | ||
context.registry.addClassDeclaration((node) { | ||
final hasRiverpodAnnotation = node.metadata.where( | ||
(element) { | ||
final annotationElement = element.element; | ||
|
||
if (annotationElement == null || | ||
annotationElement is! ExecutableElement) return false; | ||
|
||
return riverpodType.isExactlyType(annotationElement.returnType); | ||
}, | ||
).isNotEmpty; | ||
|
||
if (!hasRiverpodAnnotation) return; | ||
|
||
final hasBuildMethod = node.members | ||
.where((e) => e.declaredElement?.displayName == _buildMethodName) | ||
.isNotEmpty; | ||
|
||
if (hasBuildMethod) return; | ||
|
||
reporter.reportErrorForToken(_code, node.name); | ||
}); | ||
} | ||
|
||
@override | ||
List<RiverpodFix> getFixes() => [ | ||
AddBuildMethodFix(), | ||
]; | ||
} | ||
|
||
class AddBuildMethodFix extends RiverpodFix { | ||
@override | ||
void run( | ||
CustomLintResolver resolver, | ||
ChangeReporter reporter, | ||
CustomLintContext context, | ||
AnalysisError analysisError, | ||
List<AnalysisError> others, | ||
) { | ||
context.registry.addClassDeclaration((node) { | ||
if (!node.sourceRange.intersects(analysisError.sourceRange)) return; | ||
|
||
final changeBuilder = reporter.createChangeBuilder( | ||
message: 'Add build method', | ||
priority: 80, | ||
); | ||
|
||
changeBuilder.addDartFileEdit((builder) { | ||
final offset = node.leftBracket.offset + 1; | ||
|
||
builder.addSimpleInsertion( | ||
offset, | ||
''' | ||
@override | ||
dynamic build() { | ||
// TODO: implement build | ||
throw UnimplementedError(); | ||
} | ||
''', | ||
); | ||
}); | ||
}); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
packages/riverpod_lint_flutter_test/test/goldens/fixes/notifier_build.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,8 @@ | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
|
||
/// Fake Provider | ||
typedef _$ExampleProvider1 = Object; | ||
|
||
@riverpod | ||
// expect_lint: notifier_build | ||
class ExampleProvider1 extends _$ExampleProvider1 {} |
1 change: 1 addition & 0 deletions
1
packages/riverpod_lint_flutter_test/test/goldens/fixes/notifier_build.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 @@ | ||
[] |
33 changes: 33 additions & 0 deletions
33
packages/riverpod_lint_flutter_test/test/goldens/fixes/notifier_build_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,33 @@ | ||
import 'dart:io'; | ||
|
||
import 'package:collection/collection.dart'; | ||
import 'package:riverpod_lint/src/lints/notifier_build.dart'; | ||
import 'package:analyzer/dart/analysis/results.dart'; | ||
import 'package:analyzer/dart/analysis/utilities.dart'; | ||
|
||
import '../../golden.dart'; | ||
|
||
void main() { | ||
testGolden( | ||
'Verify that @riverpod classes has the build method', | ||
'goldens/fixes/notifier_build.json', | ||
() async { | ||
const lint = NotifierBuild(); | ||
final fix = lint.getFixes().single; | ||
final file = File( | ||
'test/goldens/fixes/notifier_build.dart', | ||
).absolute; | ||
|
||
final result = await resolveFile2(path: file.path); | ||
result as ResolvedUnitResult; | ||
|
||
final errors = await lint.testRun(result); | ||
|
||
final changes = await Future.wait([ | ||
for (final error in errors) fix.testRun(result, error, errors), | ||
]); | ||
|
||
return changes.flattened; | ||
}, | ||
); | ||
} |
17 changes: 17 additions & 0 deletions
17
packages/riverpod_lint_flutter_test/test/goldens/lints/notifier_build.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,17 @@ | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
|
||
/// Fake Provider | ||
typedef _$ExampleProvider1 = Object; | ||
|
||
/// Fake Provider | ||
typedef _$ExampleProvider = AutoDisposeNotifier<int>; | ||
|
||
@riverpod | ||
// expect_lint: notifier_build | ||
class ExampleProvider1 extends _$ExampleProvider1 {} | ||
|
||
@riverpod | ||
class ExampleProvider extends _$ExampleProvider { | ||
@override | ||
int build() => 0; | ||
} |