Skip to content

Commit

Permalink
- Support *description
Browse files Browse the repository at this point in the history
  • Loading branch information
brettz9 committed May 9, 2021
1 parent 3aa4185 commit c236ed8
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 28 deletions.
70 changes: 42 additions & 28 deletions src/rules/requireAsteriskPrefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export default iterateJsdoc(({
context,
jsdoc,
utils,
indent,
}) => {
const [defaultRequireValue = 'always', {
tags: tagMap = {},
Expand All @@ -16,32 +17,51 @@ export default iterateJsdoc(({

let currentTag;
source.some(({number, tokens}) => {
const {delimiter, tag, end} = tokens;
const {delimiter, tag, end, description} = tokens;

const neverFix = () => {
tokens.delimiter = '';
tokens.postDelimiter = '';
};
const checkNever = () => {
utils.reportJSDoc('Expected JSDoc line to have no prefix.', {
column: 0,
line: number,
}, neverFix);
const checkNever = (checkValue) => {
if (delimiter && delimiter !== '/**' && (
never && !tagMap.always?.includes(checkValue) ||
tagMap.never?.includes(checkValue)
)) {
utils.reportJSDoc('Expected JSDoc line to have no prefix.', {
column: 0,
line: number,
}, neverFix);

return true;
}

return true;
return false;
};

const alwaysFix = () => {
if (!tokens.start) {
tokens.start = indent + ' ';
}
tokens.delimiter = '*';
tokens.postDelimiter = ' ';
tokens.postDelimiter = tag || description ? ' ' : '';
};
const checkAlways = () => {
utils.reportJSDoc('Expected JSDoc line to have the prefix.', {
column: 0,
line: number,
}, alwaysFix);
const checkAlways = (checkValue) => {
if (
!delimiter && (
always && !tagMap.never?.includes(checkValue) ||
tagMap.always?.includes(checkValue)
)
) {
utils.reportJSDoc('Expected JSDoc line to have the prefix.', {
column: 0,
line: number,
}, alwaysFix);

return true;
}

return true;
return false;
};

if (tag) {
Expand All @@ -61,11 +81,11 @@ export default iterateJsdoc(({
if (tagMap.any?.includes('*description')) {
return false;
}
if (delimiter && tagMap.never?.includes('*description')) {
return checkNever();
if (checkNever('*description')) {
return true;
}
if (!delimiter && tagMap.always?.includes('*description')) {
return checkAlways();
if (checkAlways('*description')) {
return true;
}

return false;
Expand All @@ -75,18 +95,12 @@ export default iterateJsdoc(({
return false;
}

if (delimiter && (
never && !tagMap.always?.includes(currentTag) ||
tagMap.never?.includes(currentTag)
)) {
return checkNever();
if (checkNever(currentTag)) {
return true;
}

if (!delimiter && (
always && !tagMap.never?.includes(currentTag) ||
tagMap.always?.includes(currentTag))
) {
return checkAlways();
if (checkAlways(currentTag)) {
return true;
}

return false;
Expand Down
119 changes: 119 additions & 0 deletions test/rules/assertions/requireAsteriskPrefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,91 @@ export default {
}
`,
},
{
code: `
/**
* Desc
*/
function quux (foo) {
// with spaces
}
`,
errors: [
{
line: 5,
message: 'Expected JSDoc line to have the prefix.',
},
],
output: `
/**
* Desc
*
*/
function quux (foo) {
// with spaces
}
`,
},
{
code: `
/**
*
Desc
*/
function quux (foo) {
// with spaces
}
`,
errors: [
{
line: 5,
message: 'Expected JSDoc line to have the prefix.',
},
],
output: `
/**
*
* Desc
*/
function quux (foo) {
// with spaces
}
`,
},
{
code: `
/**
* Desc
*
*/
function quux (foo) {
// with spaces
}
`,
errors: [
{
line: 4,
message: 'Expected JSDoc line to have no prefix.',
},
],
options: ['never'],
output: `
/**
Desc
*
*/
function quux (foo) {
// with spaces
}
`,
},
{
code: `
Expand Down Expand Up @@ -349,5 +434,39 @@ export default {
},
}],
},
{
code: `
/**
* Desc
*
*/
function quux (foo) {
// with spaces
}
`,
options: ['never', {
tags: {
any: ['*description'],
},
}],
},
{
code: `
/**
* Desc
*/
function quux (foo) {
// with spaces
}
`,
options: ['always', {
tags: {
any: ['*description'],
},
}],
},
],
};

0 comments on commit c236ed8

Please sign in to comment.