Skip to content

Commit

Permalink
fix(match-description); preserve newlines (except trailling ones) f…
Browse files Browse the repository at this point in the history
…or purposes of regex matching; fixes part of gajus#692

Should now include fix for `comment-parser` update's dropping of newlines in jsdoc block `description`; still need to preserve newlines across rules in the `.description` of tags
  • Loading branch information
brettz9 committed Feb 16, 2021
1 parent 4f0dc8e commit 74bbe38
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .README/rules/match-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Note that `/` delimiters are optional, but necessary to add flags (besides
Also note that the default or optional regular expressions is *not*
case-insensitive unless one opts in to add the `i` flag.

You can add the `s` flag if you want `.` to match newlines. Note, however,
that the trailing newlines of a description will not be matched.

#### Options

##### `matchDescription`
Expand Down
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5959,6 +5959,9 @@ Note that `/` delimiters are optional, but necessary to add flags (besides
Also note that the default or optional regular expressions is *not*
case-insensitive unless one opts in to add the `i` flag.
You can add the `s` flag if you want `.` to match newlines. Note, however,
that the trailing newlines of a description will not be matched.
<a name="eslint-plugin-jsdoc-rules-match-description-options-11"></a>
#### Options
Expand Down Expand Up @@ -6694,6 +6697,17 @@ function quux (foo) {
}
// "jsdoc/match-description": ["error"|"warn", {"tags":{"template":true}}]
/**
* Enable or disable plugin.
*
* When enabling with this function, the script will be attached to the `document` if:.
* - the script runs in browser context.
* - the `document` doesn't have the script already attached.
* - the `loadScript` option is set to `true`.
* @param enabled `true` to enable, `false` to disable. Default: `true`.
*/
// "jsdoc/match-description": ["error"|"warn", {"contexts":["any"],"mainDescription":"/^[A-Z`-].*\\.$/us","matchDescription":"^([A-Z`-].*(\\.|:)|-\\s.*)$","tags":{"param":true,"returns":true}}]
````
Expand Down Expand Up @@ -9095,6 +9109,14 @@ class TestClass {
set Test(value) { }
}
// "jsdoc/require-description": ["error"|"warn", {"checkSetters":false}]
/**
* Multi
* line
*/
function quux () {
}
````
Expand Down
5 changes: 4 additions & 1 deletion src/rules/matchDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ export default iterateJsdoc(({
};

if (jsdoc.description) {
validateDescription(jsdoc.description);
const {description} = utils.getDescription();
validateDescription(
description.replace(/\n+$/, ''),
);
}

if (!options.tags || !Object.keys(options.tags).length) {
Expand Down
3 changes: 2 additions & 1 deletion src/rules/requireDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export default iterateJsdoc(({
};

if (descriptionStyle !== 'tag') {
if (checkDescription(jsdoc.description || '')) {
const {description} = utils.getDescription();
if (checkDescription(description || '')) {
return;
}

Expand Down
22 changes: 22 additions & 0 deletions test/rules/assertions/matchDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -1317,5 +1317,27 @@ export default {
},
],
},
{
code: `
/**
* Enable or disable plugin.
*
* When enabling with this function, the script will be attached to the \`document\` if:.
* - the script runs in browser context.
* - the \`document\` doesn't have the script already attached.
* - the \`loadScript\` option is set to \`true\`.
* @param enabled \`true\` to enable, \`false\` to disable. Default: \`true\`.
*/
`,
options: [{
contexts: ['any'],
mainDescription: '/^[A-Z`-].*\\.$/us',
matchDescription: '^([A-Z`-].*(\\.|:)|-\\s.*)$',
tags: {
param: true,
returns: true,
},
}],
},
],
};
11 changes: 11 additions & 0 deletions test/rules/assertions/requireDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -832,5 +832,16 @@ export default {
},
],
},
{
code: `
/**
* Multi
* line
*/
function quux () {
}
`,
},
],
};

0 comments on commit 74bbe38

Please sign in to comment.