-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a `Tag` linter to forbid the use of the specified tag. The following use cases are considered. * Forbid `style` tags and use the `css` embedded engine or css file instead. * Forbid the `a` tags and use Rails' `link_to` helper instead.
- Loading branch information
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -98,6 +98,10 @@ linters: | |
Tab: | ||
enabled: true | ||
|
||
Tag: | ||
enabled: false | ||
forbidden_tags: [] | ||
|
||
TagCase: | ||
enabled: true | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |