w_syntax_tree-erb 0.10.2 → 0.10.4

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: 657167139e259c320442756b2da4ce405e3ef3df7c3c370e6cd681561c3e7de6
4
- data.tar.gz: 7f369c4b9bb2416a533a10734a4b384496712712e9787465d96ff76661a8be6d
3
+ metadata.gz: 5da234530c78c55f3594957cdf44e380ed603b92e52c023df68f015d0d8c7f45
4
+ data.tar.gz: 99b3b25d533a09e6337cc552c5f6d9224e977586272afb4c358df277e08c09eb
5
5
  SHA512:
6
- metadata.gz: 2a4efea93faf97c4fe05e9a7af6e0dcfd488ffbdea1ad1380e4d13d2a033490cc65ce275b3311d1c1d4c50d7d59efce2552fdd4e13a8324fc8cd5d514ac1c513
7
- data.tar.gz: 022be3662cdffa4bf6416354a1f8b518bf1ca0f84875c99d5a919519b7b48e8e8ec608f132c6e3a0f1eaeb2651a7ae337dec0b7961ea14782019205f0221faa2
6
+ metadata.gz: a408ef26dd34ae012f516224090d65eea520b795c7a50080a8ed603b09095bc0eb7198e97b0ea7d75b9d113896344496cf1854442bf3ee9eab276e2437e86893
7
+ data.tar.gz: dbd2ff290949bc982fe16b7c3153ae01b09adfd8d4fd9e710cca7b6a3dce19646b770274cd00f0438d42ca3daa7a89b824411a9624db98e4aff13b014db03478
data/CHANGELOG.md CHANGED
@@ -6,6 +6,26 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## [0.10.4] - 2023-08-28
10
+
11
+ - Avoid grouping single tags
12
+ - Handle multiline ERB-comments
13
+
14
+ ## [0.10.3] - 2023-08-27
15
+
16
+ ## Fixes
17
+
18
+ - Allows parsing ERB-tags with if, else and end in the same tag
19
+
20
+ ```erb
21
+ <%= if true
22
+ what
23
+ end %>
24
+ ```
25
+
26
+ This opens the possibility for formatting all if-statements with SyntaxTree properly
27
+ and removes the fix where any if-statement was force to one line.
28
+
9
29
  ## [0.10.2] - 2023-08-22
10
30
 
11
31
  ### Fixes
@@ -83,7 +103,10 @@ Output:
83
103
  - Can format a lot of .html.erb-syntax and works as a plugin to syntax_tree.
84
104
  - This is still early and there are a lot of different weird syntaxes out there.
85
105
 
86
- [unreleased]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.10.1...HEAD
106
+ [unreleased]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.10.4...HEAD
107
+ [0.10.4]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.10.3...v0.10.4
108
+ [0.10.3]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.10.2...v0.10.3
109
+ [0.10.2]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.10.1...v0.10.2
87
110
  [0.10.1]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.10.0...v0.10.1
88
111
  [0.10.0]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.9.5...v0.10.0
89
112
  [0.9.5]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.9.4...v0.9.5
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- w_syntax_tree-erb (0.10.2)
4
+ w_syntax_tree-erb (0.10.4)
5
5
  prettier_print (~> 1.2, >= 1.2.0)
6
6
  syntax_tree (~> 6.1, >= 6.1.1)
7
7
 
@@ -114,7 +114,8 @@ module SyntaxTree
114
114
  q.text(" ")
115
115
  visit(node.keyword)
116
116
  end
117
- node.content.nil? ? q.text(" ") : visit(node.content)
117
+
118
+ node.content.blank? ? q.text(" ") : visit(node.content)
118
119
 
119
120
  visit(node.closing_tag)
120
121
  end
@@ -137,33 +138,32 @@ module SyntaxTree
137
138
  end
138
139
 
139
140
  def visit_erb_content(node)
