diff --git a/npm_and_yarn/lib/dependabot/npm_and_yarn/package_name.rb b/npm_and_yarn/lib/dependabot/npm_and_yarn/package_name.rb index fab9e42a0dc..3c01421ff93 100644 --- a/npm_and_yarn/lib/dependabot/npm_and_yarn/package_name.rb +++ b/npm_and_yarn/lib/dependabot/npm_and_yarn/package_name.rb @@ -11,6 +11,13 @@ class PackageName (?[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/ + ((?.+)__)? # capture scope + (?.+) # capture name + \z # end of string + }xi.freeze # multi-line/case-insensitive class InvalidPackageName < StandardError; end @@ -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? diff --git a/npm_and_yarn/spec/dependabot/npm_and_yarn/package_name_spec.rb b/npm_and_yarn/spec/dependabot/npm_and_yarn/package_name_spec.rb index 6ee71e321a2..d6f067abc60 100644 --- a/npm_and_yarn/spec/dependabot/npm_and_yarn/package_name_spec.rb +++ b/npm_and_yarn/spec/dependabot/npm_and_yarn/package_name_spec.rb @@ -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")