Skip to content

Commit

Permalink
linting
Browse files Browse the repository at this point in the history
  • Loading branch information
AbanoubGhadban committed Dec 24, 2024
1 parent fd24a52 commit 4a36576
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
2 changes: 1 addition & 1 deletion lib/react_on_rails/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -611,7 +611,7 @@ def should_raise_streaming_prerender_error?(chunk_json_result, render_options)
end

# Returns object with values that are NOT html_safe!
def server_rendered_react_component(render_options)
def server_rendered_react_component(render_options) # rubocop:disable Metrics/CyclomaticComplexity
return { "html" => "", "consoleReplayScript" => "" } unless render_options.prerender

react_component_name = render_options.react_component_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def reset_pool_if_server_bundle_was_modified
# Note, js_code does not have to be based on React.
# js_code MUST RETURN json stringify Object
# Calling code will probably call 'html_safe' on return value before rendering to the view.
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def exec_server_render_js(js_code, render_options, js_evaluator = nil)
js_evaluator ||= self
if render_options.trace
Expand Down Expand Up @@ -76,13 +76,16 @@ def exec_server_render_js(js_code, render_options, js_evaluator = nil)
raise ReactOnRails::Error, msg, err.backtrace
end

return parse_result_and_replay_console_messages(result, render_options) unless render_options.stream? || render_options.rsc?
unless render_options.stream? || render_options.rsc?
return parse_result_and_replay_console_messages(result,
render_options)
end

# Streamed component is returned as stream of strings.
# We need to parse each chunk and replay the console messages.
result.transform { |chunk| parse_result_and_replay_console_messages(chunk, render_options) }
end
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

def trace_js_code_used(msg, js_code, file_name = "tmp/server-generated.js", force: false)
return unless ReactOnRails.configuration.trace || force
Expand Down Expand Up @@ -227,11 +230,12 @@ def file_url_to_string(url)
end

def parse_result_and_replay_console_messages(result_string, render_options)
return { html: result_string } if render_options.rsc?

result = nil
begin
result = JSON.parse(result_string)
rescue JSON::ParserError => e
return { html: result_string }
raise ReactOnRails::JsonParseError.new(parse_error: e, json: result_string)
end

Expand Down
2 changes: 1 addition & 1 deletion script/convert
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ new_config = File.expand_path("../spec/dummy/config/webpacker.yml", __dir__)
File.rename(old_config, new_config)

gsub_file_content("../Gemfile.development_dependencies", 'gem "shakapacker", "8.0.0"', 'gem "shakapacker", "6.6.0"')
gsub_file_content("../Gemfile.development_dependencies", 'gem "webpacker", "6.0.0.rc.6"', '')
gsub_file_content("../Gemfile.development_dependencies", 'gem "webpacker", "6.0.0.rc.6"', "")

gsub_file_content("../spec/dummy/package.json", '"shakapacker": "8.0.0",', '"shakapacker": "6.6.0",')

Expand Down
31 changes: 15 additions & 16 deletions spec/react_on_rails/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ module ReactOnRails
# If rspec tests are run locally, we want to test both packers.
# If rspec tests are run in CI, we want to test the packer specified in the CI_PACKER_VERSION environment variable.
# Check script/convert and .github/workflows/rspec-package-specs.yml for more details.
PACKERS_TO_TEST = if ENV["CI_PACKER_VERSION"] == "old"
packers_to_test = if ENV["CI_PACKER_VERSION"] == "old"
["webpacker"]
elsif ENV["CI_PACKER_VERSION"] == "new"
["shakapacker"]
else
["shakapacker", "webpacker"]
%w[shakapacker webpacker]
end

shared_context "with packer enabled" do
Expand All @@ -36,8 +36,8 @@ module ReactOnRails
shared_context "with shakapacker enabled" do
before do
# Mock that shakapacker is not installed, so webpacker will be used instead
allow(ReactOnRails::Utils).to receive(:gem_available?).with("shakapacker").and_return(true)
allow(ReactOnRails::Utils).to receive(:gem_available?).with("webpacker").and_return(false)
allow(described_class).to receive(:gem_available?).with("shakapacker").and_return(true)
allow(described_class).to receive(:gem_available?).with("webpacker").and_return(false)
end

include_context "with packer enabled"
Expand All @@ -54,8 +54,8 @@ module ReactOnRails
shared_context "with webpacker enabled" do
before do
# Mock that shakapacker is not installed, so webpacker will be used instead
allow(ReactOnRails::Utils).to receive(:gem_available?).with("shakapacker").and_return(false)
allow(ReactOnRails::Utils).to receive(:gem_available?).with("webpacker").and_return(true)
allow(described_class).to receive(:gem_available?).with("shakapacker").and_return(false)
allow(described_class).to receive(:gem_available?).with("webpacker").and_return(true)
end

include_context "with packer enabled"
Expand All @@ -72,8 +72,8 @@ module ReactOnRails
before do
allow(ReactOnRails).to receive_message_chain(:configuration, :generated_assets_dir)
.and_return("public/webpack/dev")
allow(ReactOnRails::Utils).to receive(:gem_available?).with("shakapacker").and_return(false)
allow(ReactOnRails::Utils).to receive(:gem_available?).with("webpacker").and_return(false)
allow(described_class).to receive(:gem_available?).with("shakapacker").and_return(false)
allow(described_class).to receive(:gem_available?).with("webpacker").and_return(false)
end

it "does not use packer" do
Expand Down Expand Up @@ -128,25 +128,22 @@ def mock_dev_server_running
allow(Rails).to receive(:root).and_return(File.expand_path("."))
described_class.instance_variable_set(:@server_bundle_path, nil)
described_class.instance_variable_set(:@rsc_bundle_path, nil)
ReactOnRails::PackerUtils.instance_variables.each do |instance_variable|
ReactOnRails::PackerUtils.remove_instance_variable(instance_variable)
end
end

after do
described_class.instance_variable_set(:@server_bundle_path, nil)
described_class.instance_variable_set(:@rsc_bundle_path, nil)
end

before :each do
ReactOnRails::PackerUtils.instance_variables.each do |instance_variable|
ReactOnRails::PackerUtils.remove_instance_variable(instance_variable)
end
end

describe ".bundle_js_file_path" do
subject do
described_class.bundle_js_file_path("webpack-bundle.js")
end

PACKERS_TO_TEST.each do |packer_type|
packers_to_test.each do |packer_type|
context "with #{packer_type} enabled", packer_type.to_sym do
include_context "with #{packer_type} enabled"

Expand Down Expand Up @@ -210,9 +207,10 @@ def mock_dev_server_running
end
end

PACKERS_TO_TEST.each do |packer_type|
packers_to_test.each do |packer_type|
describe ".server_bundle_js_file_path with #{packer_type} enabled" do
let(:packer_public_output_path) { Pathname.new("public/webpack/development") }

include_context "with #{packer_type} enabled"

context "with server file not in manifest", packer_type.to_sym do
Expand Down Expand Up @@ -275,6 +273,7 @@ def mock_dev_server_running

describe ".rsc_bundle_js_file_path with #{packer_type} enabled" do
let(:packer_public_output_path) { Pathname.new("public/webpack/development") }

include_context "with #{packer_type} enabled"

context "with server file not in manifest", packer_type.to_sym do
Expand Down

0 comments on commit 4a36576

Please sign in to comment.