Milan-Chabhadiya
Milan-Chabhadiya

Reputation: 11

Writing a custom YAML file to apply Dart fixes

I have a Flutter project with two Dart files. In one of the files, I have a class with a deprecated getter that I want to replace with a new one. To achieve this, I created a .fix.yaml file with the custom fix rule. However, when running dart fix --apply, the custom fix is not being applied, and the deprecated getter remains unchanged. Both files are in the same project directory, and I have verified that the .fix.yaml file and the class file are correctly formatted. What could be causing the custom fix not to work as expected? Can someone guide me on how to write this custom YAML file effectively? Any help is appreciated. Thanks! My custom yaml file and user.dart code is provided below.

user.dart file

class User {
  String? name;
  int? age;

  User({this.name, this.age});
  @Deprecated('Use fullName instead')
  String? get userName => fullName;
  String? get fullName => name;

  void sayHello() {
    debugPrint("Hello, my name is $name and I am $age years old.");
  }
}

fix_user_rename.fix.yaml file

version: 1
transforms:
  - title: "Replace deprecated getter userName with fullName"
    description: "Replaces usages of the deprecated getter userName with the new getter fullName."
    matcher:
      type: MethodInvocation
      where:
        name: "userName"
    generator:
      type: SimpleGenerator
      template: "fullName"

I tried to see how Flutter's fixes are written here. https://github.com/flutter/flutter/blob/HEAD/packages/flutter/lib/fix_data/fix_material/fix_text_theme.yaml

and try to write custom fixes accordingly.

Upvotes: 1

Views: 357

Answers (1)

Torben Keller
Torben Keller

Reputation: 112

Create a fix_data.yaml file in your lib folder. Then you can define your fixes in that file. The specification of that file can be found here

In your case this should work:

version: 1
transforms:
  - title: Rename to fullName
    date: 2023-12-23
    bulkApply: true
    element:
      uris: [ '$$relative file import$$', '$$package file import$$' ]
      getter: 'userName'
      inClass: 'User'
    changes:
      - kind: 'rename'
        newName: 'fullName'

To see the fix as a hint in your IDE you might have to restart your IDE.

Upvotes: 1

Related Questions