haml_lint 0.48.0 → 0.49.1

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: 93d2cec8a3ce45f370d5e8d055ed8f9dc5a988e2c56e8e145f50710f04f40a3f
4
- data.tar.gz: 59a2d93bd2ab520c4fb1288cf85f784d3d20955293b6167f046bffba3f198cac
3
+ metadata.gz: ccc4d7e3d573b44a76eb95a860b47023cacc016d84a423fab45ed5521ec4c859
4
+ data.tar.gz: 30df2f2e85a8b5629fc3e7be4d236f899e4a7350520d3fa6ff1ad544e298db81
5
5
  SHA512:
6
- metadata.gz: be4b59ae43a2cab51fd4148e843177b939da14ee7b0dc40a8ed0d9dd7bbcd12d39d674c1d9c7f19f0a524d912e27fc82b882f6412083eda143c5cf60e218c397
7
- data.tar.gz: 28075057ee4c955a4966d2561ff77680b9d90c24e647dc83386dab4ebfabc404cbcb22913836f987896cc506244a4f612f74e131d8838c91f1e0db027d0f8cdd
6
+ metadata.gz: 31ee68b52d129a92e1ce5b0b08c5c1770cc79cb5fc334ead102b3512b3e01e899b9d4b48f14cfb75d047708c8274d76ef9f76c1c9ea96ce9418fbcc7d90a3d07
7
+ data.tar.gz: f5723c505efeb8b98c278d9ff6e18c6f04396542f5f45543b3fdde2c622f5431378592a03340cd3040a4ed357ab8e7f216f98c35bf0e65f17c3925e5fab14590
data/config/default.yml CHANGED
@@ -105,6 +105,9 @@ linters:
105
105
  TagName:
106
106
  enabled: true
107
107
 
108
+ TrailingEmptyLines:
109
+ enabled: true
110
+
108
111
  TrailingWhitespace:
109
112
  enabled: true
110
113
 
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HamlLint
4
+ # Checks for trailing empty lines.
5
+ class Linter::TrailingEmptyLines < Linter
6
+ include LinterRegistry
7
+
8
+ DummyNode = Struct.new(:line)
9
+
10
+ def visit_root(root)
11
+ return if document.source.empty?
12
+ line_number = document.last_non_empty_line
13
+
14
+ node = root.node_for_line(line_number)
15
+ return if node.disabled?(self)
16
+
17
+ return unless document.source.end_with?("\n\n")
18
+
19
+ record_lint(line_number, 'Files should not end with trailing empty lines')
20
+ end
21
+ end
22
+ end
@@ -102,7 +102,8 @@ module HamlLint::RubyExtraction
102
102
  # that contains interpolation.
103
103
  indent = raw_first_line.index(/\S/)
104
104
  @ruby_chunks << PlaceholderMarkerChunk.new(node, 'interpolation', indent: indent)
105
- add_interpolation_chunks(node, raw_first_line, node.line - 1, indent: indent)
105
+ lines = extract_piped_plain_multilines(node.line - 1)
106
+ add_interpolation_chunks(node, lines.join("\n"), node.line - 1, indent: indent)
106
107
  return
107
108
  end
108
109
 
@@ -327,9 +328,16 @@ module HamlLint::RubyExtraction
327
328
  # ex: %tag hello #{world}
328
329
  # Sadly, the text with interpolation is escaped from the original, but this code
329
330
  # needs the original.
330
- interpolation_original = @document.unescape_interpolation_to_original_cache[node.script]
331
331
 
332
+ interpolation_original = @document.unescape_interpolation_to_original_cache[node.script]
332
333
  line_start_index = @original_haml_lines[node.line - 1].rindex(interpolation_original)
334
+ if line_start_index.nil?
335
+ raw_lines = extract_piped_plain_multilines(node.line - 1)
336
+ equivalent_haml_code = "#{raw_lines.first} #{raw_lines[1..].map(&:lstrip).join(' ')}"
337
+ line_start_index = equivalent_haml_code.rindex(interpolation_original)
338
+
339
+ interpolation_original = raw_lines.join("\n")
340
+ end
333
341
  add_interpolation_chunks(node, interpolation_original, node.line - 1,
334
342
  line_start_index: line_start_index, indent: indent)
335
343
  else
@@ -404,11 +412,14 @@ module HamlLint::RubyExtraction
404
412
  # because Haml::Util.balance does a strip...
405
413
  interpolated_code = code[char_index...scanner.charpos - 1]
406
414
 
407
- interpolated_code = "#{' ' * indent}#{script_output_prefix}#{interpolated_code}"
408
-
409
415
  if interpolated_code.include?("\n")
410
416
  # We can't correct multiline interpolation.
411
417
  # Finding meaningful code to generate and then transfer back is pretty complex
