forked from gajus/eslint-plugin-jsdoc
-
Notifications
You must be signed in to change notification settings - Fork 1
/
requireHyphenBeforeParamDescription.js
115 lines (105 loc) · 3.4 KB
/
requireHyphenBeforeParamDescription.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import iterateJsdoc from '../iterateJsdoc';
export default iterateJsdoc(({
sourceCode,
utils,
report,
context,
jsdoc,
jsdocNode,
}) => {
const [mainCircumstance, {tags} = {}] = context.options;
const checkHyphens = (jsdocTag, targetTagName, circumstance = mainCircumstance) => {
const always = !circumstance || circumstance === 'always';
const desc = utils.getTagDescription(jsdocTag);
if (!desc.trim()) {
return;
}
const startsWithHyphen = (/^\s*-/u).test(desc);
if (always) {
if (!startsWithHyphen) {
report(`There must be a hyphen before @${targetTagName} description.`, (fixer) => {
const lineIndex = jsdocTag.line;
const sourceLines = sourceCode.getText(jsdocNode).split('\n');
// Get start index of description, accounting for multi-line descriptions
const description = desc.split('\n')[0];
const descriptionIndex = sourceLines[lineIndex].lastIndexOf(description);
const replacementLine = sourceLines[lineIndex]
.slice(0, descriptionIndex) + '- ' + description;
sourceLines.splice(lineIndex, 1, replacementLine);
const replacement = sourceLines.join('\n');
return fixer.replaceText(jsdocNode, replacement);
}, jsdocTag);
}
} else if (startsWithHyphen) {
report(`There must be no hyphen before @${targetTagName} description.`, (fixer) => {
const [unwantedPart] = /^\s*-\s*/u.exec(desc);
const replacement = sourceCode
.getText(jsdocNode)
.replace(desc, desc.slice(unwantedPart.length));
return fixer.replaceText(jsdocNode, replacement);
}, jsdocTag);
}
};
utils.forEachPreferredTag('param', checkHyphens);
if (tags) {
const tagEntries = Object.entries(tags);
tagEntries.forEach(([tagName, circumstance]) => {
if (tagName === '*') {
const preferredParamTag = utils.getPreferredTagName({tagName: 'param'});
jsdoc.tags.forEach(({tag}) => {
if (tag === preferredParamTag || tagEntries.some(([tagNme]) => {
return tagNme !== '*' && tagNme === tag;
})) {
return;
}
utils.forEachPreferredTag(tag, (jsdocTag, targetTagName) => {
checkHyphens(jsdocTag, targetTagName, circumstance);
});
});
return;
}
utils.forEachPreferredTag(tagName, (jsdocTag, targetTagName) => {
checkHyphens(jsdocTag, targetTagName, circumstance);
});
});
}
}, {
iterateAllJsdocs: true,
meta: {
docs: {
description: 'Requires a hyphen before the `@param` description.',
url: 'https://github.com/gajus/eslint-plugin-jsdoc#eslint-plugin-jsdoc-rules-require-hyphen-before-param-description',
},
fixable: 'code',
schema: [
{
enum: ['always', 'never'],
type: 'string',
},
{
additionalProperties: false,
properties: {
tags: {
anyOf: [
{
patternProperties: {
'.*': {
enum: ['always', 'never'],
type: 'string',
},
},
type: 'object',
},
{
enum: ['any'],
type: 'string',
},
],
},
},
type: 'object',
},
],
type: 'layout',
},
});