Skip to content

Commit

Permalink
fix(require-hyphen-before-param-description): allow whitespace befo…
Browse files Browse the repository at this point in the history
…re hyphen when checking for hyphens; fixes #664
  • Loading branch information
brettz9 committed Jan 3, 2021
1 parent f345419 commit fb906de
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 3 deletions.
17 changes: 17 additions & 0 deletions README.md
Expand Up @@ -9432,6 +9432,15 @@ function quux () {
// Options: ["never"]
// Message: There must be no hyphen before @param description.
/**
* @param foo - Foo.
*/
function quux () {
}
// Options: ["never"]
// Message: There must be no hyphen before @param description.
/**
* @param foo - foo
* @param foo foo
Expand Down Expand Up @@ -9504,6 +9513,14 @@ function quux () {
}
// Options: ["always"]
/**
* @param foo - Foo.
*/
function quux () {
}
// Options: ["always"]
/**
* @param foo - Foo.
* @returns {SomeType} A description.
Expand Down
7 changes: 4 additions & 3 deletions src/rules/requireHyphenBeforeParamDescription.js
Expand Up @@ -16,8 +16,9 @@ export default iterateJsdoc(({
return;
}

const startsWithHyphen = (/-/u).test(jsdocTag.description);
if (always) {
if (!jsdocTag.description.startsWith('-')) {
if (!startsWithHyphen) {
report(`There must be a hyphen before @${targetTagName} description.`, (fixer) => {
const lineIndex = jsdocTag.line;
const sourceLines = sourceCode.getText(jsdocNode).split('\n');
Expand All @@ -34,9 +35,9 @@ export default iterateJsdoc(({
return fixer.replaceText(jsdocNode, replacement);
}, jsdocTag);
}
} else if (jsdocTag.description.startsWith('-')) {
} else if (startsWithHyphen) {
report(`There must be no hyphen before @${targetTagName} description.`, (fixer) => {
const [unwantedPart] = /-\s*/u.exec(jsdocTag.description);
const [unwantedPart] = /\s*-\s*/u.exec(jsdocTag.description);

const replacement = sourceCode
.getText(jsdocNode)
Expand Down
40 changes: 40 additions & 0 deletions test/rules/assertions/requireHyphenBeforeParamDescription.js
Expand Up @@ -145,6 +145,33 @@ export default {
}
`,
},
{
code: `
/**
* @param foo - Foo.
*/
function quux () {
}
`,
errors: [
{
line: 3,
message: 'There must be no hyphen before @param description.',
},
],
options: [
'never',
],
output: `
/**
* @param foo Foo.
*/
function quux () {
}
`,
},
{
code: `
/**
Expand Down Expand Up @@ -353,6 +380,19 @@ export default {
'always',
],
},
{
code: `
/**
* @param foo - Foo.
*/
function quux () {
}
`,
options: [
'always',
],
},
{
code: `
/**
Expand Down

0 comments on commit fb906de

Please sign in to comment.