diff --git a/src/packagedcode/npm.py b/src/packagedcode/npm.py index 7618e830c19..c1382660e18 100644 --- a/src/packagedcode/npm.py +++ b/src/packagedcode/npm.py @@ -1059,18 +1059,17 @@ def parse(cls, location, package_only=False): requirements = [r.strip().strip("\"'") for r in requirements] for req in requirements: if req.startswith('@'): - # 2 = package, 4 = alias - assert req.count('@') in [2, 4] + # 2 = scoped package, 3 = scoped alias of unscoped package, + # 4 = scoped alias of scoped package + assert req.count('@') in [2, 3, 4] ns_name, _, constraint = req.rpartition('@') - ns, _ , name = ns_name.rpartition('/') constraint = constraint.strip("\"'") - # If we have an alias, just keep the package part: + # If we have an alias, just keep the target package part: # @npm: - if "@npm:" in ns: - ns = ns.split(':')[1] - if "@npm:" in name: - name = name.split(':')[1] + if '@npm:' in ns_name: + _, _, ns_name = ns_name.partition('@npm:') + ns, _, name = ns_name.rpartition('/') top_requirements.append((ns, name, constraint,)) else: diff --git a/tests/packagedcode/data/npm/yarn-lock/v1-scoped-alias/yarn.lock b/tests/packagedcode/data/npm/yarn-lock/v1-scoped-alias/yarn.lock new file mode 100644 index 00000000000..210679f97ec --- /dev/null +++ b/tests/packagedcode/data/npm/yarn-lock/v1-scoped-alias/yarn.lock @@ -0,0 +1,8 @@ +# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. +# yarn lockfile v1 + + +"@jest/react-is-18@npm:react-is@^18.3.1": + version "18.3.1" + resolved "https://registry.yarnpkg.com/react-is/-/react-is-18.3.1.tgz#4b85e8c2aa69c18a8f49d2738c67fd38451f5133" + integrity sha512-1i2/1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1x1Q== diff --git a/tests/packagedcode/data/npm/yarn-lock/v1-scoped-alias/yarn.lock-expected b/tests/packagedcode/data/npm/yarn-lock/v1-scoped-alias/yarn.lock-expected new file mode 100644 index 00000000000..78b0de4cf71 --- /dev/null +++ b/tests/packagedcode/data/npm/yarn-lock/v1-scoped-alias/yarn.lock-expected @@ -0,0 +1,101 @@ +[ + { + "type": "npm", + "namespace": null, + "name": null, + "version": null, + "qualifiers": {}, + "subpath": null, + "primary_language": "JavaScript", + "description": null, + "release_date": null, + "parties": [], + "keywords": [], + "homepage_url": null, + "download_url": null, + "size": null, + "sha1": null, + "md5": null, + "sha256": null, + "sha512": null, + "bug_tracking_url": null, + "code_view_url": null, + "vcs_url": null, + "copyright": null, + "holder": null, + "declared_license_expression": null, + "declared_license_expression_spdx": null, + "license_detections": [], + "other_license_expression": null, + "other_license_expression_spdx": null, + "other_license_detections": [], + "extracted_license_statement": null, + "notice_text": null, + "source_packages": [], + "file_references": [], + "is_private": false, + "is_virtual": false, + "extra_data": {}, + "dependencies": [ + { + "purl": "pkg:npm/react-is@18.3.1", + "extracted_requirement": "^18.3.1", + "scope": "dependencies", + "is_runtime": true, + "is_optional": false, + "is_pinned": true, + "is_direct": false, + "resolved_package": { + "type": "npm", + "namespace": "", + "name": "react-is", + "version": "18.3.1", + "qualifiers": {}, + "subpath": null, + "primary_language": "JavaScript", + "description": null, + "release_date": null, + "parties": [], + "keywords": [], + "homepage_url": null, + "download_url": "https://registry.yarnpkg.com/react-is/-/react-is-18.3.1.tgz", + "size": null, + "sha1": "4b85e8c2aa69c18a8f49d2738c67fd38451f5133", + "md5": null, + "sha256": null, + "sha512": "d62dbfd71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d71d5", + "bug_tracking_url": null, + "code_view_url": null, + "vcs_url": null, + "copyright": null, + "holder": null, + "declared_license_expression": null, + "declared_license_expression_spdx": null, + "license_detections": [], + "other_license_expression": null, + "other_license_expression_spdx": null, + "other_license_detections": [], + "extracted_license_statement": null, + "notice_text": null, + "source_packages": [], + "file_references": [], + "is_private": false, + "is_virtual": true, + "extra_data": {}, + "dependencies": [], + "repository_homepage_url": "https://www.npmjs.com/package/react-is", + "repository_download_url": "https://registry.npmjs.org/react-is/-/react-is-18.3.1.tgz", + "api_data_url": "https://registry.npmjs.org/react-is/18.3.1", + "datasource_id": "yarn_lock_v1", + "purl": "pkg:npm/react-is@18.3.1" + }, + "extra_data": {} + } + ], + "repository_homepage_url": null, + "repository_download_url": null, + "api_data_url": null, + "datasource_id": "yarn_lock_v1", + "purl": null + } +] diff --git a/tests/packagedcode/test_npm.py b/tests/packagedcode/test_npm.py index 0be0bfe0ca1..f8b68a7badf 100644 --- a/tests/packagedcode/test_npm.py +++ b/tests/packagedcode/test_npm.py @@ -353,6 +353,13 @@ def test_parse_yarn_lock_v1_complex_with_duplicate_aliases(self): packages = npm.YarnLockV1Handler.parse(test_file) self.check_packages_data(packages, expected_loc, regen=REGEN_TEST_FIXTURES) + def test_parse_yarn_lock_v1_scoped_alias_of_unscoped_package(self): + test_file = self.get_test_loc('npm/yarn-lock/v1-scoped-alias/yarn.lock') + expected_loc = self.get_test_loc( + 'npm/yarn-lock/v1-scoped-alias/yarn.lock-expected') + packages = npm.YarnLockV1Handler.parse(test_file) + self.check_packages_data(packages, expected_loc, regen=REGEN_TEST_FIXTURES) + def test_is_datafile_yarn_lock_v2(self): test_file = self.get_test_loc('npm/yarn-lock/v2/yarn.lock') assert npm.YarnLockV2Handler.is_datafile(test_file)