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 licenseVisitor and noassertion type #24

Open
wants to merge 2 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
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var scan = require('./scan')
var parse = require('./parse')

module.exports = function (source) {
return parse(scan(source))
module.exports = function (source, options) {
options = options || {}
return parse(scan(source, options))
}
3 changes: 3 additions & 0 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ module.exports = function (tokens) {
node.exception = exception
}
return node
} else if (t && t.type === 'NOASSERTION') {
next()
return {noassertion: true}
}
}

Expand Down
17 changes: 12 additions & 5 deletions scan.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var licenses = []
.concat(require('spdx-license-ids/deprecated'))
var exceptions = require('spdx-exceptions')

module.exports = function (source) {
module.exports = function (source, options) {
options = options || {}
var index = 0

function hasMore () {
Expand Down Expand Up @@ -82,9 +83,12 @@ module.exports = function (source) {
}

function identifier () {
var begin = index
var string = idstring()

if (typeof options.licenseVisitor === 'function') {
string = options.licenseVisitor(string)
}

if (licenses.indexOf(string) !== -1) {
return {
type: 'LICENSE',
Expand All @@ -95,9 +99,12 @@ module.exports = function (source) {
type: 'EXCEPTION',
string: string
}
} else {
return {
type: 'NOASSERTION',
string: string
}
}

index = begin
}

// Tries to read the next token. Returns `undefined` if no token is
Expand All @@ -120,7 +127,7 @@ module.exports = function (source) {
}

var token = parseToken()
if (!token) {
if (!token || typeof token.string === 'undefined') {
throw new Error('Unexpected `' + source[index] +
'` at offset ' + index)
}
Expand Down
25 changes: 25 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,28 @@ it('parses `AND`, `OR` and `WITH` with the correct precedence', function () {
}
)
})

it('uses licenseVisitor to normalize licenseIdentifiers', function () {
assert.deepEqual(
p('mit OR Apache-2.0', { licenseVisitor: function (identifier) {
if (identifier === 'mit') return 'MIT'
return identifier
}}),
{
left: {license: 'MIT'},
conjunction: 'or',
right: {license: 'Apache-2.0'}
}
)
})

it('parses non-spdx licenses with noassertion', function () {
assert.deepEqual(
p('MIT OR Commercial'),
{
left: {license: 'MIT'},
conjunction: 'or',
right: {noassertion: true}
}
)
})