diff --git a/config/default.yml b/config/default.yml index 6d6db4e..02d76c1 100644 --- a/config/default.yml +++ b/config/default.yml @@ -102,6 +102,10 @@ linters: enabled: false forbidden_tags: [] + TagAttribute: + enabled: false + forbidden_attributes: [] + TagCase: enabled: true diff --git a/lib/slim_lint/linter/README.md b/lib/slim_lint/linter/README.md index 944e5d8..1453af6 100644 --- a/lib/slim_lint/linter/README.md +++ b/lib/slim_lint/linter/README.md @@ -16,6 +16,7 @@ Below is a list of linters supported by `slim-lint`, ordered alphabetically. * [StrictLocalsMissing](#strictlocalsmissing) * [Tab](#tab) * [Tag](#tag) +* [TagAttribute](#tagattribute) * [TagCase](#tagcase) * [TrailingBlankLines](#trailingblanklines) * [TrailingWhitespace](#trailingwhitespace) @@ -343,6 +344,28 @@ p Something P Something else ``` +## TagAttribute + +Reports forbidden tag attribute if listed. + +Option | Description +-------|----------------------------------------------------------------- +`forbidden_attributes` | List of forbidden tag attributes. (default []) + +```yaml +linters: + TagAttribute: + enabled: true + forbidden_attributes: + - style +``` + +**Bad for above configuration** +```slim +p style="{ color: red; }" Something +P STYLE="{ color: blue; }" Something else +``` + ## TagCase Reports tag names with uppercase characters. diff --git a/lib/slim_lint/linter/tag_attribute.rb b/lib/slim_lint/linter/tag_attribute.rb new file mode 100644 index 0000000..08f0b3f --- /dev/null +++ b/lib/slim_lint/linter/tag_attribute.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +module SlimLint + # Checks for forbidden tag attributes. + class Linter::TagAttribute < Linter + include LinterRegistry + + on [:html, :attr] do |sexp| + _, _, name = sexp + + forbidden_attributes = config['forbidden_attributes'] + forbidden_attributes.each do |forbidden_attribute| + next unless name[/^#{forbidden_attribute}$/i] + + report_lint(sexp, "Forbidden tag attribute `#{name}` found") + end + end + end +end diff --git a/spec/slim_lint/linter/tag_attribute_spec.rb b/spec/slim_lint/linter/tag_attribute_spec.rb new file mode 100644 index 0000000..d5cc76e --- /dev/null +++ b/spec/slim_lint/linter/tag_attribute_spec.rb @@ -0,0 +1,37 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe SlimLint::Linter::TagAttribute do + include_context 'linter' + + context 'when a lowercased attribute is contained in forbidden_attributes' do + let(:config) do + { 'forbidden_attributes' => %w[style] } + end + + let(:slim) { 'p style="{ color: red; }"' } + + it { should report_lint line: 1 } + end + + context 'when an uppercased attribute is contained in forbidden_attributes' do + let(:config) do + { 'forbidden_attributes' => %w[style] } + end + + let(:slim) { 'P STYLE="{ color: red; }"' } + + it { should report_lint line: 1 } + end + + context 'when an attribute is not contained in forbidden_attributes' do + let(:config) do + { 'forbidden_attributes' => %w[style] } + end + + let(:slim) { 'style media="all"' } + + it { should_not report_lint } + end +end