w_syntax_tree-erb 0.9.0 → 0.9.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: 4b1234ed5afef9a96ebefb415733354ac530ffa9bb35b718195d36be9f9fed7c
4
- data.tar.gz: 03c589bb628753e874f05aa6ea504f0d3d14e2057a7843d919abfd31f9fc66e9
3
+ metadata.gz: bc192c98db6bb573305fc6bfec6cbf35415eb53376ddb19a977e95f0d0812453
4
+ data.tar.gz: 20c52afb0c5650fcb0ed0cfac5ad259e852a958ee1480d6c201d465e2dca2f33
5
5
  SHA512:
6
- metadata.gz: cb5dd5cad343aabf4eaa8bd8cb177eeabb673cf68b8a016f36c13ee5ff2fbbb277a313800849cee940013355e430edca26d5d8541d566588f7902c41816d20a9
7
- data.tar.gz: 3a147354e9fbef1bf89b604033c57f3746f1e17e69d050e7b4b41ffc409005000f4a7e5a12085e2704da71c4de4e16691795f6fe4600ba04de1106f5179c15b6
6
+ metadata.gz: 4037694cab3fb721931d23d6b71b6702c8317a8cfcc02385f5ffead99b12b05928a449cf61f2d5ccd56fde08a42c1a3eab5bce933930e5af7758334c078a2e6c
7
+ data.tar.gz: fac163a673ebf3f60900162cd63f3f6cc7d5f227ecb7098d72a769a9156d031a7f579b1237e5fa63fc50d8a5672c487f433d6e2b940fc39a002c28cb055ee26f
data/CHANGELOG.md CHANGED
@@ -6,11 +6,17 @@ 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.0.1] - 2023-05-25
9
+ ## [0.9.1] - 2023-06-28
10
+
11
+ - Handle formatting of multi-line ERB-tags with more than one statement.
12
+
13
+ ## [0.9.0] - 2023-06-22
10
14
 
11
15
  ### Added
12
16
 
13
- - 🎉 First version based on syntax_tree-xml 🎉
17
+ - 🎉 First version based on syntax_tree-xml 🎉.
18
+ - Can format a lot of .html.erb-syntax and works as a plugin to syntax_tree.
19
+ - This is still early and there are a lot of different weird syntaxes out there.
14
20
 
15
- [unreleased]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.1.0...HEAD
16
- [0.0.1]: https://github.com/davidwessman/syntax_tree-erb/compare/b280a...v0.1.0
21
+ [unreleased]: https://github.com/davidwessman/syntax_tree-erb/compare/v0.9.0...HEAD
22
+ [0.9.0]: https://github.com/davidwessman/syntax_tree-erb/compare/419727a73af94057ca0980733e69ac8b4d52fdf4...v0.9.0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree-erb (0.0.1)
4
+ w_syntax_tree-erb (0.9.1)
5
5
  prettier_print (>= 1.2.0)
6
6
  syntax_tree (>= 6.1.1)
7
7
 
@@ -9,7 +9,7 @@ GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
11
  docile (1.4.0)
12
- minitest (5.18.0)
12
+ minitest (5.18.1)
13
13
  prettier_print (1.2.1)
14
14
  rake (13.0.6)
15
15
  simplecov (0.22.0)
@@ -32,7 +32,7 @@ DEPENDENCIES
32
32
  minitest
33
33
  rake
34
34
  simplecov
35
- syntax_tree-erb!
35
+ w_syntax_tree-erb!
36
36
 
37
37
  BUNDLED WITH
38
38
  2.4.1
data/README.md CHANGED
@@ -33,6 +33,8 @@ Add this line to your application's Gemfile:
33
33
  gem "w_syntax_tree-erb", "~> 0.9", require: false
34
34
  ```
35
35
 
36
+ > I added the `w_` prefix to avoid conflicts if there will ever be an official `syntax_tree-erb` gem.
37
+
36
38
  ## Usage
37
39
 
38
40
  ```sh
@@ -80,8 +80,7 @@ module SyntaxTree
80
80
  q.text(" ")
81
81
  visit(node.keyword)
82
82
  end
83
-
84
- visit(node.content)
83
+ node.content.nil? ? q.text(" ") : visit(node.content)
85
84
 
86
85
  visit(node.closing_tag)
87
86
  end
@@ -104,29 +103,44 @@ module SyntaxTree
104
103
  end
105
104
 
106
105
  def visit_erb_content(node)
