Skip to content

Commit 69d2faa

Browse files
authored
Merge pull request #926 from dangoor/dangoor/replace-other
Replace OTHER with a LicenseRef
2 parents 8477905 + 7e14978 commit 69d2faa

File tree

4 files changed

+106
-3
lines changed

4 files changed

+106
-3
lines changed

__tests__/spdx.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ describe('satisfiesAny', () => {
5555
candidate: 'MIT OR ISC',
5656
licenses: ['MiT'],
5757
expected: false
58+
},
59+
{
60+
candidate: 'MIT AND OTHER',
61+
licenses: ['MIT'],
62+
expected: false
63+
},
64+
{
65+
candidate: 'MIT OR OTHER',
66+
licenses: ['MIT', 'LicenseRef-clearlydefined-OTHER'],
67+
expected: true
5868
}
5969
]
6070

@@ -130,6 +140,16 @@ describe('satisfiesAll', () => {
130140
candidate: 'MIT OR ISC',
131141
licenses: ['MiT'],
132142
expected: false
143+
},
144+
{
145+
candidate: 'MIT AND OTHER',
146+
licenses: ['MIT'],
147+
expected: false
148+
},
149+
{
150+
candidate: 'MIT AND OTHER',
151+
licenses: ['MIT', 'LicenseRef-clearlydefined-OTHER'],
152+
expected: true
133153
}
134154
]
135155

@@ -210,6 +230,16 @@ describe('satisfies', () => {
210230
candidate: '',
211231
allowList: ['BSD-3-Clause', 'ISC', 'MIT'],
212232
expected: false
233+
},
234+
{
235+
candidate: 'MIT OR OTHER',
236+
allowList: ['MIT', 'LicenseRef-clearlydefined-OTHER'],
237+
expected: true
238+
},
239+
{
240+
candidate: '(Apache-2.0 AND OTHER) OR (MIT AND OTHER)',
241+
allowList: ['Apache-2.0', 'LicenseRef-clearlydefined-OTHER'],
242+
expected: true
213243
}
214244
]
215245

@@ -246,6 +276,10 @@ describe('isValid', () => {
246276
{
247277
candidate: '',
248278
expected: false
279+
},
280+
{
281+
candidate: 'MIT AND OTHER',
282+
expected: true
249283
}
250284
]
251285
for (const unit of units) {
@@ -255,3 +289,38 @@ describe('isValid', () => {
255289
})
256290
}
257291
})
292+
293+
describe('cleanInvalidSPDX', () => {
294+
const units = [
295+
{
296+
candidate: 'MIT',
297+
expected: 'MIT'
298+
},
299+
{
300+
candidate: 'OTHER',
301+
expected: 'LicenseRef-clearlydefined-OTHER'
302+
},
303+
{
304+
candidate: 'LicenseRef-clearlydefined-OTHER',
305+
expected: 'LicenseRef-clearlydefined-OTHER'
306+
},
307+
{
308+
candidate: 'OTHER AND MIT',
309+
expected: 'LicenseRef-clearlydefined-OTHER AND MIT'
310+
},
311+
{
312+
candidate: 'MIT AND OTHER',
313+
expected: 'MIT AND LicenseRef-clearlydefined-OTHER'
314+
},
315+
{
316+
candidate: 'MIT AND SomethingElse-OTHER',
317+
expected: 'MIT AND SomethingElse-OTHER'
318+
}
319+
]
320+
for (const unit of units) {
321+
const got: string = spdx.cleanInvalidSPDX(unit.candidate)
322+
test(`should return ${unit.expected} for ("${unit.candidate}")`, () => {
323+
expect(got).toBe(unit.expected)
324+
})
325+
}
326+
})

dist/index.js

Lines changed: 24 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/spdx.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import parse from 'spdx-expression-parse'
1212
// accepts a pair of well-formed SPDX expressions. the
1313
// candidate is tested against the constraint
1414
export function satisfies(candidateExpr: string, allowList: string[]): boolean {
15+
candidateExpr = cleanInvalidSPDX(candidateExpr)
1516
try {
1617
return spdxSatisfies(candidateExpr, allowList)
1718
} catch (_) {
@@ -24,6 +25,7 @@ export function satisfiesAny(
2425
candidateExpr: string,
2526
licenses: string[]
2627
): boolean {
28+
candidateExpr = cleanInvalidSPDX(candidateExpr)
2729
try {
2830
return spdxlib.satisfiesAny(candidateExpr, licenses)
2931
} catch (_) {
@@ -36,6 +38,7 @@ export function satisfiesAll(
3638
candidateExpr: string,
3739
licenses: string[]
3840
): boolean {
41+
candidateExpr = cleanInvalidSPDX(candidateExpr)
3942
try {
4043
return spdxlib.satisfiesAll(candidateExpr, licenses)
4144
} catch (_) {
@@ -45,10 +48,19 @@ export function satisfiesAll(
4548

4649
// accepts any SPDX expression
4750
export function isValid(spdxExpr: string): boolean {
51+
spdxExpr = cleanInvalidSPDX(spdxExpr)
4852
try {
4953
parse(spdxExpr)
5054
return true
5155
} catch (_) {
5256
return false
5357
}
5458
}
59+
60+
const replaceOtherRegex = /(?<![\w-])OTHER(?![\w-])/g
61+
62+
// adjusts license expressions to not include the invalid `OTHER`
63+
// which ClearlyDefined adds to license strings
64+
export function cleanInvalidSPDX(spdxExpr: string): string {
65+
return spdxExpr.replace(replaceOtherRegex, 'LicenseRef-clearlydefined-OTHER')
66+
}

0 commit comments

Comments
 (0)