Skip to content

Commit

Permalink
Merge pull request #5025 from dependabot/translate-from-types-package…
Browse files Browse the repository at this point in the history
…-to-library

Translate from `@types` package to library name
  • Loading branch information
landongrindheim authored Apr 22, 2022
2 parents 479b048 + 3cb922b commit 82fcef4
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
21 changes: 21 additions & 0 deletions npm_and_yarn/lib/dependabot/npm_and_yarn/package_name.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ class PackageName
(?<name>[a-z0-9\-~][a-z0-9\-._~]*) # capture package name
\z # end of string
}xi.freeze # multi-line/case-insensitive
TYPES_PACKAGE_NAME_REGEX = %r{
\A # beginning of string
@#{DEFINITELY_TYPED_SCOPE}\/ # starts with @types/
((?<scope>.+)__)? # capture scope
(?<name>.+) # capture name
\z # end of string
}xi.freeze # multi-line/case-insensitive

class InvalidPackageName < StandardError; end

Expand Down Expand Up @@ -38,6 +45,20 @@ def eql?(other)
to_s.eql?(other.to_s)
end

def library_name
return self unless types_package?

@library_name ||=
begin
match = TYPES_PACKAGE_NAME_REGEX.match(to_s)
if match[:scope]
self.class.new("@#{match[:scope]}/#{match[:name]}")
else
self.class.new(match[:name].to_s)
end
end
end

def types_package_name
return self if types_package?

Expand Down
30 changes: 30 additions & 0 deletions npm_and_yarn/spec/dependabot/npm_and_yarn/package_name_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,36 @@
end
end

describe "#library_name" do
it "returns self if it is not a types package" do
jquery = "jquery"

library_name = described_class.new(jquery).library_name.to_s

expect(library_name).to eq(jquery)
end

it "returns the corresponding library for a types package" do
lodash_types = "@types/lodash"
lodash = "lodash"

library_name = described_class.new(lodash_types).library_name

expect(library_name.to_s).to eq(lodash)
end

context "when it is a scoped types package" do
it "returns the type packages scoped name" do
babel_core_types = "@types/babel__core"
babel_core = "@babel/core"

library_name = described_class.new(babel_core_types).library_name

expect(library_name.to_s).to eq(babel_core)
end
end
end

describe "#eql?" do
it "compares the string representation of the package name" do
package = described_class.new("package")
Expand Down

0 comments on commit 82fcef4

Please sign in to comment.