File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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 ( / ^ S E E L I C E N [ C S ] E I N ./ . test ( argument ) ) {
19+ return true
20+ }
21+ try {
22+ const ast = parse ( argument )
23+ return ! usesLicenseRef ( ast )
24+ } catch {
25+ return false
26+ }
27+ }
Original file line number Diff line number Diff line change 22
33const { URL } = require ( 'node:url' )
44const hostedGitInfo = require ( 'hosted-git-info' )
5- const validateLicense = require ( 'validate-npm-package- license' )
5+ const validateLicense = require ( './ license.js ' )
66
77const 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
Original file line number Diff line number Diff line change @@ -166,6 +166,18 @@ exports[`test/normalize-data.js TAP fixKeywordsField splits string > must match
166166Array []
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+
169181exports [ `test/normalize-data.js TAP fixLicenseField invalid > must match snapshot 1` ] = `
170182Array [
171183 "license should be a valid SPDX license expression",
Original file line number Diff line number Diff 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
335359t . test ( 'fixPeople' , async t => {
You can’t perform that action at this time.
0 commit comments