-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnode-param-description-lowercase-first-char.ts
48 lines (41 loc) · 1.27 KB
/
node-param-description-lowercase-first-char.ts
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
import { DOCUMENTATION } from "../constants";
import { utils } from "../ast/utils";
import { id } from "../ast/identifiers";
import { getters } from "../ast/getters";
export default utils.createRule({
name: utils.getRuleName(module),
meta: {
type: "problem",
docs: {
description: `First char in \`description\` in node parameter must be uppercase. ${DOCUMENTATION.APPLICABLE_BY_EXTENSION_TO_DESCRIPTION_IN_OPTION}`,
recommended: "strict",
},
fixable: "code",
schema: [],
messages: {
uppercaseFirstChar: "Change first char to uppercase [autofixable]",
},
},
defaultOptions: [],
create(context) {
return {
ObjectExpression(node) {
if (!id.isNodeParameter(node) && !id.isOption(node)) return;
if (id.isReturnValue(node)) return;
const description = getters.nodeParam.getDescription(node);
if (!description) return;
const { value } = description;
const firstChar = value.charAt(0);
if (/[a-z]/.test(firstChar)) {
const correctlyCased = firstChar.toUpperCase() + value.slice(1);
const fixed = utils.keyValue("description", correctlyCased);
context.report({
messageId: "uppercaseFirstChar",
node: description.ast,
fix: (fixer) => fixer.replaceText(description.ast, fixed),
});
}
},
};
},
});