-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move only javascript.rb to root folder
- Loading branch information
Showing
5 changed files
with
92 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |