Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose component linting api #86

Merged
merged 3 commits into from
Jan 28, 2023
Merged

expose component linting api #86

merged 3 commits into from
Jan 28, 2023

Conversation

roman01la
Copy link
Collaborator

Exposes UIx component linter

Usage

(ns app.code
  ;; have to require .clj ns with a linter so that it gets registered
  (:require-macros [app.my-linter]))
;; app/my-linter.clj
(ns app.my-linter
  (:require
    [cljs.analyzer :as ana]
    [uix.linter]))

(defn lint-a11y-button [[_ el attrs & children :as form]]
  (cond
    ;; no attrs
    (not (map? attrs))
    ;; points to an element since there's no attrs map
    (uix.linter/add-error! form :a11y/aria-label (uix.linter/form->loc form))

    ;; no :aria-label in attrs
    (nil? (:aria-label attrs))
    ;; points to attrs map
    (uix.linter/add-error! form :a11y/aria-label (uix.linter/form->loc attrs))))

(defn lint-element [form]
  (let [[_ el attrs & children] form]
    (case el
      :button (lint-a11y-button form) ;; lints a `button` element
      nil)))

(defn lint-aria [expr]
  ;; walks component's body
  (clojure.walk/prewalk
    (fn [form]
      (when (uix.linter/uix-element? form)
        (lint-element form)) ;; lints `($ ...)` element
      form)
    expr))

;; register your linter
(defmethod uix.linter/lint-component :a11y/element [_ sym body env]
  ;; `body` is a seq of top level expressions in a `defui` component
  (run! lint-aria body))

;; register error printers
(defmethod ana/error-message :a11y/aria-label [_ {:keys [source] :as v}]
  (let [[_ el] source]
    (str "The " el " is missing aria-label attribute")))

An example of the error message
Screenshot 2023-01-27 at 3 25 37 PM

@roman01la roman01la merged commit c92e593 into master Jan 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

1 participant