haml_lint 0.30.0 → 0.31.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 206ee804d052b1d36416d62a9143b4cc61e5bbc52032e314ab4f8937842c67c7
4
- data.tar.gz: d972e18ec4ca4646f31ec3b4f85450c8afeae2e3051cd5e9f87e65d68fda0cdb
3
+ metadata.gz: 91c74e1c2fec68c3fd3834999bde513ad3474b81aa946b26bb29d42098668b90
4
+ data.tar.gz: 1e31e1fb7535909de90039f71f6e4cf359905cf7691e0ae83eb30626f753a535
5
5
  SHA512:
6
- metadata.gz: 79e24f904324a9fe229bbd71d0cf46e8547af267b9e397882021205147a98e12a1a0eee89cae304d25ccafcdf6470a639656bb3c9c8eeba0a94c6b77eb5f1d01
7
- data.tar.gz: b395c2192c88a674f1abc75e88ce21e3c875278c251e0c64c85acb1cb6dac843b807c9fee0bd360735a87751dcaf74932f1e0a999ec7cc111e9c6b40655c0b9c
6
+ metadata.gz: d97ed27e7c49212447ed00b59560e8e0d87a324bdfe161eba058ba42092d1f07750f7c8984e09383635e2d4e2eecde6516869f175b93e78ae6a962af931e8a27
7
+ data.tar.gz: 4a9c449c5dffe879a9440e53fc80264b984a06cd3e565fc7dd9f4bd9553221df30a6ac576f98519a7d882992135bcbda7edf3a1e8b161b2c60150f7682e5603b
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'haml_lint/spec/normalize_indent'
4
+ require 'haml_lint/spec/shared_linter_context'
5
+ require 'haml_lint/spec/matchers/report_lint'
@@ -0,0 +1,129 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HamlLint
4
+ module Spec::Matchers
5
+ # Matcher to assert linter reports and their details
6
+ module ReportLint
7
+ RSpec::Matchers.define :report_lint do |options|
8
+ options ||= {}
9
+ count = options[:count]
10
+ expected_line = options[:line]
11
+ expected_message = options[:message]
12
+ expected_severity = options[:severity]
13
+
14
+ match do |linter|
15
+ has_lints?(linter, expected_line, count, expected_message, expected_severity)
16
+ end
17
+
18
+ failure_message do |linter|
19
+ 'expected that a lint would be reported' +
20
+ extended_expectations(expected_line, expected_message, expected_severity) +
21
+ case linter.lints.count
22
+ when 0
23
+ ''
24
+ when 1
25
+ messages = [', but']
26
+ messages << "reported message '#{linter.lints.first.message}'" if expected_message
27
+ if expected_line
28
+ messages << 'was' unless expected_message
29
+ messages << "on line #{linter.lints.first.line}"
30
+ messages << "with severity '#{linter.lints.first.severity}'" if expected_severity
31
+ end
32
+ messages.join ' '
33
+ else
34
+ lines = lint_lines(linter)
35
+ ", but lints were reported on lines #{lines[0...-1].join(', ')} and #{lines.last}"
36
+ end
37
+ end
38
+
39
+ failure_message_when_negated do |linter|
40
+ message = ['expected that a lint would not be reported, but reported ']
41
+
42
+ message <<
43
+ case linter.lints.count
44
+ when 1 then "1 lint:\n"
45
+ else "#{linter.lints.count} lints:\n"
46
+ end
47
+
48
+ linter.lints.each do |lint|
49
+ message << " - Line #{lint.line}: #{lint.message}"
50
+ end
51
+
52
+ message.join
53
+ end
54
+
55
+ description do
56
+ 'report a lint' +
57
+ extended_expectations(expected_line, expected_message, expected_severity)
58
+ end
59
+
60
+ def extended_expectations(expected_line, expected_message, expected_severity)
61
+ (expected_line ? " on line #{expected_line}" : '') +
62
+ (expected_message ? " with message '#{expected_message}'" : '') +
63
+ (expected_severity ? " with severity '#{expected_severity}'" : '')
64
+ end
65
+
66
+ def has_lints?(linter, expected_line, count, expected_message, expected_severity)
67
+ if expected_line
68
+ has_expected_line_lints?(linter,
69
+ expected_line,
70
+ count,
71
+ expected_message,
72
+ expected_severity)
73
+ elsif count
74
+ linter.lints.count == count
75
+ elsif expected_message
76
+ lint_messages_match?(linter, expected_message)
77
+ else
78
+ linter.lints.any?
79
+ end
80
+ end
81
+
82
+ def has_expected_line_lints?(linter,
83
+ expected_line,
84
+ count,
85
+ expected_message,
86
+ expected_severity)
87
+ if count
88
+ multiple_lints_match_line?(linter, expected_line, count)
89
+ elsif expected_message
90
+ lint_on_line_matches_message?(linter, expected_line, expected_message)
91
+ elsif expected_severity
92
+ lint_on_line_matches_severity?(linter, expected_line, expected_severity)
93
+ else
94
+ lint_lines(linter).include?(expected_line)
95
+ end
96
+ end
97
+
98
+ def multiple_lints_match_line?(linter, expected_line, count)
99
+ linter.lints.count == count &&
100
+ lint_lines(linter).all? { |line| line == expected_line }
101
+ end
102
+
103
+ def lint_on_line_matches_message?(linter, expected_line, expected_message)
104
+ linter
105
+ .lints
106
+ .any? { |lint| lint.line == expected_line && lint.message == expected_message }
107
+ end
108
+
109
+ def lint_on_line_matches_severity?(linter, expected_line, expected_severity)
110
+ linter
111
+ .lints
112
+ .any? { |lint| lint.line == expected_line && lint.severity == expected_severity }
113
+ end
114
+
115
+ def lint_messages_match?(linter, expected_message)
116
+ lint_messages(linter).all? { |message| message == expected_message }
117
+ end
118
+
119
+ def lint_lines(linter)
120
+ linter.lints.map(&:line)
121
+ end
122
+
123
+ def lint_messages(linter)
124
+ linter.lints.map(&:message)
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HamlLint
4
+ module Spec
5
+ # Strips off excess leading indentation from each line so we can use Heredocs
6
+ # for writing code without having the leading indentation count.
7
+ module IndentNormalizer
8
+ def normalize_indent(code)
9
+ leading_indent = code[/^(\s*)/, 1]
10
+ code.lstrip.gsub(/\n#{leading_indent}/, "\n")
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ RSpec.configure do |_config|
17
+ include HamlLint::Spec::IndentNormalizer
18
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Makes writing tests for linters a lot DRYer by taking any currently `haml`
4
+ # variable defined via `let` and normalizing it and running the linter against
5
+ # it, allowing specs to simply specify whether a lint was reported.
6
+
7
+ module HamlLint
8
+ module Spec
9
+ module SharedLinterContext
10
+ RSpec.shared_context 'linter' do
11
+ let(:options) do
12
+ {
13
+ config: HamlLint::ConfigurationLoader.default_configuration,
14
+ }
15
+ end
16
+
17
+ let(:config) { options[:config].for_linter(described_class) }
18
+
19
+ let(:document) { HamlLint::Document.new(normalize_indent(haml), options) }
20
+
21
+ subject { described_class.new(config) }
22
+
23
+ before { subject.run(document) }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  module HamlLint
5
- VERSION = '0.30.0'
5
+ VERSION = '0.31.0'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: haml_lint
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.30.0
4
+ version: 0.31.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Shane da Silva
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-05-03 00:00:00.000000000 Z
11
+ date: 2019-05-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: haml
@@ -170,6 +170,10 @@ files:
170
170
  - lib/haml_lint/ruby_parser.rb
171
171
  - lib/haml_lint/runner.rb
172
172
  - lib/haml_lint/severity.rb
173
+ - lib/haml_lint/spec.rb
174
+ - lib/haml_lint/spec/matchers/report_lint.rb
175
+ - lib/haml_lint/spec/normalize_indent.rb
176
+ - lib/haml_lint/spec/shared_linter_context.rb
173
177
  - lib/haml_lint/tree/comment_node.rb
174
178
  - lib/haml_lint/tree/doctype_node.rb
175
179
  - lib/haml_lint/tree/filter_node.rb