Skip to content

Commit 9391609

Browse files
authored
Merge pull request #34 from qtomlinson/qt/fix_license-ref
Support LicenseRef
2 parents b1cf4be + 6bdeac8 commit 9391609

File tree

4 files changed

+134
-10
lines changed

4 files changed

+134
-10
lines changed

index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ const NOASSERTION = 'NOASSERTION'
1919
*
2020
* @param {string} expression SPDX expression
2121
* @param {Function} licenseVisitor Optional. Bring your own visitor to clean each node
22+
* @param {Function} licenseRefLookup Optional. Bring your own lookup to scan licenseRef
2223
* @returns {object} the AST representing the parsed expression
2324
*/
24-
function parse(expression, licenseVisitor) {
25+
function parse(expression, licenseVisitor, licenseRefLookup) {
2526
// if the expression is already an expression, just return it.
2627
if (!(typeof expression === 'string')) return expression
2728
licenseVisitor = licenseVisitor || normalizeSingle
2829
try {
29-
return spdxExpressionParse(expression, { relaxed: true, licenseVisitor })
30+
return spdxExpressionParse(expression, { relaxed: true, licenseVisitor, licenseRefLookup })
3031
} catch (e) {
3132
return { noassertion: true }
3233
}

package-lock.json

+6-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@clearlydefined/spdx",
3-
"version": "0.1.8",
3+
"version": "0.1.9",
44
"description": "SPDX custom libraries of clearlydefined.io.",
55
"license": "MIT",
66
"repository": {
@@ -20,7 +20,7 @@
2020
},
2121
"dependencies": {
2222
"spdx-expression-parse": "github:clearlydefined/spdx-expression-parse.js#fork",
23-
"spdx-license-ids": "^3.0.18",
23+
"spdx-license-ids": "^3.0.20",
2424
"spdx-license-list": "^6.9.0",
2525
"spdx-satisfies": "github:clearlydefined/spdx-satisfies.js#parse-override"
2626
},

test.js

+123
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,129 @@ describe('SPDX utility functions', () => {
9898
})
9999
})
100100

101+
it('parses licenseRef with lookup', () => {
102+
const data = new Map([
103+
['afpl-9.0', { license: 'LicenseRef-scancode-afpl-9.0' }],
104+
['(afpl-9.0)', { license: 'LicenseRef-scancode-afpl-9.0' }],
105+
[
106+
'(afpl-9.0 OR Apache-2.0)',
107+
{
108+
left: { license: 'LicenseRef-scancode-afpl-9.0' },
109+
conjunction: 'or',
110+
right: { license: 'Apache-2.0' }
111+
}
112+
],
113+
[
114+
'Apache-2.0 AND (afpl-9.0)',
115+
{
116+
left: { license: 'Apache-2.0' },
117+
conjunction: 'and',
118+
right: { license: 'LicenseRef-scancode-afpl-9.0' }
119+
}
120+
],
121+
[
122+
'Apache-2.0 AND (afpl-9.0 AND afpl-9.0)',
123+
{
124+
left: { license: 'Apache-2.0' },
125+
conjunction: 'and',
126+
right: {
127+
left: { license: 'LicenseRef-scancode-afpl-9.0' },
128+
conjunction: 'and',
129+
right: { license: 'LicenseRef-scancode-afpl-9.0' }
130+
}
131+
}
132+
],
133+
[
134+
'(afpl-9.0 AND afpl-9.0) AND Apache-2.0',
135+
{
136+
right: { license: 'Apache-2.0' },
137+
conjunction: 'and',
138+
left: {
139+
left: { license: 'LicenseRef-scancode-afpl-9.0' },
140+
conjunction: 'and',
141+
right: { license: 'LicenseRef-scancode-afpl-9.0' }
142+
}
143+
}
144+
],
145+
[
146+
'Apache-2.0 AND (afpl-9.0 OR afpl-9.0)',
147+
{
148+
left: { license: 'Apache-2.0' },
149+
conjunction: 'and',
150+
right: {
151+
left: { license: 'LicenseRef-scancode-afpl-9.0' },
152+
conjunction: 'or',
153+
right: { license: 'LicenseRef-scancode-afpl-9.0' }
154+
}
155+
}
156+
],
157+
[
158+
'MIT AND GPL-3.0',
159+
{
160+
left: { license: 'MIT' },
161+
conjunction: 'and',
162+
right: { license: 'GPL-3.0' }
163+
}
164+
],
165+
[
166+
'AFL-1.1 AND afpl-9.0',
167+
{
168+
left: { license: 'AFL-1.1' },
169+
conjunction: 'and',
170+
right: { license: 'LicenseRef-scancode-afpl-9.0' }
171+
}
172+
],
173+
[
174+
'afpl-9.0 AND MIT',
175+
{
176+
left: { license: 'LicenseRef-scancode-afpl-9.0' },
177+
conjunction: 'and',
178+
right: { license: 'MIT' }
179+
}
180+
],
181+
[
182+
'afpl-9.0 AND activestate-community',
183+
{
184+
left: { license: 'LicenseRef-scancode-afpl-9.0' },
185+
conjunction: 'and',
186+
right: { license: 'LicenseRef-scancode-activestate-community' }
187+
}
188+
],
189+
[
190+
'afpl-9.0 AND activestate-community OR ac3filter',
191+
{
192+
left: {
193+
left: { license: 'LicenseRef-scancode-afpl-9.0' },
194+
conjunction: 'and',
195+
right: { license: 'LicenseRef-scancode-activestate-community' }
196+
},
197+
conjunction: 'or',
198+
right: { license: 'LicenseRef-scancode-ac3filter' }
199+
}
200+
],
201+
['INVALID', { noassertion: null }],
202+
[
203+
'LicenseRef-scancode-afpl-9.0 AND MIT',
204+
{
205+
left: { license: 'LicenseRef-scancode-afpl-9.0' },
206+
conjunction: 'and',
207+
right: { license: 'MIT' }
208+
}
209+
]
210+
])
211+
212+
const licenseRefLookup = function (identifier) {
213+
if (identifier === 'afpl-9.0') return 'LicenseRef-scancode-afpl-9.0'
214+
if (identifier === 'activestate-community') return 'LicenseRef-scancode-activestate-community'
215+
if (identifier === 'ac3filter') return 'LicenseRef-scancode-ac3filter'
216+
return identifier
217+
}
218+
219+
data.forEach((expected, input) => {
220+
expect(SPDX.parse(input, undefined, licenseRefLookup)).to.deep.equal(expected)
221+
})
222+
})
223+
101224
it('stringifies spdx objects', () => {
102225
const data = new Map([
103226
[{ license: 'MIT' }, 'MIT'],

0 commit comments

Comments
 (0)