super-slim 3.3.0 → 3.4.1
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +28 -1
- data/README.md +8 -1
- data/lib/super_slim/formatter/line_wrapper.rb +52 -3
- data/lib/super_slim/formatter.rb +177 -6
- data/lib/super_slim/parser.rb +4 -2
- data/lib/super_slim/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dc9a3f9a78ca93e94080ea623bc6f9a3a52e184148488861b81b53118d1a794a
|
|
4
|
+
data.tar.gz: ee5e380942cf49f07d8346d7901b4e13b51a5a992a7b5715dc4e4de73a207829
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 6597c277f4ab79f1c662e45bc705e0a7ede102ebd391b1cf5a130924735c78d7de9df6089f777c6b7179f767af39f0b5f9ef64df1693ed26c6316dc35374a4e9
|
|
7
|
+
data.tar.gz: 6f6ccfc27211da2295ed4273e10b84a867f18c1763d73c583e4f71fe90a15a837d8496fc174145bda5de14aabe71f84d0dc1faebb7b76536d6460c846497703e
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [3.4.1] — 2026-11-07
|
|
6
|
+
|
|
7
|
+
One fix: `super-slim format` no longer writes `-` lines that `super-slim lint` flags.
|
|
8
|
+
|
|
9
|
+
**Fixed**
|
|
10
|
+
- `super-slim format` no longer turns a one- or two-line `ruby:` block into `-` lines that make three in a row with
|
|
11
|
+
the ones around it, which ConsecutiveControlStatements then flagged and a format-then-lint commit hook refused.
|
|
12
|
+
`#{}` in a string no longer keeps a line out of a `ruby:` block.
|
|
13
|
+
|
|
14
|
+
## [3.4.0] — 2026-11-07
|
|
15
|
+
|
|
16
|
+
One layout for inline tags, short children, `ruby:` blocks and negated conditions.
|
|
17
|
+
|
|
18
|
+
**New**
|
|
19
|
+
- `super-slim format` puts tags nested inline after a colon (`tr: td = total`) one per line, their children going as
|
|
20
|
+
many levels deeper. A line whose text goes on below keeps its inline tags.
|
|
21
|
+
- `-` lines and `ruby:` blocks next to each other at one depth make one run of code: three lines or more go in a
|
|
22
|
+
`ruby:` block, as the ConsecutiveControlStatements rule asks, and fewer on `-` lines. Only lines of Ruby complete on
|
|
23
|
+
their own move.
|
|
24
|
+
- With `max_line_length`, a tag whose only child is one `=` output or one line of `|` text takes it on its own line
|
|
25
|
+
when both fit: `td = total`, `p Hello`. Text that could read as an attribute stays below.
|
|
26
|
+
- `- if !condition` becomes `- unless condition` when the `!` covers the whole condition and no `else` follows. The
|
|
27
|
+
formatter's check takes the two for the same code.
|
|
28
|
+
- Lines under `/ super-slim:format off` stay out of these rules.
|
|
29
|
+
|
|
5
30
|
## [3.3.0] — 2026-11-07
|
|
6
31
|
|
|
7
32
|
Formatted templates that Slim and Brakeman read, lines and rules you can opt out of, and a formatter about 30% faster.
|
|
@@ -222,7 +247,9 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
|
|
|
222
247
|
- Ruby 3.3 or later, every Ruby still maintained, Rails 8.0 or 8.1 for the railtie, and strscan 3.1.1 or later,
|
|
223
248
|
newer than the one Ruby 3.3 ships.
|
|
224
249
|
|
|
225
|
-
[Unreleased]: https://github.com/levups/super-slim/compare/v3.
|
|
250
|
+
[Unreleased]: https://github.com/levups/super-slim/compare/v3.4.1...HEAD
|
|
251
|
+
[3.4.1]: https://github.com/levups/super-slim/compare/v3.4.0...v3.4.1
|
|
252
|
+
[3.4.0]: https://github.com/levups/super-slim/compare/v3.3.0...v3.4.0
|
|
226
253
|
[3.3.0]: https://github.com/levups/super-slim/compare/v3.2.2...v3.3.0
|
|
227
254
|
[3.2.2]: https://github.com/levups/super-slim/compare/v3.2.1...v3.2.2
|
|
228
255
|
[3.2.1]: https://github.com/levups/super-slim/compare/v3.2.0...v3.2.1
|
data/README.md
CHANGED
|
@@ -95,7 +95,10 @@ Templates for XML formats (`.xml`, `.rss`, `.atom`) render as XML automatically,
|
|
|
95
95
|
`super-slim format` rewrites templates in one layout: two spaces per level, a space after `-` and `=`, no `div`
|
|
96
96
|
where a `.class` or `#id` is enough, no trailing whitespace unless it renders, single blank lines and a final
|
|
97
97
|
newline. A block starts right after the line that opens it, a partial's `/# locals:` signature stands apart from its
|
|
98
|
-
body, and Ruby code with children ends with the `do` that Slim would add.
|
|
98
|
+
body, and Ruby code with children ends with the `do` that Slim would add. Tags nested inline (`tr: td`) go one per
|
|
99
|
+
line, three `-` lines in a row or more make a `ruby:` block, and a `ruby:` block of one or two lines turns back into
|
|
100
|
+
`-` lines. `- if !condition` with no `else` reads as `- unless condition` when the `!` covers the whole condition.
|
|
101
|
+
This template:
|
|
99
102
|
|
|
100
103
|
```slim
|
|
101
104
|
div.card
|
|
@@ -156,6 +159,10 @@ that close the last one when they would. The lines after the first go two spaces
|
|
|
156
159
|
block. Code spread by hand stays as it is when it holds a comment, or when this layout would leave a line longer than
|
|
157
160
|
the limit and Slim can read it. Arguments to `new` go in parentheses.
|
|
158
161
|
|
|
162
|
+
A tag whose only child is one `=` output or one line of `|` text takes it on its own line when both fit:
|
|
163
|
+
`td = total`, `p Hello`. Text that starts with anything but a letter, a digit or `&`, or whose first word is followed
|
|
164
|
+
by `=`, stays below, where it can't read as an attribute.
|
|
165
|
+
|
|
159
166
|
A tag's attributes stand without delimiters when they fit on its line, and one per line inside the delimiters when
|
|
160
167
|
they don't, unless its shortcuts alone pass the limit. A literal `id` or `class` becomes a shortcut, a class shortcut
|
|
161
168
|
named twice goes, and the other attributes follow one order: `id`, `class`, what the element is (`type`, `name`,
|
|
@@ -11,6 +11,8 @@ module SuperSlim
|
|
|
11
11
|
CONTINUATION_KINDS = %i[continuation text text_blank].freeze
|
|
12
12
|
CLOSING_BRACKET_AT_START = /\A[)\]}]/
|
|
13
13
|
NEW_WITHOUT_PARENTHESES = /\.new [^\s=]/
|
|
14
|
+
CHILD_OUTPUT = /\A==?[<>]* \S.*\z/
|
|
15
|
+
CHILD_TEXT = /\A\| ((?![^\s=]*\s*=)[\p{L}\p{N}&]\S*(?:\s.*\S)?)\z/
|
|
14
16
|
|
|
15
17
|
# Templates attach ( and [ to their tag but set { apart, as a Ruby block: 6,097 public templates agree.
|
|
16
18
|
OPENING_BY_ATTRIBUTE_DELIMITERS = {"()" => "(", "[]" => "[", "{}" => " {"}.freeze
|
|
@@ -35,14 +37,34 @@ module SuperSlim
|
|
|
35
37
|
outline = parser.outline
|
|
36
38
|
unformatted_linenos = Parser.unformatted_linenos(lines, outline)
|
|
37
39
|
block_opening_linenos = Formatter.block_opening_linenos(parser)
|
|
40
|
+
single_child_tag_linenos = single_child_tag_linenos(parser)
|
|
38
41
|
wrapped = []
|
|
39
42
|
lineno = 1
|
|
40
43
|
while lineno <= lines.size
|
|
41
44
|
info = outline[lineno]
|
|
42
|
-
last_lineno = lineno
|
|
43
|
-
last_lineno += 1 while outline[last_lineno + 1]&.owner == lineno && CONTINUATION_KINDS.include?(outline[last_lineno + 1].kind)
|
|
45
|
+
last_lineno = structure_last_lineno(outline, lineno)
|
|
44
46
|
structure_lines = lines[(lineno - 1)...last_lineno]
|
|
45
|
-
|
|
47
|
+
if unformatted_linenos.include?(lineno)
|
|
48
|
+
wrapped.concat(structure_lines)
|
|
49
|
+
else
|
|
50
|
+
laid_out = layout(structure_lines, info, outline, lineno, block_opening_linenos)
|
|
51
|
+
child_lineno = last_lineno + 1
|
|
52
|
+
if laid_out.size == 1 && single_child_tag_linenos.include?(lineno) && outline[child_lineno]&.kind == :structure &&
|
|
53
|
+
!unformatted_linenos.include?(child_lineno)
|
|
54
|
+
# The child is laid out first: code it spreads over lines may join into one.
|
|
55
|
+
child_last_lineno = structure_last_lineno(outline, child_lineno)
|
|
56
|
+
child_lines = layout(lines[(child_lineno - 1)...child_last_lineno], outline[child_lineno], outline, child_lineno, block_opening_linenos)
|
|
57
|
+
joined = with_child(laid_out.first, child_lines.first, info) if child_lines.size == 1
|
|
58
|
+
end
|
|
59
|
+
if joined
|
|
60
|
+
wrapped << joined
|
|
61
|
+
last_lineno = child_last_lineno
|
|
62
|
+
else
|
|
63
|
+
wrapped.concat(laid_out)
|
|
64
|
+
end
|
|
65
|
+
# A `while` keeps its locals from one turn to the next.
|
|
66
|
+
joined = nil
|
|
67
|
+
end
|
|
46
68
|
lineno = last_lineno + 1
|
|
47
69
|
end
|
|
48
70
|
"#{wrapped.join("\n")}\n"
|
|
@@ -50,6 +72,33 @@ module SuperSlim
|
|
|
50
72
|
|
|
51
73
|
private
|
|
52
74
|
|
|
75
|
+
def structure_last_lineno(outline, lineno)
|
|
76
|
+
last_lineno = lineno
|
|
77
|
+
last_lineno += 1 while outline[last_lineno + 1]&.owner == lineno && CONTINUATION_KINDS.include?(outline[last_lineno + 1].kind)
|
|
78
|
+
last_lineno
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Tags whose content is one output or one text block.
|
|
82
|
+
def single_child_tag_linenos(parser)
|
|
83
|
+
parser.line_by_node.filter_map do |node, lineno|
|
|
84
|
+
next unless node.is_a?(AST::Tag) && node.content.is_a?(Array)
|
|
85
|
+
|
|
86
|
+
content = node.content.reject { _1 == AST::NEWLINE }
|
|
87
|
+
lineno if content.size == 1 && (content.first.is_a?(AST::Output) ? AST.empty?(content.first.children) : content.first.is_a?(AST::TextBlock))
|
|
88
|
+
end.to_set
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Text moves only when it starts with a letter, a digit or `&`, and its first word isn't followed by `=`: after a
|
|
92
|
+
# tag, anything else may read as an attribute.
|
|
93
|
+
def with_child(tag_line, child_line, info)
|
|
94
|
+
return if info.output_column || info.text || info.nested_tag_columns
|
|
95
|
+
|
|
96
|
+
child = child_line.lstrip
|
|
97
|
+
content = child[CHILD_OUTPUT] || child[CHILD_TEXT, 1]
|
|
98
|
+
joined = "#{tag_line} #{content}" if content
|
|
99
|
+
joined if joined && joined.size <= @max_line_length
|
|
100
|
+
end
|
|
101
|
+
|
|
53
102
|
def layout(lines, info, outline, lineno, block_opening_linenos)
|
|
54
103
|
return lines unless info.kind == :structure
|
|
55
104
|
|
data/lib/super_slim/formatter.rb
CHANGED
|
@@ -12,13 +12,23 @@ module SuperSlim
|
|
|
12
12
|
class Formatter
|
|
13
13
|
INDENT_WIDTH = 2
|
|
14
14
|
CONTROL = /\A-\s*/
|
|
15
|
+
NEGATED_IF = /\A- if !(?!=)\s*(\S.*)\z/
|
|
16
|
+
COMPOUND_CONDITION = %r{[=<>+*/%&|^~#]|\?\s|\s-\s|:\s|\b(?:and|or|not|then|do)\b}
|
|
17
|
+
ELSE_LINE = /\A\s*-\s*(?:else|elsif)\b/
|
|
15
18
|
OUTPUT = /\A(==?)([<>]*)\s*/
|
|
16
19
|
TAG_OUTPUT = /\A\s*(==?)(['<>]*)\s*/
|
|
17
20
|
REDUNDANT_DIV = /\Adiv(?=[.#][\p{Word}-])/
|
|
18
21
|
LOCALS_HEADER = %r{\A?/#\s*locals:}
|
|
19
22
|
CODE_LINE = /\A(?:-|==?[<>]*) (.+)\z/
|
|
23
|
+
CONTROL_CODE = /\A\s*-\s*(\S.*)/
|
|
24
|
+
# The threshold of the ConsecutiveControlStatements lint rule, plus one.
|
|
25
|
+
RUBY_BLOCK_MIN_LINES = 3
|
|
26
|
+
SKIPPED_KINDS = %i[blank continuation].freeze
|
|
27
|
+
RUBY_BLOCK = /\A\s*ruby:\s*\z/
|
|
28
|
+
CONTROL_RUN = /^( *)- .*\n\1- .*\n\1- /
|
|
20
29
|
# `while x do` is valid Ruby that nobody writes.
|
|
21
30
|
LOOP = /\A(?:while|until|for)\b/
|
|
31
|
+
END_KEYWORD = /\Aend\b/
|
|
22
32
|
# Prism 1.x counts in a node's flags whether it starts a line.
|
|
23
33
|
NEWLINE_FLAG = defined?(Prism::NodeFlags::NEWLINE) ? Prism::NodeFlags::NEWLINE : 0
|
|
24
34
|
FIELDS_BY_NODE_CLASS = {}
|
|
@@ -69,6 +79,13 @@ module SuperSlim
|
|
|
69
79
|
wrapped = @line_wrapper.call(formatted, parser:)
|
|
70
80
|
return formatted if wrapped == formatted
|
|
71
81
|
|
|
82
|
+
# `-` code joined by the wrapper may now make a run long enough for a `ruby:` block. A wrap Slim can't read is
|
|
83
|
+
# refused below.
|
|
84
|
+
if wrapped.match?(CONTROL_RUN)
|
|
85
|
+
wrapped_parser = parsed_template(wrapped).last
|
|
86
|
+
wrapped = layout(wrapped, wrapped_parser) if wrapped_parser
|
|
87
|
+
end
|
|
88
|
+
|
|
72
89
|
wrapped_ruby = parsed_ruby(compiled_code(parsed_template(wrapped, outline: false).first))
|
|
73
90
|
# Only Prism's tree vouches for a wrap: a compile error can read the same before and after it.
|
|
74
91
|
return formatted unless wrapped_ruby.is_a?(Prism::ParseResult)
|
|
@@ -97,7 +114,12 @@ module SuperSlim
|
|
|
97
114
|
indent_shift = 0
|
|
98
115
|
previous_structure_lineno = nil
|
|
99
116
|
blank_line_pending = false
|
|
117
|
+
# [depth, levels]: the children of a line whose inline tags were spread go that many levels deeper.
|
|
118
|
+
spread_levels = []
|
|
119
|
+
code_lines_by_lineno, rewritten_linenos = ruby_code_layout(lines, outline, parser, unformatted_linenos)
|
|
100
120
|
lines.each.with_index(1) do |line, lineno|
|
|
121
|
+
next if rewritten_linenos.include?(lineno)
|
|
122
|
+
|
|
101
123
|
info = outline[lineno]
|
|
102
124
|
# Unformatted lines move with the directive above them, so that they stay inside its block.
|
|
103
125
|
if unformatted_linenos.include?(lineno)
|
|
@@ -117,12 +139,27 @@ module SuperSlim
|
|
|
117
139
|
# A block's first line comes right after the line that opens it.
|
|
118
140
|
formatted.pop if blank_line_pending && previous_info && info.depth > previous_info.depth
|
|
119
141
|
formatted << "" if previous_structure_lineno && previous_structure_lineno == locals_header_lineno && formatted.last != ""
|
|
120
|
-
|
|
142
|
+
spread_levels.pop while spread_levels.last && info.depth <= spread_levels.last[0]
|
|
143
|
+
depth = info.depth + spread_levels.sum { |_depth, levels| levels }
|
|
144
|
+
indent = depth * INDENT_WIDTH
|
|
121
145
|
indent_shift = indent_shift_by_lineno[lineno] = indent - indent_of(line)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
146
|
+
next_info = outline[lineno + 1]
|
|
147
|
+
if (code_lines = code_lines_by_lineno[lineno])
|
|
148
|
+
formatted.concat(code_lines.map { (" " * indent) + _1 })
|
|
149
|
+
elsif info.nested_tag_columns && !(goes_on?(outline, lineno) && next_info.kind != :continuation)
|
|
150
|
+
nested_lines = spread_nested_tags(line, info, depth)
|
|
151
|
+
levels = nested_lines.size - 1
|
|
152
|
+
spread_levels << [info.depth, levels]
|
|
153
|
+
# Attributes going on over the next lines belong to the last tag, and go as many levels deeper.
|
|
154
|
+
indent_shift_by_lineno[lineno] += levels * INDENT_WIDTH
|
|
155
|
+
formatted.concat(nested_lines)
|
|
156
|
+
else
|
|
157
|
+
content = structure(line, info)
|
|
158
|
+
# Code that goes on over more lines takes its `do` once the line wrapper joins it.
|
|
159
|
+
content = Formatter.with_do(content) if block_opening_linenos.include?(lineno) && !info.text && next_info&.kind != :continuation
|
|
160
|
+
content = unless_for_negated_if(content, lines, outline, lineno) if content.start_with?("- if !")
|
|
161
|
+
formatted << (" " * indent) + content
|
|
162
|
+
end
|
|
126
163
|
previous_structure_lineno = lineno
|
|
127
164
|
blank_line_pending = false
|
|
128
165
|
else
|
|
@@ -134,6 +171,30 @@ module SuperSlim
|
|
|
134
171
|
formatted.empty? ? "" : "#{formatted.join("\n")}\n"
|
|
135
172
|
end
|
|
136
173
|
|
|
174
|
+
# `- if !condition` reads as `- unless condition` when the `!` covers the whole condition and no `else` follows,
|
|
175
|
+
# as RuboCop's Style/NegatedIf asks. A condition holding an operator, a ternary or a keyword keeps its `!`:
|
|
176
|
+
# `!a == b` compares `!a`.
|
|
177
|
+
def unless_for_negated_if(content, lines, outline, lineno)
|
|
178
|
+
condition = content[NEGATED_IF, 1]
|
|
179
|
+
return content if condition.nil? || condition.start_with?("(", "!") || condition.match?(COMPOUND_CONDITION)
|
|
180
|
+
return content if else_follows?(lines, outline, lineno)
|
|
181
|
+
|
|
182
|
+
"- unless #{condition}"
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
def else_follows?(lines, outline, lineno)
|
|
186
|
+
depth = outline[lineno].depth
|
|
187
|
+
((lineno + 1)...outline.size).each do |next_lineno|
|
|
188
|
+
info = outline[next_lineno]
|
|
189
|
+
next unless info.kind == :structure && info.depth <= depth
|
|
190
|
+
# A comment between the `if` and its `else` renders nothing.
|
|
191
|
+
next if lines[next_lineno - 1].match?(Parser::CODE_COMMENT)
|
|
192
|
+
|
|
193
|
+
return info.depth == depth && lines[next_lineno - 1].match?(ELSE_LINE)
|
|
194
|
+
end
|
|
195
|
+
false
|
|
196
|
+
end
|
|
197
|
+
|
|
137
198
|
def structure(line, info)
|
|
138
199
|
content = with_output_spacing(line, info).lstrip
|
|
139
200
|
content = content.rstrip unless info.text
|
|
@@ -145,6 +206,96 @@ module SuperSlim
|
|
|
145
206
|
end
|
|
146
207
|
end
|
|
147
208
|
|
|
209
|
+
# One threshold, both ways: `-` lines and `ruby:` blocks next to each other, at one depth, make one run of code
|
|
210
|
+
# lines; three lines or more go in a `ruby:` block, fewer go on `-` lines. Only lines of Ruby complete on their
|
|
211
|
+
# own move, with no block and no comment. Returns the lines that replace each run's first line, and the line
|
|
212
|
+
# numbers they replace.
|
|
213
|
+
def ruby_code_layout(lines, outline, parser, unformatted_linenos)
|
|
214
|
+
# [first line number, last line number, depth, code lines]
|
|
215
|
+
code_units = parser.line_by_node.filter_map do |node, lineno|
|
|
216
|
+
case node
|
|
217
|
+
when AST::Control
|
|
218
|
+
code = control_code(lines[lineno - 1]) if AST.empty?(node.children)
|
|
219
|
+
[lineno, lineno, outline[lineno].depth, [code]] if code && movable_code?(code) && !goes_on?(outline, lineno)
|
|
220
|
+
when AST::Embedded
|
|
221
|
+
next unless node.engine == "ruby" && lines[lineno - 1].match?(RUBY_BLOCK)
|
|
222
|
+
|
|
223
|
+
body_linenos = ((lineno + 1)...outline.size).take_while { outline[_1].owner == lineno }
|
|
224
|
+
body = body_linenos.map { lines[_1 - 1].strip }
|
|
225
|
+
[lineno, body_linenos.last, outline[lineno].depth, body] if body.any? && body_linenos.all? { outline[_1].kind == :text } && body.all? { movable_code?(_1) }
|
|
226
|
+
end
|
|
227
|
+
end
|
|
228
|
+
# `format off` keeps its lines as written.
|
|
229
|
+
code_units.reject! { |first_lineno, last_lineno| (first_lineno..last_lineno).any? { unformatted_linenos.include?(_1) } }
|
|
230
|
+
runs = code_units.sort_by!(&:first).chunk_while { |unit, next_unit| next_unit[0] == unit[1] + 1 && next_unit[2] == unit[2] }
|
|
231
|
+
flat_control_linenos = parser.line_by_node.filter_map { |node, lineno| lineno if node.is_a?(AST::Control) && AST.empty?(node.children) }.to_set
|
|
232
|
+
code_lines_by_lineno = {}
|
|
233
|
+
rewritten_linenos = Set.new
|
|
234
|
+
runs.each do |run|
|
|
235
|
+
code_size = run.sum { _1[3].size }
|
|
236
|
+
ruby_blocks = run.count { _1[0] != _1[1] }
|
|
237
|
+
# Already in its layout: a lone `ruby:` block long enough, or `-` lines too few.
|
|
238
|
+
next if (run.size == 1 && ruby_blocks == 1 && code_size >= RUBY_BLOCK_MIN_LINES) || (ruby_blocks.zero? && code_size < RUBY_BLOCK_MIN_LINES)
|
|
239
|
+
|
|
240
|
+
# A line Prism can't read alone stays where it is, and splits the run.
|
|
241
|
+
run.chunk { |unit| unit[3].all? { Prism.parse(_1).success? } }.each do |parsable, units|
|
|
242
|
+
code = units.flat_map(&:last)
|
|
243
|
+
ruby_block = code.size >= RUBY_BLOCK_MIN_LINES
|
|
244
|
+
layout_changes = ruby_block || units.any? { _1[0] != _1[1] }
|
|
245
|
+
next unless parsable && layout_changes
|
|
246
|
+
# `-` lines the run can't take, a comment or code going on below, still count for ConsecutiveControlStatements.
|
|
247
|
+
next if !ruby_block && code.size + flat_controls_beside(units, outline, flat_control_linenos) >= RUBY_BLOCK_MIN_LINES
|
|
248
|
+
|
|
249
|
+
code_lines_by_lineno[units.first[0]] = ruby_block ? ["ruby:", *code.map { (" " * INDENT_WIDTH) + _1 }] : code.map { "- #{_1}" }
|
|
250
|
+
rewritten_linenos.merge((units.first[0] + 1)..units.last[1])
|
|
251
|
+
end
|
|
252
|
+
end
|
|
253
|
+
[code_lines_by_lineno, rewritten_linenos]
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# The `-` lines with no children right before and after the units, at their depth, blank lines aside: the lint
|
|
257
|
+
# counts them in the same run.
|
|
258
|
+
def flat_controls_beside(units, outline, flat_control_linenos)
|
|
259
|
+
depth = units.first[2]
|
|
260
|
+
[(units.first[0] - 1).downto(1), ((units.last[1] + 1)...outline.size).each].sum do |linenos|
|
|
261
|
+
linenos.lazy
|
|
262
|
+
.reject { SKIPPED_KINDS.include?(outline[_1].kind) }
|
|
263
|
+
.take_while { outline[_1].kind == :structure && outline[_1].depth == depth && flat_control_linenos.include?(_1) }
|
|
264
|
+
.count
|
|
265
|
+
end
|
|
266
|
+
end
|
|
267
|
+
|
|
268
|
+
def goes_on?(outline, lineno)
|
|
269
|
+
next_info = outline[lineno + 1]
|
|
270
|
+
next_info&.owner == lineno && next_info.kind != :blank
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def control_code(line)
|
|
274
|
+
line[CONTROL_CODE, 1]&.rstrip
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
# A comment would end up after code on a `-` line, which Slim can't read; `#{}` in a string is no comment.
|
|
278
|
+
def movable_code?(code)
|
|
279
|
+
return false if code.match?(Compiler::BLOCK_SELF_OPENING) || code.match?(END_KEYWORD)
|
|
280
|
+
|
|
281
|
+
!code.include?("#") || Prism.parse(code).comments.empty?
|
|
282
|
+
end
|
|
283
|
+
|
|
284
|
+
# `tr: td = total` nests each tag in the one before: one per line, whatever the line holds. A line whose text goes
|
|
285
|
+
# on over the next ones keeps its inline tags, so that the text stays where it belongs.
|
|
286
|
+
def spread_nested_tags(line, info, depth)
|
|
287
|
+
starts = [0, *info.nested_tag_columns.map(&:last)]
|
|
288
|
+
ends = [*info.nested_tag_columns.map(&:first), line.size]
|
|
289
|
+
starts.zip(ends).each_with_index.map do |(start, finish), level|
|
|
290
|
+
last = level == starts.size - 1
|
|
291
|
+
tag_info = info.dup.tap do |nested_info|
|
|
292
|
+
nested_info.text = nil unless last
|
|
293
|
+
nested_info.output_column = (last && info.output_column) ? info.output_column - start : nil
|
|
294
|
+
end
|
|
295
|
+
(" " * ((depth + level) * INDENT_WIDTH)) + structure(line[start...finish], tag_info)
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
|
|
148
299
|
def continuation(line, info, indent_shift)
|
|
149
300
|
line = with_output_spacing(expand_tabs(line), info)
|
|
150
301
|
indent = [line[/\A */].size + indent_shift, 0].max
|
|
@@ -239,7 +390,12 @@ module SuperSlim
|
|
|
239
390
|
value = pending_values.pop
|
|
240
391
|
case value
|
|
241
392
|
when Prism::Node
|
|
242
|
-
|
|
393
|
+
if value.instance_of?(other_value.class)
|
|
394
|
+
return false unless node_comparator(value).call(value, other_value, pending_values)
|
|
395
|
+
else
|
|
396
|
+
pairs = negated_if_pairs(value, other_value) or return false
|
|
397
|
+
pending_values.concat(pairs)
|
|
398
|
+
end
|
|
243
399
|
when Array
|
|
244
400
|
return false unless other_value.is_a?(Array) && value.size == other_value.size
|
|
245
401
|
|
|
@@ -255,6 +411,21 @@ module SuperSlim
|
|
|
255
411
|
true
|
|
256
412
|
end
|
|
257
413
|
|
|
414
|
+
# `if !x` and `unless x` with no else run the same code, as RuboCop's Style/NegatedIf holds: the condition and the
|
|
415
|
+
# body still have to match. Only an object that redefines `!` tells them apart. Prism 0.19 names the else
|
|
416
|
+
# branch `consequent`.
|
|
417
|
+
def negated_if_pairs(node, other_node)
|
|
418
|
+
if_node, unless_node = node.is_a?(Prism::IfNode) ? [node, other_node] : [other_node, node]
|
|
419
|
+
return unless if_node.is_a?(Prism::IfNode) && unless_node.is_a?(Prism::UnlessNode)
|
|
420
|
+
|
|
421
|
+
negation = if_node.predicate
|
|
422
|
+
return unless negation.is_a?(Prism::CallNode) && negation.name == :! && negation.message == "!" && negation.arguments.nil?
|
|
423
|
+
return if if_node.respond_to?(:subsequent) ? if_node.subsequent : if_node.consequent
|
|
424
|
+
return if unless_node.respond_to?(:else_clause) ? unless_node.else_clause : unless_node.consequent
|
|
425
|
+
|
|
426
|
+
[negation.receiver, unless_node.predicate, if_node.statements, unless_node.statements]
|
|
427
|
+
end
|
|
428
|
+
|
|
258
429
|
def node_comparator(node)
|
|
259
430
|
COMPARATOR_BY_NODE_CLASS[node.class] ||= Formatter.send(:comparator_for, node.class, *node_fields(node))
|
|
260
431
|
end
|
data/lib/super_slim/parser.rb
CHANGED
|
@@ -53,8 +53,9 @@ 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
|
-
|
|
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)
|
|
58
59
|
FORMAT_DIRECTIVE = %r{\A?\s*/\s*super-slim:format\s+(off|on)\s*\z}
|
|
59
60
|
# A comment that renders nothing: not `/!` or `/[if IE]`.
|
|
60
61
|
CODE_COMMENT = %r{\A?\s*/(?![!\[])}
|
|
@@ -511,6 +512,7 @@ module SuperSlim
|
|
|
511
512
|
# Not `column`, which counts the spaces stripped from the end of a continuation line.
|
|
512
513
|
output_column = @scanner_column + scanner_charpos if @outline
|
|
513
514
|
if @scanner.skip(NESTED_TAG)
|
|
515
|
+
(@outline[@lineno].nested_tag_columns ||= []) << [output_column, @scanner_column + scanner_charpos] if @outline
|
|
514
516
|
return [[parse_embedded(@scanner[1], @scanner[2], @scanner.rest)]] if @scanner.skip(ENGINE)
|
|
515
517
|
|
|
516
518
|
(tag = scan_tag) || syntax_error!("Expected tag")
|
data/lib/super_slim/version.rb
CHANGED