107
- rows =
108
- if node.value.is_a?(String)
109
- node.value.split("\n")
110
- else
111
- formatter =
112
- SyntaxTree::Formatter.new("", [], erb_print_width(node.value))
113
- formatter.format(node.value.statements)
114
- formatter.flush
115
- formatter.output.join.split("\n")
116
- end
106
+ if node.value.is_a?(String)
107
+ output_rows(node.value.split("\n"))
108
+ else
109
+ child_nodes = node.value&.statements&.child_nodes || []
117
110
 
118
- if rows.size > 1
119
- q.group do
111
+ if child_nodes.size == 1
120
112
  q.text(" ")
121
- q.seplist(rows, -> { q.breakable(" ") }) { |row| q.text(row) }
113
+ q.seplist(child_nodes, -> { q.breakable("") }) do |child_node|
114
+ format_statement(child_node)
115
+ end
122
116
  q.text(" ")
117
+ elsif child_nodes.size > 1
118
+ q.indent do
119
+ q.breakable("")
120
+ q.seplist(child_nodes, -> { q.breakable("") }) do |child_node|
121
+ format_statement(child_node)
122
+ end
123
+ end
124
+ q.breakable
123
125
  end
126
+ end
127
+ end
128
+
129
+ def format_statement(statement)
130
+ formatter =
131
+ SyntaxTree::Formatter.new("", [], erb_print_width(statement))
132
+ formatter.format(statement)
133
+ formatter.flush
134
+ rows = formatter.output.join.split("\n")
135
+
136
+ output_rows(formatter.output.join.split("\n"))
137
+ end
138
+
139
+ def output_rows(rows)
140
+ if rows.size > 1
141
+ q.seplist(rows, -> { q.breakable("") }) { |row| q.text(row) }
124
142
  elsif rows.size == 1
125
- q.text(" ")
126
143
  q.text(rows.first)
127
- q.text(" ")
128
- else
129
- q.text(" ")
130
144
  end
131
145
  end
132
146
 
@@ -203,15 +217,10 @@ module SyntaxTree
203
217
  end
204
218
  end
205
219
 
206
- def erb_print_width(syntax_tree)
207
- statements = syntax_tree.statements.body
220
+ def erb_print_width(node)
208
221
  # Set the width to maximum if we have an IfNode or IfOp,
209
222
  # we cannot format them purely with SyntaxTree because the ERB-syntax will be unparseable.
210
- if statements.any? { |node| check_for_if_statement(node) }
211
- 999_999
212
- else
213
- SyntaxTree::ERB::MAX_WIDTH
214
- end
223
+ check_for_if_statement(node) ? 999_999 : SyntaxTree::ERB::MAX_WIDTH
215
224
  end
216
225
 
217
226
  def check_for_if_statement(node)
@@ -220,8 +220,19 @@ module SyntaxTree
220
220
 
221
221
  def initialize(opening_tag:, keyword:, content:, closing_tag:, location:)
222
222
  @opening_tag = opening_tag
223
- @keyword = keyword
224
- @content = ErbContent.new(value: content.map(&:value).join) if content
223
+ # prune whitespace from keyword
224
+ @keyword =
225
+ if keyword
226
+ Token.new(
227
+ type: keyword.type,
228
+ value: keyword.value.strip,
229
+ location: keyword.location
230
+ )
231
+ end
232
+ # Set content to nil if it is empty
233
+ content ||= []
234
+ content = content.map(&:value).join if content.is_a?(Array)
235
+ @content = ErbContent.new(value: content) unless content.strip.empty?
225
236
  @closing_tag = closing_tag
226
237
  @location = location
227
238
  end
@@ -108,12 +108,12 @@ module SyntaxTree
108
108
  end
109
109
 
110
110
  def visit_erb_end(node)
111
- q.text("erb_end")
111
+ q.pp("erb_end")
112
112
  end
113
113
 
114
114
  # Visit an ErbContent node.
115
115
  def visit_erb_content(node)
116
- q.text(node.value)
116
+ q.pp(node.value)
117
117
  end
118
118
 
119
119
  # Visit an Attribute node.
@@ -132,7 +132,7 @@ module SyntaxTree
132
132
  end
133
133
 
134
134
  def visit_erb_close(node)
135
- visit_node("erb_close", node)
135
+ visit(node.closing)
136
136
  end
137
137
 
138
138
  def visit_erb_do_close(node)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module SyntaxTree
4
4
  module ERB
5
- VERSION = "0.9.0"
5
+ VERSION = "0.9.1"
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.9.0
4
+ version: 0.9.1
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-06-22 00:00:00.000000000 Z
12
+ date: 2023-06-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: prettier_print