index.js 1.8 KB

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