Skip to content

Commit

Permalink
Merge pull request #34 from qtomlinson/qt/fix_license-ref
Browse files Browse the repository at this point in the history
Support LicenseRef
  • Loading branch information
qtomlinson authored Oct 17, 2024
2 parents b1cf4be + 6bdeac8 commit 9391609
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 10 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ const NOASSERTION = 'NOASSERTION'
*
* @param {string} expression SPDX expression
* @param {Function} licenseVisitor Optional. Bring your own visitor to clean each node
* @param {Function} licenseRefLookup Optional. Bring your own lookup to scan licenseRef
* @returns {object} the AST representing the parsed expression
*/
function parse(expression, licenseVisitor) {
function parse(expression, licenseVisitor, licenseRefLookup) {
// if the expression is already an expression, just return it.
if (!(typeof expression === 'string')) return expression
licenseVisitor = licenseVisitor || normalizeSingle
try {
return spdxExpressionParse(expression, { relaxed: true, licenseVisitor })
return spdxExpressionParse(expression, { relaxed: true, licenseVisitor, licenseRefLookup })
} catch (e) {
return { noassertion: true }
}
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@clearlydefined/spdx",
"version": "0.1.8",
"version": "0.1.9",
"description": "SPDX custom libraries of clearlydefined.io.",
"license": "MIT",
"repository": {
Expand All @@ -20,7 +20,7 @@
},
"dependencies": {
"spdx-expression-parse": "github:clearlydefined/spdx-expression-parse.js#fork",
"spdx-license-ids": "^3.0.18",
"spdx-license-ids": "^3.0.20",
"spdx-license-list": "^6.9.0",
"spdx-satisfies": "github:clearlydefined/spdx-satisfies.js#parse-override"
},
Expand Down
123 changes: 123 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,129 @@ describe('SPDX utility functions', () => {
})
})

it('parses licenseRef with lookup', () => {
const data = new Map([
['afpl-9.0', { license: 'LicenseRef-scancode-afpl-9.0' }],
['(afpl-9.0)', { license: 'LicenseRef-scancode-afpl-9.0' }],
[
'(afpl-9.0 OR Apache-2.0)',
{
left: { license: 'LicenseRef-scancode-afpl-9.0' },
conjunction: 'or',
right: { license: 'Apache-2.0' }
}
],
[
'Apache-2.0 AND (afpl-9.0)',
{
left: { license: 'Apache-2.0' },
conjunction: 'and',
right: { license: 'LicenseRef-scancode-afpl-9.0' }
}
],
[
'Apache-2.0 AND (afpl-9.0 AND afpl-9.0)',
{
left: { license: 'Apache-2.0' },
conjunction: 'and',
right: {
left: { license: 'LicenseRef-scancode-afpl-9.0' },
conjunction: 'and',
right: { license: 'LicenseRef-scancode-afpl-9.0' }
}
}
],
[
'(afpl-9.0 AND afpl-9.0) AND Apache-2.0',
{
right: { license: 'Apache-2.0' },
conjunction: 'and',
left: {
left: { license: 'LicenseRef-scancode-afpl-9.0' },
conjunction: 'and',
right: { license: 'LicenseRef-scancode-afpl-9.0' }
}
}
],
[
'Apache-2.0 AND (afpl-9.0 OR afpl-9.0)',
{
left: { license: 'Apache-2.0' },
conjunction: 'and',
right: {
left: { license: 'LicenseRef-scancode-afpl-9.0' },
conjunction: 'or',
right: { license: 'LicenseRef-scancode-afpl-9.0' }
}
}
],
[
'MIT AND GPL-3.0',
{
left: { license: 'MIT' },
conjunction: 'and',
right: { license: 'GPL-3.0' }
}
],
[
'AFL-1.1 AND afpl-9.0',
{
left: { license: 'AFL-1.1' },
conjunction: 'and',
right: { license: 'LicenseRef-scancode-afpl-9.0' }
}
],
[
'afpl-9.0 AND MIT',
{
left: { license: 'LicenseRef-scancode-afpl-9.0' },
conjunction: 'and',
right: { license: 'MIT' }
}
],
[
'afpl-9.0 AND activestate-community',
{
left: { license: 'LicenseRef-scancode-afpl-9.0' },
conjunction: 'and',
right: { license: 'LicenseRef-scancode-activestate-community' }
}
],
[
'afpl-9.0 AND activestate-community OR ac3filter',
{
left: {
left: { license: 'LicenseRef-scancode-afpl-9.0' },
conjunction: 'and',
right: { license: 'LicenseRef-scancode-activestate-community' }
},
conjunction: 'or',
right: { license: 'LicenseRef-scancode-ac3filter' }
}
],
['INVALID', { noassertion: null }],
[
'LicenseRef-scancode-afpl-9.0 AND MIT',
{
left: { license: 'LicenseRef-scancode-afpl-9.0' },
conjunction: 'and',
right: { license: 'MIT' }
}
]
])

const licenseRefLookup = function (identifier) {
if (identifier === 'afpl-9.0') return 'LicenseRef-scancode-afpl-9.0'
if (identifier === 'activestate-community') return 'LicenseRef-scancode-activestate-community'
if (identifier === 'ac3filter') return 'LicenseRef-scancode-ac3filter'
return identifier
}

data.forEach((expected, input) => {
expect(SPDX.parse(input, undefined, licenseRefLookup)).to.deep.equal(expected)
})
})

it('stringifies spdx objects', () => {
const data = new Map([
[{ license: 'MIT' }, 'MIT'],
Expand Down

0 comments on commit 9391609

Please sign in to comment.