Skip to content

Commit

Permalink
feat(check-values): add licensePattern option to allow delimiting…
Browse files Browse the repository at this point in the history
… portion of license description to extract
  • Loading branch information
brettz9 committed Nov 17, 2019
1 parent 8ca5030 commit eed7dde
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
10 changes: 9 additions & 1 deletion .README/rules/check-values.md
Expand Up @@ -23,11 +23,19 @@ be checked for.
An array of allowable license values or `true` to allow any license text.
If present as an array, will be used in place of SPDX identifiers.

##### `licensePattern`

A string to be converted into a `RegExp` (with `u` flag) and whose first
parenthetical grouping, if present, will match the portion of the license
description to check (if no grouping is present, then the whole portion
matched will be used). Defaults to `([^\n]*)`, i.e., the SPDX expression
is expected before any line breaks.

|||
|---|---|
|Context|everywhere|
|Tags|`@version`, `@since`, `@license`, `@author`|
|Options|`allowedAuthors`, `allowedLicenses`|
|Options|`allowedAuthors`, `allowedLicenses`, `licensePattern`|
|Settings|`tagNamePreference`|

<!-- assertions checkValues -->
28 changes: 27 additions & 1 deletion README.md
Expand Up @@ -3156,11 +3156,20 @@ be checked for.
An array of allowable license values or `true` to allow any license text.
If present as an array, will be used in place of SPDX identifiers.

<a name="eslint-plugin-jsdoc-rules-check-values-options-5-licensepattern"></a>
##### <code>licensePattern</code>

A string to be converted into a `RegExp` (with `u` flag) and whose first
parenthetical grouping, if present, will match the portion of the license
description to check (if no grouping is present, then the whole portion
matched will be used). Defaults to `([^\n]*)`, i.e., the SPDX expression
is expected before any line breaks.

|||
|---|---|
|Context|everywhere|
|Tags|`@version`, `@since`, `@license`, `@author`|
|Options|`allowedAuthors`, `allowedLicenses`|
|Options|`allowedAuthors`, `allowedLicenses`, `licensePattern`|
|Settings|`tagNamePreference`|

The following patterns are considered problems:
Expand Down Expand Up @@ -3223,6 +3232,15 @@ function quux (foo) {
// Options: [{"allowedLicenses":["BAR","BAX"]}]
// Message: Invalid JSDoc @license: "FOO"; expected one of BAR, BAX.

/**
* @license MIT-7
* Some extra text...
*/
function quux (foo) {

}
// Message: Invalid JSDoc @license: "MIT-7"; expected SPDX expression: https://spdx.org/licenses/.

/**
* @license (MIT OR GPL-2.5)
*/
Expand Down Expand Up @@ -3273,6 +3291,14 @@ function quux (foo) {

}

/**
* @license MIT
* Some extra text...
*/
function quux (foo) {

}

/**
* @license (MIT OR GPL-2.0)
*/
Expand Down
5 changes: 4 additions & 1 deletion src/rules/checkValues.js
Expand Up @@ -11,6 +11,7 @@ export default iterateJsdoc(({
const {
allowedLicenses = null,
allowedAuthors = null,
licensePattern = '([^\n]*)',
} = options;

utils.forEachPreferredTag('version', (jsdocParameter, targetTagName) => {
Expand Down Expand Up @@ -46,7 +47,9 @@ export default iterateJsdoc(({
}
});
utils.forEachPreferredTag('license', (jsdocParameter, targetTagName) => {
const license = jsdocParameter.description;
const licenseRegex = new RegExp(licensePattern, 'g');
const match = jsdocParameter.description.match(licenseRegex);
const license = match && match[1] || match[0];
if (!license.trim()) {
report(
`Missing JSDoc @${targetTagName}.`,
Expand Down
27 changes: 27 additions & 0 deletions test/rules/assertions/checkValues.js
Expand Up @@ -117,6 +117,22 @@ export default {
},
],
},
{
code: `
/**
* @license MIT-7
* Some extra text...
*/
function quux (foo) {
}
`,
errors: [
{
message: 'Invalid JSDoc @license: "MIT-7"; expected SPDX expression: https://spdx.org/licenses/.',
},
],
},
{
code: `
/**
Expand Down Expand Up @@ -201,6 +217,17 @@ export default {
}
`,
},
{
code: `
/**
* @license MIT
* Some extra text...
*/
function quux (foo) {
}
`,
},
{
code: `
/**
Expand Down

0 comments on commit eed7dde

Please sign in to comment.