🐈

analysis_options.yamlってコメントアウトは何を書いてるの?

に公開

プロジェクト立ち上げ時に analysis_options.yaml のコメントアウトは何を書いているのか、気になったので調べながら記事を書きます。

analysis_options.yaml とは何か

analysis_options.yaml は Dart の静的解析ツールを設定するためのファイルです。

静的解析とは?

プログラムを実行せずにコードを調べ、エラーや問題点を見つけ出す方法です。

コメントの解説

冒頭のコメント

# This file configures the analyzer, which statically analyzes Dart code to
# check for errors, warnings, and lints.
#
# The issues identified by the analyzer are surfaced in the UI of Dart-enabled
# IDEs (https://dart.dev/tools#ides-and-editors). The analyzer can also be
# invoked from the command line by running `flutter analyze`.
  1. このファイルは Dart コードを静的に解析してエラー、警告、リントをチェックするアナライザーを設定するもの
  2. 解析で見つかった問題は Dart 対応の IDE(統合開発環境)の UI に表示される
  3. コマンドラインで flutter analyze を実行することでもアナライザーを使える

flutter analyze の検証

新規プロジェクトでコマンドを実行すると、

$ flutter analyze
Analyzing sample2025_03_25...
No issues found! (ran in 0.7s)

何も Issues に表示されません。

ですが、プリント文の利用を Lint でチェックするように設定して、プリント文を実装した状態でコマンドを実行すると、

$ flutter analyze
Analyzing sample2025_03_25...

info • Don't invoke 'print' in production code • lib/main.dart:61:5 •
avoid_print

1 issue found. (ran in 1.0s)

1つの issue が見つかりました。このようにコードを静的解析ができます。

ですが、わざわざコマンドを打たなくても、VSCode であれば問題のタブ部分で検知されるため、ここで確認してもよいです。

include についてのコメント

# The following line activates a set of recommended lints for Flutter apps,
# packages, and plugins designed to encourage good coding practices.
include: package:flutter_lints/flutter.yaml

このコメントは:

  • include: package:flutter_lints/flutter.yaml の行によって、Flutter アプリ、パッケージ、プラグインに推奨されるリントのセットが有効になることを説明しています
  • これらのリントは良いコーディング習慣を促進するために設計されています

この宣言だけで Flutter チームが推奨する Lint ルールが適用されます。

リンターの設定コメント

linter:
  # The lint rules applied to this project can be customized in the
  # section below to disable rules from the `package:flutter_lints/flutter.yaml`
  # included above or to enable additional rules. A list of all available lints
  # and their documentation is published at https://dart.dev/lints.
  #
  # Instead of disabling a lint rule for the entire project in the
  # section below, it can also be suppressed for a single line of code
  # or a specific dart file by using the `// ignore: name_of_lint` and
  # `// ignore_for_file: name_of_lint` syntax on the line or in the file
  # producing the lint.
  rules:
    # avoid_print: false  # Uncomment to disable the `avoid_print` rule
    # prefer_single_quotes: true  # Uncomment to enable the `prefer_single_quotes` rule

このセクションのコメントは以下のことを説明しています:

  1. リントルールをカスタマイズする方法について

    • package:flutter_lints/flutter.yaml からのルールを無効にしたり
    • 追加のルールを有効にしたりできる
  2. 利用可能なすべてのリントとその説明は https://dart.dev/lints で公開されている

  3. プロジェクト全体でリントルールを無効にする代わりに、特定の行やファイルで抑制することも可能

    • // ignore: name_of_lint - 単一行のリントを無視
    • // ignore_for_file: name_of_lint - ファイル全体のリントを無視
  4. コメントアウトされた例:

    • avoid_print: false - print 文の使用に関する警告を無効にする
    • prefer_single_quotes: true - 文字列にシングルクォートを使用するルールを有効にする

Lint の項目の詳細に関しては、記載されています。

https://dart.dev/lints

VSCode 上での実践

上画像のように print 文を実装しており、Lint によるアラートが表示されています。
このアラートを無視したい場合は、

  • 単一行のみ
  • ファイル全体
  • プロジェクト全体

の条件で静的解析を無視することができます。

方法として上画像の電球アイコンを押下することで、クイック修正として上記の三項目が表示されます。その中から目的の条件を選択して無視します。このような機能を用いると効率よく特定の条件でのリントを無視できます。

最後のコメント

# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options

このコメントは、このファイルに関する追加情報を https://dart.dev/guides/language/analysis-options で見つけることができると説明しています。

その他の詳細情報に関しては以下のリンクに記載があります。

https://dart.dev/guides/language/analysis-options

以下のリンク先ですべてのリントを設定するテンプレートがありました。参考までに。

https://dart.dev/tools/linter-rules/all

実際の使用例と説明

例 1: リントルールを有効・無効にする

# リンターのルールをカスタマイズする
linter:
  rules:
    # print文を使用しても警告を出さないようにする
    avoid_print: false

    # シングルクォートを使用するルールを有効にする
    prefer_single_quotes: true

上記の設定をすると:

  • print() 関数を使っても警告が表示されなくなります
  • 文字列リテラルにダブルクォート("text")ではなくシングルクォート('text')を使うよう促されます

まとめ

analysis_options.yaml ファイルのコメントは、Dart と Flutter の静的解析ツールの設定方法を説明しています。このファイルを適切に設定することで:

  1. コードの品質を向上させる
  2. バグを早期に発見する
  3. チーム内でコーディング規約を統一する

などの効果が期待できます。

最初はデフォルト設定のままでも問題ありませんが、プロジェクトが大きくなってきたら、チームの習慣や好みに合わせてカスタマイズしていくと良いでしょう。

Discussion