Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions erb-formatter.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "syntax_tree", '~> 6.0'

spec.add_development_dependency "tailwindcss-rails", "~> 2.0"
spec.add_development_dependency "m", "~> 1.0"
end
34 changes: 3 additions & 31 deletions lib/erb/formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,7 @@
require 'securerandom'
require 'erb/formatter/version'

require 'syntax_tree'
require 'syntax_tree/plugin/trailing_comma'

class ERB::Formatter
module SyntaxTreeCommandPatch
def format(q)
q.group do
q.format(message)
q.text(" ")
q.format(arguments) # WAS: q.nest(message.value.length + 1) { q.format(arguments) }
end
end
end

autoload :IgnoreList, 'erb/formatter/ignore_list'

class Error < StandardError; end
Expand All @@ -37,7 +24,7 @@ class Error < StandardError; end
BAD_ATTR = %r{#{ATTR_NAME}=\s+}u
QUOTED_ATTR = Regexp.union(SINGLE_QUOTE_ATTR, DOUBLE_QUOTE_ATTR)
ATTR = Regexp.union(SINGLE_QUOTE_ATTR, DOUBLE_QUOTE_ATTR, UNQUOTED_ATTR, UNQUOTED_VALUE)
MULTILINE_ATTR_NAMES = %w[class data-action]
MULTILINE_ATTR_NAMES = %w[data-action]

ERB_TAG = %r{(<%(?:==|=|-|))\s*(.*?)\s*(-?%>)}m
ERB_PLACEHOLDER = %r{erb[a-z0-9]+tag}
Expand Down Expand Up @@ -144,12 +131,12 @@ def format_attributes(tag_name, attrs, tag_closing)
name, value = attr.split('=', 2)

if value.nil?
attr_html << indented("#{name}")
attr_html << (within_line_width ? " #{name}" : indented("#{name}"))
next
end

if /\A#{UNQUOTED_VALUE}\z/o.match?(value)
attr_html << indented("#{name}=\"#{value}\"")
attr_html << (within_line_width ? " #{name}=\"#{value}\"" : indented("#{name}=\"#{value}\""))
next
end

Expand Down Expand Up @@ -268,21 +255,6 @@ def format_ruby(code, autoclose: false)
code += "\nend" unless RUBY_OPEN_BLOCK["#{code}\nend"]
code += "\n}" unless RUBY_OPEN_BLOCK["#{code}\n}"]
end
p RUBY_IN_: code if @debug

SyntaxTree::Command.prepend SyntaxTreeCommandPatch

code = begin
SyntaxTree.format(code, @line_width)
rescue SyntaxTree::Parser::ParseError => error
p RUBY_PARSE_ERROR: error if @debug
code
end

lines = code.strip.lines
lines = lines[0...-1] if autoclose
code = lines.map { |l| indented(l.chomp("\n"), strip: false) }.join.strip
p RUBY_OUT: code if @debug
code
end

Expand Down
2 changes: 1 addition & 1 deletion lib/erb/formatter/command_line.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def self.tailwindcss_class_sorter(css_path)
css = css.tr("\n", " ").gsub(%r{\/\*.*?\*\/},"") # remove comments
css = css.gsub(%r<@media.*?\{>, "") # strip media queries
css = css.scan(%r<(?:^|\}|\{) *(\S.*?) *\{>).join(" ") # extract selectors
classes = css.tr(","," ").split(" ").grep(/\./).uniq.map { _1.split('.').last.gsub("\\", "") }
classes = css.tr(","," ").split(" ").grep(/\./).uniq.map { _1.split('.').last.gsub("\\3", "").gsub("\\", "") }
indexed_classes = Hash[classes.zip((0...classes.size).to_a)]

->(class_name) do
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/attributes.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
data-short-url="//test.com/?q=v"
data-long-url="https://google.ca/this-is-a-long-url-with-a-query-string?query=something"
data-long-url-single='https://google.ca/this-is-a-long-url-with-a-query-string?query=something'>
<input type=text class="text-sm" hidden />
<input name="very-long-name-should-break-because-of-longer-than-width" type=text class="text-sm" hidden />
7 changes: 7 additions & 0 deletions test/fixtures/attributes.html.expected.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,10 @@
data-long-url="https://google.ca/this-is-a-long-url-with-a-query-string?query=something"
data-long-url-single='https://google.ca/this-is-a-long-url-with-a-query-string?query=something'
>
<input type="text" class="text-sm" hidden/>
<input
name="very-long-name-should-break-because-of-longer-than-width"
type="text"
class="text-sm"
hidden
/>
2 changes: 1 addition & 1 deletion test/fixtures/tailwindcss/class_sorting.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="hover:opacity-75 opacity-50 hover:scale-150 scale-125">
</div>

<div class="lg:grid-cols-4 grid sm:grid-cols-3 grid-cols-2">
<div class="lg:grid-cols-4 grid 2xl:grid-cols-4 sm:grid-cols-3 grid-cols-2">
</div>

<div class="p-3 shadow-xl select2-dropdown">
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/tailwindcss/class_sorting.html.expected.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="scale-125 opacity-50 hover:scale-150 hover:opacity-75">
</div>

<div class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4">
<div class="grid grid-cols-2 sm:grid-cols-3 lg:grid-cols-4 2xl:grid-cols-4">
</div>

<div class="select2-dropdown p-3 shadow-xl">
Expand Down
Loading