Skip to content

Commit 1a38c89

Browse files
justin808claude
andcommitted
Improve lockfile parsing robustness and error handling
- Fix error handling in version_from_package_lock to safely check for version key existence using safe navigation operator - Improve lockfile path construction using File.dirname instead of ".." for more robust path resolution - Add ClassLength RuboCop disable for NodePackageVersion class These changes address code review feedback to make the implementation more robust and handle edge cases better. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent ae649d1 commit 1a38c89

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/react_on_rails/version_checker.rb

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def package_json_location
212212
"Package.json location: #{VersionChecker::NodePackageVersion.package_json_path}"
213213
end
214214

215+
# rubocop:disable Metrics/ClassLength
215216
class NodePackageVersion
216217
attr_reader :package_json, :yarn_lock, :package_lock
217218

@@ -224,11 +225,13 @@ def self.package_json_path
224225
end
225226

226227
def self.yarn_lock_path
227-
Rails.root.join(ReactOnRails.configuration.node_modules_location, "..", "yarn.lock")
228+
lockfile_dir = File.dirname(Rails.root.join(ReactOnRails.configuration.node_modules_location).to_s)
229+
File.join(lockfile_dir, "yarn.lock")
228230
end
229231

230232
def self.package_lock_path
231-
Rails.root.join(ReactOnRails.configuration.node_modules_location, "..", "package-lock.json")
233+
lockfile_dir = File.dirname(Rails.root.join(ReactOnRails.configuration.node_modules_location).to_s)
234+
File.join(lockfile_dir, "package-lock.json")
232235
end
233236

234237
def initialize(package_json, yarn_lock = nil, package_lock = nil)
@@ -393,12 +396,14 @@ def version_from_package_lock(package_name)
393396
if parsed["packages"]
394397
# Look for node_modules/package-name entry
395398
node_modules_key = "node_modules/#{package_name}"
396-
return parsed["packages"][node_modules_key]["version"] if parsed["packages"][node_modules_key]
399+
package_data = parsed["packages"][node_modules_key]
400+
return package_data["version"] if package_data&.key?("version")
397401
end
398402

399403
# Fall back to v1 format (dependencies)
400-
if parsed["dependencies"] && parsed["dependencies"][package_name]
401-
return parsed["dependencies"][package_name]["version"]
404+
if parsed["dependencies"]
405+
dependency_data = parsed["dependencies"][package_name]
406+
return dependency_data["version"] if dependency_data&.key?("version")
402407
end
403408
rescue JSON::ParserError
404409
# If we can't parse the lockfile, fall back to package.json version
@@ -443,5 +448,6 @@ def parsed_package_contents
443448
end
444449
end
445450
end
451+
# rubocop:enable Metrics/ClassLength
446452
end
447453
end

0 commit comments

Comments
 (0)