-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathnode-param-operation-option-action-miscased.ts
85 lines (71 loc) · 2.34 KB
/
node-param-operation-option-action-miscased.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
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
import { utils } from "../ast/utils";
import { id } from "../ast/identifiers";
import { getters } from "../ast/getters";
import { AST_NODE_TYPES, TSESTree } from "@typescript-eslint/utils";
import { sentenceCase } from "sentence-case";
export default utils.createRule({
name: utils.getRuleName(module),
meta: {
type: "problem",
docs: {
description:
"The property `action` in an option in an Operation node parameter must be sentence-cased.",
recommended: "strict",
},
fixable: "code",
schema: [],
messages: {
useSentenceCase: "Change to sentence case [autofixable]",
},
},
defaultOptions: [],
create(context) {
return {
ObjectExpression(node) {
if (!id.isNodeParameter(node)) return;
if (!id.nodeParam.isOperation(node) && !id.nodeParam.isAction(node)) {
return;
}
const options = getters.nodeParam.getOptions(node);
if (!options) return;
// skip `options: [...].sort()`, see EditImage.node.ts
if (!Array.isArray(options.ast.value.elements)) return;
for (const option of options.ast.value.elements) {
const action = option.properties.find(isActionProperty);
if (!action) continue;
const actionSentence = action.value.value;
if (!isSentenceCase(actionSentence)) {
const sentenceCased = sentenceCase(actionSentence);
const fixed = utils.keyValue("action", sentenceCased);
context.report({
messageId: "useSentenceCase",
node: action,
fix: (fixer) => fixer.replaceText(action, fixed),
});
}
}
},
};
},
});
function isActionProperty(
property: TSESTree.ObjectLiteralElement
): property is TSESTree.Property & { value: { value: string } } {
return (
property.type === AST_NODE_TYPES.Property &&
property.computed === false &&
property.key.type === AST_NODE_TYPES.Identifier &&
property.key.name === "action" &&
property.value.type === AST_NODE_TYPES.Literal &&
typeof property.value.value === "string"
);
}
function isSentenceCase(sentence: string) {
const withoutAllUppercaseWords = sentence
.split(" ")
.filter((word) => !isAllUppercase(word)) // tolerate all-uppercase word
.filter((word) => !word.includes("\\'")) // tolerate escaped single quote
.join(" ");
return withoutAllUppercaseWords === sentenceCase(withoutAllUppercaseWords);
}
const isAllUppercase = (str: string) => str === str.toUpperCase();