Skip to content

Commit 6ddc79c

Browse files
committed
feat(no-multi-asterisks): add allowWhitespace option; fixes #803
1 parent f308667 commit 6ddc79c

File tree

4 files changed

+202
-6
lines changed

4 files changed

+202
-6
lines changed

.README/rules/no-multi-asterisks.md

+10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,16 @@ and that rule is for catching blocks which only seem like jsdoc).
88

99
#### Options
1010

11+
##### `allowWhitespace` (defaults to `false`)
12+
13+
Set to `true` if you wish to allow asterisks after a space (as with Markdown):
14+
15+
```js
16+
/**
17+
* *bold* text
18+
*/
19+
```
20+
1121
##### `preventAtMiddleLines` (defaults to `true`)
1222

1323
Prevent the likes of this:

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -8712,6 +8712,17 @@ and that rule is for catching blocks which only seem like jsdoc).
87128712
<a name="eslint-plugin-jsdoc-rules-no-multi-asterisks-options-18"></a>
87138713
#### Options
87148714

8715+
<a name="eslint-plugin-jsdoc-rules-no-multi-asterisks-options-18-allowwhitespace-defaults-to-false"></a>
8716+
##### <code>allowWhitespace</code> (defaults to <code>false</code>)
8717+
8718+
Set to `true` if you wish to allow asterisks after a space (as with Markdown):
8719+
8720+
```js
8721+
/**
8722+
* *bold* text
8723+
*/
8724+
```
8725+
87158726
<a name="eslint-plugin-jsdoc-rules-no-multi-asterisks-options-18-preventatmiddlelines-defaults-to-true"></a>
87168727
##### <code>preventAtMiddleLines</code> (defaults to <code>true</code>)
87178728

@@ -8823,6 +8834,25 @@ The following patterns are considered problems:
88238834
* */
88248835
// "jsdoc/no-multi-asterisks": ["error"|"warn", {"preventAtEnd":true}]
88258836
// Message: Should be no multiple asterisks on end lines.
8837+
8838+
/**
8839+
* The method does 2 things:
8840+
* * Thing 1
8841+
* * Thing 2
8842+
*/
8843+
// "jsdoc/no-multi-asterisks": ["error"|"warn", {"allowWhitespace":false}]
8844+
// Message: Should be no multiple asterisks on middle lines.
8845+
8846+
/**
8847+
* This muti-line comment contains some
8848+
* *non-standard bold* syntax
8849+
*/
8850+
// "jsdoc/no-multi-asterisks": ["error"|"warn", {"allowWhitespace":false}]
8851+
// Message: Should be no multiple asterisks on middle lines.
8852+
8853+
/** Desc. **/
8854+
// "jsdoc/no-multi-asterisks": ["error"|"warn", {"allowWhitespace":true}]
8855+
// Message: Should be no multiple asterisks on end lines.
88268856
````
88278857

