Skip to content

Commit

Permalink
move only javascript.rb to root folder
Browse files Browse the repository at this point in the history
  • Loading branch information
kbukum1 committed Feb 8, 2025
1 parent f7c2fe6 commit 96b557e
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 43 deletions.
39 changes: 39 additions & 0 deletions javascript/lib/dependabot/javascript.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# typed: strong
# frozen_string_literal: true

require "dependabot/bun"

module Dependabot
module Javascript
DEFAULT_PACKAGE_MANAGER = "npm"
ERROR_MALFORMED_VERSION_NUMBER = "Malformed version number"
MANIFEST_ENGINES_KEY = "engines"
MANIFEST_FILENAME = "package.json"
MANIFEST_PACKAGE_MANAGER_KEY = "packageManager"

# Define a type alias for the expected class interface
JavascriptPackageManagerClassType = T.type_alias do
T.class_of(Bun::PackageManager)
end

PACKAGE_MANAGER_CLASSES = T.let({
Bun::PackageManager::NAME => Bun::PackageManager
}.freeze, T::Hash[String, JavascriptPackageManagerClassType])

PACKAGE_MANAGER_VERSION_REGEX = /
^ # Start of string
(?<major>\d+) # Major version (required, numeric)
\. # Separator between major and minor versions
(?<minor>\d+) # Minor version (required, numeric)
\. # Separator between minor and patch versions
(?<patch>\d+) # Patch version (required, numeric)
( # Start pre-release section
-(?<pre_release>[a-zA-Z0-9.]+) # Pre-release label (optional, alphanumeric or dot-separated)
)?
( # Start build metadata section
\+(?<build>[a-zA-Z0-9.]+) # Build metadata (optional, alphanumeric or dot-separated)
)?
$ # End of string
/x # Extended mode for readability
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ def fetch_package_json_if_present(instance, workspace)

sig { params(instance: FileFetcher).returns(DependencyFile) }
def package_json(instance)
@package_json ||= T.let(instance.fetch_file(Javascript::Shared::MANIFEST_FILENAME), T.nilable(DependencyFile))
@package_json ||= T.let(instance.fetch_file(Javascript::MANIFEST_FILENAME), T.nilable(DependencyFile))
end

sig { params(instance: FileFetcher).returns(T.untyped) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def fetch_package_json_if_present(instance, workspace)

sig { params(instance: FileFetcher).returns(DependencyFile) }
def package_json(instance)
@package_json ||= T.let(instance.fetch_file(Javascript::Shared::MANIFEST_FILENAME), T.nilable(DependencyFile))
@package_json ||= T.let(instance.fetch_file(Javascript::MANIFEST_FILENAME), T.nilable(DependencyFile))
end

sig { params(instance: FileFetcher).returns(T.untyped) }
Expand Down
41 changes: 0 additions & 41 deletions javascript/lib/dependabot/javascript/shared/javascript.rb

This file was deleted.

51 changes: 51 additions & 0 deletions test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# typed: true

require "benchmark"

# Original implementation
def original_unglobbed_path(glob)
unglobbed_path =
glob.gsub(%r{^\./}, "").gsub(/!\(.*?\)/, "*")
.split("*")
.first&.gsub(%r{(?<=/)[^/]*$}, "") || "."
end

# Optimized implementation (Fixed `sub` syntax)
def optimized_unglobbed_path(glob)
unglobbed_path =
glob.sub(%r{^\./}, "") # Remove leading "./"
.gsub(/!\([^)]+\)/, "*") # Efficiently replace `!(...)`
.split("*")
.first&.sub(%r{/[^/]*$}, "") || "."
end

# Test cases
test_cases = [
"./normal/path/file.txt",
"!(/excluded/path/file.txt)",
"!(/excluded/path)/subdir/file.txt",
"!(/many(repeated(patterns)))/file.txt",
"!(/deeply/nested!(exclusion))/file.txt",
"!(/nested1(nested2(nested3(nested4(nested5)))))/file.txt",
"!(/multiple!(!(!(!(!(!(!(!(!(!(!(!(!(!(!(!()))))))))))))))))/file.txt"
]

puts "Testing original vs optimized implementation..."

test_cases.each do |test|
original_result = original_unglobbed_path(test)
optimized_result = optimized_unglobbed_path(test)
match = original_result == optimized_result ? "✅ Match" : "❌ Mismatch"

puts "\nInput: #{test}"
puts " Original: #{original_result}"
puts " Optimized: #{optimized_result}"
puts " #{match}"
end

# Benchmark comparison
puts "\nBenchmarking performance..."
Benchmark.bm do |bm|
bm.report("Original:") { test_cases.each { |test| original_unglobbed_path(test) } }
bm.report("Optimized:") { test_cases.each { |test| optimized_unglobbed_path(test) } }
end

0 comments on commit 96b557e

Please sign in to comment.