|
| 1 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 2 | +import 'package:analyzer/dart/element/element.dart'; |
| 3 | +import 'package:analyzer/dart/element/type.dart'; |
| 4 | +import 'package:over_react_analyzer_plugin/src/diagnostic_contributor.dart'; |
| 5 | +import 'package:over_react_analyzer_plugin/src/util/ast_util.dart'; |
| 6 | +import 'package:over_react_analyzer_plugin/src/util/is_props_from_render.dart'; |
| 7 | +import 'package:over_react_analyzer_plugin/src/util/prop_forwarding/parse_forwarding_config.dart'; |
| 8 | +import 'package:over_react_analyzer_plugin/src/util/util.dart'; |
| 9 | + |
| 10 | +import 'forwarding_config.dart'; |
| 11 | +import 'util.dart'; |
| 12 | + |
| 13 | +/// A representation of props forwarded to a component usage. |
| 14 | +class ForwardedProps { |
| 15 | + /// The props class that props are being forwarded from. |
| 16 | + /// |
| 17 | + /// For example, the type of `props` in `..addUnconsumedProps(props, ...)`. |
| 18 | + final InterfaceElement propsClassBeingForwarded; |
| 19 | + |
| 20 | + /// The configuration of which props to forward, or null if it could not be resolved. |
| 21 | + final PropForwardingConfig? forwardingConfig; |
| 22 | + |
| 23 | + /// A node that represents the addition of forwarded props, for use in debug infos only. |
| 24 | + final AstNode debugSourceNode; |
| 25 | + |
| 26 | + ForwardedProps(this.propsClassBeingForwarded, this.forwardingConfig, this.debugSourceNode); |
| 27 | + |
| 28 | + /// Returns whether these forwarded props definitely include props from [propsClass], or false |
| 29 | + /// if forwarded props could not be resolved. |
| 30 | + /// |
| 31 | + /// This is true only when all of the following conditions are met: |
| 32 | + /// - [propsClassBeingForwarded] inherits from [propsClass] (i.e., is or mixes in those props) |
| 33 | + /// - [propsClass] is not excluded by [forwardingConfig] |
| 34 | + bool definitelyForwardsPropsFrom(InterfaceElement propsClass) { |
| 35 | + final forwardingConfig = this.forwardingConfig; |
| 36 | + if (forwardingConfig == null) return false; |
| 37 | + |
| 38 | + // Handle legacy classes being passed in. |
| 39 | + if (propsClass.name.startsWith(r'_$')) { |
| 40 | + // Look up the companion and use that instead, since that's what will be referenced in the forwarding config. |
| 41 | + // E.g., for `_$FooProps`, find `FooProps`, since consumers will be using `FooProps` when setting up prop forwarding. |
| 42 | + final companion = propsClassBeingForwarded.thisAndAllSuperInterfaces |
| 43 | + .whereType<ClassElement>() |
| 44 | + .singleWhereOrNull((c) => c.supertype?.element == propsClass && '_\$${c.name}' == propsClass.name); |
| 45 | + // If we can't find the companion, return false, since it won't show up in the forwarding config. |
| 46 | + if (companion == null) return false; |
| 47 | + propsClass = companion; |
| 48 | + } |
| 49 | + |
| 50 | + return !forwardingConfig.excludesProps(propsClass) && |
| 51 | + propsClassBeingForwarded.thisAndAllSuperInterfaces.contains(propsClass); |
| 52 | + } |
| 53 | + |
| 54 | + @override |
| 55 | + String toString() => 'Forwards props from ${propsClassBeingForwarded.name}: ${forwardingConfig ?? '(unresolved)'}'; |
| 56 | +} |
| 57 | + |
| 58 | +extension on InterfaceElement { |
| 59 | + /// This interface and all its superinterfaces. |
| 60 | + /// |
| 61 | + /// Computed lazily, since [allSupertypes] is expensive. |
| 62 | + Iterable<InterfaceElement> get thisAndAllSuperInterfaces sync* { |
| 63 | + yield this; |
| 64 | + yield* allSupertypes.map((s) => s.element); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// Computes and returns forwarded props for a given component [usage], or `null` if the usage does not receive any |
| 69 | +/// forwarded props. |
| 70 | +ForwardedProps? computeForwardedProps(FluentComponentUsage usage) { |
| 71 | + // Lazy variables for potentially expensive values that may get used in multiple loop iterations. |
| 72 | + late final enclosingComponentPropsClass = |
| 73 | + getTypeOfPropsInEnclosingInterface(usage.node)?.typeOrBound.element.tryCast<InterfaceElement>(); |
| 74 | + |
| 75 | + for (final invocation in usage.cascadedMethodInvocations) { |
| 76 | + final methodName = invocation.methodName.name; |
| 77 | + final arg = invocation.node.argumentList.arguments.firstOrNull; |
| 78 | + |
| 79 | + if (methodName == 'addProps' || methodName == 'modifyProps') { |
| 80 | + // If props are conditionally forwarded, don't count them. |
| 81 | + final hasConditionArg = invocation.node.argumentList.arguments.length > 1; |
| 82 | + if (hasConditionArg) continue; |
| 83 | + } |
| 84 | + |
| 85 | + final isAddAllOrAddProps = methodName == 'addProps' || methodName == 'addAll'; |
| 86 | + |
| 87 | + // ..addProps(props) |
| 88 | + if (isAddAllOrAddProps && arg != null && isPropsFromRender(arg)) { |
| 89 | + final propsType = arg.staticType?.typeOrBound.tryCast<InterfaceType>()?.element; |
| 90 | + if (propsType != null) { |
| 91 | + return ForwardedProps(propsType, PropForwardingConfig.all(), invocation.node); |
| 92 | + } |
| 93 | + } else if ( |
| 94 | + // ..addProps(props.getPropsToForward(...)) |
| 95 | + (isAddAllOrAddProps && arg is MethodInvocation && arg.methodName.name == 'getPropsToForward') || |
| 96 | + // ..modifyProps(props.addPropsToForward(...)) |
| 97 | + (methodName == 'modifyProps' && arg is MethodInvocation && arg.methodName.name == 'addPropsToForward')) { |
| 98 | + final realTarget = arg.realTarget; |
| 99 | + if (realTarget != null && isPropsFromRender(realTarget)) { |
| 100 | + final propsType = realTarget.staticType?.typeOrBound.tryCast<InterfaceType>()?.element; |
| 101 | + if (propsType != null) { |
| 102 | + return ForwardedProps(propsType, parsePropsToForwardMethodArgs(arg.argumentList, propsType), invocation.node); |
| 103 | + } |
| 104 | + } |
| 105 | + } else if ( |
| 106 | + // ..addProps(copyUnconsumedProps()) |
| 107 | + (isAddAllOrAddProps && arg is MethodInvocation && arg.methodName.name == 'copyUnconsumedProps') || |
| 108 | + // ..modifyProps(addUnconsumedProps) |
| 109 | + (methodName == 'modifyProps' && arg is Identifier && arg.name == 'addUnconsumedProps')) { |
| 110 | + if (enclosingComponentPropsClass != null) { |
| 111 | + return ForwardedProps( |
| 112 | + enclosingComponentPropsClass, parseEnclosingClassComponentConsumedProps(usage.node), invocation.node); |
| 113 | + } |
| 114 | + } else if ( |
| 115 | + // ..addUnconsumedProps(props, consumedProps) |
| 116 | + methodName == 'addUnconsumedProps') { |
| 117 | + final consumedPropsArg = invocation.node.argumentList.arguments.elementAtOrNull(1); |
| 118 | + if (arg != null && consumedPropsArg != null && isPropsFromRender(arg)) { |
| 119 | + final propsType = arg.staticType?.typeOrBound.tryCast<InterfaceType>()?.element; |
| 120 | + if (propsType != null) { |
| 121 | + return ForwardedProps(propsType, parseConsumedProps(consumedPropsArg), invocation.node); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return null; |
| 128 | +} |
0 commit comments