Skip to content

Commit

Permalink
replace_division: balance parens
Browse files Browse the repository at this point in the history
  • Loading branch information
AprilArcus authored and glebm committed Feb 28, 2022
1 parent e2ecd03 commit 96917c6
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions tasks/converter/less_conversion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,41 @@ def wrap_at_groups_with_at_root(file)
end

def replace_division(less)
re = /(?<!\w)\(\s*([^(]+?)\s+\/\s+([^)]+?)\s*\)/
re = %r{
(?<expression>
(?<callee>[[:alpha:]\.]+)?
\(
(?:
(?>
(?<dividend>
[^()/]+
|
\([^/]+\)
)
\s+
/
\s+
(?<divisor>
[^()/]+
|
\([^/]+\)
)
)
|
\g<expression>
)
\)
)
}x
return less if less !~ re
"@use \"sass:math\";\n" + less.gsub(re, 'math.div(\1, \2)')
"@use \"sass:math\";\n" + less.gsub(re) do
named_captures = $~.named_captures
callee = named_captures['callee']
dividend = named_captures['dividend']
divisor = named_captures['divisor']
expression = "math.div(#{dividend}, #{divisor})"
callee.nil? ? expression : "#{callee}(#{expression})"
end
end

def sass_fn_exists(fn)
Expand Down

0 comments on commit 96917c6

Please sign in to comment.