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.

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

Class Method Summary collapse

Class Method Details

.parse_json_or_jsonc(content) ⇒ Object

Raises:

  • (JSON::ParserError)


30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dependabot/deno/helpers.rb', line 30

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



67
68
69
70
71
72
73
# File 'lib/dependabot/deno/helpers.rb', line 67

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)


49
50
51
52
53
54
# File 'lib/dependabot/deno/helpers.rb', line 49

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