erbfmt

Reference

設定

erbfmt.jsonで利用できる全項目、指定できる値、defaultの挙動をまとめています。

設定ファイル

erbfmt initを実行すると、current directoryにdefault configを作成します。既存ファイルを置き換える場合は erbfmt init --forceを使います。

erbfmt init
erbfmt init --force

--configを指定しない場合、current directoryから親directoryへ向かって erbfmt.jsonを検索します。明示したpathが常に優先されます。

erbfmt --config path/to/erbfmt.json app/views/users/show.html.erb

公開schemaを追加すると、editorの補完とvalidationを利用できます。

{
  "$schema": "https://raw.githubusercontent.com/hinamimi/erbfmt/main/docs/schema/erbfmt.schema.json"
}

全項目の例

{
  "$schema": "https://raw.githubusercontent.com/hinamimi/erbfmt/main/docs/schema/erbfmt.schema.json",
  "files": {
    "includes": ["**/*.html.erb", "!vendor/**"]
  },
  "parser": {
    "allowHtmlOptionalClosingTags": false
  },
  "formatter": {
    "enabled": true,
    "indentStyle": "space",
    "indentWidth": 2,
    "indentHtml": true,
    "lineEnding": "lf",
    "lineWidth": 80,
    "trailingNewline": true
  },
  "linter": {
    "enabled": true,
    "rules": {
      "recommended": true,
      "emptyErbBranch": "error",
      "emptyErbCodeTag": "error",
      "emptyErbControlBlock": "error",
      "noDeprecatedHtmlTag": "error",
      "noDuplicateHtmlAttribute": "error",
      "noInvalidHtmlBooleanAttribute": "error",
      "noInvalidHtmlNesting": "error",
      "noNonDoubleQuotedHtmlAttributeValue": "error",
      "noSelfClosingHtmlTag": "error",
      "unsupportedErbBlockStarter": "error"
    }
  }
}

Files

files.includesglob patternのarray

format、check、lintの前にCLI targetを絞り込みます。可能な場合、patternはconfig fileのdirectoryからの相対pathとして評価します。除外には ! を使います。例: ["**/*.html.erb", "!vendor/**"]。対応wildcardは *?**です。

Parser

parser.allowHtmlOptionalClosingTagsboolean · default: false

</li></p></td></tr>など、HTMLで省略可能なclosing tagを許容します。defaultではerbfmtは省略されたclosing tagをparse errorとして扱います。有効にした場合、省略形を受け入れますが、formatterがclosing tagを自動挿入することはありません。

Formatter

formatter.enabledboolean · default: true

format全体を有効または無効にします。無効な場合、formatter modeはsource textを変更しません。

formatter.indentStyle"space" | "tab" · default: "space"

erbfmtが生成するindentにspaceまたはtab characterを使います。

formatter.indentWidth1以上のinteger · default: 2

indentStylespaceの場合に、indent 1 levelで使うspace数を指定します。

formatter.indentHtmlboolean · default: true

nested HTML elementに合わせてcontentをindentします。無効にしてもERB control-flowのindentは維持されます。

formatter.lineEnding"lf" | "crlf" · default: "lf"

format結果のline endingをUnix LFまたはWindows CRLFから選びます。

formatter.lineWidth1以上のinteger · default: 80

目標line widthを指定します。長いHTML opening tagはattributeごとに展開されます。format-sensitive subtreeはcontentを保持しますが、opening tagのattributeは折り返すことがあります。standalone ERB tagはmarkerを別行へ移し、安全に認識できるmethod callはtop-level argumentごとに折り返すことがあります。曖昧なRuby式は変更しません。

formatter.trailingNewlineboolean · default: true

format結果の末尾へ1つのnewlineを追加します。defaultは通常のsource file conventionsに合わせています。周囲のtextへinline partial fragmentとして差し込むERB fileで、末尾newlineがvisible whitespaceになる場合は falseを指定します。

Linter

linter.enabledboolean · default: true

すべてのlint diagnosticsを有効または無効にします。

linter.rules.recommendedboolean · default: true

個別指定していないruleのdefault stateを制御します。trueでは未指定ruleをerrorとして有効にし、falseでは無効にします。

個別ruleには "off""warn""error"を指定できます。warnは表示されますが erbfmt --lintを失敗させません。errorはnonzero exit statusになります。

Lint rules

emptyErbBranchoff | warn | error

空のERB branchを検出します。 elseelsifwhenrescueensureなどに意味のあるcontentがない場合に報告します。

emptyErbCodeTagoff | warn | error

空のcode/output tagを検出します。 Ruby式を含まない <% %><%= %>を報告します。

emptyErbControlBlockoff | warn | error

空のcontrol-flow blockを検出します。 対応しているERB block openerと、それに対応する endの間に意味のあるbodyがない場合に報告します。

noDeprecatedHtmlTagoff | warn | error

非推奨HTML elementを禁止します。 現在のHTMLとCSSへ置き換えるべきpresentationalまたはobsolete tagを報告します。

noDuplicateHtmlAttributeoff | warn | error

重複attributeを禁止します。 ひとつのHTML opening tagで同じattribute名を複数回指定した場合に報告します。

noInvalidHtmlBooleanAttributeoff | warn | error

boolean attributeの値を検証します。 HTML boolean attributeは値を省略するかattribute自身の名前を使い、任意のstring valueは指定しません。

noInvalidHtmlNestingoff | warn | error

一般的なHTML content modelを検証します。 list child、table構造、paragraph内のblock-level elementを確認します。

noNonDoubleQuotedHtmlAttributeValueoff | warn | error

HTML attribute valueにdouble quoteを要求します。 class=<%= foo %>class='card' のような未quoted/single-quoted valueを報告します。値なしboolean attributeは許可します。

noSelfClosingHtmlTagoff | warn | error

XML形式のself-closing HTML構文を禁止します。 HTML5で通常のopening/closing pair、またはslashなしのvoid elementを使う場所の />を報告します。

unsupportedErbBlockStarteroff | warn | error

未対応のblock starterを検出します。 ERB blockに見えるものの、formatterの対応範囲外にあるRuby control-flow formを報告します。