-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add always_provide_flag_property_parameter
- Loading branch information
Showing
10 changed files
with
251 additions
and
72 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
37 changes: 37 additions & 0 deletions
37
forui_internal_lints/lib/src/always_provide_flag_property_parameter.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,37 @@ | ||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
const _code = LintCode( | ||
name: 'always_provide_flag_property_parameter', | ||
problemMessage: 'Provide `ifTrue` and/or `ifFalse` parameter', | ||
); | ||
|
||
const _flagProperty = TypeChecker.fromName('FlagProperty', packageName: 'flutter'); | ||
|
||
/// A lint rule that ensures flag property provides `ifTrue` and/or `ifFalse` parameter. | ||
class AlwaysProvideFlagPropertyArgument extends DartLintRule { | ||
/// Creates a new [AlwaysProvideFlagPropertyArgument]. | ||
const AlwaysProvideFlagPropertyArgument() : super(code: _code); | ||
|
||
@override | ||
void run(CustomLintResolver resolver, ErrorReporter reporter, CustomLintContext context) { | ||
context.registry.addInstanceCreationExpression((node) { | ||
if (node.staticType case final type when type == null || !_flagProperty.isExactlyType(type)) { | ||
return; | ||
} | ||
|
||
if (node.argumentList.length < 2) { | ||
return; | ||
} | ||
|
||
if (node.argumentList.arguments | ||
.whereType<NamedExpression>() | ||
.any((expression) => expression.element?.name == 'ifTrue' || expression.element?.name == 'ifFalse')) { | ||
return; | ||
} | ||
|
||
reporter.atNode(node, _code); | ||
}); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,81 @@ | ||
import 'package:analyzer/error/listener.dart'; | ||
import 'package:custom_lint_builder/custom_lint_builder.dart'; | ||
|
||
const _code = LintCode( | ||
name: 'style_api', | ||
problemMessage: 'Style(s) should conform to the Diagnosticable interface.', | ||
); | ||
|
||
const _suffixes = {'Style', 'Styles'}; | ||
|
||
const _checker = TypeChecker.fromName('Diagnosticable', packageName: 'flutter'); | ||
|
||
/// A lint rule that checks if a class ending with 'Style' or 'Styles' conforms to the required interface. | ||
class StyleApiRule extends DartLintRule { | ||
/// Creates a new [StyleApiRule]. | ||
const StyleApiRule() : super(code: _code); | ||
|
||
@override | ||
void run(CustomLintResolver resolver, ErrorReporter reporter, CustomLintContext context) { | ||
context.registry.addClassDeclaration((node) { | ||
final declared = node.declaredElement; | ||
if (declared == null) { | ||
return; | ||
} | ||
|
||
if (declared.name case final name when _suffixes.every((s) => !name.endsWith(s))) { | ||
return; | ||
} | ||
|
||
if (declared.isConstructable && !_checker.isAssignableFrom(declared)) { | ||
reporter.atElement( | ||
declared, | ||
LintCode( | ||
name: 'style_api', | ||
problemMessage: '${declared.name}, should be assignable to Diagnosticable.', | ||
), | ||
); | ||
} | ||
|
||
if (!declared.isConstructable) { | ||
return; | ||
} | ||
|
||
final copyWith = declared.getMethod('copyWith'); | ||
if (copyWith == null || | ||
copyWith.isStatic || | ||
copyWith.returnType != declared.thisType || | ||
copyWith.parameters.isEmpty) { | ||
reporter.atElement( | ||
declared, | ||
LintCode( | ||
name: 'style_api', | ||
problemMessage: '${declared.name} does not provide a valid copyWith(...) method.', | ||
), | ||
); | ||
} | ||
|
||
final equality = declared.getMethod('=='); | ||
if (equality == null) { | ||
reporter.atElement( | ||
declared, | ||
LintCode( | ||
name: 'style_api', | ||
problemMessage: '${declared.name} does not provide a valid == operator.', | ||
), | ||
); | ||
} | ||
|
||
final hashCode = declared.getGetter('hashCode'); | ||
if (hashCode == null) { | ||
reporter.atElement( | ||
declared, | ||
LintCode( | ||
name: 'style_api', | ||
problemMessage: '${declared.name} does not provide a valid hashCode getter.', | ||
), | ||
); | ||
} | ||
}); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
forui_internal_lints/testbed/lib/src/always_provide_flag_property_parameter.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,12 @@ | ||
import 'package:flutter/foundation.dart'; | ||
|
||
final goodBoth = FlagProperty('good', value: true, ifTrue: 'good', ifFalse: 'bad'); | ||
|
||
final goodTrue = FlagProperty('good', value: true, ifTrue: 'good'); | ||
|
||
final goodFalse = FlagProperty('good', value: false, ifFalse: 'bad'); | ||
|
||
// expect_lint: always_provide_flag_property_parameter | ||
final bad = FlagProperty('bad', value: false); | ||
|
||
|
13 changes: 0 additions & 13 deletions
13
forui_internal_lints/testbed/lib/src/diagnosticable_styles.dart
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.