Skip to content

Commit

Permalink
Inherit from BaseLinter for HTML class checking
Browse files Browse the repository at this point in the history
BaseLinter already has class checking and exception count functionality.

We did have to modify BaseLinter to allow an empty `TAGS` array
constant, thereby checking all tags.
  • Loading branch information
neall committed Feb 10, 2023
1 parent 1dd03b3 commit 281d8d6
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/primer/view_components/linters/base_linter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def run(processed_source)

tags.each do |tag|
next if tag.closing?
next unless self.class::TAGS&.include?(tag.name)
next if self.class::TAGS&.none?(tag.name)

classes = tag.attributes["class"]&.value&.split(" ") || []
tag_tree[tag][:offense] = false
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

require_relative "base_linter"

# Load all the other linters so we can filter out their restricted
# CLASSES—they will be responsible for complaining about the use of
# those HTML classes.
Dir[File.join(__dir__, "*.rb")].sort.each do |file|
require file unless file == __FILE__
end

module ERBLint
module Linters
# Counts the number of times a class reserved for ViewComponents is used
class ViewComponentHtmlClassesCounter < BaseLinter
CLASSES = (
JSON.parse(
File.read(
File.join(__dir__, "..", "..", "..", "..", "static", "classes.json")
)
) - BaseLinter.subclasses.reduce([]) do |html_classes, klass|
html_classes.concat(klass.const_get(:CLASSES))
end
).freeze

TAGS = nil
MESSAGE = "Primer ViewComponents defines some HTML classes with associated styles that should not be used outside those components. (These classes might have their styles changed or even disappear in the future.) Instead of using this class directly, please use its component if appropriate or define the styles you need some other way."
end
end
end
8 changes: 5 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,14 @@
"@github/mini-throttle": "^2.1.0",
"@github/relative-time-element": "^4.0.0",
"@github/tab-container-element": "^3.1.2",
"@primer/behaviors": "^1.2.0",
"css-what": "^6.1.0"
"@primer/behaviors": "^1.2.0"
},
"devDependencies": {
"@changesets/changelog-github": "^0.4.6",
"@changesets/cli": "^2.24.1",
"@github/browserslist-config": "^1.0.0",
"@github/markdownlint-github": "^0.2.2",
"@github/prettier-config": "0.0.4",
"@github/markdownlint-github": "^0.2.2",
"@playwright/test": "^1.27.1",
"@primer/css": "20.4.7",
"@primer/primitives": "^7.9.0",
Expand Down

0 comments on commit 281d8d6

Please sign in to comment.