erb-formatter 0.3.0 → 0.4.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: 74ce05292fc99dcfeb80526b22aa762325c40589640086886190cf40d534facd
4
- data.tar.gz: faecec389f0cd92c85e1300d9c32920ac76f508e0fc37b7c2e6a5449b5d7db35
3
+ metadata.gz: 30ad88d7ebea80bf22ba7b6801a3ed030803b64725d618df54570442b351987d
4
+ data.tar.gz: 64f2fb608592b02e4ca2ba009495eaba662bd09dba8da215f88a8800b9f14915
5
5
  SHA512:
6
- metadata.gz: 13dfae2c05ecbb7f283ed1e76e1239dbad2923f1b0bc63b5d67e8de229ea3dfd97fe11d3d51734a2a33382a0d5a566dd16e8b2d8c73cc334eb8e424b685a7e3b
7
- data.tar.gz: 64b9b47e1a6dd2c324c8167daf9dafcc0126950e0ae509088e19abc9ffb51b14bfd21cefb98aaf538e2c4a46bf5073fbab90c187e02e7831aa32024d55529ca1
6
+ metadata.gz: 5fc9ffcb7b3539d287f4cb318ebafdd8fcc422d6cb1e93950f5d39d28ff0febe1754212204eccdc657461a97329673f43556e483c281d0ecd8cc1df53a4755d8
7
+ data.tar.gz: 7a7313fd434fa69f286f4cab42d2a5ee4739dcc0a555acadf9727a1bb9f4ebbf3f063384d2c1eb923908aa9e82b74984a25e48dee15bc3343a68856be5943d3d
data/README.md CHANGED
@@ -6,7 +6,7 @@ Features:
6
6
 
7
7
  - very fast
8
8
  - attempts to limit length (configurable)
9
- - tries to have an ouput similar to prettier for HTML
9
+ - tries to have an output similar to prettier for HTML
10
10
  - indents correctly ruby blocks (e.g. `if`/`elsif`/`do`/`end`)
11
11
  - designed to be integrated into editors and commit hooks
12
12
  - gives meaningful output in case of errors (most of the time)
@@ -19,19 +19,13 @@ Roadmap:
19
19
  - more ruby reformatting capabilities
20
20
  - JavaScript and CSS formatting
21
21
  - VSCode plugin
22
+ - fix spaces after attribute equal signs instead of complaining
22
23
 
23
24
  ## Installation
24
25
 
25
26
  Add this line to your application's Gemfile:
26
27
 
27
- ```ruby
28
- gem 'erb-formatter'
29
- gem 'rufo' # for enabling minimal ruby re-formatting
30
- ```
31
-
32
- And then execute:
33
-
34
- $ bundle install
28
+ $ bundle add erb-formatter
35
29
 
36
30
  Or install it yourself as:
37
31
 
@@ -41,6 +35,12 @@ Or install it yourself as:
41
35
 
42
36
  ### From the command line
43
37
 
38
+ Update files in-place:
39
+
40
+ $ erb-format app/views/**/*.html.erb --write
41
+
42
+ or use stdin/stdout (useful for editor integrations):
43
+
44
44
  $ echo "<div > asdf <% if 123%> <%='foobar'%> <%end-%> </div>" | erb-format --stdin
45
45
  <div>
46
46
  asdf
@@ -49,7 +49,6 @@ Or install it yourself as:
49
49
  <% end -%>
50
50
  </div>
51
51
 
52
-
53
52
  Check out `erb-format --help` for more options.
54
53
 
55
54
  ### From Ruby
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "lib/erb/formatter"
3
+ require_relative "lib/erb/formatter/version"
4
4
 
5
5
  Gem::Specification.new do |spec|
6
6
  spec.name = "erb-formatter"
@@ -28,5 +28,5 @@ Gem::Specification.new do |spec|
28
28
  spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
29
  spec.require_paths = ["lib"]
30
30
 
31
- spec.add_development_dependency "rufo"
31
+ spec.add_dependency "syntax_tree", '~> 5.0'
32
32
  end
data/exe/erb-format CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env ruby
1
+ #!/usr/bin/env ruby -W:no-experimental
2
2
 
3
3
  require 'erb/formatter/command_line'
4
4
 
data/exe/erb-formatter CHANGED
@@ -1,4 +1,4 @@
1
- #!/usr/bin/env ruby
1
+ #!/usr/bin/env ruby -W:no-experimental
2
2
 
3
3
  require 'erb/formatter/command_line'
4
4
 
@@ -33,6 +33,10 @@ class ERB::Formatter::CommandLine
33
33
  @filename ||= '-'
34
34
  end
35
35
 
36
+ parser.on("--print-width WIDTH", Integer, "Set the formatted output width") do |value|
37
+ @width = value
38
+ end
39
+
36
40
  parser.on("--[no-]debug", "Enable debug mode") do |value|
37
41
  $DEBUG = value
38
42
  end
