简体   繁体   English

Flutter Dart 分析器不应用我的 linter 规则

[英]Flutter Dart analyzer not applying my linter rules

I'm working on a pretty standard Flutter project.我正在做一个非常标准的 Flutter 项目。 Because I'm new to Flutter and Dart, I'd like my tools to be as helpful as possible.因为我是 Flutter 和 Dart 的新手,所以我希望我的工具尽可能有用。 So I added pedantic: ^1.9.0 to dev_dependencies and wrote analysis_options.yaml like this:所以我添加pedantic: ^1.9.0dev_dependencies并这样写analysis_options.yaml

include: package:pedantic/analysis_options.yaml

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

According to https://dart-lang.github.io/linter/lints/ , pedantic should enable the avoid_empty_else and avoid_relative_lib_imports lints, among others, as errors.根据https://dart-lang.github.io/linter/lints/pedantic应该启用avoid_empty_elseavoid_relative_lib_imports lints,作为错误。 But when I write code like:但是当我编写如下代码时:

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

or this:或这个:

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

I'm not getting any errors in IntelliJ IDEA, nor when I run flutter analyze manually:我在 IntelliJ IDEA 中没有收到任何错误,手动运行flutter analyze时也没有收到任何错误:

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

I've tried enabling these lints explicitly:我已经尝试明确启用这些 lints:

linter:
  rules:
    - avoid_empty_else
    - avoid_relative_lib_imports

This doesn't make any difference.这没有任何区别。

I've tried adding a nonexistent lint foo to that list to verify that the file is being used, and it is:我已经尝试在该列表中添加一个不存在的 lint foo以验证该文件是否正在使用,它是:

$ 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)

I even tried running the dartanalyzer from the Flutter installation directory directly, with all verbosity options that I could find:我什至尝试直接从 Flutter 安装目录运行dartanalyzer ,使用我能找到的所有详细选项:

$ ~/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!

For completeness, here's my doctor output:为了完整起见,这是我的医生 output:

$ 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.

Is there something else I should do to get the linter to work?我还应该做些什么来让 linter 工作吗?

Ah, so... the linter is working fine;啊,所以... linter 工作正常; it was just my assumptions that were broken.只是我的假设被打破了。

avoid_empty_else does not check for empty {} blocks, but only for ; avoid_empty_else不检查空的{}块,但只检查; right after else , which is why it didn't trigger.就在else之后,这就是它没有触发的原因。

avoid_relative_lib_imports literally only checks for relative imports whose path contains /lib/ in the name, not for relative imports whose target resolves to some file inside lib/ . avoid_relative_lib_imports 从字面上只检查名称中路径包含/lib/的相对导入,而不检查其目标解析为lib/内某个文件的相对导入。

Bummer.无赖。 I was hoping to forbid relative imports altogether, but that's still unimplemented .我希望完全禁止相对导入,但仍未实现

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM