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

Add Tag linter #179

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ linters:
Tab:
enabled: true

Tag:
enabled: false
forbidden_tags: []

TagCase:
enabled: true

Expand Down
23 changes: 23 additions & 0 deletions lib/slim_lint/linter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Below is a list of linters supported by `slim-lint`, ordered alphabetically.
* [RuboCop](#rubocop)
* [StrictLocalsMissing](#strictlocalsmissing)
* [Tab](#tab)
* [Tag](#tag)
* [TagCase](#tagcase)
* [TrailingBlankLines](#trailingblanklines)
* [TrailingWhitespace](#trailingwhitespace)
Expand Down Expand Up @@ -320,6 +321,28 @@ for the template that shows which local variables it accepts.

Reports detection of tabs used for indentation.

## Tag

Reports forbidden tag if listed.

Option | Description
-------|-----------------------------------------------------------------
`forbidden_tags` | List of forbidden tags. (default [])

```yaml
linters:
Tag:
enabled: true
forbidden_tags:
- p
```

**Bad for above configuration**
```slim
p Something
P Something else
```

## TagCase

Reports tag names with uppercase characters.
Expand Down
19 changes: 19 additions & 0 deletions lib/slim_lint/linter/tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

module SlimLint
# Checks for forbidden tags.
class Linter::Tag < Linter
include LinterRegistry

on [:html, :tag] do |sexp|
_, _, name = sexp

forbidden_tags = config['forbidden_tags']
forbidden_tags.each do |forbidden_tag|
next unless name[/^#{forbidden_tag}$/i]

report_lint(sexp, "Forbidden tag `#{name}` found")
end
end
end
end
37 changes: 37 additions & 0 deletions spec/slim_lint/linter/tag_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

require 'spec_helper'

describe SlimLint::Linter::Tag do
include_context 'linter'

context 'when a lowercased tag is contained in forbidden_tags' do
let(:config) do
{ 'forbidden_tags' => %w[a] }
end

let(:slim) { 'a href="/foo"' }

it { should report_lint line: 1 }
end

context 'when an uppercased tag is contained in forbidden_tags' do
let(:config) do
{ 'forbidden_tags' => %w[a] }
end

let(:slim) { 'A href="/foo"' }

it { should report_lint line: 1 }
end

context 'when a tag is not contained in forbidden_tags' do
let(:config) do
{ 'forbidden_tags' => %w[a] }
end

let(:slim) { 'textarea name="foo"' }

it { should_not report_lint }
end
end