@@ -41,6 +45,11 @@ class ERB::Formatter::CommandLine
41
45
  puts parser
42
46
  exit
43
47
  end
48
+
49
+ parser.on("-v", "--version", "Show ERB::Formatter version number and quit") do
50
+ puts "ERB::Formatter #{ERB::Formatter::VERSION}"
51
+ exit
52
+ end
44
53
  end.parse!(@argv)
45
54
  end
46
55
 
@@ -68,7 +77,7 @@ class ERB::Formatter::CommandLine
68
77
  if ignore_list.should_ignore_file? filename
69
78
  print code unless write
70
79
  else
71
- html = ERB::Formatter.format(code, filename: filename)
80
+ html = ERB::Formatter.new(code, filename: filename, line_width: @width || 80)
72
81
 
73
82
  if write
74
83
  File.write(filename, html)
@@ -1,7 +1,7 @@
1
1
  class ERB::Formatter::IgnoreList
2
2
  def initialize(contents: nil, base_dir: Dir.pwd)
3
3
  ignore_list_path = "#{base_dir}/.format-erb-ignore"
4
- @contents = contents || (File.exists?(ignore_list_path) ? File.read(ignore_list_path) : '')
4
+ @contents = contents || (File.exist?(ignore_list_path) ? File.read(ignore_list_path) : '')
5
5
  @ignore_list = @contents.lines
6
6
  end
7
7
 
@@ -12,4 +12,3 @@ class ERB::Formatter::IgnoreList
12
12
  end
13
13
  end
14
14
  end
15
-
@@ -1,3 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'erb/formatter'
3
+ require 'erb'
4
+
5
+ class ERB::Formatter
6
+ VERSION = "0.4.1"
7
+ end
data/lib/erb/formatter.rb CHANGED
@@ -6,9 +6,20 @@ require "ripper"
6
6
  require 'securerandom'
7
7
  require 'strscan'
8
8
  require 'stringio'
9
+ require 'erb/formatter/version'
10
+
11
+ require 'syntax_tree'
9
12
 
10
13
  class ERB::Formatter
11
- VERSION = "0.3.0"
14
+ module SyntaxTreeCommandPatch
15
+ def format(q)
16
+ q.group do
17
+ q.format(message)
18
+ q.text(" ")
19
+ q.format(arguments) # WAS: q.nest(message.value.length + 1) { q.format(arguments) }
20
+ end
21
+ end
22
+ end
12
23
 
13
24
  autoload :IgnoreList, 'erb/formatter/ignore_list'
14
25
 
@@ -67,7 +78,7 @@ class ERB::Formatter
67
78
  @original_source = source
68
79
  @filename = filename || '(erb)'
69
80
  @line_width = line_width
70
- @source = source.dup
81
+ @source = remove_front_matter source.dup
71
82
  @html = +""
72
83
  @debug = debug
73
84
 
@@ -92,6 +103,13 @@ class ERB::Formatter
92
103
  freeze
93
104
  end
94
105
 
106
+ def remove_front_matter(source)
107
+ source.sub(/\A---\n[\s\S]*?\n---\n/) do |match|
108
+ @front_matter = match
109
+ match.gsub(/[^\n]/, ' ')
110
+ end
111
+ end
112
+
95
113
  attr_accessor \
96
114
  :source, :html, :tag_stack, :pre_pos, :pre_placeholders, :erb_tags, :erb_tags_regexp,
97
115
  :pre_placeholders_regexp, :tags_regexp, :line_width
@@ -153,7 +171,6 @@ class ERB::Formatter
153
171
  html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
154
172
  tag_stack_push('%erb%', ruby_code)
155
173
  when ERB_OPEN_BLOCK
156
- ruby_code = format_ruby(ruby_code, autoclose: true)
157
174
  html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
158
175
  tag_stack_push('%erb%', ruby_code)
159
176
  else
@@ -197,13 +214,16 @@ class ERB::Formatter
197
214
  super error
198
215
  end
199
216
 
200
- def indented(string)
217
+ def indented(string, strip: true)
218
+ string = string.strip if strip
201
219
  indent = " " * tag_stack.size
202
- "\n#{indent}#{string.strip}"
220
+ "\n#{indent}#{string}"
203
221
  end
204
222
 
205
223
  def format_text(text)
206
224
  p format_text: text if @debug
225
+ return unless text
226
+
207
227
  starting_space = text.match?(/\A\s/)
208
228
 
209
229
  final_newlines_count = text.match(/(\s*)\z/m).captures.last.count("\n")
@@ -236,30 +256,6 @@ class ERB::Formatter
236
256
  end
237
257
  end
238
258
 
