-
Notifications
You must be signed in to change notification settings - Fork 58
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
FED-2112 Required props lint: respect @Props(ignoreRequiredProps: ...) #873
Changes from 23 commits
939993f
e0c0f45
4846adf
b2a1191
c859acb
77e3ec5
008939c
92a2e82
ef7d8d2
b97d9ad
8a2e187
56d5c97
a8e401a
fb68796
6b1e1c7
14d6c32
3dc54cf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,17 +52,52 @@ class _PropsStateStringHelpersImpl extends Object with PropsStateStringHelpers { | |
|
||
/// Uses [InstantiatedMeta] to analyze [node] and determine the proper annotation. | ||
annotations.TypedMap getPropsOrStateAnnotation(bool isProps, AnnotatedNode node) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whitespace-agnostic diff here is helpful |
||
final meta = isProps | ||
? (InstantiatedMeta.fromNode<annotations.Props>(node) ?? | ||
InstantiatedMeta.fromNode<annotations.AbstractProps>(node) ?? | ||
// ignore: deprecated_member_use_from_same_package | ||
InstantiatedMeta.fromNode<annotations.PropsMixin>(node)) | ||
: (InstantiatedMeta.fromNode<annotations.State>(node) ?? | ||
InstantiatedMeta.fromNode<annotations.AbstractState>(node) ?? | ||
// ignore: deprecated_member_use_from_same_package | ||
InstantiatedMeta.fromNode<annotations.StateMixin>(node)); | ||
|
||
return meta?.value ?? (isProps ? annotations.Props() : annotations.State()); | ||
late final defaultValue = isProps ? annotations.Props() : annotations.State(); | ||
|
||
InstantiatedMeta<annotations.TypedMap>? meta; | ||
try { | ||
meta = isProps | ||
? (InstantiatedMeta.fromNode<annotations.Props>(node) ?? | ||
InstantiatedMeta.fromNode<annotations.AbstractProps>(node) ?? | ||
// ignore: deprecated_member_use_from_same_package | ||
InstantiatedMeta.fromNode<annotations.PropsMixin>(node)) | ||
: (InstantiatedMeta.fromNode<annotations.State>(node) ?? | ||
InstantiatedMeta.fromNode<annotations.AbstractState>(node) ?? | ||
// ignore: deprecated_member_use_from_same_package | ||
InstantiatedMeta.fromNode<annotations.StateMixin>(node)); | ||
|
||
if (meta == null) return defaultValue; | ||
|
||
if (meta.potentiallyIncompleteValue is annotations.Props) { | ||
if (meta.unsupportedArguments.length == 1) { | ||
final arg = meta.unsupportedArguments[0]; | ||
if (arg is NamedExpression && arg.name.label.name == 'ignoreRequiredProps') { | ||
// Attempt to parse the value, and fall through if something goes wrong, | ||
// and let `meta?.value` below throw. | ||
final expression = arg.expression; | ||
if (expression is SetOrMapLiteral) { | ||
final simpleStringElements = | ||
expression.elements.whereType<SimpleStringLiteral>().toList(); | ||
if (simpleStringElements.length == expression.elements.length) { | ||
return annotations.Props( | ||
keyNamespace: meta.potentiallyIncompleteValue.keyNamespace, | ||
ignoreRequiredProps: simpleStringElements.map((e) => e.value).toSet(), | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
return meta.value; | ||
} catch (e, st) { | ||
// Log a severe error instead of throwing, so that the error doesn't propagate when we're doing parsing within | ||
// the analyzer plugin. | ||
// This severe error will fail the build and be presented to the consumer. | ||
log.severe( | ||
'Error reading annotation${meta == null ? '' : ': ${meta.metaNode.toSource()}'}', e, st); | ||
return defaultValue; | ||
} | ||
} | ||
|
||
/// If a [ClassMember] exists in [node] with the name `meta`, this will | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
import 'dart:mirrors' as mirrors; | ||
|
||
import 'package:analyzer/dart/ast/ast.dart'; | ||
import 'package:build/build.dart' show log; | ||
import 'package:collection/collection.dart' show IterableExtension; | ||
import 'package:transformer_utils/transformer_utils.dart'; | ||
|
||
|
@@ -44,7 +45,7 @@ Annotation? _getMatchingAnnotation(AnnotatedNode member, Type annotationType) { | |
/// Based off of [NodeWithMeta]. | ||
class InstantiatedMeta<TMeta> { | ||
/// The node of the [TMeta] annotation, if it exists. | ||
final Annotation? metaNode; | ||
final Annotation metaNode; | ||
|
||
/// A reflectively-instantiated version of [metaNode], if it exists. | ||
final TMeta _value; | ||
|
@@ -63,6 +64,8 @@ class InstantiatedMeta<TMeta> { | |
/// The instantiated annotation will be available via [value]. | ||
static InstantiatedMeta<T>? fromNode<T>(AnnotatedNode node) { | ||
final metaNode = _getMatchingAnnotation(node, T); | ||
if (metaNode == null) return null; | ||
|
||
final unsupportedArguments = <Expression>[]; | ||
final value = | ||
instantiateAnnotationTyped<T>(node, onUnsupportedArgument: unsupportedArguments.add); | ||
|
@@ -102,31 +105,38 @@ class InstantiatedComponentMeta<TMeta> extends InstantiatedMeta<TMeta> { | |
final Identifier? subtypeOfValue; | ||
|
||
InstantiatedComponentMeta._( | ||
Annotation? metaNode, TMeta meta, List<Expression> unsupportedArguments, this.subtypeOfValue) | ||
Annotation metaNode, TMeta meta, List<Expression> unsupportedArguments, this.subtypeOfValue) | ||
: super._(metaNode, meta, unsupportedArguments); | ||
|
||
static InstantiatedComponentMeta<T>? fromNode<T>(AnnotatedNode node) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whitespace-agnostic diff here is helpful |
||
final instantiated = InstantiatedMeta.fromNode<T>(node); | ||
|
||
if (instantiated == null) return null; | ||
|
||
Identifier? subtypeOfValue; | ||
|
||
NamedExpression? subtypeOfParam = instantiated.unsupportedArguments | ||
.whereType<NamedExpression>() | ||
.firstWhereOrNull((expression) => expression.name.label.name == _subtypeOfParamName); | ||
|
||
if (subtypeOfParam != null) { | ||
final expression = subtypeOfParam.expression; | ||
if (expression is Identifier) { | ||
subtypeOfValue = expression; | ||
instantiated.unsupportedArguments.remove(subtypeOfParam); | ||
} else { | ||
throw Exception('`$_subtypeOfParamName` must be an identifier: $subtypeOfParam'); | ||
try { | ||
final instantiated = InstantiatedMeta.fromNode<T>(node); | ||
if (instantiated == null) return null; | ||
|
||
Identifier? subtypeOfValue; | ||
|
||
NamedExpression? subtypeOfParam = instantiated.unsupportedArguments | ||
.whereType<NamedExpression>() | ||
.firstWhereOrNull((expression) => expression.name.label.name == _subtypeOfParamName); | ||
|
||
if (subtypeOfParam != null) { | ||
final expression = subtypeOfParam.expression; | ||
if (expression is Identifier) { | ||
subtypeOfValue = expression; | ||
instantiated.unsupportedArguments.remove(subtypeOfParam); | ||
} else { | ||
throw Exception('`$_subtypeOfParamName` must be an identifier: $subtypeOfParam'); | ||
} | ||
} | ||
} | ||
|
||
return InstantiatedComponentMeta._(instantiated.metaNode, instantiated.value, | ||
instantiated.unsupportedArguments, subtypeOfValue); | ||
return InstantiatedComponentMeta._(instantiated.metaNode, instantiated.value, | ||
instantiated.unsupportedArguments, subtypeOfValue); | ||
} catch (e, st) { | ||
// Log a severe error instead of throwing, so that the error doesn't propagate when we're doing parsing within | ||
// the analyzer plugin. | ||
// This severe error will fail the build and be presented to the consumer. | ||
log.severe('Error reading component annotation', e, st); | ||
return null; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 added this ignore to fix my test setup, which validated there were no warnings in generated code