Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle licenses property used in old packages #11

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

tasshi-me
Copy link

@tasshi-me tasshi-me commented Dec 6, 2024

Why

Some old packages used invalid styles for license notation. (ref)

  • license object in the license property
  • license objects array in the licenses property

The license-manager has already supported license objects in license but not array in licenses.

Some popular packages depend on such packages with invalid license notation, so I want to handle them correctly.
e.g. npm-run-all depends on memorystream or undici depends on busboy

I also checked the pnpm behavior, and it handles the licenses property. (ref)

What

This PR adds a handler for the licenses property, as pnpm does.

  • detect licenses property
  • use type of the first element if licenses contains only an element
  • use SPDX converted value if licenses contains multiple elements

@tasshi-me tasshi-me marked this pull request as ready for review December 6, 2024 20:56
],
})
)
).toBe("(MIT OR ISC)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test using an actual package.json file to the basic-cases directory under tests/integrations? Since license information is retrieved via npm query, we need to prepare a test that uses it.

Comment on lines +12 to +18
if (Array.isArray(dep.licenses)) {
if (dep.licenses.length > 1) {
const joined = dep.licenses.map((l) => l.type).join(" OR ");
return `(${joined})`;
}
return dep.licenses.at(0)?.type ?? "";
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there are elements without a type, could you treat the entire retrieval as a failure and return ""?

The licenses specification is vague, and there's no guarantee that type will always be present.
https://docs.npmjs.com/cli/v10/configuring-npm/package-json#license

If none of the elements have a type, a string like " OR OR OR " might be generated. However, if elements without type are excluded, there's a risk that a string like "MIR OR ISC" could be generated when "MIT OR ISC OR Apache" is expected.

It might be safer to make the function work only when all elements in the array have a type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants