Skip to content

Commit

Permalink
fix(no-multi-asterisks): avoid false positives and erring in fixer …
Browse files Browse the repository at this point in the history
…of single line blocks; fixes gajus#735
  • Loading branch information
brettz9 committed May 15, 2021
1 parent 8848246 commit 355da58
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 5 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8193,6 +8193,15 @@ The following patterns are considered problems:
**/
// "jsdoc/no-multi-asterisks": ["error"|"warn", {"preventAtMiddleLines":false}]
// Message: Should be no multiple asterisks on end lines.

/** Desc. **/
// Message: Should be no multiple asterisks on end lines.

/** @someTag name desc. **/
// Message: Should be no multiple asterisks on end lines.

/** abc * */
// Message: Should be no multiple asterisks on end lines.
````

The following patterns are not considered problems:
Expand Down Expand Up @@ -8245,6 +8254,21 @@ The following patterns are not considered problems:
* @tag {SomeType} and a tag with details
*
*/

/** abc */
function foo() {
//
}

/** foo */
function foo(): void {
//
}

/** @aTag abc */
function foo() {
//
}
````


Expand Down
18 changes: 13 additions & 5 deletions src/rules/noMultiAsterisks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import iterateJsdoc from '../iterateJsdoc';

const middleAsterisks = /^[* \t]+/u;
const endAsterisks = /((?:\*|(?: |\t))*)\*$/u;

export default iterateJsdoc(({
context,
Expand Down Expand Up @@ -39,20 +38,29 @@ export default iterateJsdoc(({
if (!preventAtEnd || !end) {
return false;
}
const endingAsterisksAndSpaces = (description + delimiter).match(

const isSingleLineBlock = delimiter === '/**';
const delim = isSingleLineBlock ? '*' : delimiter;
const endAsterisks = isSingleLineBlock ?
/\*((?:\*|(?: |\t))*)\*$/u :
/((?:\*|(?: |\t))*)\*$/u;

const endingAsterisksAndSpaces = (description + delim).match(
endAsterisks,
);

if (
!endingAsterisksAndSpaces ||
endingAsterisksAndSpaces[1] && !endingAsterisksAndSpaces[1].trim()
!isSingleLineBlock && endingAsterisksAndSpaces[1] && !endingAsterisksAndSpaces[1].trim()
) {
return false;
}

const endFix = () => {
tokens.delimiter = '';
tokens.description = (description + delimiter).replace(endAsterisks, '');
if (!isSingleLineBlock) {
tokens.delimiter = '';
}
tokens.description = (description + delim).replace(endAsterisks, '');
};

utils.reportJSDoc(
Expand Down
61 changes: 61 additions & 0 deletions test/rules/assertions/noMultiAsterisks.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,42 @@ export default {
*/
`,
},
{
code: `
/** Desc. **/
`,
errors: [{
line: 2,
message: 'Should be no multiple asterisks on end lines.',
}],
output: `
/** Desc. */
`,
},
{
code: `
/** @someTag name desc. **/
`,
errors: [{
line: 2,
message: 'Should be no multiple asterisks on end lines.',
}],
output: `
/** @someTag name desc. */
`,
},
{
code: `
/** abc * */
`,
errors: [{
line: 2,
message: 'Should be no multiple asterisks on end lines.',
}],
output: `
/** abc */
`,
},
],
valid: [
{
Expand Down Expand Up @@ -260,5 +296,30 @@ export default {
*/
`,
},
{
code: `
/** abc */
function foo() {
//
}
`,
},
{
code: `
/** foo */
function foo(): void {
//
}
`,
parser: require.resolve('@typescript-eslint/parser'),
},
{
code: `
/** @aTag abc */
function foo() {
//
}
`,
},
],
};

0 comments on commit 355da58

Please sign in to comment.