-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
require-deprecation-date.ts
120 lines (111 loc) · 3.58 KB
/
require-deprecation-date.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { GraphQLESLintRule } from '../types';
import { valueFromNode } from '../estree-parser/utils';
import { getLocation } from '../utils';
const DATE_REGEX = /^\d{2}\/\d{2}\/\d{4}$/;
const MESSAGE_REQUIRE_DATE = 'MESSAGE_REQUIRE_DATE';
const MESSAGE_INVALID_FORMAT = 'MESSAGE_INVALID_FORMAT';
const MESSAGE_INVALID_DATE = 'MESSAGE_INVALID_DATE';
const MESSAGE_CAN_BE_REMOVED = 'MESSAGE_CAN_BE_REMOVED';
const rule: GraphQLESLintRule<[{ argumentName?: string }]> = {
meta: {
type: 'suggestion',
docs: {
category: 'Best Practices',
description:
'Require deletion date on `@deprecated` directive. Suggest removing deprecated things after deprecated date.',
url: 'https://github.com/dotansimha/graphql-eslint/blob/master/docs/rules/require-deprecation-date.md',
examples: [
{
title: 'Incorrect',
code: /* GraphQL */ `
type User {
firstname: String @deprecated
firstName: String
}
`,
},
{
title: 'Incorrect',
code: /* GraphQL */ `
type User {
firstname: String @deprecated(reason: "Use 'firstName' instead")
firstName: String
}
`,
},
{
title: 'Correct',
code: /* GraphQL */ `
type User {
firstname: String @deprecated(reason: "Use 'firstName' instead", deletionDate: "25/12/2022")
firstName: String
}
`,
},
],
},
messages: {
[MESSAGE_REQUIRE_DATE]: 'Directive "@deprecated" must have a deletion date',
[MESSAGE_INVALID_FORMAT]: 'Deletion date must be in format "DD/MM/YYYY"',
[MESSAGE_INVALID_DATE]: 'Invalid "{{ deletionDate }}" deletion date',
[MESSAGE_CAN_BE_REMOVED]: '"{{ nodeName }}" сan be removed',
},
schema: [
{
type: 'object',
additionalProperties: false,
properties: {
argumentName: {
type: 'string',
},
},
},
],
},
create(context) {
return {
'Directive[name.value=deprecated]'(node) {
const argName = context.options[0]?.argumentName || 'deletionDate';
const deletionDateNode = node.arguments.find(arg => arg.name.value === argName);
if (!deletionDateNode) {
context.report({
loc: getLocation(node.loc, node.name.value, { offsetEnd: 0 }),
messageId: MESSAGE_REQUIRE_DATE,
});
return;
}
const deletionDate = valueFromNode(deletionDateNode.value);
const isValidDate = DATE_REGEX.test(deletionDate);
if (!isValidDate) {
context.report({ node: deletionDateNode.value, messageId: MESSAGE_INVALID_FORMAT });
return;
}
let [day, month, year] = deletionDate.split('/');
day = day.toString().padStart(2, '0');
month = month.toString().padStart(2, '0');
const deletionDateInMS = Date.parse(`${year}-${month}-${day}`);
if (Number.isNaN(deletionDateInMS)) {
context.report({
node: deletionDateNode.value,
messageId: MESSAGE_INVALID_DATE,
data: {
deletionDate,
},
});
return;
}
const canRemove = Date.now() > deletionDateInMS;
if (canRemove) {
context.report({
node,
messageId: MESSAGE_CAN_BE_REMOVED,
data: {
nodeName: node.parent.name.value,
},
});
}
},
};
},
};
export default rule;