super-slim 3.2.2 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,9 +3,9 @@
3
3
  module SuperSlim
4
4
  class Parser
5
5
  # Read by hand, a line at a time: Prism would cost every process that renders templates its load time and memory.
6
- # Misread, all rare after a bracket that ends its line or before a comment that ends the code: heredocs, `?(`, `$'`, a regexp after a method name or a
7
- # keyword (`when /'/`), `%` glued to a value (`"%d"%[a]`), interpolation in a %-literal and %-literals between
8
- # other delimiters (`%|a(|`).
6
+ # Misread, all rare after a bracket that ends its line or before a comment that ends the code: heredocs, `?(`,
7
+ # `$'`, a regexp after a method name or a keyword (`when /'/`), `%` glued to a value (`"%d"%[a]`), interpolation
8
+ # in a %-literal and %-literals between other delimiters (`%|a(|`).
9
9
  class RubyBrackets
10
10
  PLAIN_RUBY = /[^()\[\]{}"'`#%\/]+/
11
11
  QUOTED_TEXT_BY_DELIMITER = %w[" ` /].to_h { |delimiter| [delimiter, /(?:[^\\#{delimiter}#]|\\.|#(?!\{))*/m] }
@@ -18,6 +18,19 @@ module SuperSlim
18
18
  # `%w[a) b(]` nests only its own kind of bracket.
19
19
  PercentLiteral = Struct.new(:opening, :closing, :depth)
20
20
 
21
+ # Where a comment that follows code on its line starts in code, nil when there is none. One alone on a line is
22
+ # left out: a backslash before it carries the code on to that line.
23
+ def self.code_comment_offset(code)
24
+ return unless code.include?("#")
25
+
26
+ offset = new(code).trailing_comment_offset or return
27
+ code_before = code.byteslice(0, offset).sub(/[ \t]+\z/, "")
28
+ offset unless code_before.empty? || code_before.end_with?("\n")
29
+ end
30
+
31
+ # The byte offset of the comment that ends the code, if one does.
32
+ attr_reader :trailing_comment_offset
33
+
21
34
  def initialize(code)
22
35
  @scanner = StringScanner.new(code.dup)
23
36
  # Whether each bracket still open ends its line.
@@ -28,9 +41,6 @@ module SuperSlim
28
41
  read
29
42
  end
30
43
 
31
- # The byte offset of the comment that ends the code, if one does.
32
- attr_reader :trailing_comment_offset
33
-
34
44
  def <<(line)
35
45
  @trailing_comment_offset = nil
36
46
  @scanner << "\n" << line
@@ -53,11 +53,58 @@ module SuperSlim
53
53
  CODE_END_BY_CLOSING = [nil, *DELIMITERS.values].to_h { |closing| [closing, /[\s#{Regexp.escape(closing.to_s)}]/] }.freeze
54
54
 
55
55
  # What the formatter and the linter need to know about each source line, indexed by line number. A tag's
56
- # attribute list on one line is [first, last, [[first, last], …]], in columns of that line.
57
- LineInfo = Struct.new(:kind, :depth, :owner, :text, :output_column, :attribute_lists)
56
+ # attribute list on one line is [first, last, [[first, last], …]], in columns of that line, and so is a tag nested
57
+ # inline after `:`, as [column of the `:`, column of the nested tag].
58
+ LineInfo = Struct.new(:kind, :depth, :owner, :text, :output_column, :attribute_lists, :nested_tag_columns)
59
+ FORMAT_DIRECTIVE = %r{\A?\s*/\s*super-slim:format\s+(off|on)\s*\z}
60
+ # A comment that renders nothing: not `/!` or `/[if IE]`.
61
+ CODE_COMMENT = %r{\A?\s*/(?![!\[])}
62
+
63
+ # A string separator splits 6x faster than the regexp, which only templates with \r need.
64
+ def self.split_lines(source)
65
+ source.include?("\r") ? source.split(/\r?\n/) : source.split("\n")
66
+ end
67
+
68
+ # Whether Slim, and so Brakeman, reads these lines of code as one: it goes on after a comma or a backslash, not
69
+ # after an opening bracket.
70
+ def self.slim_continuation?(lines)
71
+ lines[0...-1].all? { _1.rstrip.match?(LINE_CONTINUATION) }
72
+ end
73
+
74
+ # The lines after `/ super-slim:format off`, up to `/ super-slim:format on` at its depth or the end of the block
75
+ # that holds it. A directive deeper than the `off` is a line like the others; blank lines that close the block
76
+ # are not in it.
77
+ def self.unformatted_linenos(lines, outline)
78
+ unformatted_linenos = Set.new
79
+ return unformatted_linenos if lines.none? { _1.include?("super-slim:format") }
80
+
81
+ off_depth = nil
82
+ blank_linenos = []
83
+ lines.each.with_index(1) do |line, lineno|
84
+ info = outline[lineno]
85
+ if info.kind == :blank
86
+ blank_linenos << lineno if off_depth
87
+ next
88
+ end
89
+
90
+ if info.kind == :structure
91
+ off_depth = nil if off_depth && info.depth < off_depth
92
+ if (off_depth.nil? || info.depth == off_depth) && (directive = line[FORMAT_DIRECTIVE, 1])
93
+ off_depth = (directive == "off") ? info.depth : nil
94
+ blank_linenos.clear
95
+ next
96
+ end
97
+ end
98
+ unformatted_linenos.merge(blank_linenos) if off_depth
99
+ blank_linenos.clear
100
+ unformatted_linenos << lineno if off_depth
101
+ end
102
+ unformatted_linenos
103
+ end
58
104
 
105
+ attr_reader :outline, :line_by_node
59
106
  # Ruby code and interpolated text, as [kind, first, last] character offsets in the source: what the RuboCop plugin lints.
60
- attr_reader :outline, :line_by_node, :ruby_ranges
107
+ attr_reader :ruby_ranges
61
108
 
62
109
  def initialize(file: nil, outline: false, tailwind_classes: SuperSlim.tailwind_classes)
63
110
  @file = file
@@ -74,8 +121,7 @@ module SuperSlim
74
121
  end
75
122
  normalized = normalize(source)
76
123
  @line_starts = line_starts(normalized, source.size - normalized.size) if @record_outline
77
- # A string separator splits 6x faster than the regexp, which only templates with \r need.
78
- @lines = normalized.include?("\r") ? normalized.split(/\r?\n/) : normalized.split("\n")
124
+ @lines = Parser.split_lines(normalized)
79
125
  @scanner = StringScanner.new("")
80
126
  @stacks = [root]
81
127
  @indents = []
@@ -466,6 +512,7 @@ module SuperSlim
466
512
  # Not `column`, which counts the spaces stripped from the end of a continuation line.
467
513
  output_column = @scanner_column + scanner_charpos if @outline
468
514
  if @scanner.skip(NESTED_TAG)
515
+ (@outline[@lineno].nested_tag_columns ||= []) << [output_column, @scanner_column + scanner_charpos] if @outline
469
516
  return [[parse_embedded(@scanner[1], @scanner[2], @scanner.rest)]] if @scanner.skip(ENGINE)
470
517
 
471
518
  (tag = scan_tag) || syntax_error!("Expected tag")
@@ -530,15 +577,13 @@ module SuperSlim
530
577
  end
531
578
  break unless closing
532
579
 
533
- if @scanner.skip(ATTRIBUTE_LIST_END_BY_CLOSING[closing])
534
- break
535
- else
536
- @scanner.skip(WHITESPACE)
537
- syntax_error!("Expected attribute") unless @scanner.eos?
538
- @stacks.last << NEWLINE
539
- syntax_error!("Expected closing delimiter #{closing}") if @lines.empty?
540
- next_line
541
- end
580
+ break if @scanner.skip(ATTRIBUTE_LIST_END_BY_CLOSING[closing])
581
+
582
+ @scanner.skip(WHITESPACE)
583
+ syntax_error!("Expected attribute") unless @scanner.eos?
584
+ @stacks.last << NEWLINE
585
+ syntax_error!("Expected closing delimiter #{closing}") if @lines.empty?
586
+ next_line
542
587
  end
543
588
  if outline && lineno == @lineno && !spans.empty?
544
589
  list_first = spans.first.first unless closing
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SuperSlim
4
- VERSION = "3.2.2"
4
+ VERSION = "3.4.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: super-slim
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.2.2
4
+ version: 3.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Level UP Solutions