4

我正在做一个非常标准的 Flutter 项目。因为我是 Flutter 和 Dart 的新手,所以我希望我的工具尽可能有用。所以我添加pedantic: ^1.9.0并这样dev_dependenciesanalysis_options.yaml

include: package:pedantic/analysis_options.yaml

analyzer:
  exclude: [build/**]
  strong-mode:
    implicit-casts: false
    implicit-dynamic: false

根据https://dart-lang.github.io/linter/lints/pedantic应该启用avoid_empty_elseavoid_relative_lib_importslints 等错误。但是当我编写如下代码时:

import '../model/model.dart';

或这个:

  if (context == null) {
    print('context is null');
  } else {
  }

我在 IntelliJ IDEA 中没有收到任何错误,flutter analyze手动运行时也没有:

$ flutter analyze
Analyzing app...                                                        
No issues found! (ran in 5.0s)

我已经尝试明确启用这些 lints:

linter:
  rules:
    - avoid_empty_else
    - avoid_relative_lib_imports

这没有任何区别。

我尝试foo在该列表中添加一个不存在的 lint 以验证该文件是否正在被使用,它是:

$ flutter analyze
Analyzing app...                                                        

warning • 'foo' is not a recognized lint rule • analysis_options.yaml:12:7 • undefined_lint_warning

1 issue found. (ran in 4.9s)

我什至尝试dartanalyzer直接从 Flutter 安装目录运行,并使用我能找到的所有详细选项:

$ ~/flutter/bin/cache/dart-sdk/bin/dartanalyzer --lints --verbose --log --options analysis_options.yaml .
Analyzing app...
Loaded analysis options from analysis_options.yaml
Analysis options: lints = true
No issues found!

为了完整起见,这是我的医生输出:

$ flutter doctor -v
[✓] Flutter (Channel stable, v1.17.1, on Linux, locale en_US.UTF-8)
    • Flutter version 1.17.1 at /home/thomas/flutter
    • Framework revision f7a6a7906b (5 days ago), 2020-05-12 18:39:00 -0700
    • Engine revision 6bc433c6b6
    • Dart version 2.8.2


[✓] Android toolchain - develop for Android devices (Android SDK version 28.0.3)
    • Android SDK at /opt/android-sdk
    • Platform android-28, build-tools 28.0.3
    • ANDROID_HOME = /opt/android-sdk
    • ANDROID_SDK_ROOT = /opt/android-sdk
    • Java binary at: /usr/lib/jvm/default/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-b08)
    • All Android licenses accepted.

[!] Android Studio (not installed)
    • Android Studio not found; download from https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for detailed instructions).

[✓] IntelliJ IDEA Community Edition (version 2019.3)
    • IntelliJ at /usr/share/jetbrains-idea-ce
    • Flutter plugin version 44.0.3
    • Dart plugin version 193.6911.31

[✓] Connected device (1 available)
    • FP2 • 1e95f6f3 • android-arm • Android 7.1.2 (API 25)

! Doctor found issues in 1 category.

我还应该做些什么来让 linter 工作吗?

4

1 回答 1

1

啊,所以... linter 工作正常;只是我的假设被打破了。

avoid_empty_else不检查空{}块,而只检查;after else,这就是它没有触发的原因。

avoid_relative_lib_imports 从字面上看,仅检查路径包含/lib/在名称中的相对导入,而不检查其目标解析为内部某个文件的相对导入lib/

真可惜。我希望完全禁止相对进口,但这仍然没有实现

于 2020-05-18T09:24:40.630 回答