invalid-attr
属性が仕様上(あるいは独自に指定したルール上)、存在しない属性であったり、無効な型の値だった場合に警告をします。
HTML Living Standardに準拠します。@markuplint/html-spec
に設定値を持っています。
❌ 間違ったコード例
<div unexist-attr>
<button tabindex="non-integer">The Button</button>
<a href="/" referrerpolicy="invalid-value">The Anchor</a>
</div>
✅ 正しいコード例
<div>
<button tabindex="0">The Button</button>
<a href="/" referrerpolicy="no-referrer">The Anchor</a>
</div>
注記
このルールは条件によってはスプレッド属性をもつ要素は評価しません。例えば、href
属性を持たないa
要素はtarget
属性が許可されていませんが、スプレッド属性にhref
が含まれているかMarkuplintは知ることができないため評価できません。
const Component = (props) => {
return <a target="_blank" {...props}>;
}
Interface
{
"invalid-attr": boolean
}
Options
{
"invalid-attr": {
"options": {
"allowAttrs"?: (string | Attr)[] | Record<string, ValueRule>
"disallowAttrs"?: (string | Attr)[] | Record<string, ValueRule>
"ignoreAttrNamePrefix"?: string | string[]
"allowToAddPropertiesForPretender"?: boolean
"attrs"?: Object
}
}
}
type Attr = {
name: string;
value: AttributeType | ValueRule;
}
type ValueRule =
| { enum: [string, ...string[]]; }
| { pattern: string; }
| { type: AttributeType; };
AttributeType
はタイプAPIを参照してください。
Property | Type | Default Value | Description |
---|---|---|---|
allowAttrs | (string | | undefined | 許可する属性を指定します。これは、HTML標準に存在しない属性をあえて指定したい場合や、フレームワークなどで必要な属性を警告されないようにするものです。属性名だけの指定もできますし、属性値に対してのパターンやデータ型の指定が可能です。 |
disallowAttrs | (string | | undefined | 許可しない属性を指定します。HTML標準で許可されていたとしても、あえてプロジェクトのルールで禁止するような場合に利用します。指定内容はallowAttrs と同じ形式を受け取りますが、その意味はすべて逆になります。 |
ignoreAttrNamePrefix | string | | undefined | HTMLの仕様には存在しない、Viewライブラリやテンプレートエンジン固有の属性およびディレクティブを除外するために、プレフィックスを設定します。 |
allowToAddPropertiesForPretender | boolean | "true" | HTML要素に偽装しているコンポーネントのプロパティを追加できるようにします。デフォルトはtrue です。pretenders オプションを使用している場合にfalse に設定されていると、存在しない属性が見つかると警告します。 |
attrs | Object | undefined | [非推奨(v3.7.0より): allowAttrs かdisallowAttrs を利用してください] enum pattern type disallowed のいずれかで設定します。 |
Default Severity
error