Skip to content

Commit c236ed8

Browse files
committed
- Support *description
1 parent 3aa4185 commit c236ed8

File tree

2 files changed

+161
-28
lines changed

2 files changed

+161
-28
lines changed

src/rules/requireAsteriskPrefix.js

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default iterateJsdoc(({
44
context,
55
jsdoc,
66
utils,
7+
indent,
78
}) => {
89
const [defaultRequireValue = 'always', {
910
tags: tagMap = {},
@@ -16,32 +17,51 @@ export default iterateJsdoc(({
1617

1718
let currentTag;
1819
source.some(({number, tokens}) => {
19-
const {delimiter, tag, end} = tokens;
20+
const {delimiter, tag, end, description} = tokens;
2021

2122
const neverFix = () => {
2223
tokens.delimiter = '';
2324
tokens.postDelimiter = '';
2425
};
25-
const checkNever = () => {
26-
utils.reportJSDoc('Expected JSDoc line to have no prefix.', {
27-
column: 0,
28-
line: number,
29-
}, neverFix);
26+
const checkNever = (checkValue) => {
27+
if (delimiter && delimiter !== '/**' && (
28+
never && !tagMap.always?.includes(checkValue) ||
29+
tagMap.never?.includes(checkValue)
30+
)) {
31+
utils.reportJSDoc('Expected JSDoc line to have no prefix.', {
32+
column: 0,
33+
line: number,
34+
}, neverFix);
35+
36+
return true;
37+
}
3038

31-
return true;
39+
return false;
3240
};
3341

3442
const alwaysFix = () => {
43+
if (!tokens.start) {
44+
tokens.start = indent + ' ';
45+
}
3546
tokens.delimiter = '*';
36-
tokens.postDelimiter = ' ';
47+
tokens.postDelimiter = tag || description ? ' ' : '';
3748
};
38-
const checkAlways = () => {
39-
utils.reportJSDoc('Expected JSDoc line to have the prefix.', {
40-
column: 0,
41-
line: number,
42-
}, alwaysFix);
49+
const checkAlways = (checkValue) => {
50+
if (
51+
!delimiter && (
52+
always && !tagMap.never?.includes(checkValue) ||
53+
tagMap.always?.includes(checkValue)
54+
)
55+
) {
56+
utils.reportJSDoc('Expected JSDoc line to have the prefix.', {
57+
column: 0,
58+
line: number,
59+
}, alwaysFix);
60+
61+
return true;
62+
}
4363

44-
return true;
64+
return false;
4565
};
4666

4767
if (tag) {
@@ -61,11 +81,11 @@ export default iterateJsdoc(({
6181
if (tagMap.any?.includes('*description')) {
6282
return false;
6383
}
64-
if (delimiter && tagMap.never?.includes('*description')) {
65-
return checkNever();
84+
if (checkNever('*description')) {
85+
return true;
6686
}
67-
if (!delimiter && tagMap.always?.includes('*description')) {
68-
return checkAlways();
87+
if (checkAlways('*description')) {
88+
return true;
6989
}
7090

7191
return false;
@@ -75,18 +95,12 @@ export default iterateJsdoc(({
7595
return false;
7696
}
7797

78-
if (delimiter && (
79-
never && !tagMap.always?.includes(currentTag) ||
80-
tagMap.never?.includes(currentTag)
81-
)) {
82-
return checkNever();
98+
if (checkNever(currentTag)) {
99+
return true;
83100
}
84101

85-
if (!delimiter && (
86-
always && !tagMap.never?.includes(currentTag) ||
87-
tagMap.always?.includes(currentTag))
88-
) {
89-
return checkAlways();
102+
if (checkAlways(currentTag)) {
103+
return true;
90104
}
91105

92106
return false;

test/rules/assertions/requireAsteriskPrefix.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,91 @@ export default {
2626
}
2727
`,
2828
},
29+
{
30+
code: `
31+
32+
/**
33+
* Desc
34+
35+
*/
36+
function quux (foo) {
37+
// with spaces
38+
}
39+
`,
40+
errors: [
41+
{
42+
line: 5,
43+
message: 'Expected JSDoc line to have the prefix.',
44+
},
45+
],
46+
output: `
47+
48+
/**
49+
* Desc
50+
*
51+
*/
52+
function quux (foo) {
53+
// with spaces
54+
}
55+
`,
56+
},
57+
{
58+
code: `
59+
60+
/**
61+
*
62+
Desc
63+
*/
64+
function quux (foo) {
65+
// with spaces
66+
}
67+
`,
68+
errors: [
69+
{
70+
line: 5,
71+
message: 'Expected JSDoc line to have the prefix.',
72+
},
73+
],
74+
output: `
75+
76+
/**
77+
*
78+
* Desc
79+
*/
80+
function quux (foo) {
81+
// with spaces
82+
}
83+
`,
84+
},
85+
{
86+
code: `
87+
88+
/**
89+
* Desc
90+
*
91+
*/
92+
function quux (foo) {
93+
// with spaces
94+
}
95+
`,
96+
errors: [
97+
{
98+
line: 4,
99+
message: 'Expected JSDoc line to have no prefix.',
100+
},
101+
],
102+
options: ['never'],
103+
output: `
104+
105+
/**
106+
Desc
107+
*
108+
*/
109+
function quux (foo) {
110+
// with spaces
111+
}
112+
`,
113+
},
29114
{
30115
code: `
31116
@@ -349,5 +434,39 @@ export default {
349434
},
350435
}],
351436
},
437+
{
438+
code: `
439+
440+
/**
441+
* Desc
442+
*
443+
*/
444+
function quux (foo) {
445+
// with spaces
446+
}
447+
`,
448+
options: ['never', {
449+
tags: {
450+
any: ['*description'],
451+
},
452+
}],
453+
},
454+
{
455+
code: `
456+
457+
/**
458+
* Desc
459+
460+
*/
461+
function quux (foo) {
462+
// with spaces
463+
}
464+
`,
465+
options: ['always', {
466+
tags: {
467+
any: ['*description'],
468+
},
469+
}],
470+
},
352471
],
353472
};

0 commit comments

Comments
 (0)