Skip to content

Commit bca8d3c

Browse files
committed
fix(require-hyphen-before-param-description): fixer should insert hyphen in correct line (#176)
1 parent a05741a commit bca8d3c

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/rules/requireHyphenBeforeParamDescription.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,12 @@ export default iterateJsdoc(({
2626
if (always) {
2727
if (!jsdocTag.description.startsWith('-')) {
2828
report('There must be a hyphen before @' + targetTagName + ' description.', (fixer) => {
29-
const replacement = sourceCode.getText(jsdocNode).replace(jsdocTag.description, '- ' + jsdocTag.description);
29+
const lineIndex = jsdocTag.line;
30+
const sourceLines = sourceCode.getText(jsdocNode).split('\n');
31+
const replacementLine = sourceLines[lineIndex]
32+
.replace(jsdocTag.description, '- ' + jsdocTag.description);
33+
sourceLines.splice(lineIndex, 1, replacementLine);
34+
const replacement = sourceLines.join('\n');
3035

3136
return fixer.replaceText(jsdocNode, replacement);
3237
}, jsdocTag);

test/rules/assertions/requireHyphenBeforeParamDescription.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,35 @@ export default {
5151
*/
5252
function quux () {
5353
54+
}
55+
`
56+
},
57+
{
58+
code: `
59+
/**
60+
* @param foo - Foo.
61+
* @param foo Foo.
62+
*/
63+
function quux () {
64+
65+
}
66+
`,
67+
errors: [
68+
{
69+
line: 4,
70+
message: 'There must be a hyphen before @param description.'
71+
}
72+
],
73+
options: [
74+
'always'
75+
],
76+
output: `
77+
/**
78+
* @param foo - Foo.
79+
* @param foo - Foo.
80+
*/
81+
function quux () {
82+
5483
}
5584
`
5685
}

0 commit comments

Comments
 (0)