Skip to content

Commit 52119a5

Browse files
authored
fix(require-hyphen-before-param-description): more targeted replacement for hyphen removal; fixes #1074 (#1075)
1 parent fc56924 commit 52119a5

File tree

3 files changed

+127
-10
lines changed

3 files changed

+127
-10
lines changed

docs/rules/require-hyphen-before-param-description.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,29 @@ function quux () {
170170
}
171171
// "jsdoc/require-hyphen-before-param-description": ["error"|"warn", "always",{"tags":{"returns":"never"}}]
172172
// Message: There must be a hyphen before @param description.
173+
174+
/**
175+
* Split a unit to metric prefix and basic unit.
176+
*
177+
* @param {string} unit - Unit to split.
178+
* @param {string} [basicUnit] - Basic unit regardless of the metric prefix.
179+
* If omitted, basic unit will be inferred by trying to remove the metric
180+
* prefix in `unit`.
181+
*
182+
* @returns {{ prefix: string, basicUnit: string }} - Split result.
183+
* If `unit` does not have a metric prefix, `''` is returned for `prefix`.
184+
* If `unit` does not have a basic unit, `''` is returned for `basicUnit`.
185+
*/
186+
// "jsdoc/require-hyphen-before-param-description": ["error"|"warn", "always",{"tags":{"*":"never","property":"always"}}]
187+
// Message: There must be no hyphen before @returns description.
188+
189+
/**
190+
* @returns {{
191+
* prefix: string, basicUnit: string
192+
* }} - Split result.
193+
*/
194+
// "jsdoc/require-hyphen-before-param-description": ["error"|"warn", "always",{"tags":{"*":"never","property":"always"}}]
195+
// Message: There must be no hyphen before @returns description.
173196
````
174197

175198

src/rules/requireHyphenBeforeParamDescription.js

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,36 @@ export default iterateJsdoc(({
4242
}, jsdocTag);
4343
}
4444
} else if (startsWithHyphen) {
45-
report(`There must be no hyphen before @${targetTagName} description.`, (fixer) => {
46-
const [
47-
unwantedPart,
48-
] = /^\s*-\s*/u.exec(desc);
45+
let lines = 0;
46+
for (const {
47+
tokens,
48+
} of jsdocTag.source) {
49+
if (tokens.description) {
50+
break;
51+
}
4952

50-
const replacement = sourceCode
51-
.getText(jsdocNode)
52-
.replace(desc, desc.slice(unwantedPart.length));
53+
lines++;
54+
}
5355

54-
return fixer.replaceText(jsdocNode, replacement);
55-
}, jsdocTag);
56+
utils.reportJSDoc(
57+
`There must be no hyphen before @${targetTagName} description.`,
58+
{
59+
line: jsdocTag.source[0].number + lines,
60+
},
61+
() => {
62+
for (const {
63+
tokens,
64+
} of jsdocTag.source) {
65+
if (tokens.description) {
66+
tokens.description = tokens.description.replace(
67+
/^\s*-\s*/u, '',
68+
);
69+
break;
70+
}
71+
}
72+
},
73+
true,
74+
);
5675
}
5776
};
5877

test/rules/assertions/requireHyphenBeforeParamDescription.js

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,82 @@ export default {
371371
}
372372
`,
373373
},
374-
374+
{
375+
code: `
376+
/**
377+
* Split a unit to metric prefix and basic unit.
378+
*
379+
* @param {string} unit - Unit to split.
380+
* @param {string} [basicUnit] - Basic unit regardless of the metric prefix.
381+
* If omitted, basic unit will be inferred by trying to remove the metric
382+
* prefix in \`unit\`.
383+
*
384+
* @returns {{ prefix: string, basicUnit: string }} - Split result.
385+
* If \`unit\` does not have a metric prefix, \`''\` is returned for \`prefix\`.
386+
* If \`unit\` does not have a basic unit, \`''\` is returned for \`basicUnit\`.
387+
*/
388+
`,
389+
errors: [
390+
{
391+
line: 10,
392+
message: 'There must be no hyphen before @returns description.',
393+
},
394+
],
395+
options: [
396+
'always',
397+
{
398+
tags: {
399+
'*': 'never',
400+
property: 'always',
401+
},
402+
},
403+
],
404+
output: `
405+
/**
406+
* Split a unit to metric prefix and basic unit.
407+
*
408+
* @param {string} unit - Unit to split.
409+
* @param {string} [basicUnit] - Basic unit regardless of the metric prefix.
410+
* If omitted, basic unit will be inferred by trying to remove the metric
411+
* prefix in \`unit\`.
412+
*
413+
* @returns {{ prefix: string, basicUnit: string }} Split result.
414+
* If \`unit\` does not have a metric prefix, \`''\` is returned for \`prefix\`.
415+
* If \`unit\` does not have a basic unit, \`''\` is returned for \`basicUnit\`.
416+
*/
417+
`,
418+
},
419+
{
420+
code: `
421+
/**
422+
* @returns {{
423+
* prefix: string, basicUnit: string
424+
* }} - Split result.
425+
*/
426+
`,
427+
errors: [
428+
{
429+
line: 5,
430+
message: 'There must be no hyphen before @returns description.',
431+
},
432+
],
433+
options: [
434+
'always',
435+
{
436+
tags: {
437+
'*': 'never',
438+
property: 'always',
439+
},
440+
},
441+
],
442+
output: `
443+
/**
444+
* @returns {{
445+
* prefix: string, basicUnit: string
446+
* }} Split result.
447+
*/
448+
`,
449+
},
375450
],
376451
valid: [
377452
{

0 commit comments

Comments
 (0)