@@ -269,7 +269,25 @@ def semver_wildcard?
269269 # See https://docs.npmjs.com/cli/v10/configuring-npm/package-json#dependencies
270270 # We want to disallow all expressions other than exact versions
271271 # and the ones allowed by local_path_or_url?
272- raw . blank? || raw . start_with? ( /[~^><*]/ ) || raw . include? ( " - " ) || raw . include? ( " || " )
272+ return true if raw . blank?
273+
274+ special_version_string? || wildcard_or_x_range? || range_operator? || range_syntax?
275+ end
276+
277+ def special_version_string?
278+ %w[ latest next canary beta alpha rc ] . include? ( raw . downcase )
279+ end
280+
281+ def wildcard_or_x_range?
282+ raw == "*" || raw =~ /^x$/i || raw =~ /\d x\b /i || raw =~ /^\* \. / || raw =~ /\. x/i
283+ end
284+
285+ def range_operator?
286+ raw . start_with? ( /[~^><*]/ )
287+ end
288+
289+ def range_syntax?
290+ raw . include? ( " - " ) || raw . include? ( " || " )
273291 end
274292
275293 def local_path_or_url?
@@ -304,7 +322,26 @@ def package_json_contents
304322 end
305323
306324 def parsed_package_contents
307- @parsed_package_contents ||= JSON . parse ( package_json_contents )
325+ return @parsed_package_contents if defined? ( @parsed_package_contents )
326+
327+ begin
328+ @parsed_package_contents = JSON . parse ( package_json_contents )
329+ rescue JSON ::ParserError => e
330+ raise ReactOnRails ::Error , <<~MSG . strip
331+ **ERROR** ReactOnRails: Failed to parse package.json file.
332+
333+ Location: #{ package_json }
334+ Error: #{ e . message }
335+
336+ The package.json file contains invalid JSON. Please check the file for syntax errors.
337+
338+ Common issues:
339+ - Missing or extra commas
340+ - Unquoted keys or values
341+ - Trailing commas (not allowed in JSON)
342+ - Comments (not allowed in standard JSON)
343+ MSG
344+ end
308345 end
309346 end
310347 end
0 commit comments