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

Relaxed option with noassertion #25

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, options) {
return parse(scan(source), options)
}
25 changes: 21 additions & 4 deletions parse.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
'use strict'

var licenses = []
.concat(require('spdx-license-ids'))
.concat(require('spdx-license-ids/deprecated'))
var exceptions = require('spdx-exceptions')

// The ABNF grammar in the spec is totally ambiguous.
//
// This parser follows the operator precedence defined in the
// `Order of Precedence and Parentheses` section.

module.exports = function (tokens) {
//
// options:
// - Set `relaxed` to `true` to accept invalid license or exception IDs.
module.exports = function (tokens, options) {
options = options || {}
var index = 0

function hasMore () {
Expand Down Expand Up @@ -34,7 +42,10 @@ module.exports = function (tokens) {
function parseWith () {
if (parseOperator('WITH')) {
var t = token()
if (t && t.type === 'EXCEPTION') {
if (t && t.type === 'IDENTIFIER') {
if (!options.relaxed && exceptions.indexOf(t.string) === -1) {
throw new Error('`' + t.string + '` is not a valid exception name')
}
next()
return t.string
}
Expand Down Expand Up @@ -67,8 +78,14 @@ module.exports = function (tokens) {

function parseLicense () {
var t = token()
if (t && t.type === 'LICENSE') {
if (t && t.type === 'IDENTIFIER') {
next()
if (licenses.indexOf(t.string) === -1) {
if (options.relaxed) {
return {noassertion: t.string}
}
throw new Error('`' + t.string + '` is not a valid license name')
}
var node = {license: t.string}
if (parseOperator('+')) {
node.plus = true
Expand Down
22 changes: 3 additions & 19 deletions scan.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
'use strict'

var licenses = []
.concat(require('spdx-license-ids'))
.concat(require('spdx-license-ids/deprecated'))
var exceptions = require('spdx-exceptions')

module.exports = function (source) {
var index = 0

Expand Down Expand Up @@ -82,22 +77,11 @@ module.exports = function (source) {
}

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

if (licenses.indexOf(string) !== -1) {
return {
type: 'LICENSE',
string: string
}
} else if (exceptions.indexOf(string) !== -1) {
return {
type: 'EXCEPTION',
string: string
}
return string && {
type: 'IDENTIFIER',
string: string
}

index = begin
}

// Tries to read the next token. Returns `undefined` if no token is
Expand Down
33 changes: 33 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,36 @@ it('parses `AND`, `OR` and `WITH` with the correct precedence', function () {
}
)
})

it('rejects invalid license and exception names by default', function () {
assert.throws(
function () { p('unknownLicense') },
/`unknownLicense` is not a valid license name/
)

assert.throws(
function () { p('MIT WITH unknownException') },
/`unknownException` is not a valid exception name/
)
})

it('accepts invalid license and exception names in relaxed mode', function () {
assert.deepEqual(
p('unknownLicense', {relaxed: true}),
{noassertion: 'unknownLicense'}
)

assert.deepEqual(
p('MIT WITH unknownException', {relaxed: true}),
{license: 'MIT', exception: 'unknownException'}
)

assert.deepEqual(
p('MIT OR Commercial', {relaxed: true}),
{
left: {license: 'MIT'},
conjunction: 'or',
right: {noassertion: 'Commercial'}
}
)
})