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.includesformat、check、lintの前にCLI targetを絞り込みます。可能な場合、patternはconfig fileのdirectoryからの相対pathとして評価します。除外には
!を使います。例:["**/*.html.erb", "!vendor/**"]。対応wildcardは*、?、**です。
Parser
parser.allowHtmlOptionalClosingTags</li>、</p>、</td>、</tr>など、HTMLで省略可能なclosing tagを許容します。defaultではerbfmtは省略されたclosing tagをparse errorとして扱います。有効にした場合、省略形を受け入れますが、formatterがclosing tagを自動挿入することはありません。
Formatter
formatter.enabledformat全体を有効または無効にします。無効な場合、formatter modeはsource textを変更しません。
formatter.indentStyleerbfmtが生成するindentにspaceまたはtab characterを使います。
formatter.indentWidthindentStyleがspaceの場合に、indent 1 levelで使うspace数を指定します。formatter.indentHtmlnested HTML elementに合わせてcontentをindentします。無効にしてもERB control-flowのindentは維持されます。
formatter.lineEndingformat結果のline endingをUnix LFまたはWindows CRLFから選びます。
formatter.lineWidth目標line widthを指定します。長いHTML opening tagはattributeごとに展開されます。format-sensitive subtreeはcontentを保持しますが、opening tagのattributeは折り返すことがあります。standalone ERB tagはmarkerを別行へ移し、安全に認識できるmethod callはtop-level argumentごとに折り返すことがあります。曖昧なRuby式は変更しません。
formatter.trailingNewlineformat結果の末尾へ1つのnewlineを追加します。defaultは通常のsource file conventionsに合わせています。周囲のtextへinline partial fragmentとして差し込むERB fileで、末尾newlineがvisible whitespaceになる場合は
falseを指定します。
Linter
linter.enabledすべてのlint diagnosticsを有効または無効にします。
linter.rules.recommended個別指定していないruleのdefault stateを制御します。
trueでは未指定ruleをerrorとして有効にし、falseでは無効にします。
個別ruleには "off"、"warn"、"error"を指定できます。warnは表示されますが erbfmt --lintを失敗させません。errorはnonzero exit statusになります。
Lint rules
emptyErbBranch空のERB branchを検出します。
else、elsif、when、rescue、ensureなどに意味のあるcontentがない場合に報告します。emptyErbCodeTag空のcode/output tagを検出します。 Ruby式を含まない
<% %>や<%= %>を報告します。emptyErbControlBlock空のcontrol-flow blockを検出します。 対応しているERB block openerと、それに対応する
endの間に意味のあるbodyがない場合に報告します。noDeprecatedHtmlTag非推奨HTML elementを禁止します。 現在のHTMLとCSSへ置き換えるべきpresentationalまたはobsolete tagを報告します。
noDuplicateHtmlAttribute重複attributeを禁止します。 ひとつのHTML opening tagで同じattribute名を複数回指定した場合に報告します。
noInvalidHtmlBooleanAttributeboolean attributeの値を検証します。 HTML boolean attributeは値を省略するかattribute自身の名前を使い、任意のstring valueは指定しません。
noInvalidHtmlNesting一般的なHTML content modelを検証します。 list child、table構造、paragraph内のblock-level elementを確認します。
noNonDoubleQuotedHtmlAttributeValueHTML attribute valueにdouble quoteを要求します。
class=<%= foo %>やclass='card'のような未quoted/single-quoted valueを報告します。値なしboolean attributeは許可します。noSelfClosingHtmlTagXML形式のself-closing HTML構文を禁止します。 HTML5で通常のopening/closing pair、またはslashなしのvoid elementを使う場所の
/>を報告します。unsupportedErbBlockStarter未対応のblock starterを検出します。 ERB blockに見えるものの、formatterの対応範囲外にあるRuby control-flow formを報告します。