From 78c17a28d2089d4792b52fdf78176633cf2ea7b9 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Thu, 6 Feb 2025 14:07:32 +0100 Subject: [PATCH] Improve code generation to avoid unused variable warnings The Rails test suite had these warnings for ages, which I'd like to fix: ``` aws-sdk-s3/endpoint_provider.rb:22: warning: assigned but unused variable - key aws-sdk-s3/endpoint_provider.rb:24: warning: assigned but unused variable - copy_source aws-sdk-s3/endpoint_provider.rb:23: warning: assigned but unused variable - prefix ``` Ultimately I don't think pre-assigning to variables is really worth it, as the `parameters` argument is a T_STRUCT so access is really fast. But I suspect this was done to make the code generation simpler. In the end this adds a little bit of complexity, but not that much. --- .../views/endpoint_provider_class.rb | 25 +++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/views/endpoint_provider_class.rb b/build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/views/endpoint_provider_class.rb index 48eabb8a3cd..7993402f163 100644 --- a/build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/views/endpoint_provider_class.rb +++ b/build_tools/aws-sdk-code-generator/lib/aws-sdk-code-generator/views/endpoint_provider_class.rb @@ -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 @@ -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| @@ -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) @@ -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 @@ -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("") @@ -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