1var parse = require('spdx-expression-parse'); 2var correct = require('spdx-correct'); 3 4var genericWarning = ( 5 'license should be ' + 6 'a valid SPDX license expression (without "LicenseRef"), ' + 7 '"UNLICENSED", or ' + 8 '"SEE LICENSE IN <filename>"' 9); 10 11var fileReferenceRE = /^SEE LICEN[CS]E IN (.+)$/; 12 13function startsWith(prefix, string) { 14 return string.slice(0, prefix.length) === prefix; 15} 16 17function usesLicenseRef(ast) { 18 if (ast.hasOwnProperty('license')) { 19 var license = ast.license; 20 return ( 21 startsWith('LicenseRef', license) || 22 startsWith('DocumentRef', license) 23 ); 24 } else { 25 return ( 26 usesLicenseRef(ast.left) || 27 usesLicenseRef(ast.right) 28 ); 29 } 30} 31 32module.exports = function(argument) { 33 var ast; 34 35 try { 36 ast = parse(argument); 37 } catch (e) { 38 var match 39 if ( 40 argument === 'UNLICENSED' || 41 argument === 'UNLICENCED' 42 ) { 43 return { 44 validForOldPackages: true, 45 validForNewPackages: true, 46 unlicensed: true 47 }; 48 } else if (match = fileReferenceRE.exec(argument)) { 49 return { 50 validForOldPackages: true, 51 validForNewPackages: true, 52 inFile: match[1] 53 }; 54 } else { 55 var result = { 56 validForOldPackages: false, 57 validForNewPackages: false, 58 warnings: [genericWarning] 59 }; 60 if (argument.trim().length !== 0) { 61 var corrected = correct(argument); 62 if (corrected) { 63 result.warnings.push( 64 'license is similar to the valid expression "' + corrected + '"' 65 ); 66 } 67 } 68 return result; 69 } 70 } 71 72 if (usesLicenseRef(ast)) { 73 return { 74 validForNewPackages: false, 75 validForOldPackages: false, 76 spdx: true, 77 warnings: [genericWarning] 78 }; 79 } else { 80 return { 81 validForNewPackages: true, 82 validForOldPackages: true, 83 spdx: true 84 }; 85 } 86}; 87