88288858
The following patterns are not considered problems:
@@ -8900,6 +8930,25 @@ function foo() {
89008930
*
89018931
* **Bold example:** Hi there.
89028932
*/
8933+
8934+
/**
8935+
* The method does 2 things:
8936+
* * Thing 1
8937+
* * Thing 2
8938+
*/
8939+
// "jsdoc/no-multi-asterisks": ["error"|"warn", {"allowWhitespace":true}]
8940+
8941+
/**
8942+
* This muti-line comment contains some
8943+
* *non-standard bold* syntax
8944+
*/
8945+
// "jsdoc/no-multi-asterisks": ["error"|"warn", {"allowWhitespace":true}]
8946+
8947+
/** abc */
8948+
function foo() {
8949+
//
8950+
}
8951+
// "jsdoc/no-multi-asterisks": ["error"|"warn", {"allowWhitespace":true}]
89038952
````
89048953

89058954

src/rules/noMultiAsterisks.js

+32-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
11
import iterateJsdoc from '../iterateJsdoc';
22

3-
const middleAsterisks = /^([\t ]|\*(?!\*))+/u;
3+
const middleAsterisksBlockWS = /^([\t ]|\*(?!\*))+/u;
4+
const middleAsterisksNoBlockWS = /^\*+/u;
5+
6+
const endAsterisksSingleLineBlockWS = /\*((?:\*|(?: |\t))*)\*$/u;
7+
const endAsterisksMultipleLineBlockWS = /((?:\*|(?: |\t))*)\*$/u;
8+
9+
const endAsterisksSingleLineNoBlockWS = /\*(\**)\*$/u;
10+
const endAsterisksMultipleLineNoBlockWS = /(\**)\*$/u;
411

512
export default iterateJsdoc(({
613
context,
714
jsdoc,
815
utils,
916
}) => {
1017
const {
18+
allowWhitespace = false,
1119
preventAtEnd = true,
1220
preventAtMiddleLines = true,
1321
} = context.options[0] || {};
1422

23+
const middleAsterisks = allowWhitespace ? middleAsterisksNoBlockWS : middleAsterisksBlockWS;
24+
25+
// eslint-disable-next-line complexity -- Todo
1526
jsdoc.source.some(({
1627
tokens,
1728
number,
@@ -23,11 +34,18 @@ export default iterateJsdoc(({
2334
type,
2435
description,
2536
end,
37+
postDelimiter,
2638
} = tokens;
39+
2740
if (
2841
preventAtMiddleLines &&
29-
!end && !tag && !type && !name && middleAsterisks.test(description)
42+
!end && !tag && !type && !name &&
43+
(
44+
!allowWhitespace && middleAsterisks.test(description) ||
45+
allowWhitespace && middleAsterisks.test(postDelimiter + description)
46+
)
3047
) {
48+
// console.log('description', JSON.stringify(description));
3149
const fix = () => {
3250
tokens.description = description.replace(middleAsterisks, '');
3351
};
@@ -50,11 +68,16 @@ export default iterateJsdoc(({
5068

5169
const isSingleLineBlock = delimiter === '/**';
5270
const delim = isSingleLineBlock ? '*' : delimiter;
53-
const endAsterisks = isSingleLineBlock ?
54-
/\*((?:\*|(?: |\t))*)\*$/u :
55-
/((?:\*|(?: |\t))*)\*$/u;
71+
let endAsterisks;
72+
if (allowWhitespace) {
73+
endAsterisks = isSingleLineBlock ? endAsterisksSingleLineNoBlockWS : endAsterisksMultipleLineNoBlockWS;
74+
} else {
75+
endAsterisks = isSingleLineBlock ? endAsterisksSingleLineBlockWS : endAsterisksMultipleLineBlockWS;
76+
}
5677

57-
const endingAsterisksAndSpaces = (description + delim).match(
78+
const endingAsterisksAndSpaces = (
79+
allowWhitespace ? postDelimiter + description + delim : description + delim
80+
).match(
5881
endAsterisks,
5982
);
6083

@@ -96,6 +119,9 @@ export default iterateJsdoc(({
96119
{
97120
additionalProperies: false,
98121
properties: {
122+
allowWhitespace: {
123+
type: 'boolean',
124+
},
99125
preventAtEnd: {
100126
type: 'boolean',
101127
},

test/rules/assertions/noMultiAsterisks.js

+111
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,77 @@ export default {
276276
*/
277277
`,
278278
},
279+
{
280+
code: `
281+
/**
282+
* The method does 2 things:
283+
* * Thing 1
284+
* * Thing 2
285+
*/
286+
`,
287+
errors: [
288+
{
289+
line: 4,
290+
message: 'Should be no multiple asterisks on middle lines.',
291+
},
292+
],
293+
options: [
294+
{
295+
allowWhitespace: false,
296+
},
297+
],
298+
output: `
299+
/**
300+
* The method does 2 things:
301+
* Thing 1
302+
* * Thing 2
303+
*/
304+
`,
305+
},
306+
{
307+
code: `
308+
/**
309+
* This muti-line comment contains some
310+
* *non-standard bold* syntax
311+
*/
312+
`,
313+
errors: [
314+
{
315+
line: 4,
316+
message: 'Should be no multiple asterisks on middle lines.',
317+
},
318+
],
319+
options: [
320+
{
321+
allowWhitespace: false,
322+
},
323+
],
324+
output: `
325+
/**
326+
* This muti-line comment contains some
327+
* non-standard bold* syntax
328+
*/
329+
`,
330+
},
331+
{
332+
code: `
333+
/** Desc. **/
334+
`,
335+
errors: [
336+
{
337+
line: 2,
338+
message: 'Should be no multiple asterisks on end lines.',
339+
},
340+
],
341+
options: [
342+
{
343+
allowWhitespace: true,
344+
},
345+
],
346+
output: `
347+
/** Desc. */
348+
`,
349+
},
279350
],
280351
valid: [
281352
{
@@ -402,5 +473,45 @@ export default {
402473
*/
403474
`,
404475
},
476+
{
477+
code: `
478+
/**
479+
* The method does 2 things:
480+
* * Thing 1
481+
* * Thing 2
482+
*/
483+
`,
484+
options: [
485+
{
486+
allowWhitespace: true,
487+
},
488+
],
489+
},
490+
{
491+
code: `
492+
/**
493+
* This muti-line comment contains some
494+
* *non-standard bold* syntax
495+
*/
496+
`,
497+
options: [
498+
{
499+
allowWhitespace: true,
500+
},
501+
],
502+
},
503+
{
504+
code: `
505+
/** abc */
506+
function foo() {
507+
//
508+
}
509+
`,
510+
options: [
511+
{
512+
allowWhitespace: true,
513+
},
514+
],
515+
},
405516
],
406517
};

0 commit comments

Comments
 (0)