-
-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathallow-null-in-optional-parameter.js
More file actions
53 lines (51 loc) · 1.42 KB
/
Copy pathallow-null-in-optional-parameter.js
File metadata and controls
53 lines (51 loc) · 1.42 KB
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
export default {
meta: {
type: "problem",
fixable: "code",
schema: [],
messages: {
optionalWithoutNull: "Always use `T | null` for optional property",
nullWithoutOptional: "Mark nullable parameter as optional",
},
},
create(context) {
return {
TSPropertySignature(node) {
if (node.optional) {
if (node.typeAnnotation.type !== "TSTypeAnnotation") {
return;
}
if (
node.typeAnnotation.typeAnnotation.type !== "TSUnionType" ||
node.typeAnnotation.typeAnnotation.types.every(
(type) => type.type !== "TSNullKeyword",
)
) {
context.report({
node,
messageId: "optionalWithoutNull",
fix: function* (fixer) {
yield fixer.insertTextAfter(node.typeAnnotation, " | null");
},
});
}
} else {
if (
node.typeAnnotation.typeAnnotation.type === "TSUnionType" &&
node.typeAnnotation.typeAnnotation.types.some(
(type) => type.type === "TSNullKeyword",
)
) {
context.report({
node,
messageId: "nullWithoutOptional",
fix: function* (fixer) {
yield fixer.insertTextAfter(node.key, "?");
},
});
}
}
},
};
},
};