140
- if node.value.is_a?(String)
141
- output_rows(node.value.split("\n"))
142
- else
143
- nodes = node.value&.statements&.child_nodes || []
144
- nodes = nodes.reject { |node| node.is_a?(SyntaxTree::VoidStmt) }
141
+ # Reject all VoidStmt to avoid empty lines
142
+ nodes =
143
+ (node.value&.statements&.child_nodes || []).reject do |node|
144
+ node.is_a?(SyntaxTree::VoidStmt)
145
+ end
145
146
 
146
- if nodes.size == 1
147
- q.text(" ")
147
+ if nodes.size == 1
148
+ q.text(" ")
149
+ format_statement(nodes.first)
150
+ q.text(" ")
151
+ elsif nodes.size > 1
152
+ q.indent do
153
+ q.breakable("")
148
154
  q.seplist(nodes, -> { q.breakable("") }) do |child_node|
149
155
  format_statement(child_node)
150
156
  end
151
- q.text(" ")
152
- elsif nodes.size > 1
153
- q.indent do
154
- q.breakable("")
155
- q.seplist(nodes, -> { q.breakable("") }) do |child_node|
156
- format_statement(child_node)
157
- end
158
- end
159
- q.breakable
160
157
  end
158
+
159
+ q.breakable
161
160
  end
162
161
  end
163
162
 
164
163
  def format_statement(statement)
165
164
  formatter =
166
- SyntaxTree::Formatter.new("", [], erb_print_width(statement))
165
+ SyntaxTree::Formatter.new("", [], SyntaxTree::ERB::MAX_WIDTH)
166
+
167
167
  formatter.format(statement)
168
168
  formatter.flush
169
169
  rows = formatter.output.join.split("\n")
@@ -305,24 +305,6 @@ module SyntaxTree
305
305
  node.respond_to?(:new_line) ? node.new_line&.count || 0 : 0
306
306
  end
307
307
 
308
- def erb_print_width(node)
309
- # Set the width to maximum if we have an IfNode or IfOp,
310
- # we cannot format them purely with SyntaxTree because the ERB-syntax will be unparseable.
311
- check_for_if_statement(node) ? 999_999 : SyntaxTree::ERB::MAX_WIDTH
312
- end
313
-
314
- def check_for_if_statement(node)
315
- return false if node.nil?
316
-
317
- if node.is_a?(SyntaxTree::IfNode) || node.is_a?(SyntaxTree::IfOp)
318
- return true
319
- end
320
-
321
- node.child_nodes.any? do |child_node|
322
- check_for_if_statement(child_node)
323
- end
324
- end
325
-
326
308
  def handle_child_nodes(child_nodes)
327
309
  group = []
328
310
 
@@ -358,20 +340,26 @@ module SyntaxTree
358
340
  end
359
341
 
360
342
  def handle_group(nodes, break_after:)
361
- return unless nodes.any?
362
-
363
- q.group do
364
- nodes.each_with_index do |node, group_index|
365
- visit(node)
366
- next_node = nodes[group_index + 1]
367
- next if next_node.nil?
368
- breakable_between_group(node, next_node)
369
- end
343
+ if nodes.size == 1
344
+ handle_group_nodes(nodes)
345
+ elsif nodes.size > 1
346
+ q.group { handle_group_nodes(nodes) }
347
+ else
348
+ return
370
349
  end
371
350
 
372
351
  breakable_between_group(nodes.last, nil) if break_after
373
352
  end
374
353
 
354
+ def handle_group_nodes(nodes)
355
+ nodes.each_with_index do |node, group_index|
356
+ visit(node)
357
+ next_node = nodes[group_index + 1]
358
+ next if next_node.nil?
359
+ breakable_between_group(node, next_node)
360
+ end
361
+ end
362
+
375
363
  def node_should_group(node)
376
364
  node.is_a?(SyntaxTree::ERB::CharData) ||
377
365
  node.is_a?(SyntaxTree::ERB::ErbNode)
@@ -295,16 +295,7 @@ module SyntaxTree
295
295
  )
296
296
  end
297
297
 
