haml_lint 0.54.0 → 0.56.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: b017823b15563d033480ee99317c4d0c007624b10ad2ed3b53c254c5686289c8
4
- data.tar.gz: cf83ac415fb7749551466fcc0b23a5a2019bbecb99a67b2c54051a78523f1a33
3
+ metadata.gz: b0bb69db6857a5c9b072203e93e4c9964bd60a9f8e4f1024e4db31453deec8a6
4
+ data.tar.gz: 94fcce12ae043e72f638d532fcddccfad2de1c8af33a261c37920fa738c4609e
5
5
  SHA512:
6
- metadata.gz: 17ea23cce399f12712d0f73560553b82068e2ac3be9124c590bbea78948a330811402c15fd2b5f8cbc0b5e8e044bc044e69df78f2e05462580665db814298595
7
- data.tar.gz: d619d3018e139b00ce539da9b9f7e1bfef644c71c8e102d146778c3c322770458a16feb89bb44a1a3699eadacc6038c7186407fc8361e6b354f0c4bdbb2dcd57
6
+ metadata.gz: 973dd46634cfde27066acfcd5d44bebc538716f7ca0d34114b183c19808513fc397b688a66664f6bf1b5ae0367762d4daa7f203d894cd3eb068bcbcfdc12edc5
7
+ data.tar.gz: ac57887f196f37a3f2f40b77cc5c1e24dd2b29bc29a9d693fc4a675a69ced56f4a49386e2e7a8694cfa04ecbb3ac75bb4fb412c760a0a9fe83d2d20b7bdef40a
@@ -247,31 +247,29 @@ module HamlLint::RubyExtraction
247
247
  def visit_tag(node)
248
248
  indent = @original_haml_lines[node.line - 1].index(/\S/)
249
249
 
250
+ @ruby_chunks << PlaceholderMarkerChunk.new(node, 'tag', indent: indent)
251
+
252
+ current_line_index = visit_tag_attributes(node, indent: indent)
253
+ visit_tag_script(node, line_index: current_line_index, indent: indent)
254
+
250
255
  # We don't want to use a block because assignments in a block are local to that block,
251
256
  # so the semantics of the extracted ruby would be different from the one generated by
252
257
  # Haml. Those differences can make some cops, such as UselessAssignment, have false
253
258
  # positives
254
259
  code = 'begin'
255
- @ruby_chunks << AdHocChunk.new(node,
256
- [' ' * indent + code])
257
- indent += 2
258
-
259
- tag_chunk = PlaceholderMarkerChunk.new(node, 'tag', indent: indent)
260
- @ruby_chunks << tag_chunk
260
+ begin_chunk = AdHocChunk.new(node, [' ' * indent + code])
261
+ @ruby_chunks << begin_chunk
261
262
 
262
- current_line_index = visit_tag_attributes(node, indent: indent)
263
- visit_tag_script(node, line_index: current_line_index, indent: indent)
263
+ indent += 2
264
264
 
265
265
  yield
266
266
 
267
267
  indent -= 2
268
268
 
269
- if @ruby_chunks.last.equal?(tag_chunk)
269
+ if @ruby_chunks.last.equal?(begin_chunk)
270
270
  # So there is nothing going "in" the tag, remove the wrapping "begin" and replace the PlaceholderMarkerChunk
271
271
  # by one less indented
272
272
  @ruby_chunks.pop
273
- @ruby_chunks.pop
274
- @ruby_chunks << PlaceholderMarkerChunk.new(node, 'tag', indent: indent)
275
273
  else
