Debian Codegraph @

eslint/plugin-fig-linter/rules/conventional-descriptions.js

raw symbols SCIP
1 const PROBLEMS = {
2 trailingDot: "no trailing dot",
3 leadingWhitespaces: "no leading whitespaces",
4 trailingWhitespaces: "no trailing whitespaces",
5 uncapitalized: "first letter capitalized",
6 newlineLeadingWhitespaces: "no leading whitespaces on newlines",
7 };
8
9 module.exports = {
10 meta: {
11 type: "problem",
12 fixable: "code",
13 },
14 create(context) {
15 return {
16 'ObjectExpression > Property[key.name="description"]': function (node) {
17 const propValue = node.value;
18
19 if (
20 (propValue.type === "Literal" &&
21 typeof propValue.value === "string") ||
22 (propValue.type === "TemplateLiteral" &&
23 propValue.quasis.length === 1)
24 ) {
25 const isTemplateLiteral = propValue.type === "TemplateLiteral"
26
27 const problems = [];
28 let newString = isTemplateLiteral ? propValue.quasis[0].value.raw : propValue.raw.slice(1, -1); // exclude the StringLiteral op but keep escapes
29 if (newString.charAt(0).match(/[\n\s]/g)) {
30 problems.push(PROBLEMS.leadingWhitespaces);
31 newString = newString.trimStart();
32 }
33 if (newString.charAt(newString.length - 1).match(/[\n\s]/g)) {
34 problems.push(PROBLEMS.trailingWhitespaces);
35 newString = newString.trimEnd();
36 }
37 if (newString.charAt(0) !== newString.charAt(0).toUpperCase()) {
38 problems.push(PROBLEMS.uncapitalized);
39 newString = newString.charAt(0).toUpperCase() + newString.slice(1);
40 }
41 if (newString.endsWith(".")) {
42 problems.push(PROBLEMS.trailingDot);
43 newString = newString.slice(0, -1);
44 }
45 if (isTemplateLiteral && newString.match(/\n\s+/g)) {
46 problems.push(PROBLEMS.newlineLeadingWhitespaces);
47 newString = newString.split("\n").filter(s => !!s).map(s => s.trimStart()).join("\n") // remove trailing whitespaces for template literals
48 }
49
50 if (problems.length) {
51 context.report({
52 node: propValue,
53 message: `Descriptions should have: ${problems.join(", ")}.`,
54 fix(fixer) {
55 if (isTemplateLiteral) {
56 return fixer.replaceText(propValue, "`" + newString + "`")
57 } else {
58 const d = propValue.raw.slice(0, 1); // keep the original StringLiteral op
59 return fixer.replaceText(propValue, d + newString + d);
60 }
61 },
62 });
63 }
64 }
65 },
66 };
67 },
68 };
69