Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code generation to avoid unused variable warnings #3184

Merged
merged 1 commit into from
Feb 6, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class EndpointProviderClass < View
# @option options [required, Service] :service
# @option options [required, Hash] :endpoint_rules
def initialize(options)
@assigned_variables = []
@service = options.fetch(:service)
@endpoint_rules = @service.endpoint_rules
# Used to collect metrics in the generated endpoint provider
Expand All @@ -33,10 +34,6 @@ def module_name

def endpoint_rules_code
res = StringIO.new
# map parameters first
@endpoint_rules["parameters"].each do |k,_v|
res << indent("#{underscore(k)} = parameters.#{underscore(k)}\n", 3)
end

# map rules
@endpoint_rules["rules"].each do |rule|
Expand Down Expand Up @@ -164,6 +161,7 @@ def conditions(conditions, level)

def condition(condition)
if condition['assign']
@assigned_variables << condition['assign']
"(#{underscore(condition['assign'])} = #{fn(condition)})"
else
fn(condition)
Expand All @@ -173,7 +171,11 @@ def condition(condition)
def str(s)
if s.is_a?(Hash)
if s['ref']
underscore(s['ref'])
if @assigned_variables.include?(s['ref'])
underscore(s['ref'])
else
"parameters.#{underscore(s['ref'])}"
end
elsif s['fn']
fn(s)
else
Expand All @@ -195,7 +197,12 @@ def template_str(string, wrap=true)

def template_replace(value)
indexes = value.split("#")
res = underscore(indexes.shift)
variable = indexes.shift
res = if @assigned_variables.include?(variable)
underscore(variable)
else
"parameters.#{underscore(variable)}"
end
res += indexes.map do |index|
"['#{index}']"
end.join("")
Expand All @@ -210,7 +217,11 @@ def fn(fn)
def fn_arg(arg)
if arg.is_a?(Hash)
if arg['ref']
underscore(arg['ref'])
if @assigned_variables.include?(arg['ref'])
underscore(arg['ref'])
else
"parameters.#{underscore(arg['ref'])}"
end
elsif arg['fn']
fn(arg)
else
Expand Down
Loading