Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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