-
Notifications
You must be signed in to change notification settings - Fork 3
/
node-param-collection-type-item-required.ts
64 lines (54 loc) · 1.66 KB
/
node-param-collection-type-item-required.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
import { utils } from "../ast/utils";
import { id } from "../ast/identifiers";
import { getters } from "../ast/getters";
import { AST_NODE_TYPES } from "@typescript-eslint/utils";
export default utils.createRule({
name: utils.getRuleName(module),
meta: {
type: "problem",
docs: {
description: `Items in collection-type node parameter must not have a \`required\` property.`,
recommended: "strict",
},
fixable: "code",
schema: [],
messages: {
removeRequired: "Remove `required: true` [autofixable]",
},
},
defaultOptions: [],
create(context) {
return {
ObjectExpression(node) {
if (!id.isNodeParameter(node)) return;
if (!id.nodeParam.isCollectionType(node)) return;
const options = getters.nodeParam.getCollectionOptions(node);
if (!options) return;
if (options.value.every((param) => param.required === undefined)) {
return;
}
if (options.ast.value.type !== AST_NODE_TYPES.ArrayExpression) return;
const [objectExpression] = options.ast.value.elements;
const requiredTrueProperties = objectExpression.properties.filter(
(property) => {
return (
property.type === AST_NODE_TYPES.Property &&
property.key.type === AST_NODE_TYPES.Identifier &&
property.key.name === "required" &&
property.value.type === AST_NODE_TYPES.Literal &&
property.value.value === true
);
}
);
for (const property of requiredTrueProperties) {
const rangeToRemove = utils.getRangeToRemove({ ast: property });
context.report({
messageId: "removeRequired",
node: property,
fix: (fixer) => fixer.removeRange(rangeToRemove),
});
}
},
};
},
});