11cb0ef41Sopenharmony_civar parse = require('spdx-expression-parse');
21cb0ef41Sopenharmony_civar correct = require('spdx-correct');
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_civar genericWarning = (
51cb0ef41Sopenharmony_ci  'license should be ' +
61cb0ef41Sopenharmony_ci  'a valid SPDX license expression (without "LicenseRef"), ' +
71cb0ef41Sopenharmony_ci  '"UNLICENSED", or ' +
81cb0ef41Sopenharmony_ci  '"SEE LICENSE IN <filename>"'
91cb0ef41Sopenharmony_ci);
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_civar fileReferenceRE = /^SEE LICEN[CS]E IN (.+)$/;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cifunction startsWith(prefix, string) {
141cb0ef41Sopenharmony_ci  return string.slice(0, prefix.length) === prefix;
151cb0ef41Sopenharmony_ci}
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_cifunction usesLicenseRef(ast) {
181cb0ef41Sopenharmony_ci  if (ast.hasOwnProperty('license')) {
191cb0ef41Sopenharmony_ci    var license = ast.license;
201cb0ef41Sopenharmony_ci    return (
211cb0ef41Sopenharmony_ci      startsWith('LicenseRef', license) ||
221cb0ef41Sopenharmony_ci      startsWith('DocumentRef', license)
231cb0ef41Sopenharmony_ci    );
241cb0ef41Sopenharmony_ci  } else {
251cb0ef41Sopenharmony_ci    return (
261cb0ef41Sopenharmony_ci      usesLicenseRef(ast.left) ||
271cb0ef41Sopenharmony_ci      usesLicenseRef(ast.right)
281cb0ef41Sopenharmony_ci    );
291cb0ef41Sopenharmony_ci  }
301cb0ef41Sopenharmony_ci}
311cb0ef41Sopenharmony_ci
321cb0ef41Sopenharmony_cimodule.exports = function(argument) {
331cb0ef41Sopenharmony_ci  var ast;
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  try {
361cb0ef41Sopenharmony_ci    ast = parse(argument);
371cb0ef41Sopenharmony_ci  } catch (e) {
381cb0ef41Sopenharmony_ci    var match
391cb0ef41Sopenharmony_ci    if (
401cb0ef41Sopenharmony_ci      argument === 'UNLICENSED' ||
411cb0ef41Sopenharmony_ci      argument === 'UNLICENCED'
421cb0ef41Sopenharmony_ci    ) {
431cb0ef41Sopenharmony_ci      return {
441cb0ef41Sopenharmony_ci        validForOldPackages: true,
451cb0ef41Sopenharmony_ci        validForNewPackages: true,
461cb0ef41Sopenharmony_ci        unlicensed: true
471cb0ef41Sopenharmony_ci      };
481cb0ef41Sopenharmony_ci    } else if (match = fileReferenceRE.exec(argument)) {
491cb0ef41Sopenharmony_ci      return {
501cb0ef41Sopenharmony_ci        validForOldPackages: true,
511cb0ef41Sopenharmony_ci        validForNewPackages: true,
521cb0ef41Sopenharmony_ci        inFile: match[1]
531cb0ef41Sopenharmony_ci      };
541cb0ef41Sopenharmony_ci    } else {
551cb0ef41Sopenharmony_ci      var result = {
561cb0ef41Sopenharmony_ci        validForOldPackages: false,
571cb0ef41Sopenharmony_ci        validForNewPackages: false,
581cb0ef41Sopenharmony_ci        warnings: [genericWarning]
591cb0ef41Sopenharmony_ci      };
601cb0ef41Sopenharmony_ci      if (argument.trim().length !== 0) {
611cb0ef41Sopenharmony_ci        var corrected = correct(argument);
621cb0ef41Sopenharmony_ci        if (corrected) {
631cb0ef41Sopenharmony_ci          result.warnings.push(
641cb0ef41Sopenharmony_ci            'license is similar to the valid expression "' + corrected + '"'
651cb0ef41Sopenharmony_ci          );
661cb0ef41Sopenharmony_ci        }
671cb0ef41Sopenharmony_ci      }
681cb0ef41Sopenharmony_ci      return result;
691cb0ef41Sopenharmony_ci    }
701cb0ef41Sopenharmony_ci  }
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  if (usesLicenseRef(ast)) {
731cb0ef41Sopenharmony_ci    return {
741cb0ef41Sopenharmony_ci      validForNewPackages: false,
751cb0ef41Sopenharmony_ci      validForOldPackages: false,
761cb0ef41Sopenharmony_ci      spdx: true,
771cb0ef41Sopenharmony_ci      warnings: [genericWarning]
781cb0ef41Sopenharmony_ci    };
791cb0ef41Sopenharmony_ci  } else {
801cb0ef41Sopenharmony_ci    return {
811cb0ef41Sopenharmony_ci      validForNewPackages: true,
821cb0ef41Sopenharmony_ci      validForOldPackages: true,
831cb0ef41Sopenharmony_ci      spdx: true
841cb0ef41Sopenharmony_ci    };
851cb0ef41Sopenharmony_ci  }
861cb0ef41Sopenharmony_ci};
87