super-slim 3.3.0 → 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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -1
- data/README.md +8 -1
- data/lib/super_slim/formatter/line_wrapper.rb +52 -3
- data/lib/super_slim/formatter.rb +158 -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: d9189c83ed4be022cdc4c2a3bd54f130e6fe66826811fa47c15149a72dc04419
|
|
4
|
+
data.tar.gz: 63648f18984eb4fe426be14a8af378c94703ae4e02793fadd4182608531db72f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 635a722ab669d1e1f3410592759c5a3055ebaead833178884a848e3bfbcf1cd2af5044c8cd33e5974cf8c17607e1ffde37bc3fb5ae21d282b6337da3f240e049
|
|
7
|
+
data.tar.gz: 505ca7cbfdf29e34a6ba0ef46ccb981066ae72e0321a0bf442428b6965f99b20cf247a4bafa10271c533fa6d52144d0d28949bc7eecc918e4f2bda6fc1379249
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,22 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [3.4.0] — 2026-11-07
|
|
6
|
+
|
|
7
|
+
One layout for inline tags, short children, `ruby:` blocks and negated conditions.
|
|
8
|
+
|
|
9
|
+
**New**
|
|
10
|
+
- `super-slim format` puts tags nested inline after a colon (`tr: td = total`) one per line, their children going as
|
|
11
|
+
many levels deeper. A line whose text goes on below keeps its inline tags.
|
|
12
|
+
- `-` lines and `ruby:` blocks next to each other at one depth make one run of code: three lines or more go in a
|
|
13
|
+
`ruby:` block, as the ConsecutiveControlStatements rule asks, and fewer on `-` lines. Only lines of Ruby complete on
|
|
14
|
+
their own move.
|
|
15
|
+
- With `max_line_length`, a tag whose only child is one `=` output or one line of `|` text takes it on its own line
|
|
16
|
+
when both fit: `td = total`, `p Hello`. Text that could read as an attribute stays below.
|
|
17
|
+
- `- if !condition` becomes `- unless condition` when the `!` covers the whole condition and no `else` follows. The
|
|
18
|
+
formatter's check takes the two for the same code.
|
|
19
|
+
- Lines under `/ super-slim:format off` stay out of these rules.
|
|
20
|
+
|
|
5
21
|
## [3.3.0] — 2026-11-07
|
|
6
22
|
|
|
7
23
|
Formatted templates that Slim and Brakeman read, lines and rules you can opt out of, and a formatter about 30% faster.
|
|
@@ -222,7 +238,8 @@ renders what Slim 5 renders with `format: :html, sort_attrs: false`, byte for by
|
|
|
222
238
|
- 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
239
|
newer than the one Ruby 3.3 ships.
|
|
224
240
|
|
|
225
|
-
[Unreleased]: https://github.com/levups/super-slim/compare/v3.
|
|
241
|
+
[Unreleased]: https://github.com/levups/super-slim/compare/v3.4.0...HEAD
|
|
242
|
+
[3.4.0]: https://github.com/levups/super-slim/compare/v3.3.0...v3.4.0
|
|
226
243
|
[3.3.0]: https://github.com/levups/super-slim/compare/v3.2.2...v3.3.0
|
|
227
244
|
[3.2.2]: https://github.com/levups/super-slim/compare/v3.2.1...v3.2.2
|
|
228
245
|
[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,22 @@ 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
|
+
RUBY_BLOCK = /\A\s*ruby:\s*\z/
|
|
27
|
+
CONTROL_RUN = /^( *)- .*\n\1- .*\n\1- /
|
|
20
28
|
# `while x do` is valid Ruby that nobody writes.
|
|
21
29
|
LOOP = /\A(?:while|until|for)\b/
|
|
30
|
+
END_KEYWORD = /\Aend\b/
|
|
22
31
|
# Prism 1.x counts in a node's flags whether it starts a line.
|
|
23
32
|
NEWLINE_FLAG = defined?(Prism::NodeFlags::NEWLINE) ? Prism::NodeFlags::NEWLINE : 0
|
|
24
33
|
FIELDS_BY_NODE_CLASS = {}
|
|
@@ -69,6 +78,13 @@ module SuperSlim
|
|
|
69
78
|
wrapped = @line_wrapper.call(formatted, parser:)
|
|
70
79
|
return formatted if wrapped == formatted
|
|
71
80
|
|
|
81
|
+
# `-` code joined by the wrapper may now make a run long enough for a `ruby:` block. A wrap Slim can't read is
|
|
82
|
+
# refused below.
|
|
83
|
+
if wrapped.match?(CONTROL_RUN)
|
|
84
|
+
wrapped_parser = parsed_template(wrapped).last
|
|
85
|
+
wrapped = layout(wrapped, wrapped_parser) if wrapped_parser
|
|
86
|
+
end
|
|
87
|
+
|
|
72
88
|
wrapped_ruby = parsed_ruby(compiled_code(parsed_template(wrapped, outline: false).first))
|
|
73
89
|
# Only Prism's tree vouches for a wrap: a compile error can read the same before and after it.
|
|
74
90
|
return formatted unless wrapped_ruby.is_a?(Prism::ParseResult)
|
|
@@ -97,7 +113,12 @@ module SuperSlim
|
|
|
97
113
|
indent_shift = 0
|
|
98
114
|
previous_structure_lineno = nil
|
|
99
115
|
blank_line_pending = false
|
|
116
|
+
# [depth, levels]: the children of a line whose inline tags were spread go that many levels deeper.
|
|
117
|
+
spread_levels = []
|
|
118
|
+
code_lines_by_lineno, rewritten_linenos = ruby_code_layout(lines, outline, parser, unformatted_linenos)
|
|
100
119
|
lines.each.with_index(1) do |line, lineno|
|
|
120
|
+
next if rewritten_linenos.include?(lineno)
|
|
121
|
+
|
|
101
122
|
info = outline[lineno]
|
|
102
123
|
# Unformatted lines move with the directive above them, so that they stay inside its block.
|
|
103
124
|
if unformatted_linenos.include?(lineno)
|
|
@@ -117,12 +138,27 @@ module SuperSlim
|
|
|
117
138
|
# A block's first line comes right after the line that opens it.
|
|
118
139
|
formatted.pop if blank_line_pending && previous_info && info.depth > previous_info.depth
|
|
119
140
|
formatted << "" if previous_structure_lineno && previous_structure_lineno == locals_header_lineno && formatted.last != ""
|
|
120
|
-
|
|
141
|
+
spread_levels.pop while spread_levels.last && info.depth <= spread_levels.last[0]
|
|
142
|
+
depth = info.depth + spread_levels.sum { |_depth, levels| levels }
|
|
143
|
+
indent = depth * INDENT_WIDTH
|
|
121
144
|
indent_shift = indent_shift_by_lineno[lineno] = indent - indent_of(line)
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
145
|
+
next_info = outline[lineno + 1]
|
|
146
|
+
if (code_lines = code_lines_by_lineno[lineno])
|
|
147
|
+
formatted.concat(code_lines.map { (" " * indent) + _1 })
|
|
148
|
+
elsif info.nested_tag_columns && !(goes_on?(outline, lineno) && next_info.kind != :continuation)
|
|
149
|
+
nested_lines = spread_nested_tags(line, info, depth)
|
|
150
|
+
levels = nested_lines.size - 1
|
|
151
|
+
spread_levels << [info.depth, levels]
|
|
152
|
+
# Attributes going on over the next lines belong to the last tag, and go as many levels deeper.
|
|
153
|
+
indent_shift_by_lineno[lineno] += levels * INDENT_WIDTH
|
|
154
|
+
formatted.concat(nested_lines)
|
|
155
|
+
else
|
|
156
|
+
content = structure(line, info)
|
|
157
|
+
# Code that goes on over more lines takes its `do` once the line wrapper joins it.
|
|
158
|
+
content = Formatter.with_do(content) if block_opening_linenos.include?(lineno) && !info.text && next_info&.kind != :continuation
|
|
159
|
+
content = unless_for_negated_if(content, lines, outline, lineno) if content.start_with?("- if !")
|
|
160
|
+
formatted << (" " * indent) + content
|
|
161
|
+
end
|
|
126
162
|
previous_structure_lineno = lineno
|
|
127
163
|
blank_line_pending = false
|
|
128
164
|
else
|
|
@@ -134,6 +170,30 @@ module SuperSlim
|
|
|
134
170
|
formatted.empty? ? "" : "#{formatted.join("\n")}\n"
|
|
135
171
|
end
|
|
136
172
|
|
|
173
|
+
# `- if !condition` reads as `- unless condition` when the `!` covers the whole condition and no `else` follows,
|
|
174
|
+
# as RuboCop's Style/NegatedIf asks. A condition holding an operator, a ternary or a keyword keeps its `!`:
|
|
175
|
+
# `!a == b` compares `!a`.
|
|
176
|
+
def unless_for_negated_if(content, lines, outline, lineno)
|
|
177
|
+
condition = content[NEGATED_IF, 1]
|
|
178
|
+
return content if condition.nil? || condition.start_with?("(", "!") || condition.match?(COMPOUND_CONDITION)
|
|
179
|
+
return content if else_follows?(lines, outline, lineno)
|
|
180
|
+
|
|
181
|
+
"- unless #{condition}"
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def else_follows?(lines, outline, lineno)
|
|
185
|
+
depth = outline[lineno].depth
|
|
186
|
+
((lineno + 1)...outline.size).each do |next_lineno|
|
|
187
|
+
info = outline[next_lineno]
|
|
188
|
+
next unless info.kind == :structure && info.depth <= depth
|
|
189
|
+
# A comment between the `if` and its `else` renders nothing.
|
|
190
|
+
next if lines[next_lineno - 1].match?(Parser::CODE_COMMENT)
|
|
191
|
+
|
|
192
|
+
return info.depth == depth && lines[next_lineno - 1].match?(ELSE_LINE)
|
|
193
|
+
end
|
|
194
|
+
false
|
|
195
|
+
end
|
|
196
|
+
|
|
137
197
|
def structure(line, info)
|
|
138
198
|
content = with_output_spacing(line, info).lstrip
|
|
139
199
|
content = content.rstrip unless info.text
|
|
@@ -145,6 +205,78 @@ module SuperSlim
|
|
|
145
205
|
end
|
|
146
206
|
end
|
|
147
207
|
|
|
208
|
+
# One threshold, both ways: `-` lines and `ruby:` blocks next to each other, at one depth, make one run of code
|
|
209
|
+
# lines; three lines or more go in a `ruby:` block, fewer go on `-` lines. Only lines of Ruby complete on their
|
|
210
|
+
# own move, with no block and no comment. Returns the lines that replace each run's first line, and the line
|
|
211
|
+
# numbers they replace.
|
|
212
|
+
def ruby_code_layout(lines, outline, parser, unformatted_linenos)
|
|
213
|
+
# [first line number, last line number, depth, code lines]
|
|
214
|
+
code_units = parser.line_by_node.filter_map do |node, lineno|
|
|
215
|
+
case node
|
|
216
|
+
when AST::Control
|
|
217
|
+
code = control_code(lines[lineno - 1]) if AST.empty?(node.children)
|
|
218
|
+
[lineno, lineno, outline[lineno].depth, [code]] if code && movable_code?(code) && !goes_on?(outline, lineno)
|
|
219
|
+
when AST::Embedded
|
|
220
|
+
next unless node.engine == "ruby" && lines[lineno - 1].match?(RUBY_BLOCK)
|
|
221
|
+
|
|
222
|
+
body_linenos = ((lineno + 1)...outline.size).take_while { outline[_1].owner == lineno }
|
|
223
|
+
body = body_linenos.map { lines[_1 - 1].strip }
|
|
224
|
+
[lineno, body_linenos.last, outline[lineno].depth, body] if body.any? && body_linenos.all? { outline[_1].kind == :text } && body.all? { movable_code?(_1) }
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
# `format off` keeps its lines as written.
|
|
228
|
+
code_units.reject! { |first_lineno, last_lineno| (first_lineno..last_lineno).any? { unformatted_linenos.include?(_1) } }
|
|
229
|
+
runs = code_units.sort_by!(&:first).chunk_while { |unit, next_unit| next_unit[0] == unit[1] + 1 && next_unit[2] == unit[2] }
|
|
230
|
+
code_lines_by_lineno = {}
|
|
231
|
+
rewritten_linenos = Set.new
|
|
232
|
+
runs.each do |run|
|
|
233
|
+
code_size = run.sum { _1[3].size }
|
|
234
|
+
ruby_blocks = run.count { _1[0] != _1[1] }
|
|
235
|
+
# Already in its layout: a lone `ruby:` block long enough, or `-` lines too few.
|
|
236
|
+
next if (run.size == 1 && ruby_blocks == 1 && code_size >= RUBY_BLOCK_MIN_LINES) || (ruby_blocks.zero? && code_size < RUBY_BLOCK_MIN_LINES)
|
|
237
|
+
|
|
238
|
+
# A line Prism can't read alone stays where it is, and splits the run.
|
|
239
|
+
run.chunk { |unit| unit[3].all? { Prism.parse(_1).success? } }.each do |parsable, units|
|
|
240
|
+
code = units.flat_map(&:last)
|
|
241
|
+
ruby_block = code.size >= RUBY_BLOCK_MIN_LINES
|
|
242
|
+
layout_changes = ruby_block || units.any? { _1[0] != _1[1] }
|
|
243
|
+
next unless parsable && layout_changes
|
|
244
|
+
|
|
245
|
+
code_lines_by_lineno[units.first[0]] = ruby_block ? ["ruby:", *code.map { (" " * INDENT_WIDTH) + _1 }] : code.map { "- #{_1}" }
|
|
246
|
+
rewritten_linenos.merge((units.first[0] + 1)..units.last[1])
|
|
247
|
+
end
|
|
248
|
+
end
|
|
249
|
+
[code_lines_by_lineno, rewritten_linenos]
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def goes_on?(outline, lineno)
|
|
253
|
+
next_info = outline[lineno + 1]
|
|
254
|
+
next_info&.owner == lineno && next_info.kind != :blank
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
def control_code(line)
|
|
258
|
+
line[CONTROL_CODE, 1]&.rstrip
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
def movable_code?(code)
|
|
262
|
+
!code.include?("#") && !code.match?(Compiler::BLOCK_SELF_OPENING) && !code.match?(END_KEYWORD)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
# `tr: td = total` nests each tag in the one before: one per line, whatever the line holds. A line whose text goes
|
|
266
|
+
# on over the next ones keeps its inline tags, so that the text stays where it belongs.
|
|
267
|
+
def spread_nested_tags(line, info, depth)
|
|
268
|
+
starts = [0, *info.nested_tag_columns.map(&:last)]
|
|
269
|
+
ends = [*info.nested_tag_columns.map(&:first), line.size]
|
|
270
|
+
starts.zip(ends).each_with_index.map do |(start, finish), level|
|
|
271
|
+
last = level == starts.size - 1
|
|
272
|
+
tag_info = info.dup.tap do |nested_info|
|
|
273
|
+
nested_info.text = nil unless last
|
|
274
|
+
nested_info.output_column = (last && info.output_column) ? info.output_column - start : nil
|
|
275
|
+
end
|
|
276
|
+
(" " * ((depth + level) * INDENT_WIDTH)) + structure(line[start...finish], tag_info)
|
|
277
|
+
end
|
|
278
|
+
end
|
|
279
|
+
|
|
148
280
|
def continuation(line, info, indent_shift)
|
|
149
281
|
line = with_output_spacing(expand_tabs(line), info)
|
|
150
282
|
indent = [line[/\A */].size + indent_shift, 0].max
|
|
@@ -239,7 +371,12 @@ module SuperSlim
|
|
|
239
371
|
value = pending_values.pop
|
|
240
372
|
case value
|
|
241
373
|
when Prism::Node
|
|
242
|
-
|
|
374
|
+
if value.instance_of?(other_value.class)
|
|
375
|
+
return false unless node_comparator(value).call(value, other_value, pending_values)
|
|
376
|
+
else
|
|
377
|
+
pairs = negated_if_pairs(value, other_value) or return false
|
|
378
|
+
pending_values.concat(pairs)
|
|
379
|
+
end
|
|
243
380
|
when Array
|
|
244
381
|
return false unless other_value.is_a?(Array) && value.size == other_value.size
|
|
245
382
|
|
|
@@ -255,6 +392,21 @@ module SuperSlim
|
|
|
255
392
|
true
|
|
256
393
|
end
|
|
257
394
|
|
|
395
|
+
# `if !x` and `unless x` with no else run the same code, as RuboCop's Style/NegatedIf holds: the condition and the
|
|
396
|
+
# body still have to match. Only an object that redefines `!` tells them apart. Prism 0.19 names the else
|
|
397
|
+
# branch `consequent`.
|
|
398
|
+
def negated_if_pairs(node, other_node)
|
|
399
|
+
if_node, unless_node = node.is_a?(Prism::IfNode) ? [node, other_node] : [other_node, node]
|
|
400
|
+
return unless if_node.is_a?(Prism::IfNode) && unless_node.is_a?(Prism::UnlessNode)
|
|
401
|
+
|
|
402
|
+
negation = if_node.predicate
|
|
403
|
+
return unless negation.is_a?(Prism::CallNode) && negation.name == :! && negation.message == "!" && negation.arguments.nil?
|
|
404
|
+
return if if_node.respond_to?(:subsequent) ? if_node.subsequent : if_node.consequent
|
|
405
|
+
return if unless_node.respond_to?(:else_clause) ? unless_node.else_clause : unless_node.consequent
|
|
406
|
+
|
|
407
|
+
[negation.receiver, unless_node.predicate, if_node.statements, unless_node.statements]
|
|
408
|
+
end
|
|
409
|
+
|
|
258
410
|
def node_comparator(node)
|
|
259
411
|
COMPARATOR_BY_NODE_CLASS[node.class] ||= Formatter.send(:comparator_for, node.class, *node_fields(node))
|
|
260
412
|
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