Skip to content

Commit 7e74433

Browse files
committed
Add relaxed option to allow invalid licenses and exception names
Fix #11.
1 parent 390a1dc commit 7e74433

File tree

4 files changed

+44
-22
lines changed

4 files changed

+44
-22
lines changed

index.js

Lines changed: 2 additions & 2 deletions
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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
var licenses = require('spdx-license-ids')
2+
var exceptions = require('spdx-exceptions')
13
var util = require('./util')
24

35
// The ABNF grammar in the spec is totally ambiguous.
46
//
57
// This parser follows the operator precedence defined in the
68
// `Order of Precedence and Parentheses` section.
7-
8-
module.exports = function (tokens) {
9+
//
10+
// options:
11+
// - Set `relaxed` to `true` to accept invalid license or exception IDs.
12+
module.exports = function (tokens, options) {
13+
options = options || {}
914
var index = 0
1015

1116
function hasMore () {
@@ -34,7 +39,10 @@ module.exports = function (tokens) {
3439
function parseWith () {
3540
if (parseOperator('WITH')) {
3641
var t = token()
37-
if (t && t.type === 'EXCEPTION') {
42+
if (t && t.type === 'IDENTIFIER') {
43+
if (!options.relaxed && exceptions.indexOf(t.string) === -1) {
44+
throw new Error('`' + t.string + '` is not a valid exception name')
45+
}
3846
next()
3947
return t.string
4048
}
@@ -67,7 +75,10 @@ module.exports = function (tokens) {
6775

6876
function parseLicense () {
6977
var t = token()
70-
if (t && t.type === 'LICENSE') {
78+
if (t && t.type === 'IDENTIFIER') {
79+
if (!options.relaxed && licenses.indexOf(t.string) === -1) {
80+
throw new Error('`' + t.string + '` is not a valid license name')
81+
}
7182
next()
7283
var node = {license: t.string}
7384
if (parseOperator('+')) {

scan.js

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
var licenses = require('spdx-license-ids')
2-
var exceptions = require('spdx-exceptions')
31
var util = require('./util')
42

53
module.exports = function (source) {
@@ -77,22 +75,11 @@ module.exports = function (source) {
7775
}
7876

7977
function identifier () {
80-
var begin = index
8178
var string = idstring()
82-
83-
if (licenses.indexOf(string) !== -1) {
84-
return {
85-
type: 'LICENSE',
86-
string
87-
}
88-
} else if (exceptions.indexOf(string) !== -1) {
89-
return {
90-
type: 'EXCEPTION',
91-
string
92-
}
79+
return string && {
80+
type: 'IDENTIFIER',
81+
string
9382
}
94-
95-
index = begin
9683
}
9784

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

test/index.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,27 @@ it('parses `AND`, `OR` and `WITH` with the correct precedence', () => {
8181
}
8282
)
8383
})
84+
85+
it('rejects invalid license and exception names by default', () => {
86+
assert.throws(
87+
() => p('unknownLicense'),
88+
/`unknownLicense` is not a valid license name/
89+
)
90+
91+
assert.throws(
92+
() => 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', () => {
98+
assert.deepStrictEqual(
99+
p('unknownLicense', {relaxed: true}),
100+
{license: 'unknownLicense'}
101+
)
102+
103+
assert.deepStrictEqual(
104+
p('MIT WITH unknownException', {relaxed: true}),
105+
{license: 'MIT', exception: 'unknownException'}
106+
)
107+
})

0 commit comments

Comments
 (0)