编写自定义YAML文件以应用Dart修复

cgfeq70w  于 9个月前  发布在  其他
关注(0)|答案(1)|浏览(65)

我有一个Flutter项目,有两个Dart文件。在其中一个文件中,我有一个类,其中有一个不推荐的getter,我想用一个新的getter替换。为了实现这一点,我创建了一个带有自定义修复规则的.fix.yaml文件。但是,当运行dartfix--apply时,自定义修复不会被应用,而不推荐的getter保持不变。这两个文件都在同一个项目目录中,我已经验证了.fix.yaml文件和类文件的格式正确。是什么原因导致自定义修复无法按预期工作?有人可以指导我如何有效地编写此自定义YAML文件吗?任何帮助都很感激。谢谢!下面提供了我的自定义yaml文件和用户.dart代码。
用户.dart文件

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文件

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"


我试着看看Flutter的修复是如何写在这里的。https://github.com/flutter/flutter/blob/HEAD/packages/flutter/lib/fix_data/fix_material/fix_text_theme.yaml
并尝试编写相应的自定义修复程序。

svmlkihl

svmlkihl1#

在lib文件夹中创建一个fix_data.yaml文件。然后您可以在该文件中定义修复。该文件的规范可以在here中找到
在你的情况下,这应该起作用:

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'

字符串
若要在IDE中将修复视为提示,您可能必须重新启动IDE。

相关问题