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
27 changes: 27 additions & 0 deletions lib/license.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// This is an implementation of the validForNewPackage flag in validate-npm-package-license, which is no longer maintained

const parse = require('spdx-expression-parse')

function usesLicenseRef (ast) {
if (Object.hasOwn(ast, 'license')) {
return ast.license.startsWith('LicenseRef') || ast.license.startsWith('DocumentRef')
} else {
return usesLicenseRef(ast.left) || usesLicenseRef(ast.right)
}
}

// license should be a valid SPDX license expression (without "LicenseRef"), "UNLICENSED", or "SEE LICENSE IN <filename>"
module.exports = function licenseValidForNewPackage (argument) {
if (argument === 'UNLICENSED' || argument === 'UNLICENCED') {
return true
}
if (/^SEE LICEN[CS]E IN ./.test(argument)) {
return true
}
try {
const ast = parse(argument)
return !usesLicenseRef(ast)
} catch {
return false
}
}
4 changes: 2 additions & 2 deletions lib/normalize-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { URL } = require('node:url')
const hostedGitInfo = require('hosted-git-info')
const validateLicense = require('validate-npm-package-license')
const validateLicense = require('./license.js')

const typos = {
dependancies: 'dependencies',
Expand Down Expand Up @@ -230,7 +230,7 @@ function normalizeData (data, changes) {
changes?.push('No license field.')
} else if (typeof (license) !== 'string' || license.length < 1 || license.trim() === '') {
changes?.push('license should be a valid SPDX license expression')
} else if (!validateLicense(license).validForNewPackages) {
} else if (!validateLicense(license)) {
changes?.push('license should be a valid SPDX license expression')
}
// fixPeople
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"json-parse-even-better-errors": "^5.0.0",
"proc-log": "^6.0.0",
"semver": "^7.5.3",
"validate-npm-package-license": "^3.0.4"
"spdx-expression-parse": "^4.0.0"
},
"devDependencies": {
"@npmcli/eslint-config": "^6.0.0",
Expand Down
12 changes: 12 additions & 0 deletions tap-snapshots/test/normalize-data.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,18 @@ exports[`test/normalize-data.js TAP fixKeywordsField splits string > must match
Array []
`

exports[`test/normalize-data.js TAP fixLicenseField SPDX > must match snapshot 1`] = `
Array []
`

exports[`test/normalize-data.js TAP fixLicenseField SPDX LEFT and RIGHT > must match snapshot 1`] = `
Array []
`

exports[`test/normalize-data.js TAP fixLicenseField file ref > must match snapshot 1`] = `
Array []
`

exports[`test/normalize-data.js TAP fixLicenseField invalid > must match snapshot 1`] = `
Array [
"license should be a valid SPDX license expression",
Expand Down
24 changes: 24 additions & 0 deletions test/normalize-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,30 @@ t.test('fixLicenseField', async t => {
})
t.equal(content.license, 'BESPOKE LICENSE')
})

t.test('SPDX', async t => {
const { content } = await normalizeData(t, {
...base,
license: 'MIT',
})
t.equal(content.license, 'MIT')
})

t.test('file ref', async t => {
const { content } = await normalizeData(t, {
...base,
license: 'SEE LICENSE IN LICENSE.TXT',
})
t.equal(content.license, 'SEE LICENSE IN LICENSE.TXT')
})

t.test('SPDX LEFT and RIGHT', async t => {
const { content } = await normalizeData(t, {
...base,
license: 'MIT or ISC',
})
t.equal(content.license, 'MIT or ISC')
})
})

t.test('fixPeople', async t => {
Expand Down
Loading