erb-formatter 0.3.0 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 74ce05292fc99dcfeb80526b22aa762325c40589640086886190cf40d534facd
4
- data.tar.gz: faecec389f0cd92c85e1300d9c32920ac76f508e0fc37b7c2e6a5449b5d7db35
3
+ metadata.gz: 93f3695219c717c8c307ccecad4141890ed86c11c397e36af0e569a24e303fd4
4
+ data.tar.gz: 67b9bd4f85b75b3ba8aaa18f370df3fa9f9f178f561aa02b4b1def6d04f6b8c2
5
5
  SHA512:
6
- metadata.gz: 13dfae2c05ecbb7f283ed1e76e1239dbad2923f1b0bc63b5d67e8de229ea3dfd97fe11d3d51734a2a33382a0d5a566dd16e8b2d8c73cc334eb8e424b685a7e3b
7
- data.tar.gz: 64b9b47e1a6dd2c324c8167daf9dafcc0126950e0ae509088e19abc9ffb51b14bfd21cefb98aaf538e2c4a46bf5073fbab90c187e02e7831aa32024d55529ca1
6
+ metadata.gz: 1cf1a5c6d0cdf543b18d0203daa82e532168b1665b9343a066b40a20a60d0236a442ce018337814b079ae7f0d094e3a6583b2862659bccbc9de64ae02ce5cdbe
7
+ data.tar.gz: 1694c549a0ad05b6495b9632e3941179b62437892ee7e3c0e3712839ac23ae1056a2bc12c1f445ca8499d42448810e5eb2ba117498f296a9685cade08ff48426
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.0"
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,9 +214,10 @@ 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)
@@ -236,30 +254,6 @@ class ERB::Formatter
236
254
  end
237
255
  end
238
256
 
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
257
  def format_ruby(code, autoclose: false)
264
258
  if autoclose
265
259
  code += "\nend" unless ERB_OPEN_BLOCK["#{code}\nend"]
@@ -267,16 +261,17 @@ class ERB::Formatter
267
261
  end
268
262
  p RUBY_IN_: code if @debug
269
263
 
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
264
+ SyntaxTree::Command.prepend SyntaxTreeCommandPatch
265
+
266
+ code = begin
267
+ SyntaxTree.format(code)
268
+ rescue SyntaxTree::Parser::ParseError
269
+ code
275
270
  end
276
271
 
277
272
  lines = code.strip.lines
278
273
  lines = lines[0...-1] if autoclose
279
- code = lines.map { |l| indented(l) }.join.strip
274
+ code = lines.map { |l| indented(l.chomp("\n"), strip: false) }.join.strip
280
275
  p RUBY_OUT: code if @debug
281
276
  code
282
277
  end
@@ -303,22 +298,24 @@ class ERB::Formatter
303
298
 
304
299
  erb_open, ruby_code, erb_close = ERB_TAG.match(erb_code).captures
305
300
  erb_open << ' ' unless ruby_code.start_with?('#')
306
- full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
307
301
 
308
302
  case ruby_code
309
303
  when /\Aend\z/
304
+ full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
310
305
  tag_stack_pop('%erb%', ruby_code)
311
306
  html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
312
307
  when /\A(else|elsif\b(.*))\z/
308
+ full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
313
309
  tag_stack_pop('%erb%', ruby_code)
314
310
  html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
315
311
  tag_stack_push('%erb%', ruby_code)
316
312
  when ERB_OPEN_BLOCK
317
- ruby_code = format_ruby(ruby_code, autoclose: true)
313
+ full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
318
314
  html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
319
315
  tag_stack_push('%erb%', ruby_code)
320
316
  else
321
317
  ruby_code = format_ruby(ruby_code, autoclose: false)
318
+ full_erb_tag = "#{erb_open}#{ruby_code} #{erb_close}"
322
319
  html << (erb_pre_match.match?(/\s+\z/) ? indented(full_erb_tag) : full_erb_tag)
323
320
  end
324
321
  else
@@ -335,7 +332,7 @@ class ERB::Formatter
335
332
 
336
333
  until scanner.eos?
337
334
  if matched = scanner.scan_until(tags_regexp)
338
- p format_pre_match: [pre_pos, '..', scanner.pre_match[pre_pos..]] if @debug
335
+ p format_pre_match: [pre_pos, '..', scanner.pre_match[pre_pos..]] if @debug
339
336
  pre_match = scanner.pre_match[pre_pos..]
340
337
  p POS: pre_pos...scanner.pos, advanced: source[pre_pos...scanner.pos] if @debug
341
338
  p MATCHED: matched if @debug
@@ -378,6 +375,7 @@ class ERB::Formatter
378
375
  html.gsub!(erb_tags_regexp, erb_tags)
379
376
  html.gsub!(pre_placeholders_regexp, pre_placeholders)
380
377
  html.strip!
378
+ html.prepend @front_matter + "\n" if @front_matter
381
379
  html << "\n"
382
380
  end
383
381
  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.0
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