418
+
419
+ # Since we can't fix it, strip around the code to reduce RuboCop lints that we won't be able to fix.
420
+ interpolated_code = interpolated_code.strip
421
+ interpolated_code = "#{' ' * indent}#{script_output_prefix}#{interpolated_code}"
422
+
412
423
  placeholder_code = interpolated_code.gsub(/\s*\n\s*/, ' ').rstrip
413
424
  unless parse_ruby(placeholder_code)
414
425
  placeholder_code = interpolated_code.gsub(/\s*\n\s*/, '; ').rstrip
@@ -416,6 +427,7 @@ module HamlLint::RubyExtraction
416
427
  @ruby_chunks << AdHocChunk.new(node, [placeholder_code],
417
428
  haml_line_index: haml_line_index + line_index)
418
429
  else
430
+ interpolated_code = "#{' ' * indent}#{script_output_prefix}#{interpolated_code}"
419
431
  @ruby_chunks << InterpolationChunk.new(node, [interpolated_code],
420
432
  haml_line_index: haml_line_index + line_index,
421
433
  start_char_index: start_char_index,
@@ -433,6 +445,15 @@ module HamlLint::RubyExtraction
433
445
  end
434
446
  end
435
447
 
448
+ def process_plain_multiline!(line)
449
+ if line&.end_with?(' |')
450
+ line[-2..] = ''
451
+ true
452
+ else
453
+ false
454
+ end
455
+ end
456
+
436
457
  # Returns the raw lines from the haml for the given index.
437
458
  # Multiple lines are returned when a line ends with a comma as that is the only
438
459
  # time HAMLs allows Ruby lines to be split.
@@ -517,6 +538,25 @@ module HamlLint::RubyExtraction
517
538
  [first_line_offset, ruby_lines]
518
539
  end
519
540
 
541
+ def extract_piped_plain_multilines(first_line_index)
542
+ lines = []
543
+
544
+ cur_line = @original_haml_lines[first_line_index].rstrip
545
+ cur_line_index = first_line_index
546
+
547
+ # The pipes must also be on the last line of the multi-line section
548
+ while cur_line && process_plain_multiline!(cur_line)
549
+ lines << cur_line
550
+ cur_line_index += 1
551
+ cur_line = @original_haml_lines[cur_line_index].rstrip
552
+ end
553
+
554
+ if lines.empty?
555
+ lines << cur_line
556
+ end
557
+ lines
558
+ end
559
+
520
560
  # Tag attributes actually handle multiline differently than scripts.
521
561
  # The basic system basically keeps considering more lines until it meets the closing braces, but still
522
562
  # processes pipes too (same as extract_raw_ruby_lines).
@@ -2,7 +2,6 @@
2
2
 
3
3
  require 'rubocop'
4
4
  require 'rubocop/ast/builder'
5
- require 'parser/current'
6
5
 
7
6
  module HamlLint
8
7
  # Parser for the Ruby language.
@@ -14,10 +13,21 @@ module HamlLint
14
13
  class RubyParser
15
14
  # Creates a reusable parser.
16
15
  def initialize
16
+ require_parser
17
17
  @builder = ::RuboCop::AST::Builder.new
18
18
  @parser = ::Parser::CurrentRuby.new(@builder)
19
19
  end
20
20
 
21
+ # Require the current parser version while suppressing the
22
+ # compliancy warning for minor version differences.
23
+ def require_parser
24
+ prev = $VERBOSE
25
+ $VERBOSE = nil
26
+ require 'parser/current'
27
+ ensure
28
+ $VERBOSE = prev
29
+ end
30
+
21
31
  # Parse the given Ruby source into an abstract syntax tree.
22
32
  #
23
33
  # @param source [String] Ruby source code
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Defines the gem version.
4
4
  module HamlLint
5
- VERSION = '0.48.0'
5
+ VERSION = '0.49.1'
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.48.0
4
+ version: 0.49.1
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: 2023-07-09 00:00:00.000000000 Z
11
+ date: 2023-07-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: haml
@@ -143,6 +143,7 @@ files:
143
143
  - lib/haml_lint/linter/space_inside_hash_attributes.rb
144
144
  - lib/haml_lint/linter/syntax.rb
145
145
  - lib/haml_lint/linter/tag_name.rb
146
+ - lib/haml_lint/linter/trailing_empty_lines.rb
146
147
  - lib/haml_lint/linter/trailing_whitespace.rb
147
148
  - lib/haml_lint/linter/unnecessary_interpolation.rb
148
149
  - lib/haml_lint/linter/unnecessary_string_output.rb
@@ -220,7 +221,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
220
221
  - !ruby/object:Gem::Version
221
222
  version: '0'
222
223
  requirements: []
223
- rubygems_version: 3.1.6
224
+ rubygems_version: 3.0.3.1
224
225
  signing_key:
225
226
  specification_version: 4
226
227
  summary: HAML lint tool