From f4a92498190ca530dbc17d58c2514ac102ab719a Mon Sep 17 00:00:00 2001 From: Brett Zamir Date: Tue, 26 Apr 2022 07:22:54 +0800 Subject: [PATCH] fix(`check-values`): handle mismatched `licensePattern`; fixes #884 Also: - test: better convey usage of `licensePattern` --- README.md | 45 ++++++++++++++ src/rules/checkValues.js | 52 +++++++++------- test/rules/assertions/checkValues.js | 90 ++++++++++++++++++++++++++++ 3 files changed, 167 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index dd1daa485..dc30918d8 100644 --- a/README.md +++ b/README.md @@ -6122,6 +6122,25 @@ function quux (foo) { } // "jsdoc/check-values": ["error"|"warn", {"numericOnlyVariation":true}] // Message: Invalid JSDoc @variation: "5.2". + +/** + * @license license-prefix Oops + */ +function quux (foo) { + +} +// "jsdoc/check-values": ["error"|"warn", {"licensePattern":"(?<=license-prefix ).*"}] +// Message: Invalid JSDoc @license: "Oops"; expected SPDX expression: https://spdx.org/licenses/. + +/** + * @license Oops + * Copyright 2022 + */ +function quux (foo) { + +} +// "jsdoc/check-values": ["error"|"warn", {"licensePattern":"^([^\n]+)\nCopyright"}] +// Message: Invalid JSDoc @license: "Oops"; expected SPDX expression: https://spdx.org/licenses/. ```` The following patterns are not considered problems: @@ -6244,6 +6263,32 @@ function quux (foo) { function quux (foo) { } + +/** + * @license license-prefix MIT + */ +function quux (foo) { + +} +// "jsdoc/check-values": ["error"|"warn", {"licensePattern":"(?<=license-prefix )MIT|GPL3.0"}] + +/** + * @license + * Copyright 2022 + */ +function quux (foo) { + +} +// "jsdoc/check-values": ["error"|"warn", {"licensePattern":"^([^\n]+)(?!\nCopyright)"}] + +/** + * @license MIT + * Copyright 2022 + */ +function quux (foo) { + +} +// "jsdoc/check-values": ["error"|"warn", {"licensePattern":"^([^\n]+)\nCopyright"}] ```` diff --git a/src/rules/checkValues.js b/src/rules/checkValues.js index 254faf015..6daef5e9e 100644 --- a/src/rules/checkValues.js +++ b/src/rules/checkValues.js @@ -106,31 +106,43 @@ export default iterateJsdoc(({ }); utils.forEachPreferredTag('license', (jsdocParameter, targetTagName) => { const licenseRegex = utils.getRegexFromString(licensePattern, 'g'); - const match = utils.getTagDescription(jsdocParameter).match(licenseRegex); - const license = match && match[1] || match[0]; - if (!license.trim()) { - report( - `Missing JSDoc @${targetTagName} value.`, - null, - jsdocParameter, - ); - } else if (allowedLicenses) { - if (allowedLicenses !== true && !allowedLicenses.includes(license)) { - report( - `Invalid JSDoc @${targetTagName}: "${license}"; expected one of ${allowedLicenses.join(', ')}.`, - null, - jsdocParameter, - ); + const matches = utils.getTagDescription(jsdocParameter).matchAll(licenseRegex); + let positiveMatch = false; + for (const match of matches) { + const license = match[1] || match[0]; + if (license) { + positiveMatch = true; } - } else { - try { - spdxExpressionParse(license); - } catch { + + if (!license.trim()) { + // Avoid reporting again as empty match + if (positiveMatch) { + return; + } + report( - `Invalid JSDoc @${targetTagName}: "${license}"; expected SPDX expression: https://spdx.org/licenses/.`, + `Missing JSDoc @${targetTagName} value.`, null, jsdocParameter, ); + } else if (allowedLicenses) { + if (allowedLicenses !== true && !allowedLicenses.includes(license)) { + report( + `Invalid JSDoc @${targetTagName}: "${license}"; expected one of ${allowedLicenses.join(', ')}.`, + null, + jsdocParameter, + ); + } + } else { + try { + spdxExpressionParse(license); + } catch { + report( + `Invalid JSDoc @${targetTagName}: "${license}"; expected SPDX expression: https://spdx.org/licenses/.`, + null, + jsdocParameter, + ); + } } } }); diff --git a/test/rules/assertions/checkValues.js b/test/rules/assertions/checkValues.js index 289d6c760..5138ba9ea 100644 --- a/test/rules/assertions/checkValues.js +++ b/test/rules/assertions/checkValues.js @@ -310,6 +310,49 @@ export default { }, ], }, + { + code: ` + /** + * @license license-prefix Oops + */ + function quux (foo) { + + } + `, + errors: [ + { + line: 3, + message: 'Invalid JSDoc @license: "Oops"; expected SPDX expression: https://spdx.org/licenses/.', + }, + ], + options: [ + { + licensePattern: '(?<=license-prefix ).*', + }, + ], + }, + { + code: ` + /** + * @license Oops + * Copyright 2022 + */ + function quux (foo) { + + } + `, + errors: [ + { + line: 3, + message: 'Invalid JSDoc @license: "Oops"; expected SPDX expression: https://spdx.org/licenses/.', + }, + ], + options: [ + { + licensePattern: '^([^\n]+)\nCopyright', + }, + ], + }, ], valid: [ { @@ -502,5 +545,52 @@ export default { } `, }, + { + code: ` + /** + * @license license-prefix MIT + */ + function quux (foo) { + + } + `, + options: [ + { + licensePattern: '(?<=license-prefix )MIT|GPL3.0', + }, + ], + }, + { + code: ` + /** + * @license + * Copyright 2022 + */ + function quux (foo) { + + } + `, + options: [ + { + licensePattern: '^([^\n]+)(?!\nCopyright)', + }, + ], + }, + { + code: ` + /** + * @license MIT + * Copyright 2022 + */ + function quux (foo) { + + } + `, + options: [ + { + licensePattern: '^([^\n]+)\nCopyright', + }, + ], + }, ], };