Skip to content

Commit fa5a295

Browse files
authored
Merge pull request #265 from jasminexie/master
fix(require-hyphen-before-param-description): fixer should insert hyphen in correct line
2 parents a05741a + ee49f06 commit fa5a295

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3031,6 +3031,16 @@ function quux () {
30313031
}
30323032
// Options: ["never"]
30333033
// Message: There must be no hyphen before @param description.
3034+
3035+
/**
3036+
* @param foo - Foo.
3037+
* @param foo Foo.
3038+
*/
3039+
function quux () {
3040+
3041+
}
3042+
// Options: ["always"]
3043+
// Message: There must be a hyphen before @param description.
30343044
````
30353045

30363046
The following patterns are not considered problems:

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)