298
- @content =
299
- if content.is_a?(ErbContent)
300
- content
301
- else
302
- # Set content to nil if it is empty
303
- content ||= []
304
- content = content.map(&:value).join if content.is_a?(Array)
305
- ErbContent.new(value: content) unless content.strip.empty?
306
- end
307
-
298
+ @content = prepare_content(content)
308
299
  @closing_tag = closing_tag
309
300
  end
310
301
 
@@ -338,6 +329,24 @@ module SyntaxTree
338
329
  closing_tag: closing_tag
339
330
  )
340
331
  end
332
+
333
+ private
334
+
335
+ def prepare_content(content)
336
+ if content.is_a?(ErbContent)
337
+ content
338
+ else
339
+ # Set content to nil if it is empty
340
+ content ||= []
341
+
342
+ ErbContent.new(value: content)
343
+ end
344
+ rescue SyntaxTree::Parser::ParseError
345
+ # Try to add the keyword to see if it parses
346
+ result = ErbContent.new(value: [keyword, *content])
347
+ @keyword = nil
348
+ result
349
+ end
341
350
  end
342
351
 
343
352
  class ErbBlock < Block
@@ -413,7 +422,7 @@ module SyntaxTree
413
422
 
414
423
  class ErbElse < ErbIf
415
424
  def accept(visitor)
416
- visitor.visit_erb_if(self)
425
+ visitor.visit_erb_else(self)
417
426
  end
418
427
  end
419
428
 
@@ -448,16 +457,23 @@ module SyntaxTree
448
457
  end
449
458
 
450
459
  class ErbContent < Node
451
- attr_reader(:value, :unparsed_value)
460
+ attr_reader(:value)
452
461
 
453
462
  def initialize(value:)
454
- @unparsed_value = value
455
- begin
456
- @value = SyntaxTree.parse(value.strip)
457
- rescue SyntaxTree::Parser::ParseError
458
- # Removes leading and trailing whitespace
459
- @value = value&.lstrip&.rstrip
463
+ if value.is_a?(Array)
464
+ value =
465
+ value.map { |token| token.is_a?(Token) ? token.value : token }.join
460
466
  end
467
+ @value = SyntaxTree.parse(value.strip)
468
+ end
469
+
470
+ def blank?
471
+ value.nil? ||
472
+ value
473
+ .statements
474
+ .child_nodes
475
+ .reject { |node| node.is_a?(SyntaxTree::VoidStmt) }
476
+ .empty?
461
477
  end
462
478
 
463
479
  def accept(visitor)
@@ -94,7 +94,7 @@ module SyntaxTree
94
94
  # <!DOCTYPE
95
95
  enum.yield :doctype, $&, index, line
96
96
  state << :inside
97
- when /\A<%#.*%>/
97
+ when /\A<%#[\s\S]*?%>/
98
98
  # An ERB-comment
99
99
  # <%# this is an ERB comment %>
100
100
  enum.yield :erb_comment, $&, index, line
@@ -588,7 +588,7 @@ module SyntaxTree
588
588
  location: opening_tag.location.to(closing_tag.location)
589
589
  )
590
590
 
591
- case keyword&.type
591
+ case erb_node.keyword&.type
592
592
  when :erb_if, :erb_unless, :erb_elsif
593
593
  parse_erb_if(erb_node)
594
594
  when :erb_case, :erb_when
@@ -2,6 +2,6 @@
2
2
 
3
3
  module SyntaxTree
4
4
  module ERB
5
- VERSION = "0.10.2"
5
+ VERSION = "0.10.4"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: w_syntax_tree-erb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.2
4
+ version: 0.10.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Newton
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-08-22 00:00:00.000000000 Z
12
+ date: 2023-08-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: prettier_print
@@ -159,7 +159,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
159
159
  - !ruby/object:Gem::Version
160
160
  version: '0'
161
161
  requirements: []
162
- rubygems_version: 3.4.19
162
+ rubygems_version: 3.4.10
163
163
  signing_key:
164
164
  specification_version: 4
165
165
  summary: Syntax Tree support for ERB