index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. /*
  2. Copyright spdx-correct.js contributors
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. var parse = require('spdx-expression-parse')
  14. var spdxLicenseIds = require('spdx-license-ids')
  15. function valid (string) {
  16. try {
  17. parse(string)
  18. return true
  19. } catch (error) {
  20. return false
  21. }
  22. }
  23. // Sorting function that orders the given array of transpositions such
  24. // that a transposition with the longer pattern comes before a transposition
  25. // with a shorter pattern. This is to prevent e.g. the transposition
  26. // ["General Public License", "GPL"] from matching to "Lesser General Public License"
  27. // before a longer and more accurate transposition ["Lesser General Public License", "LGPL"]
  28. // has a chance to be recognized.
  29. function sortTranspositions(a, b) {
  30. var length = b[0].length - a[0].length
  31. if (length !== 0) return length
  32. return a[0].toUpperCase().localeCompare(b[0].toUpperCase())
  33. }
  34. // Common transpositions of license identifier acronyms
  35. var transpositions = [
  36. ['APGL', 'AGPL'],
  37. ['Gpl', 'GPL'],
  38. ['GLP', 'GPL'],
  39. ['APL', 'Apache'],
  40. ['ISD', 'ISC'],
  41. ['GLP', 'GPL'],
  42. ['IST', 'ISC'],
  43. ['Claude', 'Clause'],
  44. [' or later', '+'],
  45. [' International', ''],
  46. ['GNU', 'GPL'],
  47. ['GUN', 'GPL'],
  48. ['+', ''],
  49. ['GNU GPL', 'GPL'],
  50. ['GNU LGPL', 'LGPL'],
  51. ['GNU/GPL', 'GPL'],
  52. ['GNU GLP', 'GPL'],
  53. ['GNU LESSER GENERAL PUBLIC LICENSE', 'LGPL'],
  54. ['GNU Lesser General Public License', 'LGPL'],
  55. ['GNU LESSER GENERAL PUBLIC LICENSE', 'LGPL-2.1'],
  56. ['GNU Lesser General Public License', 'LGPL-2.1'],
  57. ['LESSER GENERAL PUBLIC LICENSE', 'LGPL'],
  58. ['Lesser General Public License', 'LGPL'],
  59. ['LESSER GENERAL PUBLIC LICENSE', 'LGPL-2.1'],
  60. ['Lesser General Public License', 'LGPL-2.1'],
  61. ['GNU General Public License', 'GPL'],
  62. ['Gnu public license', 'GPL'],
  63. ['GNU Public License', 'GPL'],
  64. ['GNU GENERAL PUBLIC LICENSE', 'GPL'],
  65. ['MTI', 'MIT'],
  66. ['Mozilla Public License', 'MPL'],
  67. ['Universal Permissive License', 'UPL'],
  68. ['WTH', 'WTF'],
  69. ['WTFGPL', 'WTFPL'],
  70. ['-License', '']
  71. ].sort(sortTranspositions)
  72. var TRANSPOSED = 0
  73. var CORRECT = 1
  74. // Simple corrections to nearly valid identifiers.
  75. var transforms = [
  76. // e.g. 'mit'
  77. function (argument) {
  78. return argument.toUpperCase()
  79. },
  80. // e.g. 'MIT '
  81. function (argument) {
  82. return argument.trim()
  83. },
  84. // e.g. 'M.I.T.'
  85. function (argument) {
  86. return argument.replace(/\./g, '')
  87. },
  88. // e.g. 'Apache- 2.0'
  89. function (argument) {
  90. return argument.replace(/\s+/g, '')
  91. },
  92. // e.g. 'CC BY 4.0''
  93. function (argument) {
  94. return argument.replace(/\s+/g, '-')
  95. },
  96. // e.g. 'LGPLv2.1'
  97. function (argument) {
  98. return argument.replace('v', '-')
  99. },
  100. // e.g. 'Apache 2.0'
  101. function (argument) {
  102. return argument.replace(/,?\s*(\d)/, '-$1')
  103. },
  104. // e.g. 'GPL 2'
  105. function (argument) {
  106. return argument.replace(/,?\s*(\d)/, '-$1.0')
  107. },
  108. // e.g. 'Apache Version 2.0'
  109. function (argument) {
  110. return argument
  111. .replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2')
  112. },
  113. // e.g. 'Apache Version 2'
  114. function (argument) {
  115. return argument
  116. .replace(/,?\s*(V\.|v\.|V|v|Version|version)\s*(\d)/, '-$2.0')
  117. },
  118. // e.g. 'ZLIB'
  119. function (argument) {
  120. return argument[0].toUpperCase() + argument.slice(1)
  121. },
  122. // e.g. 'MPL/2.0'
  123. function (argument) {
  124. return argument.replace('/', '-')
  125. },
  126. // e.g. 'Apache 2'
  127. function (argument) {
  128. return argument
  129. .replace(/\s*V\s*(\d)/, '-$1')
  130. .replace(/(\d)$/, '$1.0')
  131. },
  132. // e.g. 'GPL-2.0', 'GPL-3.0'
  133. function (argument) {
  134. if (argument.indexOf('3.0') !== -1) {
  135. return argument + '-or-later'
  136. } else {
  137. return argument + '-only'
  138. }
  139. },
  140. // e.g. 'GPL-2.0-'
  141. function (argument) {
  142. return argument + 'only'
  143. },
  144. // e.g. 'GPL2'
  145. function (argument) {
  146. return argument.replace(/(\d)$/, '-$1.0')
  147. },
  148. // e.g. 'BSD 3'
  149. function (argument) {
  150. return argument.replace(/(-| )?(\d)$/, '-$2-Clause')
  151. },
  152. // e.g. 'BSD clause 3'
  153. function (argument) {
  154. return argument.replace(/(-| )clause(-| )(\d)/, '-$3-Clause')
  155. },
  156. // e.g. 'New BSD license'
  157. function (argument) {
  158. return argument.replace(/\b(Modified|New|Revised)(-| )?BSD((-| )License)?/i, 'BSD-3-Clause')
  159. },
  160. // e.g. 'Simplified BSD license'
  161. function (argument) {
  162. return argument.replace(/\bSimplified(-| )?BSD((-| )License)?/i, 'BSD-2-Clause')
  163. },
  164. // e.g. 'Free BSD license'
  165. function (argument) {
  166. return argument.replace(/\b(Free|Net)(-| )?BSD((-| )License)?/i, 'BSD-2-Clause-$1BSD')
  167. },
  168. // e.g. 'Clear BSD license'
  169. function (argument) {
  170. return argument.replace(/\bClear(-| )?BSD((-| )License)?/i, 'BSD-3-Clause-Clear')
  171. },
  172. // e.g. 'Old BSD License'
  173. function (argument) {
  174. return argument.replace(/\b(Old|Original)(-| )?BSD((-| )License)?/i, 'BSD-4-Clause')
  175. },
  176. // e.g. 'BY-NC-4.0'
  177. function (argument) {
  178. return 'CC-' + argument
  179. },
  180. // e.g. 'BY-NC'
  181. function (argument) {
  182. return 'CC-' + argument + '-4.0'
  183. },
  184. // e.g. 'Attribution-NonCommercial'
  185. function (argument) {
  186. return argument
  187. .replace('Attribution', 'BY')
  188. .replace('NonCommercial', 'NC')
  189. .replace('NoDerivatives', 'ND')
  190. .replace(/ (\d)/, '-$1')
  191. .replace(/ ?International/, '')
  192. },
  193. // e.g. 'Attribution-NonCommercial'
  194. function (argument) {
  195. return 'CC-' +
  196. argument
  197. .replace('Attribution', 'BY')
  198. .replace('NonCommercial', 'NC')
  199. .replace('NoDerivatives', 'ND')
  200. .replace(/ (\d)/, '-$1')
  201. .replace(/ ?International/, '') +
  202. '-4.0'
  203. }
  204. ]
  205. var licensesWithVersions = spdxLicenseIds
  206. .map(function (id) {
  207. var match = /^(.*)-\d+\.\d+$/.exec(id)
  208. return match
  209. ? [match[0], match[1]]
  210. : [id, null]
  211. })
  212. .reduce(function (objectMap, item) {
  213. var key = item[1]
  214. objectMap[key] = objectMap[key] || []
  215. objectMap[key].push(item[0])
  216. return objectMap
  217. }, {})
  218. var licensesWithOneVersion = Object.keys(licensesWithVersions)
  219. .map(function makeEntries (key) {
  220. return [key, licensesWithVersions[key]]
  221. })
  222. .filter(function identifySoleVersions (item) {
  223. return (
  224. // Licenses has just one valid version suffix.
  225. item[1].length === 1 &&
  226. item[0] !== null &&
  227. // APL will be considered Apache, rather than APL-1.0
  228. item[0] !== 'APL'
  229. )
  230. })
  231. .map(function createLastResorts (item) {
  232. return [item[0], item[1][0]]
  233. })
  234. licensesWithVersions = undefined
  235. // If all else fails, guess that strings containing certain substrings
  236. // meant to identify certain licenses.
  237. var lastResorts = [
  238. ['UNLI', 'Unlicense'],
  239. ['WTF', 'WTFPL'],
  240. ['2 CLAUSE', 'BSD-2-Clause'],
  241. ['2-CLAUSE', 'BSD-2-Clause'],
  242. ['3 CLAUSE', 'BSD-3-Clause'],
  243. ['3-CLAUSE', 'BSD-3-Clause'],
  244. ['AFFERO', 'AGPL-3.0-or-later'],
  245. ['AGPL', 'AGPL-3.0-or-later'],
  246. ['APACHE', 'Apache-2.0'],
  247. ['ARTISTIC', 'Artistic-2.0'],
  248. ['Affero', 'AGPL-3.0-or-later'],
  249. ['BEER', 'Beerware'],
  250. ['BOOST', 'BSL-1.0'],
  251. ['BSD', 'BSD-2-Clause'],
  252. ['CDDL', 'CDDL-1.1'],
  253. ['ECLIPSE', 'EPL-1.0'],
  254. ['FUCK', 'WTFPL'],
  255. ['GNU', 'GPL-3.0-or-later'],
  256. ['LGPL', 'LGPL-3.0-or-later'],
  257. ['GPLV1', 'GPL-1.0-only'],
  258. ['GPL-1', 'GPL-1.0-only'],
  259. ['GPLV2', 'GPL-2.0-only'],
  260. ['GPL-2', 'GPL-2.0-only'],
  261. ['GPL', 'GPL-3.0-or-later'],
  262. ['MIT +NO-FALSE-ATTRIBS', 'MITNFA'],
  263. ['MIT', 'MIT'],
  264. ['MPL', 'MPL-2.0'],
  265. ['X11', 'X11'],
  266. ['ZLIB', 'Zlib']
  267. ].concat(licensesWithOneVersion).sort(sortTranspositions)
  268. var SUBSTRING = 0
  269. var IDENTIFIER = 1
  270. var validTransformation = function (identifier) {
  271. for (var i = 0; i < transforms.length; i++) {
  272. var transformed = transforms[i](identifier).trim()
  273. if (transformed !== identifier && valid(transformed)) {
  274. return transformed
  275. }
  276. }
  277. return null
  278. }
  279. var validLastResort = function (identifier) {
  280. var upperCased = identifier.toUpperCase()
  281. for (var i = 0; i < lastResorts.length; i++) {
  282. var lastResort = lastResorts[i]
  283. if (upperCased.indexOf(lastResort[SUBSTRING]) > -1) {
  284. return lastResort[IDENTIFIER]
  285. }
  286. }
  287. return null
  288. }
  289. var anyCorrection = function (identifier, check) {
  290. for (var i = 0; i < transpositions.length; i++) {
  291. var transposition = transpositions[i]
  292. var transposed = transposition[TRANSPOSED]
  293. if (identifier.indexOf(transposed) > -1) {
  294. var corrected = identifier.replace(
  295. transposed,
  296. transposition[CORRECT]
  297. )
  298. var checked = check(corrected)
  299. if (checked !== null) {
  300. return checked
  301. }
  302. }
  303. }
  304. return null
  305. }
  306. module.exports = function (identifier, options) {
  307. options = options || {}
  308. var upgrade = options.upgrade === undefined ? true : !!options.upgrade
  309. function postprocess (value) {
  310. return upgrade ? upgradeGPLs(value) : value
  311. }
  312. var validArugment = (
  313. typeof identifier === 'string' &&
  314. identifier.trim().length !== 0
  315. )
  316. if (!validArugment) {
  317. throw Error('Invalid argument. Expected non-empty string.')
  318. }
  319. identifier = identifier.trim()
  320. if (valid(identifier)) {
  321. return postprocess(identifier)
  322. }
  323. var noPlus = identifier.replace(/\+$/, '').trim()
  324. if (valid(noPlus)) {
  325. return postprocess(noPlus)
  326. }
  327. var transformed = validTransformation(identifier)
  328. if (transformed !== null) {
  329. return postprocess(transformed)
  330. }
  331. transformed = anyCorrection(identifier, function (argument) {
  332. if (valid(argument)) {
  333. return argument
  334. }
  335. return validTransformation(argument)
  336. })
  337. if (transformed !== null) {
  338. return postprocess(transformed)
  339. }
  340. transformed = validLastResort(identifier)
  341. if (transformed !== null) {
  342. return postprocess(transformed)
  343. }
  344. transformed = anyCorrection(identifier, validLastResort)
  345. if (transformed !== null) {
  346. return postprocess(transformed)
  347. }
  348. return null
  349. }
  350. function upgradeGPLs (value) {
  351. if ([
  352. 'GPL-1.0', 'LGPL-1.0', 'AGPL-1.0',
  353. 'GPL-2.0', 'LGPL-2.0', 'AGPL-2.0',
  354. 'LGPL-2.1'
  355. ].indexOf(value) !== -1) {
  356. return value + '-only'
  357. } else if ([
  358. 'GPL-1.0+', 'GPL-2.0+', 'GPL-3.0+',
  359. 'LGPL-2.0+', 'LGPL-2.1+', 'LGPL-3.0+',
  360. 'AGPL-1.0+', 'AGPL-3.0+'
  361. ].indexOf(value) !== -1) {
  362. return value.replace(/\+$/, '-or-later')
  363. } else if (['GPL-3.0', 'LGPL-3.0', 'AGPL-3.0'].indexOf(value) !== -1) {
  364. return value + '-or-later'
  365. } else {
  366. return value
  367. }
  368. }