visitInstanceCreationExpression method

  1. @override
R? visitInstanceCreationExpression(
  1. InstanceCreationExpression node
)
override

Implementation

@override
R? visitInstanceCreationExpression(InstanceCreationExpression node) {
  if (_shouldIgnore(node)) {
    return null;
  }

  final constructorName = node.constructorName.toString();

  if (constructorName == 'Border.only' ||
      constructorName == 'BorderRadius.only' ||
      constructorName == 'BorderRadius.horizontal' ||
      constructorName == 'Positioned' ||
      constructorName == 'Positioned.fill' ||
      constructorName == 'EdgeInsets' ||
      constructorName == 'EdgeInsets.only' ||
      constructorName == 'EdgeInsets.fromLTRB') {
    bool shouldFix = false;
    List<String> arguments = [];

    node.argumentList.arguments.forEach((element) {
      if (element is! NamedExpression) return;

      NamedExpression express = element;
      var result = express.expression.accept(_evaluator);
      if (result == ConstantEvaluator.NOT_A_CONSTANT) {
        result = express.expression.toString();
      }

      String label = express.name.toString();
      if (label == 'left:') {
        label = 'start:';
        shouldFix = true;
      }
      if (label == 'right:') {
        label = 'end:';
        shouldFix = true;
      }
      String split = ' ';

      arguments.add("$label$split$result");
    });
    if (!shouldFix) return null;

    final constructorNameType = node.constructorName.type.toString();
    String fixResult = constructorName;

    fixResult = fixResult.replaceFirst(constructorNameType, "${constructorNameType}Directional");

    if ('Positioned.fill' == constructorName) fixResult = 'PositionedDirectional';
    if ('EdgeInsets.fromLTRB' == constructorName) fixResult = 'EdgeInsetsDirectional.fromSTEB';

    fixResult += '( ';
    fixResult += arguments.join(',');
    fixResult += ' )';

    final lineInfo = unit.lineInfo!;
    final begin = node.beginToken.charOffset;
    final end = node.endToken.charEnd;
    final loc = lineInfo.getLocation(begin);
    final locEnd = lineInfo.getLocation(end);

    foundRtlWidget(FoundRtlWidget(
        filePath: filePath,
        offset: node.offset,
        length: node.length,
        startLine: loc.lineNumber,
        startColumn: loc.columnNumber,
        endLine: locEnd.lineNumber,
        endColumn: locEnd.columnNumber,
        fix: fixResult,
        msg: 'RTL:Use ${constructorNameType}Directional instead',
        error: '$constructorName is not RTL campatiable'));
  }

  return super.visitInstanceCreationExpression(node);
}