Skip to content

dart fix + new formatter omits whitespace from before #59747

@Piinks

Description

@Piinks

Found in flutter/flutter#160453

We are preparing to turn on the auto formatter in flutter/flutter (🎊 )

As such, the dart fix test files we use in the framework have been formatted and I noticed that where previously dart fix would respect the whitespace of a written argumentValue + expression, it does not now.

I discovered this by running dart format on my_fix.dart, then running dart fix, and observed the whitespace changed.

fix.yaml
 # Changes made in https://github.com/flutter/flutter/pull/152981
  - title: "Replace with 'fillColor'"
    date: 2024-08-08
    element:
      uris: [ 'cupertino.dart' ]
      constructor: ''
      inClass: 'CupertinoCheckbox'
    oneOf:
      - if: "fillColor == '' && activeColor != ''"
        changes:
          - kind: 'addParameter'
            index: 5
            name: 'fillColor'
            style: optional_named
            argumentValue:
              expression: "WidgetStateProperty.resolveWith<Color>((Set<WidgetState> states) {\n
                        if (states.contains(WidgetState.disabled)) {\n
                          return CupertinoColors.white.withOpacity(0.5);\n
                        }\n
                        if (states.contains(WidgetState.selected)) {\n
                          return {% activeColor %};\n
                        }\n
                        return {% inactiveColor %};\n
                  })"
              requiredIf: "inactiveColor != ''"
          - kind: 'removeParameter'
            name: inactiveColor
      - if: "fillColor == '' && activeColor == ''"
        changes:
          - kind: 'addParameter'
            index: 5
            name: 'fillColor'
            style: optional_named
            argumentValue:
              expression: "WidgetStateProperty.resolveWith<Color>((Set<WidgetState> states) {\n
                        if (states.contains(WidgetState.disabled)) {\n
                          return CupertinoColors.white.withOpacity(0.5);\n
                        }\n
                        if (states.contains(WidgetState.selected)) {\n
                          return CupertinoDynamicColor.resolve(CupertinoDynamicColor.withBrightness(\n
                            color: CupertinoColors.activeBlue,\n
                            darkColor: Color.fromARGB(255, 50, 100, 215),\n
                          ), context);\n
                        }\n
                        return {% inactiveColor %};\n
                  })"
              requiredIf: "inactiveColor != ''"
          - kind: 'removeParameter'
            name: inactiveColor
      - if: "fillColor != ''"
        changes:
          - kind: 'removeParameter'
            name: inactiveColor
    variables:
      inactiveColor:
        kind: fragment
        value: 'arguments[inactiveColor]'
      activeColor:
        kind: fragment
        value: 'arguments[activeColor]'
      fillColor:
        kind: fragment
        value: 'arguments[fillColor]'

Unformatted code before dart fix
  // https://github.com/flutter/flutter/pull/152981
  CupertinoCheckbox(inactiveColor: Colors.red);
  CupertinoCheckbox(inactiveColor: Colors.red, activeColor: Colors.white);
  CupertinoCheckbox(inactiveColor: Colors.red, fillColor: WidgetStatePropertyAll(CupertinoColors.white));
Original result after dart fix
  // https://github.com/flutter/flutter/pull/152981
  CupertinoCheckbox(fillColor: WidgetStateProperty.resolveWith<Color>((Set<WidgetState> states) {
    if (states.contains(WidgetState.disabled)) {
      return CupertinoColors.white.withOpacity(0.5);
    }
    if (states.contains(WidgetState.selected)) {
      return CupertinoDynamicColor.resolve(CupertinoDynamicColor.withBrightness(
        color: CupertinoColors.activeBlue,
        darkColor: Color.fromARGB(255, 50, 100, 215),
      ), context);
    }
    return Colors.red;
  }));
  CupertinoCheckbox(activeColor: Colors.white, fillColor: WidgetStateProperty.resolveWith<Color>((Set<WidgetState> states) {
    if (states.contains(WidgetState.disabled)) {
      return CupertinoColors.white.withOpacity(0.5);
    }
    if (states.contains(WidgetState.selected)) {
      return Colors.white;
    }
    return Colors.red;
  }));
  CupertinoCheckbox(fillColor: WidgetStatePropertyAll(CupertinoColors.white));
}

Formatted code before dart fix
  // https://github.com/flutter/flutter/pull/152981
  CupertinoCheckbox(inactiveColor: Colors.red);
  CupertinoCheckbox(inactiveColor: Colors.red, activeColor: Colors.white);
  CupertinoCheckbox(
    inactiveColor: Colors.red,
    fillColor: WidgetStatePropertyAll(CupertinoColors.white),
  );
New no whitespace result after dart fix
// https://github.com/flutter/flutter/pull/152981
  CupertinoCheckbox(fillColor: WidgetStateProperty.resolveWith<Color>((Set<WidgetState> states) {
 if (states.contains(WidgetState.disabled)) {
 return CupertinoColors.white.withOpacity(0.5);
 }
 if (states.contains(WidgetState.selected)) {
 return CupertinoDynamicColor.resolve(CupertinoDynamicColor.withBrightness(
 color: CupertinoColors.activeBlue,
 darkColor: Color.fromARGB(255, 50, 100, 215),
 ), context);
 }
 return Colors.red;
 }));
  CupertinoCheckbox(activeColor: Colors.white, fillColor: WidgetStateProperty.resolveWith<Color>((Set<WidgetState> states) {
 if (states.contains(WidgetState.disabled)) {
 return CupertinoColors.white.withOpacity(0.5);
}
 if (states.contains(WidgetState.selected)) {
 return Colors.white;
 }
 return Colors.red;
 }));
  CupertinoCheckbox(
    fillColor: WidgetStatePropertyAll(CupertinoColors.white),
  );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-dart-cliUse area-dart-cli for issues related to the 'dart' command like tool.dart-cli-fixtype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions