Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
uroborosql-fmt にフォーマッタとは独立した lint 基盤を追加しました。
仕組みの最小構成(ルール登録、診断収集、テスト、簡易 CLI)までを今回で整備しています。実装済みルールはサンプル的位置づけで、設計が妥当か見てもらうことを目的にしています。
config 解決の仕組みなどは現状では実装していません。
基本的なアーキテクチャ
Linter は SQL をパース → 全ノードをプリオーダー走査 → ルールの target_kinds に一致したノードへ run_on_node を呼び出すという流れでで作成しています。
ルールは
Ruleトレイトを実装する構造体として定義し、Ruletrait に生えているメソッドをを適宜実装するとエンジン側で呼び出されて実行されるという流れです。各ルールで検出された違反は
Diagnostic(診断)としてLintContextが集約します。ルールの実装イメージ
ルール作者は
target_kinds()で監視したいSyntaxKindを宣言し、run_on_node()内に対象ノードの検査処理を書きます。診断はctx.report()を呼び出して返します。サンプルとして以下3つのルールを実装しています:
no-distinctSyntaxKind::DISTINCTがある場合に呼び出され、DISTINCT 使用を警告するno-union-distinctSyntaxKind::UNIONで呼び出され、、UNION ALL でないパターンの結合であれば警告するtoo-large-in-listSyntaxKind::in_exprで呼び出され、式の個数を数えて 100 件超なら警告するテスト
ルールごとにユニットテストを用意し、該当ルールだけ有効化した Lint を通じて単体検証を実施しています。
現状では素直に Rust のテストを使っています。
CLI について
uroborosql-lint-cli を追加しました。設定ファイルは未対応かつファイルは1つ限定です。
fmt のCLI と将来的には統合すると思いますが、まずは別で用意しています。
SQL ファイルを受け取り、診断を stdout に
path:line:column: rule_id: message形式で表示します。現状はシンプルなテキスト出力で、ハイライトなどのリッチなアノテーションは未作成です。