239
- def format_code_with_rubocop(code, line_width)
240
- stdin, stdout = $stdin, $stdout
241
- $stdin = StringIO.new(code)
242
- $stdout = StringIO.new
243
-
244
- Thread.current['RuboCop::Cop::Layout::LineLength#max'] = line_width
245
-
246
- @rubocop_cli ||= begin
247
- RuboCop::Cop::Layout::LineLength.prepend self
248
- RuboCop::CLI.new
249
- end
250
-
251
- @rubocop_cli.run([
252
- '--auto-correct',
253
- '--stdin', @filename,
254
- '-f', 'quiet',
255
- ])
256
-
257
- $stdout.string.split(RUBOCOP_STDIN_MARKER, 2).last
258
- ensure
259
- $stdin, $stdout = stdin, stdout
260
- Thread.current['RuboCop::Cop::Layout::LineLength#max'] = nil
261
- end
262
-
263
259
  def format_ruby(code, autoclose: false)
264
260
  if autoclose
265
261
  code += "\nend" unless ERB_OPEN_BLOCK["#{code}\nend"]
@@ -267,16 +263,17 @@ class ERB::Formatter
267
263
  end
268
264
  p RUBY_IN_: code if @debug
269
265
 
270
- offset = tag_stack.size * 2
271
- if defined? Rubocop
272
- code = format_code_with_rubocop(code, line_width - offset) if (offset + code.size) > line_width
273
- elsif defined?(Rufo)
274
- code = Rufo.format(code) rescue code
266
+ SyntaxTree::Command.prepend SyntaxTreeCommandPatch
267
+
268
+ code = begin
269
+ SyntaxTree.format(code)
270
+ rescue SyntaxTree::Parser::ParseError
271
+ code
275
272
  end
276
273
 
277
274
  lines = code.strip.lines
278
275
  lines = lines[0...-1] if autoclose
279
- code = lines.map { |l| indented(l) }.join.strip
276
+ code = lines.map { |l| indented(l.chomp("\n"), strip: false) }.join.strip
280
277
  p RUBY_OUT: code if @debug
281
278
  code
282
279
  end
@@ -294,7 +291,7 @@ class ERB::Formatter
294
291
  if erb_scanner.scan_until(erb_tags_regexp)
295
292
  p PRE_MATCH: [erb_pre_pos, '..', erb_scanner.pre_match] if @debug
296
293
  erb_pre_match = erb_scanner.pre_match
297
- erb_pre_match = erb_pre_match[erb_pre_pos..]
294
+ erb_pre_match = erb_pre_match[erb_pre_pos..].to_s
298
295
  erb_pre_pos = erb_scanner.pos
299
296
 
300
297
  erb_code = erb_tags[erb_scanner.captures.first]
@@ -303,22 +300,24 @@ class ERB::Formatter
303
300
 
304
301
  erb_open, ruby_code, erb_close = ERB_TAG.match(erb_code).captures
305
302
  erb_open << ' ' unless ruby_code.start_with?('#')
306
- full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
307
303
 
308
304
  case ruby_code
309
305
  when /\Aend\z/
306
+ full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
310
307
  tag_stack_pop('%erb%', ruby_code)
311
308
  html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
312
309
  when /\A(else|elsif\b(.*))\z/
310
+ full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
313
311
  tag_stack_pop('%erb%', ruby_code)
314
312
  html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
315
313
  tag_stack_push('%erb%', ruby_code)
316
314
  when ERB_OPEN_BLOCK
317
- ruby_code = format_ruby(ruby_code, autoclose: true)
315
+ full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
318
316
  html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
319
317
  tag_stack_push('%erb%', ruby_code)
320
318
  else
321
319
  ruby_code = format_ruby(ruby_code, autoclose: false)
320
+ full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
322
321
  html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
323
322
  end
324
323
  else
@@ -335,7 +334,7 @@ class ERB::Formatter
335
334
 
336
335
  until scanner.eos?
337
336
  if matched = scanner.scan_until(tags_regexp)
338
- p format_pre_match: [pre_pos, '..', scanner.pre_match[pre_pos..]] if @debug
337
+ p format_pre_match: [pre_pos, '..', scanner.pre_match[pre_pos..]] if @debug
339
338
  pre_match = scanner.pre_match[pre_pos..]
340
339
  p POS: pre_pos...scanner.pos, advanced: source[pre_pos...scanner.pos] if @debug
341
340
  p MATCHED: matched if @debug
@@ -378,6 +377,7 @@ class ERB::Formatter
378
377
  html.gsub!(erb_tags_regexp, erb_tags)
379
378
  html.gsub!(pre_placeholders_regexp, pre_placeholders)
380
379
  html.strip!
380
+ html.prepend @front_matter + "\n" if @front_matter
381
381
  html << "\n"
382
382
  end
383
383
  end
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erb-formatter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Elia Schito
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-07 00:00:00.000000000 Z
11
+ date: 2023-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: rufo
14
+ name: syntax_tree
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '0'
20
- type: :development
19
+ version: '5.0'
20
+ type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '0'
26
+ version: '5.0'
27
27
  description:
28
28
  email:
29
29
  - elia@schito.me