Linting Component Usage
Views compose components; components own markup. That single sentence is the premise the whole library rests on, and since 0.8.0 it is something you can enforce rather than merely agree with. This guide covers why the rule exists, how to wire it up for HAML or ERB, exactly what it does and does not flag, and how to adopt it on a codebase that already has a backlog.
1. Why Lint This At All?
A .card written by hand in a view works. That is exactly the problem:
nothing stops it, so it happens — one hurried template at a time — until
the app has two ways of rendering every component. The hand-rolled copies
miss the accessibility attributes the component adds, drift from it as
DaisyUI renames classes between releases, and cannot pick up improvements
(a new slot, a Stimulus action, a Turbo option) without being found and
rewritten individually.
The library exists so that the markup for a card lives in one place.
Every daisy_card call is a promise that the view does not know, or care,
what classes a card is made of. The linter turns that promise into a
build step.
.card.bg-base-100.shadow
.card-body
%h2.card-title Order #1042
%p Shipped yesterday.
.card-actions.justify-end
%a.btn.btn-primary{ href: order_path(order) } View= daisy_card(title: "Order #1042", css: "bg-base-100 shadow") do |card|
%p Shipped yesterday.
- card.with_actions(css: "justify-end") do
= daisy_button("View", css: "btn-primary", href: order_path(order))Run against the Before column, the rule reports the .card, the
.card-body, and the .btn, and each message names the helper to reach
for. The After column passes — and notice that bg-base-100, shadow,
justify-end, and btn-primary are all still there. Utilities and
modifiers belong in the view; css: is how you hand them to the helper.
2. Setup
The rule ships inside the gem as a linter for haml_lint and one for erb_lint. Both read the same derived class map, so HAML and ERB views are held to identical rules.
haml_lint and erb_lint are development dependencies of the gem, and
development dependencies do not propagate to the apps that install it.
Add whichever one you need to your own Gemfile.
Add the gem, then register the rule in .haml-lint.yml. The
require: entry is a plain Ruby require, resolved from the gem's
load path, so there is nothing to copy into your project.
# Gemfile
group :development do
gem "haml_lint", require: false
end# .haml-lint.yml
require:
- loco_motion/lint/haml_lint/component_usage
linters:
LocoMotionComponentUsage:
enabled: trueRun it on its own with --include-linter, or drop that flag to run
it alongside whatever haml_lint rules you already use.
bundle exec haml-lint --include-linter LocoMotionComponentUsage app/views
erblint loads custom linters from a directory rather than a
config key, so the registration is a one-line shim in
`.erblinters/plus the usual.erb_lint.yml` entry.
# Gemfile
group :development do
gem "erb_lint", require: false
end# .erb_linters/loco_motion.rb
require "loco_motion/lint/erb_lint/component_usage"# .erb_lint.yml
linters:
LocoMotionComponentUsage:
enabled: truebundle exec erb_lint --lint-all --enable-linters LocoMotionComponentUsage
Either way, the output says what to do rather than merely what is wrong:
bundle exec haml-lint --include-linter LocoMotionComponentUsage app/views
app/views/orders/show.html.haml:12 [W] LocoMotionComponentUsage: `.card` is LocoMotion's daisy_card — call the helper instead of hand-rolling the markup
3. What Is (and Isn't) Flagged
Whether a team trusts a lint rule comes down to its false positives, so
the rule is deliberately narrow. It reads the static classes on a tag
— the .card shorthand and the class: "card" attribute spelling alike —
and reports a class only when it is one a LocoMotion component declares
as its own.
| Markup | Flagged? | Why |
|---|---|---|
.card
| Yes |
|
.card-body
| Yes | Reported as |
.btn.btn-primary
| Yes | Only |
%span.badge-success
| No | A modifier on its own is not hand-rolled markup. |
.flex.gap-4.p-4
| No | Tailwind utilities are never flagged — utilities in a view are correct usage. |
%div{ class: card_classes }
| No | Computed values are not the rule's to judge; guessing would only produce false positives. |
The class list is derived, not hand-maintained. At load time the rule
walks LocoMotion::COMPONENTS and pairs each component with the classes
its own add_css calls declare, so a component and the rule covering it
cannot drift apart: a component added in a release is enforced by that
release. (The hand-written list this replaced had quietly fallen to 25 of
70 components.) You can inspect the map from a Rails console:
require "loco_motion/lint/component_map"
LocoMotion::Lint::ComponentMap.build.slice("card", "card-body", "drawer-side")
{"card" => "daisy_card", "card-body" => "daisy_card (body part)", "drawer-side" => "daisy_drawer (nested part)"}
4. Adopting on an Existing Codebase
Turn the rule on in an app that has been around for a while and you will get a backlog. That is fine — the point is to stop the backlog growing while it drains, not to convert everything in one sitting. Two ratchets work well.
Lint only what changed. Hold new and edited views to the rule and let the rest wait their turn. A pre-commit hook or CI step can feed the changed HAML files straight to the linter:
git diff --name-only --diff-filter=AMR origin/main... -- '*.haml' | xargs -r bundle exec haml-lint --include-linter LocoMotionComponentUsage
This is what LocoMotion's own repository does — see
.highball/bin/haml-lint-changed,
which runs the rule against every HAML file an editing session touches.
Exclude the backlog by file. List today's offenders under exclude:
and delete each line as you convert that view. Your own component
templates belong on this list permanently: components own their markup,
so raw DaisyUI classes are exactly right there.
# .haml-lint.yml
linters:
LocoMotionComponentUsage:
enabled: true
exclude:
# Components own their markup — raw classes are correct here.
- "app/components/**/*.haml"
# Backlog. Delete each line as the view is converted.
- "app/views/orders/show.html.haml"
- "app/views/admin/**/*.haml"RuboCop's TODO file excludes offenders file by file. haml_lint's
--auto-gen-config only does that while a rule has offences in 15 or
fewer files (the --auto-gen-exclude-limit default). Past that — which
is what a real backlog looks like — it writes enabled: false for the
rule instead, switching it off wholesale. Raise the limit
(--auto-gen-exclude-limit 500) or write the exclude list yourself.
5. Deliberate Exceptions
Sometimes the raw class is the point — a docs page demonstrating what
.stack does, or a snippet the design team asked to see verbatim. Suppress
the rule the usual way for your template language, and say why in the
same breath. A suppression without a reason is indistinguishable from
someone who gave up.
-# The raw class IS this example: it shows the CSS `stack` used directly.
-# haml-lint:disable LocoMotionComponentUsage
.stack.gap-3.w-full
%img{ src: image_path("one.jpg") }
%img{ src: image_path("two.jpg") }
-# haml-lint:enable LocoMotionComponentUsage<%# The raw class IS this example: it shows the CSS `stack` used directly. %>
<div class="stack gap-3 w-full"><%# erb_lint:disable LocoMotionComponentUsage %>
<%= image_tag "one.jpg" %>
<%= image_tag "two.jpg" %>
</div>If the reason you are reaching for a suppression is "the component can't
render what I need", that is a gap in the component, not an exception to
the rule. Extend it — the
Building Components guide covers
parts, slots, and the css: / html: options — or
open an issue so
the fix lands for everyone.
Success!
The rule is in place and every new view is held to the premise the library is built on. A quick checklist before you move on: