Skip to content

Commit 09b981d

Browse files
committed
fix: inline license validation code
1 parent cb3e677 commit 09b981d

4 files changed

Lines changed: 65 additions & 2 deletions

File tree

lib/license.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// This is an implementation of the validForNewPackage flag in validate-npm-package-license, which is no longer maintained
2+
3+
const parse = require('spdx-expression-parse')
4+
5+
function usesLicenseRef (ast) {
6+
if (Object.hasOwn(ast, 'license')) {
7+
return ast.license.startsWith('LicenseRef') || ast.license.startsWith('DocumentRef')
8+
} else {
9+
return usesLicenseRef(ast.left) || usesLicenseRef(ast.right)
10+
}
11+
}
12+
13+
// license should be a valid SPDX license expression (without "LicenseRef"), "UNLICENSED", or "SEE LICENSE IN <filename>"
14+
module.exports = function licenseValidForNewPackage (argument) {
15+
if (argument === 'UNLICENSED' || argument === 'UNLICENCED') {
16+
return true
17+
}
18+
if (/^SEE LICEN[CS]E IN ./.test(argument)) {
19+
return true
20+
}
21+
try {
22+
const ast = parse(argument)
23+
return !usesLicenseRef(ast)
24+
} catch {
25+
return false
26+
}
27+
}

lib/normalize-data.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

77
const typos = {
88
dependancies: 'dependencies',
@@ -230,7 +230,7 @@ function normalizeData (data, changes) {
230230
changes?.push('No license field.')
231231
} else if (typeof (license) !== 'string' || license.length < 1 || license.trim() === '') {
232232
changes?.push('license should be a valid SPDX license expression')
233-
} else if (!validateLicense(license).validForNewPackages) {
233+
} else if (!validateLicense(license)) {
234234
changes?.push('license should be a valid SPDX license expression')
235235
}
236236
// fixPeople

tap-snapshots/test/normalize-data.js.test.cjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,18 @@ exports[`test/normalize-data.js TAP fixKeywordsField splits string > must match
166166
Array []
167167
`
168168

169+
exports[`test/normalize-data.js TAP fixLicenseField SPDX > must match snapshot 1`] = `
170+
Array []
171+
`
172+
173+
exports[`test/normalize-data.js TAP fixLicenseField SPDX LEFT and RIGHT > must match snapshot 1`] = `
174+
Array []
175+
`
176+
177+
exports[`test/normalize-data.js TAP fixLicenseField file ref > must match snapshot 1`] = `
178+
Array []
179+
`
180+
169181
exports[`test/normalize-data.js TAP fixLicenseField invalid > must match snapshot 1`] = `
170182
Array [
171183
"license should be a valid SPDX license expression",

test/normalize-data.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,30 @@ t.test('fixLicenseField', async t => {
330330
})
331331
t.equal(content.license, 'BESPOKE LICENSE')
332332
})
333+
334+
t.test('SPDX', async t => {
335+
const { content } = await normalizeData(t, {
336+
...base,
337+
license: 'MIT',
338+
})
339+
t.equal(content.license, 'MIT')
340+
})
341+
342+
t.test('file ref', async t => {
343+
const { content } = await normalizeData(t, {
344+
...base,
345+
license: 'SEE LICENSE IN LICENSE.TXT',
346+
})
347+
t.equal(content.license, 'SEE LICENSE IN LICENSE.TXT')
348+
})
349+
350+
t.test('SPDX LEFT and RIGHT', async t => {
351+
const { content } = await normalizeData(t, {
352+
...base,
353+
license: 'MIT or ISC',
354+
})
355+
t.equal(content.license, 'MIT or ISC')
356+
})
333357
})
334358

335359
t.test('fixPeople', async t => {

0 commit comments

Comments
 (0)