Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Dart fix may apply extra commas, causing analysis failure #49065

Closed
@Piinks

Description

@Piinks

Found in flutter/flutter#97972

This change adds a fix rule that removes one parameter from a constructor and adds several others. In some cases, more than one comma is being applied by the fix rule, and is causing a customer test to fail because of it.

The fix rule includes a comma, but if it is omitted, the other cases fail analysis due to a missing comma.

I have simplified the rule to reproduce:

- title: "Migrate 'ThemeData.toggleableActiveColor' to individual themes"
    date: 2022-05-18
    element:
      uris: [ 'material.dart' ]
      constructor: ''
      inClass: 'ThemeData'
    changes:
    - kind: 'removeParameter'
      name: 'toggleableActiveColor'
    - kind: 'addParameter'
      index: 96
      name: 'checkboxTheme'
      style: optional_named
      argumentValue:
        expression: "CheckboxThemeData(\n
                       fillColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {\n
                         return states.contains(MaterialState.selected) ? {% toggleableActiveColor %} : null;\n
                       }),\n
                     ),"
        requiredIf: "toggleableActiveColor != ''"
    - kind: 'addParameter'
      index: 98
      name: 'radioTheme'
      style: optional_named
      argumentValue:
        expression: "RadioThemeData(\n
                       fillColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {\n
                         return states.contains(MaterialState.selected) ? {% toggleableActiveColor %} : null;\n
                       }),\n
                     ),"
        requiredIf:  "toggleableActiveColor != ''"
    - kind: 'addParameter'
      index: 100
      name: 'switchTheme'
      style: optional_named
      argumentValue:
        expression: "SwitchThemeData(\n
                       thumbColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {\n
                         return states.contains(MaterialState.selected) ? {% toggleableActiveColor %} : null;\n
                       }),\n
                       trackColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {\n
                         return states.contains(MaterialState.selected) ? {% toggleableActiveColor %} : null;\n
                       }),\n
                     )"
        requiredIf: "toggleableActiveColor != ''"
    variables:
      toggleableActiveColor:
        kind: 'fragment'
        value: 'arguments[toggleableActiveColor]'

Code migration

// Before
  ThemeData themeData = ThemeData();
  themeData = ThemeData(toggleableActiveColor: Colors.black);
  themeData = ThemeData(
    toggleableActiveColor: Colors.black,
  );
  themeData = ThemeData(
    colorScheme: _colorScheme,
    appBarTheme: AppBarTheme(
      color: _colorScheme.primary,
      iconTheme: IconThemeData(color: _colorScheme.onPrimary),
    ),
    bottomAppBarTheme: BottomAppBarTheme(
      color: _colorScheme.primary,
    ),
    canvasColor: _colorScheme.background,
    toggleableActiveColor: _colorScheme.primary,
    highlightColor: Colors.transparent,
    indicatorColor: _colorScheme.onPrimary,
    primaryColor: _colorScheme.primary,
    backgroundColor: Colors.white,
    scaffoldBackgroundColor: _colorScheme.background,
    snackBarTheme: const SnackBarThemeData(
      behavior: SnackBarBehavior.floating,
    ),
    typography: Typography.material2018(
      platform: defaultTargetPlatform,
    ),
    visualDensity: VisualDensity.standard,
  );
// After
  ThemeData themeData = ThemeData();
  themeData = ThemeData(checkboxTheme: CheckboxThemeData(
 fillColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
 return states.contains(MaterialState.selected) ? Colors.black : null;
 }),
 ),radioTheme: RadioThemeData(
 fillColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
 return states.contains(MaterialState.selected) ? Colors.black : null;
 }),
 ),switchTheme: SwitchThemeData(
 thumbColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
 return states.contains(MaterialState.selected) ? Colors.black : null;
 }),
 trackColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
 return states.contains(MaterialState.selected) ? Colors.black : null;
 }),
 ));
  themeData = ThemeData(
    checkboxTheme: CheckboxThemeData(
 fillColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
 return states.contains(MaterialState.selected) ? Colors.black : null;
 }),
 ),radioTheme: RadioThemeData(
 fillColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
 return states.contains(MaterialState.selected) ? Colors.black : null;
 }),
 ),switchTheme: SwitchThemeData(
 thumbColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
 return states.contains(MaterialState.selected) ? Colors.black : null;
 }),
 trackColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
 return states.contains(MaterialState.selected) ? Colors.black : null;
 }),
 ),
  );
  themeData = ThemeData(
    colorScheme: _colorScheme,
    appBarTheme: AppBarTheme(
      color: _colorScheme.primary,
      iconTheme: IconThemeData(color: _colorScheme.onPrimary),
    ),
    bottomAppBarTheme: BottomAppBarTheme(
      color: _colorScheme.primary,
    ),
    canvasColor: _colorScheme.background,
    highlightColor: Colors.transparent,
    indicatorColor: _colorScheme.onPrimary,
    primaryColor: _colorScheme.primary,
    backgroundColor: Colors.white,
    scaffoldBackgroundColor: _colorScheme.background,
    snackBarTheme: const SnackBarThemeData(
      behavior: SnackBarBehavior.floating,
    ),
    typography: Typography.material2018(
      platform: defaultTargetPlatform,
    ),
    visualDensity: VisualDensity.standard, checkboxTheme: CheckboxThemeData(
 fillColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
 return states.contains(MaterialState.selected) ? _colorScheme.primary : null;
 }),
 ),, radioTheme: RadioThemeData( // <-- extra comma
 fillColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
 return states.contains(MaterialState.selected) ? _colorScheme.primary : null;
 }),
 ),, switchTheme: SwitchThemeData( // <-- extra comma
 thumbColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
 return states.contains(MaterialState.selected) ? _colorScheme.primary : null;
 }),
 trackColor: MaterialStateProperty.resolveWith<Color?>((Set<MaterialState> states) {
 return states.contains(MaterialState.selected) ? _colorScheme.primary : null;
 }),
 ),
  );

Metadata

Metadata

Assignees

Labels

P2A bug or feature request we're likely to work ondevexp-data-driven-fixesIssues with the analysis server's support for data-driven fixeslegacy-area-analyzerUse area-devexp instead.type-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