1
1
import Ajv from 'ajv/dist/jtd.js'
2
- import { parse } from 'license-expressions'
2
+ import { parse , validate } from 'license-expressions'
3
3
import license_information from '../../lib/license/license_information.js'
4
4
import translations from '../../lib/language_specific_translation/translations.js'
5
5
import bcp47 from 'bcp47'
@@ -80,61 +80,67 @@ function isAboutCodeLicense(licenseRefToCheck) {
80
80
* Recursively checks if a parsed license expression contains not listed licenses.
81
81
*
82
82
* @param {import('license-expressions').ParsedSpdxExpression } parsedExpression - The parsed license expression
83
- * @returns {boolean } True if the expression contains any license references, false otherwise
83
+ * @returns {Array<string> } all not listed licenses
84
84
*/
85
- function containsNotListedLicenses ( parsedExpression ) {
85
+ function notListedLicenses ( parsedExpression ) {
86
+ /** @type {Array<string> } */
87
+ const deprecatedLicenses = [ ]
86
88
// If it's a LicenseRef type directly
87
89
if ( 'licenseRef' in parsedExpression ) {
88
- return ! isAboutCodeLicense ( parsedExpression . licenseRef )
90
+ if ( ! isAboutCodeLicense ( parsedExpression . licenseRef ) ) {
91
+ deprecatedLicenses . push ( parsedExpression . licenseRef )
92
+ }
89
93
}
90
94
91
95
if (
92
96
'license' in parsedExpression &&
93
97
! SPDX_LICENSE_KEYS . has ( parsedExpression . license )
94
98
) {
95
- return true
99
+ deprecatedLicenses . push ( parsedExpression . license )
96
100
}
97
101
98
102
if (
99
103
'exception' in parsedExpression &&
100
104
parsedExpression . exception &&
101
105
! SPDX_LICENSE_KEYS . has ( parsedExpression . exception )
102
106
) {
103
- return true
107
+ deprecatedLicenses . push ( parsedExpression . exception )
104
108
}
105
109
106
110
// If it's a conjunction, check both sides
107
111
if ( 'conjunction' in parsedExpression ) {
108
- return (
109
- containsNotListedLicenses ( parsedExpression . left ) ||
110
- containsNotListedLicenses ( parsedExpression . right )
111
- )
112
+ deprecatedLicenses . push ( ...notListedLicenses ( parsedExpression . left ) )
113
+ deprecatedLicenses . push ( ...notListedLicenses ( parsedExpression . right ) )
112
114
}
113
115
114
116
// If it's a LicenseInfo type, it doesn't contain not listed licenses
115
- return false
117
+ return deprecatedLicenses
116
118
}
117
119
118
120
/**
119
121
* Checks if a license expression string contains any not listed licenses.
120
122
*
121
123
* @param {string } licenseToCheck - The license expression to check
122
- * @returns {boolean } True if the license expression contains any document references, false otherwise
124
+ * @returns {Array<string> } all not listed licenses
123
125
*/
124
- function hasNotListedLicenses ( licenseToCheck ) {
126
+ function allNotListedLicenses ( licenseToCheck ) {
125
127
const parseResult = parse ( licenseToCheck )
126
- return containsNotListedLicenses ( parseResult )
128
+ return notListedLicenses ( parseResult )
127
129
}
128
130
129
131
/**
130
132
* check if the license_expression contains license identifiers or exceptions
131
133
* that are not listed in the SPDX license list or Aboutcode's "ScanCode LicenseDB"
132
134
*
133
135
* @param {string } licenseToCheck - The license expression to check
134
- * @returns {boolean } True if the license has not listed licenses, false otherwise
136
+ * @returns {Array<string> } all not listed licenses
135
137
*/
136
- export function existsNotListedLicenses ( licenseToCheck ) {
137
- return ! ! licenseToCheck && hasNotListedLicenses ( licenseToCheck )
138
+ export function getNotListedLicenses ( licenseToCheck ) {
139
+ if ( ! licenseToCheck || ! validate ( licenseToCheck ) . valid ) {
140
+ return [ ]
141
+ } else {
142
+ return allNotListedLicenses ( licenseToCheck )
143
+ }
138
144
}
139
145
140
146
/**
@@ -216,7 +222,8 @@ export function recommendedTest_6_2_46(doc) {
216
222
217
223
const licenseToCheck = doc . document . license_expression
218
224
if ( isLangSpecifiedAndNotEnglish ( doc . document . lang ) ) {
219
- if ( existsNotListedLicenses ( licenseToCheck ) ) {
225
+ const notListedLicenses = getNotListedLicenses ( licenseToCheck )
226
+ if ( notListedLicenses . length > 0 ) {
220
227
const notes = doc . document . notes
221
228
if (
222
229
! notes ||
@@ -225,9 +232,11 @@ export function recommendedTest_6_2_46(doc) {
225
232
ctx . warnings . push ( {
226
233
instancePath : '/document/notes' ,
227
234
message :
228
- 'The license_expression contains a license identifiers or exceptions that is not ' +
229
- 'listed in Aboutcode or SPDX license list. Therefore exactly one note with ' +
230
- 'title "License" and category "legal_disclaimer" must exist' ,
235
+ `The license_expression contains contains the following license identifiers that ` +
236
+ `are nor listed in Aboutcode or SPDX license list: ` +
237
+ `"${ notListedLicenses . join ( ', ' ) } ". ` +
238
+ `Therefore exactly one note with ` +
239
+ `title "License" and category "legal_disclaimer" must exist` ,
231
240
} )
232
241
}
233
242
}
0 commit comments