From 10dba3e20882d900419aa482ea9c1a2b92a4ad65 Mon Sep 17 00:00:00 2001 From: Julien Caselmann Date: Tue, 10 Sep 2019 11:29:49 +0200 Subject: [PATCH 1/3] Add option to skip validation of license names --- index.js | 4 ++-- scan.js | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 52fab56..13e3eb6 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,6 @@ var scan = require('./scan') var parse = require('./parse') -module.exports = function (source) { - return parse(scan(source)) +module.exports = function (source, validateLicenseNames = true) { + return parse(scan(source, validateLicenseNames)) } diff --git a/scan.js b/scan.js index b74fce2..728819b 100644 --- a/scan.js +++ b/scan.js @@ -5,7 +5,7 @@ var licenses = [] .concat(require('spdx-license-ids/deprecated')) var exceptions = require('spdx-exceptions') -module.exports = function (source) { +module.exports = function (source, validateLicenseNames = true) { var index = 0 function hasMore () { @@ -85,14 +85,14 @@ module.exports = function (source) { var begin = index var string = idstring() - if (licenses.indexOf(string) !== -1) { + if (exceptions.indexOf(string) !== -1) { return { - type: 'LICENSE', + type: 'EXCEPTION', string: string } - } else if (exceptions.indexOf(string) !== -1) { + } else if (licenses.indexOf(string) !== -1 || !validateLicenseNames) { return { - type: 'EXCEPTION', + type: 'LICENSE', string: string } } From f56e01916b5b5c2325738dfcde0babccca48e05d Mon Sep 17 00:00:00 2001 From: Julien Caselmann Date: Wed, 11 Sep 2019 14:39:23 +0200 Subject: [PATCH 2/3] Changed the default flag to false and added a second flag for whitespace skipping. --- index.js | 4 ++-- scan.js | 39 ++++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 13e3eb6..b13d125 100644 --- a/index.js +++ b/index.js @@ -3,6 +3,6 @@ var scan = require('./scan') var parse = require('./parse') -module.exports = function (source, validateLicenseNames = true) { - return parse(scan(source, validateLicenseNames)) +module.exports = function (source, doNotValidateLicenseNames = false, expectWhiteSpaceInLicenseNames = false) { + return parse(scan(source, doNotValidateLicenseNames, expectWhiteSpaceInLicenseNames)) } diff --git a/scan.js b/scan.js index 728819b..d2c5814 100644 --- a/scan.js +++ b/scan.js @@ -5,7 +5,7 @@ var licenses = [] .concat(require('spdx-license-ids/deprecated')) var exceptions = require('spdx-exceptions') -module.exports = function (source, validateLicenseNames = true) { +module.exports = function (source, doNotValidateLicenseNames = false, expectWhiteSpaceInLicenseNames = false) { var index = 0 function hasMore () { @@ -90,7 +90,7 @@ module.exports = function (source, validateLicenseNames = true) { type: 'EXCEPTION', string: string } - } else if (licenses.indexOf(string) !== -1 || !validateLicenseNames) { + } else if (licenses.indexOf(string) !== -1 || doNotValidateLicenseNames) { return { type: 'LICENSE', string: string @@ -118,7 +118,6 @@ module.exports = function (source, validateLicenseNames = true) { if (!hasMore()) { break } - var token = parseToken() if (!token) { throw new Error('Unexpected `' + source[index] + @@ -127,5 +126,39 @@ module.exports = function (source, validateLicenseNames = true) { tokens.push(token) } + + if(expectWhiteSpaceInLicenseNames){ + reduceExpandedLicenses(tokens) + } + return tokens } + +/** + * + * @param {Array} tokens array of tokens that the scan generated + * + * This method deals with the case of license names spelled with whitespace. + * When doNotValidateLicenseNames is flagged true, license names as Apache 2.0 will generate this token array: + * + * [ {type: LICENSE, string: 'Apache'}, {type: LICENSE, string: '2.0'} ] + * + * which will then lead to errors thrown in the parser. + * + * After application of this method the token array will look like this: + * + * [ { type : LICENSE, string: 'Apache 2.0'}] + * + */ +function reduceExpandedLicenses(tokens) { + var i = 0 + while (i < tokens.length) { + if (i < tokens.length - 1 && tokens[i].type === 'LICENSE' && tokens[i + 1].type === 'LICENSE') { + var concatName = tokens[i].string + ' ' + tokens[i + 1].string + tokens.splice(i + 1, 1) + tokens[i] = { type: 'LICENSE', string: concatName } + } else { + i += 1 + } + } +} \ No newline at end of file From 7411cea7a6ae79d63b8669a8b3252b3986f287c9 Mon Sep 17 00:00:00 2001 From: Julien Caselmann Date: Wed, 11 Sep 2019 14:45:49 +0200 Subject: [PATCH 3/3] Fixed the code style issues --- scan.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scan.js b/scan.js index d2c5814..d9a4252 100644 --- a/scan.js +++ b/scan.js @@ -127,7 +127,7 @@ module.exports = function (source, doNotValidateLicenseNames = false, expectWhit tokens.push(token) } - if(expectWhiteSpaceInLicenseNames){ + if (expectWhiteSpaceInLicenseNames) { reduceExpandedLicenses(tokens) } @@ -150,7 +150,7 @@ module.exports = function (source, doNotValidateLicenseNames = false, expectWhit * [ { type : LICENSE, string: 'Apache 2.0'}] * */ -function reduceExpandedLicenses(tokens) { +function reduceExpandedLicenses (tokens) { var i = 0 while (i < tokens.length) { if (i < tokens.length - 1 && tokens[i].type === 'LICENSE' && tokens[i + 1].type === 'LICENSE') { @@ -161,4 +161,4 @@ function reduceExpandedLicenses(tokens) { i += 1 } } -} \ No newline at end of file +}