Skip to content

Commit 6926d6a

Browse files
committed
Add relaxed option to allow invalid licenses and exception names
Fix #11.
1 parent 6b1625e commit 6926d6a

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
@@ -3,6 +3,6 @@
33
var scan = require('./scan')
44
var parse = require('./parse')
55

6-
module.exports = function (source) {
7-
return parse(scan(source))
6+
module.exports = function (source, options) {
7+
return parse(scan(source), options)
88
}

parse.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
'use strict'
22

3+
var licenses = require('spdx-license-ids')
4+
var exceptions = require('spdx-exceptions')
5+
36
// The ABNF grammar in the spec is totally ambiguous.
47
//
58
// This parser follows the operator precedence defined in the
69
// `Order of Precedence and Parentheses` section.
7-
8-
module.exports = function (tokens) {
10+
//
11+
// options:
12+
// - Set `relaxed` to `true` to accept invalid license or exception IDs.
13+
module.exports = function (tokens, options) {
14+
options = options || {}
915
var index = 0
1016

1117
function hasMore () {
@@ -34,7 +40,10 @@ module.exports = function (tokens) {
3440
function parseWith () {
3541
if (parseOperator('WITH')) {
3642
var t = token()
37-
if (t && t.type === 'EXCEPTION') {
43+
if (t && t.type === 'IDENTIFIER') {
44+
if (!options.relaxed && exceptions.indexOf(t.string) === -1) {
45+
throw new Error('`' + t.string + '` is not a valid exception name')
46+
}
3847
next()
3948
return t.string
4049
}
@@ -67,7 +76,10 @@ module.exports = function (tokens) {
6776

6877
function parseLicense () {
6978
var t = token()
70-
if (t && t.type === 'LICENSE') {
79+
if (t && t.type === 'IDENTIFIER') {
80+
if (!options.relaxed && licenses.indexOf(t.string) === -1) {
81+
throw new Error('`' + t.string + '` is not a valid license name')
82+
}
7183
next()
7284
var node = {license: t.string}
7385
if (parseOperator('+')) {

scan.js

+3-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
'use strict'
22

3-
var licenses = require('spdx-license-ids')
4-
var exceptions = require('spdx-exceptions')
5-
63
module.exports = function (source) {
74
var index = 0
85

@@ -80,22 +77,11 @@ module.exports = function (source) {
8077
}
8178

8279
function identifier () {
83-
var begin = index
8480
var string = idstring()
85-
86-
if (licenses.indexOf(string) !== -1) {
87-
return {
88-
type: 'LICENSE',
89-
string: string
90-
}
91-
} else if (exceptions.indexOf(string) !== -1) {
92-
return {
93-
type: 'EXCEPTION',
94-
string: string
95-
}
81+
return string && {
82+
type: 'IDENTIFIER',
83+
string: string
9684
}
97-
98-
index = begin
9985
}
10086

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

test/index.js

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

0 commit comments

Comments
 (0)