diff --git a/.haml-lint.yml b/.haml-lint.yml index 75139cb43dd9d230f85775a1e4a8deab94a2f70b..c0c0e57e0f278bef32e16671ccc6755726659658 100644 --- a/.haml-lint.yml +++ b/.haml-lint.yml @@ -8,8 +8,9 @@ exclude: - 'spec/**/*' - 'ee/spec/**/*' require: - - './haml_lint/linter/no_plain_nodes.rb' - './haml_lint/linter/documentation_links.rb' + - './haml_lint/linter/inline_javascript.rb' + - './haml_lint/linter/no_plain_nodes.rb' linters: AltText: diff --git a/haml_lint/inline_javascript.rb b/haml_lint/inline_javascript.rb deleted file mode 100644 index c87d77d7a4b69dcca8b336074a6199f534ab20b0..0000000000000000000000000000000000000000 --- a/haml_lint/inline_javascript.rb +++ /dev/null @@ -1,25 +0,0 @@ -# frozen_string_literal: true - -unless Rails.env.production? - require 'haml_lint/haml_visitor' - require 'haml_lint/linter' - require 'haml_lint/linter_registry' - - module HamlLint - class Linter::InlineJavaScript < Linter - include ::HamlLint::LinterRegistry - - def visit_filter(node) - return unless node.filter_type == 'javascript' - - record_lint(node, 'Inline JavaScript is discouraged (https://docs.gitlab.com/ee/development/gotchas.html#do-not-use-inline-javascript-in-views)') - end - - def visit_tag(node) - return unless node.tag_name == 'script' - - record_lint(node, 'Inline JavaScript is discouraged (https://docs.gitlab.com/ee/development/gotchas.html#do-not-use-inline-javascript-in-views)') - end - end - end -end diff --git a/haml_lint/linter/inline_javascript.rb b/haml_lint/linter/inline_javascript.rb new file mode 100644 index 0000000000000000000000000000000000000000..f7672c9834719c38034e4e9b4efc1ee442fa6c95 --- /dev/null +++ b/haml_lint/linter/inline_javascript.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module HamlLint + class Linter + class InlineJavaScript < Linter + include ::HamlLint::LinterRegistry + + MSG = 'Inline JavaScript is discouraged (https://docs.gitlab.com/ee/development/gotchas.html#do-not-use-inline-javascript-in-views)' + + def visit_filter(node) + return unless node.filter_type == 'javascript' + + record_lint(node, MSG) + end + + def visit_tag(node) + return unless node.tag_name == 'script' + + record_lint(node, MSG) + end + end + end +end diff --git a/lib/tasks/haml-lint.rake b/lib/tasks/haml-lint.rake index 71e84d3795f988f7535f0082a0d7025707b42b68..2958957134424083276bad644b715d06b59974a7 100644 --- a/lib/tasks/haml-lint.rake +++ b/lib/tasks/haml-lint.rake @@ -2,7 +2,6 @@ unless Rails.env.production? require 'haml_lint/rake_task' - require Rails.root.join('haml_lint/inline_javascript') HamlLint::RakeTask.new end diff --git a/spec/haml_lint/linter/documentation_links_spec.rb b/spec/haml_lint/linter/documentation_links_spec.rb index f2aab4304c1f1710b9dc99e263d4e95e227dc9bd..49a720700da9e6b527137d0f57800f906792f13a 100644 --- a/spec/haml_lint/linter/documentation_links_spec.rb +++ b/spec/haml_lint/linter/documentation_links_spec.rb @@ -1,9 +1,10 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' require 'haml_lint' require 'haml_lint/spec' -require Rails.root.join('haml_lint/linter/documentation_links') + +require_relative '../../../haml_lint/linter/documentation_links' RSpec.describe HamlLint::Linter::DocumentationLinks do include_context 'linter' diff --git a/spec/haml_lint/linter/inline_javascript_spec.rb b/spec/haml_lint/linter/inline_javascript_spec.rb new file mode 100644 index 0000000000000000000000000000000000000000..fb35bb68247383eb9c628198954bbefb03338937 --- /dev/null +++ b/spec/haml_lint/linter/inline_javascript_spec.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +require 'fast_spec_helper' +require 'haml_lint' +require 'haml_lint/spec' +require 'rspec-parameterized' + +require_relative '../../../haml_lint/linter/inline_javascript' + +RSpec.describe HamlLint::Linter::InlineJavaScript do # rubocop:disable RSpec/FilePath + using RSpec::Parameterized::TableSyntax + + include_context 'linter' + + let(:message) { described_class::MSG } + + where(:haml, :should_report) do + '%script' | true + '%javascript' | false + ':javascript' | true + ':markdown' | false + end + + with_them do + if params[:should_report] + it { is_expected.to report_lint message: message } + else + it { is_expected.not_to report_lint } + end + end +end diff --git a/spec/haml_lint/linter/no_plain_nodes_spec.rb b/spec/haml_lint/linter/no_plain_nodes_spec.rb index 08f7e6131ccdec29681c4eca4b8a252a202fb9f0..eeb0e4ea96f54a228c1c2437152d3f2d61bd13d2 100644 --- a/spec/haml_lint/linter/no_plain_nodes_spec.rb +++ b/spec/haml_lint/linter/no_plain_nodes_spec.rb @@ -1,9 +1,10 @@ # frozen_string_literal: true -require 'spec_helper' +require 'fast_spec_helper' require 'haml_lint' require 'haml_lint/spec' -require Rails.root.join('haml_lint/linter/no_plain_nodes') + +require_relative '../../../haml_lint/linter/no_plain_nodes' RSpec.describe HamlLint::Linter::NoPlainNodes do include_context 'linter'