rewriteTearOff function

ASTWithSource rewriteTearOff(
  1. ASTWithSource original,
  2. AnalyzedClass analyzedClass
)

Rewrites an event tear-off as a method call.

If original is a ast.PropertyRead, and a method with the same name exists in analyzedClass, then convert original into a ast.MethodCall.

If the underlying method has any parameters, then assume one parameter of '$event'.

Implementation

ast.ASTWithSource rewriteTearOff(
    ast.ASTWithSource original, AnalyzedClass analyzedClass) {
  var unwrappedExpression = original.ast;

  if (unwrappedExpression is ast.PropertyRead) {
    // Find the method, either on "this." or "super.".
    final method = analyzedClass.classElement.thisType.lookUpMethod2(
        unwrappedExpression.name, analyzedClass.classElement.library);

    // If not found, we do not perform any re-write.
    if (method == null) {
      return original;
    }

    // If we have no positional parameters (optional or otherwise), then we
    // translate the call into "foo()". If we have at least one, we translate
    // the call into "foo($event)".
    final positionalParameters = method.parameters.where((p) => !p.isNamed);
    if (positionalParameters.isEmpty) {
      return ast.ASTWithSource.from(
          original, _simpleMethodCall(unwrappedExpression));
    } else {
      return ast.ASTWithSource.from(
          original, _complexMethodCall(unwrappedExpression));
    }
  }
  return original;
}