276
274
  @ruby_chunks << AdHocChunk.new(node,
277
275
  [' ' * indent + 'ensure', ' ' * indent + ' HL.noop', ' ' * indent + 'end'],
@@ -600,9 +598,12 @@ module HamlLint::RubyExtraction
600
598
  return [char_index, [haml_processed_ruby_code]]
601
599
  end
602
600
 
603
- min_non_white_chars_to_add = haml_processed_ruby_code.scan(/\S/).size
601
+ # The +1 is for the closing brace, which we need
602
+ min_non_white_chars_to_add = haml_processed_ruby_code.scan(/\S/).size + 1
604
603
 
605
- regexp = HamlLint::Utils.regexp_for_parts(haml_processed_ruby_code.split(/\s+/), '\\s+')
604
+ regexp = HamlLint::Utils.regexp_for_parts(
605
+ haml_processed_ruby_code.split(/\s+/), '\\s+', prefix: '\s*', suffix: '\s*(?=[)}])'
606
+ )
606
607
 
607
608
  joined_lines = first_line.rstrip
608
609
  process_multiline!(joined_lines)
@@ -624,7 +625,7 @@ module HamlLint::RubyExtraction
624
625
 
625
626
  first_line_offset = match.begin(0)
626
627
  raw_ruby = match[0]
627
- ruby_lines = raw_ruby.split("\n")
628
+ ruby_lines = raw_ruby.split("\n", -1)
628
629
 
629
630
  [first_line_offset, ruby_lines]
630
631
  end
@@ -137,14 +137,14 @@ module HamlLint
137
137
  # @return [Array<HamlLint::Source>]
138
138
  def extract_applicable_sources(config, options)
139
139
  if options[:stdin]
140
- [HamlLint::Source.new($stdin, options[:stdin])]
140
+ [HamlLint::Source.new(io: $stdin, path: options[:stdin])]
141
141
  else
142
142
  included_patterns = options[:files]
143
143
  excluded_patterns = config['exclude']
144
144
  excluded_patterns += options.fetch(:excluded_files, [])
145
145
 
146
146
  HamlLint::FileFinder.new(config).find(included_patterns, excluded_patterns).map do |file_path|
147
- HamlLint::Source.new File.new(file_path), file_path
147
+ HamlLint::Source.new path: file_path
148
148
  end
149
149
  end
150
150
  end
@@ -1,24 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HamlLint
4
- # Wrapper class representing a single target for HamlLint::Runner to run against, comprised of an IO object
5
- # containing haml code, as well as a file path.
4
+ # Wrapper class representing a single target for HamlLint::Runner to run against, comprised of a file path to
5
+ # eventually read from, and an optional IO argument to override with.
6
6
  class Source
7
7
  # @return [String] File path associated with the given IO object.
8
8
  attr_reader :path
9
9
 
10
- # Wraps an IO object and file path to a source object.
10
+ # Wraps an optional IO object and file path to a source object.
11
11
  #
12
- # @param [IO] io
13
12
  # @param [String] path
14
- def initialize(io, path)
15
- @io = io
13
+ # @param [IO] io
14
+ def initialize(path: nil, io: nil)
16
15
  @path = path
16
+ @io = io
17
17
  end
18
18
 
19
19
  # @return [String] Contents of the given IO object.
20
20
  def contents
21
- @contents ||= @io.read
21
+ @contents ||= @io&.read || File.read(path)
22
22
  end
23
23
  end
24
24
  end
@@ -115,7 +115,7 @@ module HamlLint
115
115
 
116
116
  syntax_lints = subject.lints.select { |lint| lint.message =~ %r{Lint/Syntax} }
117
117
 
118
- if start_ruby.strip != 'SKIP' && subject.last_extracted_source
118
+ if start_ruby.strip != 'SKIP' && subject.last_extracted_source && ENV['UPDATE_EXAMPLES'] != '1'
119
119
  matcher = eq(start_ruby)
120
120
  subject.last_extracted_source.source.should(
121
121
  matcher,
@@ -125,7 +125,7 @@ module HamlLint
125
125
 
126
126
  syntax_lints.should(be_empty, "Generated Ruby has Syntax Lints:\n#{format_lints(syntax_lints)}")
127
127
 
128
- if end_ruby.strip != 'SKIP' && subject.last_new_ruby_source
128
+ if end_ruby.strip != 'SKIP' && subject.last_new_ruby_source && ENV['UPDATE_EXAMPLES'] != '1'
129
129
  matcher = eq(end_ruby)
130
130
  subject.last_new_ruby_source.should(
131
131
  matcher,
@@ -141,10 +141,40 @@ module HamlLint
141
141
  -> { "Final HAML is different from expected. #{matcher.failure_message}\n#{format_lints}" }
142
142
  )
143
143
 
144
- if subject.last_extracted_source && start_ruby.strip != 'SKIP'
144
+ if subject.last_extracted_source && start_ruby.strip != 'SKIP' && ENV['UPDATE_EXAMPLES'] != '1'
145
145
  subject.last_extracted_source.source_map.should == source_map
146
146
  end
147
147
 
148
+ if ENV['UPDATE_EXAMPLES'] == '1' && respond_to?(:example_first_line_no) &&
149
+ start_ruby.strip != 'SKIP' && end_ruby.strip != 'SKIP'
150
+ original_content = File.read(example_path)
151
+ old_steps_string = '---' + steps_string.partition('---').last.rpartition('---').first + '---'
152
+
153
+ if old_steps_string.scan(/\$\$\d+/).tally.values.any? { |v| v > 1 }
154
+ # If the $$3 was there twice, let's not update the example
155
+ return
156
+ end
157
+ original_content.scan(old_steps_string).count
158
+
159
+ new_generated_ruby_lines = subject.last_extracted_source.source.split("\n", -1)
160
+ last_seen_haml_line_no = 1
161
+ subject.last_extracted_source.source_map.each do |ruby_line_no, haml_line_no|
162
+ if haml_line_no != last_seen_haml_line_no
163
+ last_seen_haml_line_no = haml_line_no
164
+ new_generated_ruby_lines[ruby_line_no - 1] += " $$#{haml_line_no}"
165
+ end
166
+ end
167
+
168
+ new_steps_string = <<~NEW.chop # Chop to remove the added newlines
169
+ ---
170
+ #{new_generated_ruby_lines.join("\n")}---
171
+ #{subject.last_new_ruby_source}---
172
+ NEW
173
+
174
+ new_content = original_content.gsub(old_steps_string, new_steps_string)
175
+ File.write(example_path, new_content)
176
+ end
177
+
148
178
  haml_different = start_haml != end_haml
149
179
  document.source_was_changed.should == haml_different
150
180
  end
@@ -277,8 +277,9 @@ module HamlLint
277
277
  $stdin = original_stdin
278
278
  end
279
279
 
280
- def regexp_for_parts(parts, join_regexp)
280
+ def regexp_for_parts(parts, join_regexp, prefix: nil, suffix: nil)
281
281
  regexp_code = parts.map { |c| Regexp.quote(c) }.join(join_regexp)
282
+ regexp_code = "#{prefix}#{regexp_code}#{suffix}"
282
283
  Regexp.new(regexp_code)
283
284
  end
284
285
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  module HamlLint
5
- VERSION = '0.54.0'
5
+ VERSION = '0.56.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.54.0
4
+ version: 0.56.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: 2024-01-23 00:00:00.000000000 Z
11
+ date: 2024-02-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: haml