Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(require-hyphen-before-param-description): inject hyphen at proper place with multiline type #1325

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/rules/require-hyphen-before-param-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ function quux () {
*/
// "jsdoc/require-hyphen-before-param-description": ["error"|"warn", "always",{"tags":{"*":"never","property":"always"}}]
// Message: There must be no hyphen before @returns description.

/**
* @param {(
* | string
* | number
* )} input The input value
*/
function test(input) {}
// Message: There must be a hyphen before @param description.
````


Expand Down
57 changes: 29 additions & 28 deletions src/rules/requireHyphenBeforeParamDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,38 +33,39 @@ export default iterateJsdoc(({
}

const startsWithHyphen = (/^\s*-/u).test(desc);
if (always) {
if (!startsWithHyphen) {
report(`There must be a hyphen before @${targetTagName} description.`, (fixer) => {
const lineIndex = /** @type {import('../iterateJsdoc.js').Integer} */ (
jsdocTag.line
);
const sourceLines = sourceCode.getText(jsdocNode).split('\n');

// Get start index of description, accounting for multi-line descriptions
const description = desc.split('\n')[0];
const descriptionIndex = sourceLines[lineIndex].lastIndexOf(description);
let lines = 0;
for (const {
tokens,
} of jsdocTag.source) {
if (tokens.description) {
break;
}

const replacementLine = sourceLines[lineIndex]
.slice(0, descriptionIndex) + '- ' + description;
sourceLines.splice(lineIndex, 1, replacementLine);
const replacement = sourceLines.join('\n');
lines++;
}

return fixer.replaceText(jsdocNode, replacement);
}, jsdocTag);
if (always) {
if (!startsWithHyphen) {
utils.reportJSDoc(
`There must be a hyphen before @${targetTagName} description.`,
{
line: jsdocTag.source[0].number + lines,
},
() => {
for (const {
tokens,
} of jsdocTag.source) {
if (tokens.description) {
tokens.description = tokens.description.replace(
/^(\s*)/u, '$1- ',
);
break;
}
}
}
);
}
} else if (startsWithHyphen) {
let lines = 0;
for (const {
tokens,
} of jsdocTag.source) {
if (tokens.description) {
break;
}

lines++;
}

utils.reportJSDoc(
`There must be no hyphen before @${targetTagName} description.`,
{
Expand Down
26 changes: 26 additions & 0 deletions test/rules/assertions/requireHyphenBeforeParamDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,32 @@ export default {
*/
`,
},
{
code: `
/**
* @param {(
* | string
* | number
* )} input The input value
*/
function test(input) {}
`,
errors: [
{
line: 6,
message: 'There must be a hyphen before @param description.',
},
],
output: `
/**
* @param {(
* | string
* | number
* )} input - The input value
*/
function test(input) {}
`,
},
],
valid: [
{
Expand Down
Loading