Module: Dependabot::Deno::Helpers

Extended by:
T::Sig
Defined in:
lib/dependabot/deno/helpers.rb

Constant Summary collapse

JSONC_TOKEN =

Matches either a JSON string literal (with escapes), a line comment, a block comment, or a trailing comma. The alternation lets gsub preserve strings while stripping the JSONC-only constructs, so e.g. "//" inside a URL value is not mistaken for the start of a comment.

%r{
  ("(?:\\.|[^"\\])*")    # JSON string literal
  | //[^\n]*             # line comment
  | /\*.*?\*/            # block comment
  | ,(?=\s*[\}\]])       # trailing comma
}mx

Class Method Summary collapse

Class Method Details

.parse_json_or_jsonc(content) ⇒ Object

Raises:

  • (JSON::ParserError)


27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/dependabot/deno/helpers.rb', line 27

def self.parse_json_or_jsonc(content)
  return {} unless content

  cleaned = content.gsub(JSONC_TOKEN) { ::Regexp.last_match(1) || "" }

  parsed = JSON.parse(cleaned)
  # A deno.json(c) must be a JSON object. Guard here so a malformed manifest
  # (e.g. a top-level array) surfaces as a clear parse error rather than an
  # opaque sorbet-runtime type error at the call site.
  raise JSON::ParserError, "Expected a JSON object, got #{parsed.class}" unless parsed.is_a?(Hash)

  parsed
end

.run_deno_command(*args, dir:) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/dependabot/deno/helpers.rb', line 64

def self.run_deno_command(*args, dir:)
  Dependabot::SharedHelpers.run_shell_command(
    "deno #{args.join(' ')}",
    cwd: dir,
    env: { "DENO_DIR" => File.join(dir, ".deno_cache") }
  )
end

.safe_relative_path?(path) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/dependabot/deno/helpers.rb', line 46

def self.safe_relative_path?(path)
  return false if path.empty?
  return false if Pathname.new(path).absolute?

  Pathname.new(path).each_filename.none?("..")
end