UNPKG

1.86 kBJavaScriptView Raw
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 var corrected = correct(argument);
61 if (corrected) {
62 result.warnings.push(
63 'license is similar to the valid expression "' + corrected + '"'
64 );
65 }
66 return result;
67 }
68 }
69
70 if (usesLicenseRef(ast)) {
71 return {
72 validForNewPackages: false,
73 validForOldPackages: false,
74 spdx: true,
75 warnings: [genericWarning]
76 };
77 } else {
78 return {
79 validForNewPackages: true,
80 validForOldPackages: true,
81 spdx: true
82 };
83 }
84};