-
Notifications
You must be signed in to change notification settings - Fork 3
/
node-param-description-line-break-html-tag.ts
52 lines (43 loc) · 1.47 KB
/
node-param-description-line-break-html-tag.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
49
50
51
52
import { DOCUMENTATION, LINE_BREAK_HTML_TAG_REGEX } 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: `\`description\` in node parameter must not contain an HTML line break. ${DOCUMENTATION.APPLICABLE_BY_EXTENSION_TO_DESCRIPTION_IN_OPTION}`,
recommended: "strict",
},
fixable: "code",
schema: [],
messages: {
removeTag: "Remove line break [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;
if (description.value.includes("PRIVATE KEY")) return; // <br> allowed in PEM key example
if (description.value.includes("<br><br>")) return; // <br><br> allowed (temp until fixed in client)
if (LINE_BREAK_HTML_TAG_REGEX.test(description.value)) {
const clean = description.value
.replace(new RegExp(LINE_BREAK_HTML_TAG_REGEX, "g"), "")
.replace(/\s{2,}/, " ");
const fixed = utils.keyValue("description", clean);
context.report({
messageId: "removeTag",
node: description.ast,
fix: (fixer) => fixer.replaceText(description.ast, fixed),
});
}
},
};
},
});