Skip to content

Commit

Permalink
fix(check-values): handle mismatched licensePattern; fixes #884
Browse files Browse the repository at this point in the history
Also:
- test: better convey usage of `licensePattern`
  • Loading branch information
brettz9 committed Apr 26, 2022
1 parent 56966d0 commit f4a9249
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 20 deletions.
45 changes: 45 additions & 0 deletions README.md
Expand Up @@ -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:
Expand Down Expand Up @@ -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"}]
````


Expand Down
52 changes: 32 additions & 20 deletions src/rules/checkValues.js
Expand Up @@ -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,
);
}
}
}
});
Expand Down
90 changes: 90 additions & 0 deletions test/rules/assertions/checkValues.js
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -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',
},
],
},
],
};

0 comments on commit f4a9249

Please sign in to comment.