Skip to content

Commit

Permalink
fix: Improved CSS rewriter to support symbol array literals
Browse files Browse the repository at this point in the history
  • Loading branch information
joelmoss committed Mar 19, 2024
1 parent 7136081 commit 3598ea7
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions lib/proscenium/css_module/rewriter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,28 @@ module CssModule
class Rewriter < RubyNext::Language::Rewriters::Text
NAME = 'proscenium-css-module'

def safe_rewrite(source)
source.gsub(/:@([\w_]+)/) do |_|
context.track! self
def rewrite(source)
source = source.gsub(/%i\[((@[\w@ ]+)|([\w@ ]+ @[\w@ ]+))\]/) do |_|
arr = ::Regexp.last_match(1).split.map do |x|
x.start_with?('@') ? css_module_string(x[1..]) : ":#{x}"
end
"[#{arr.join(',')}]"
end

source.gsub(/:@([\w]+)/) do |_|
context.track!(self)
css_module_string(::Regexp.last_match(1))
end
end

private

match = ::Regexp.last_match(1)
"Proscenium::CssModule::Name.new(:@#{match}, css_module(:#{match}))"
def css_module_string(name)
if (path = Pathname.new(context.path).sub_ext('.module.css')).exist?
tname = Transformer.new(path).class_name!(name, name.dup).first
"Proscenium::CssModule::Name.new(:@#{name}, '#{tname}')"
else
"Proscenium::CssModule::Name.new(:@#{name}, css_module(:#{name}))"
end
end
end
Expand Down

0 comments on commit 3598ea7

Please sign in to comment.