Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to skip validation of license names #27

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
var scan = require('./scan')
var parse = require('./parse')

module.exports = function (source) {
return parse(scan(source))
module.exports = function (source, doNotValidateLicenseNames = false, expectWhiteSpaceInLicenseNames = false) {
return parse(scan(source, doNotValidateLicenseNames, expectWhiteSpaceInLicenseNames))
}
45 changes: 39 additions & 6 deletions scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, doNotValidateLicenseNames = false, expectWhiteSpaceInLicenseNames = false) {
var index = 0

function hasMore () {
Expand Down Expand Up @@ -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 || doNotValidateLicenseNames) {
return {
type: 'EXCEPTION',
type: 'LICENSE',
string: string
}
}
Expand All @@ -118,7 +118,6 @@ module.exports = function (source) {
if (!hasMore()) {
break
}

var token = parseToken()
if (!token) {
throw new Error('Unexpected `' + source[index] +
Expand All @@ -127,5 +126,39 @@ module.exports = function (source) {

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
}
}
}