33
24

More than 3 years have passed since last update.

Denoでlintする! Deno@v1.1.0で実装されたdeno lintコマンドの紹介

Last updated at Posted at 2020-06-14

はじめに

Denoのv1.1.0がリリースされました。
本記事では、その新機能であるdeno lintコマンドについて紹介します。

deno lintコマンドについて

  • linterがDenoに組み込まれています。そのため、Denoがインストールされていればすぐに使用できます。
  • Rustで記述されているためとても速いです。
    • deno_lintREADME.mdにベンチマークの結果が記載されています。詳しくはそちらを参照ください。
  • 現時点では、eslintほどの機能や拡張性はありません

使い方

deno lintコマンドはまだunstableという扱いなので、使用する際は--unstableフラグが必要です。

指定したファイルに対してlintを実行する

$ deno lint --unstable ./application.ts ./mod.ts

カレントディレクトリ配下から再帰的にファイルを探してlintを実行する

$ deno lint --unstable

指定したディレクトリ配下から再帰的にファイルを探してlintを実行する

$ deno lint --unstable ./tests

出力例

ルール違反があると、以下のように出力されます。

(no-explicit-any) `any` type is not allowed
  evaluate(expression: string): Promise<any>;
                                        ~~~
    at ./application.ts:5:40

(no-explicit-any) `any` type is not allowed
  exposeFunction(name: string, binding: (...args: any[]) => any): Promise<void>;
                                                  ~~~
    at ./application.ts:7:50

(no-explicit-any) `any` type is not allowed
  exposeFunction(name: string, binding: (...args: any[]) => any): Promise<void>;
                                                            ~~~
    at ./application.ts:7:60

(no-explicit-any) `any` type is not allowed
    evaluate(expression: string): Promise<any> {
                                          ~~~
    at ./application.ts:18:42

(no-explicit-any) `any` type is not allowed
    exposeFunction(name: string, binding: (...args: any[]) => any) {
                                                    ~~~
    at ./application.ts:24:52

(no-explicit-any) `any` type is not allowed
    exposeFunction(name: string, binding: (...args: any[]) => any) {
                                                              ~~~
    at ./application.ts:24:62

Found 6 problems

ルール

eslint同様、いくつかのルールが定義されています。

ルール 説明
no-explicit-any 明示的なanyの使用を許可しない
constructor-super サブクラスのconstructorsuper()を呼ばなければならない
no-delete-var 変数のdeleteを許可しない
no-this-alias thisの他の変数への割当(const self = this;)を許可しない
prefer-namespace-keyword moduleキーワードではなくnamespaceキーワードの使用を推奨する

ルールを一覧表示する

以下のコマンドを実行すると、サポートされているルールを一覧表示できます。

$ deno lint --unstable --rules

ディレクティブ

linterの挙動を制御するためのディレクティブがいくつか提供されています。

deno-lint-ignore-file

このディレクティブをファイルの先頭付近で宣言しておくと、そのファイルはlint対象から除外されます。
import宣言や関数定義よりも前に指定する必要があります。

// deno-lint-ignore-file

deno-lint-ignore

eslintのeslint-disable-next-lineに相当します。
特定のルールを無効化したいときに使います。

export interface Evaluator {
  // deno-lint-ignore no-explicit-any
  evaluate(expression: string): Promise<any>;
}

deno lintでできないこと

以下のような機能はまだサポートされていません。

  • 設定ファイル
  • 適用するルールのカスタマイズ

おわりに

deno lintコマンドが利用しているリンターについては、deno_lintリポジトリにて開発が行われています。
このリポジトリでは、毎日のように活発にコミットが行われており、これからもどんどん拡張されていくと思われます。

興味があれば、ぜひ試してみてください。

参考

33
24
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
33
24