Skip to content

feat(require-hyphen-before-param-description): when always is set, disallow hyphen at end of line #1454

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion .README/rules/require-hyphen-before-param-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Requires (or disallows) a hyphen before the `@param` description.

## Fixer

(Todo)
Adds a hyphen for "always" and removes a hyphen for "never".

## Options

Expand All @@ -16,6 +16,10 @@ If the string is `"always"` then a problem is raised when there is no hyphen
before the description. If it is `"never"` then a problem is raised when there
is a hyphen before the description. The default value is `"always"`.

Even if hyphens are set to "always" appear, they will actually be forbidden
in the event that they are at the end of a line (this will otherwise cause
Visual Studio Code to display incorrectly).

The options object may have the following properties to indicate behavior for
other tags besides the `@param` tag (or the `@arg` tag if so set):

Expand Down
33 changes: 32 additions & 1 deletion docs/rules/require-hyphen-before-param-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Requires (or disallows) a hyphen before the `@param` description.
<a name="require-hyphen-before-param-description-fixer"></a>
## Fixer

(Todo)
Adds a hyphen for "always" and removes a hyphen for "never".

<a name="user-content-require-hyphen-before-param-description-options"></a>
<a name="require-hyphen-before-param-description-options"></a>
Expand All @@ -27,6 +27,10 @@ If the string is `"always"` then a problem is raised when there is no hyphen
before the description. If it is `"never"` then a problem is raised when there
is a hyphen before the description. The default value is `"always"`.

Even if hyphens are set to "always" appear, they will actually be forbidden
in the event that they are at the end of a line (this will otherwise cause
Visual Studio Code to display incorrectly).

The options object may have the following properties to indicate behavior for
other tags besides the `@param` tag (or the `@arg` tag if so set):

Expand Down Expand Up @@ -202,6 +206,14 @@ function quux () {
*/
function test(input) {}
// Message: There must be a hyphen before @param description.

/**
* @param foo -
* The possible values for `foo` are as follows.
* - `"option1"`: Description of option 1.
* - `"option2"`: Description of option 2.
*/
// Message: There must be no hyphen before @param description.
````


Expand Down Expand Up @@ -294,5 +306,24 @@ function main(argv) {
* @typedef {any} Test
*/
// "jsdoc/require-hyphen-before-param-description": ["error"|"warn", "always",{"tags":{"template":"always"}}]

/**
* @param foo - The possible values for `foo` are as follows.
* - `"option1"`: Description of option 1.
* - `"option2"`: Description of option 2.
*/

/**
* @param foo
* The possible values for `foo` are as follows.
* - `"option1"`: Description of option 1.
* - `"option2"`: Description of option 2.
*/

/**
* @param foo
* - `"option1"`: Description of option 1.
* - `"option2"`: Description of option 2.
*/
````

53 changes: 34 additions & 19 deletions src/rules/requireHyphenBeforeParamDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export default iterateJsdoc(({
}

const startsWithHyphen = (/^\s*-/v).test(desc);
const hyphenNewline = (/^\s*-\n/v).test(desc);

let lines = 0;
for (const {
tokens,
Expand All @@ -41,26 +43,35 @@ export default iterateJsdoc(({
lines++;
}

if (always) {
if (always && !hyphenNewline) {
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*)/v, '$1- ',
);
break;
}
}
},
);
let fixIt = true;
for (const {
tokens,
} of jsdocTag.source) {
if (tokens.description) {
tokens.description = tokens.description.replace(
/^(\s*)/v, '$1- ',
);
break;
}

// Linebreak after name since has no description
if (tokens.name) {
fixIt = false;
break;
}
}

if (fixIt) {
utils.reportJSDoc(
`There must be a hyphen before @${targetTagName} description.`,
{
line: jsdocTag.source[0].number + lines,
},
() => {},
);
}
}
} else if (startsWithHyphen) {
utils.reportJSDoc(
Expand All @@ -76,6 +87,10 @@ export default iterateJsdoc(({
tokens.description = tokens.description.replace(
/^\s*-\s*/v, '',
);
if (hyphenNewline) {
tokens.postName = '';
}

break;
}
}
Expand Down
52 changes: 52 additions & 0 deletions test/rules/assertions/requireHyphenBeforeParamDescription.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,30 @@ export default /** @type {import('../index.js').TestCases} */ ({
function test(name) {}
`,
},
{
code: `
/**
* @param foo -
* The possible values for \`foo\` are as follows.
* - \`"option1"\`: Description of option 1.
* - \`"option2"\`: Description of option 2.
*/
`,
errors: [
{
line: 3,
message: 'There must be no hyphen before @param description.',
},
],
output: `
/**
* @param foo
* The possible values for \`foo\` are as follows.
* - \`"option1"\`: Description of option 1.
* - \`"option2"\`: Description of option 2.
*/
`,
},
],
valid: [
{
Expand Down Expand Up @@ -658,5 +682,33 @@ export default /** @type {import('../index.js').TestCases} */ ({
},
],
},
{
code: `
/**
* @param foo - The possible values for \`foo\` are as follows.
* - \`"option1"\`: Description of option 1.
* - \`"option2"\`: Description of option 2.
*/
`,
},
{
code: `
/**
* @param foo
* The possible values for \`foo\` are as follows.
* - \`"option1"\`: Description of option 1.
* - \`"option2"\`: Description of option 2.
*/
`,
},
{
code: `
/**
* @param foo
* - \`"option1"\`: Description of option 1.
* - \`"option2"\`: Description of option 2.
*/
`,
},
],
});
Loading