Skip to content

Commit

Permalink
Add relaxed option to allow invalid licenses and exception names
Browse files Browse the repository at this point in the history
Fix #11.
  • Loading branch information
motet-a committed Jun 15, 2017
1 parent 390a1dc commit 7e74433
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,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)
}
19 changes: 15 additions & 4 deletions parse.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
var licenses = require('spdx-license-ids')
var exceptions = require('spdx-exceptions')
var util = require('./util')

// 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 +39,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,7 +75,10 @@ module.exports = function (tokens) {

function parseLicense () {
var t = token()
if (t && t.type === 'LICENSE') {
if (t && t.type === 'IDENTIFIER') {
if (!options.relaxed && licenses.indexOf(t.string) === -1) {
throw new Error('`' + t.string + '` is not a valid license name')
}
next()
var node = {license: t.string}
if (parseOperator('+')) {
Expand Down
19 changes: 3 additions & 16 deletions scan.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
var licenses = require('spdx-license-ids')
var exceptions = require('spdx-exceptions')
var util = require('./util')

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

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

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

index = begin
}

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

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

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

it('accepts invalid license and exception names in relaxed mode', () => {
assert.deepStrictEqual(
p('unknownLicense', {relaxed: true}),
{license: 'unknownLicense'}
)

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

0 comments on commit 7e74433

Please sign in to comment.