Skip to content

Commit 768f682

Browse files
committed
Add relaxed option to allow invalid licenses and exception names
Fix #11.
1 parent 17c2f51 commit 768f682

File tree

4 files changed

+45
-23
lines changed

4 files changed

+45
-23
lines changed

index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
var scan = require('./scan')
22
var parse = require('./parse')
33

4-
module.exports = function (source) {
5-
return parse(scan(source))
4+
module.exports = function (source, options) {
5+
return parse(scan(source), options)
66
}

parse.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
var licenses = require('spdx-license-ids')
2+
var exceptions = require('spdx-exceptions')
3+
14
// The ABNF grammar in the spec is totally ambiguous.
25
//
36
// This parser follows the operator precedence defined in the
47
// `Order of Precedence and Parentheses` section.
5-
6-
module.exports = function (tokens) {
8+
//
9+
// options:
10+
// - Set `relaxed` to `true` to accept invalid license or exception IDs.
11+
module.exports = function (tokens, options) {
12+
options = options || {}
713
var index = 0
814

915
function hasMore () {
@@ -32,7 +38,10 @@ module.exports = function (tokens) {
3238
function parseWith () {
3339
if (parseOperator('WITH')) {
3440
var t = token()
35-
if (t && t.type === 'EXCEPTION') {
41+
if (t && t.type === 'IDENTIFIER') {
42+
if (!options.relaxed && exceptions.indexOf(t.string) === -1) {
43+
throw new Error('`' + t.string + '` is not a valid exception name')
44+
}
3645
next()
3746
return t.string
3847
}
@@ -65,7 +74,10 @@ module.exports = function (tokens) {
6574

6675
function parseLicense () {
6776
var t = token()
68-
if (t && t.type === 'LICENSE') {
77+
if (t && t.type === 'IDENTIFIER') {
78+
if (!options.relaxed && licenses.indexOf(t.string) === -1) {
79+
throw new Error('`' + t.string + '` is not a valid license name')
80+
}
6981
next()
7082
var node = {license: t.string}
7183
if (parseOperator('+')) {

scan.js

+3-17
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
var licenses = require('spdx-license-ids')
2-
var exceptions = require('spdx-exceptions')
3-
41
module.exports = function (source) {
52
var index = 0
63

@@ -78,22 +75,11 @@ module.exports = function (source) {
7875
}
7976

8077
function identifier () {
81-
var begin = index
8278
var string = idstring()
83-
84-
if (licenses.indexOf(string) !== -1) {
85-
return {
86-
type: 'LICENSE',
87-
string: string
88-
}
89-
} else if (exceptions.indexOf(string) !== -1) {
90-
return {
91-
type: 'EXCEPTION',
92-
string: string
93-
}
79+
return string && {
80+
type: 'IDENTIFIER',
81+
string: string
9482
}
95-
96-
index = begin
9783
}
9884

9985
// Tries to read the next token. Returns `undefined` if no token is

test/index.js

+24
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,27 @@ it('parses `AND`, `OR` and `WITH` with the correct precedence', function () {
8181
}
8282
)
8383
})
84+
85+
it('rejects invalid license and exception names by default', function () {
86+
assert.throws(
87+
function () { p('unknownLicense') },
88+
/`unknownLicense` is not a valid license name/
89+
)
90+
91+
assert.throws(
92+
function () { p('MIT WITH unknownException') },
93+
/`unknownException` is not a valid exception name/
94+
)
95+
})
96+
97+
it('accepts invalid license and exception names in relaxed mode', function () {
98+
assert.deepEqual(
99+
p('unknownLicense', {relaxed: true}),
100+
{license: 'unknownLicense'}
101+
)
102+
103+
assert.deepEqual(
104+
p('MIT WITH unknownException', {relaxed: true}),
105+
{license: 'MIT', exception: 'unknownException'}
106+
)
107+
})

0 commit comments

Comments
 (0)