Skip to content

Commit

Permalink
fix error report for require-deprecation-date rule (#740)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri POSTOLOV committed Oct 31, 2021
1 parent ced6789 commit 578b890
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-otters-add.md
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix error report for `require-deprecation-date` rule
20 changes: 12 additions & 8 deletions packages/plugin/src/rules/require-deprecation-date.ts
@@ -1,5 +1,6 @@
import { GraphQLESLintRule } from '../types';
import { valueFromNode } from '../estree-parser/utils';
import { getLocation } from '../utils';

const DATE_REGEX = /^\d{2}\/\d{2}\/\d{4}$/;

Expand Down Expand Up @@ -47,10 +48,10 @@ const rule: GraphQLESLintRule<[{ argumentName?: 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.',
[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: [
{
Expand All @@ -71,14 +72,17 @@ const rule: GraphQLESLintRule<[{ argumentName?: string }]> = {
const deletionDateNode = node.arguments.find(arg => arg.name.value === argName);

if (!deletionDateNode) {
context.report({ node: node.name, messageId: MESSAGE_REQUIRE_DATE });
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: node.name, messageId: MESSAGE_INVALID_FORMAT });
context.report({ node: deletionDateNode.value, messageId: MESSAGE_INVALID_FORMAT });
return;
}
let [day, month, year] = deletionDate.split('/');
Expand All @@ -88,7 +92,7 @@ const rule: GraphQLESLintRule<[{ argumentName?: string }]> = {

if (Number.isNaN(deletionDateInMS)) {
context.report({
node: node.name,
node: deletionDateNode.value,
messageId: MESSAGE_INVALID_DATE,
data: {
deletionDate,
Expand All @@ -101,7 +105,7 @@ const rule: GraphQLESLintRule<[{ argumentName?: string }]> = {

if (canRemove) {
context.report({
node: node.name,
node,
messageId: MESSAGE_CAN_BE_REMOVED,
data: {
nodeName: node.parent.name.value,
Expand Down
Expand Up @@ -2,20 +2,25 @@

exports[` 1`] = `
> 1 | scalar Old @deprecated(deletionDate: "22/08/2021")
| ^ "Old" сan be removed.
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "Old" сan be removed
`;

exports[` 2`] = `
> 1 | scalar Old @deprecated(untilDate: "22/08/2021")
| ^ "Old" сan be removed.
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ "Old" сan be removed
`;

exports[` 3`] = `
> 1 | scalar Old @deprecated(deletionDate: "bad")
| ^ Deletion date must be in format "DD/MM/YYYY".
| ^ Deletion date must be in format "DD/MM/YYYY"
`;

exports[` 4`] = `
> 1 | scalar Old @deprecated(deletionDate: "32/08/2021")
| ^ Invalid "32/08/2021" deletion date.
| ^ Invalid "32/08/2021" deletion date
`;

exports[` 5`] = `
> 1 | type Old { oldField: ID @deprecated }
| ^^^^^^^^^^^ Directive "@deprecated" must have a deletion date
`;
12 changes: 8 additions & 4 deletions packages/plugin/tests/require-deprecation-date.spec.ts
Expand Up @@ -30,20 +30,24 @@ ruleTester.runGraphQLTests('require-deprecation-date', rule, {
invalid: [
{
code: 'scalar Old @deprecated(deletionDate: "22/08/2021")',
errors: [{ message: '"Old" сan be removed.' }],
errors: [{ message: '"Old" сan be removed' }],
},
{
code: 'scalar Old @deprecated(untilDate: "22/08/2021")',
options: [{ argumentName: 'untilDate' }],
errors: [{ message: '"Old" сan be removed.' }],
errors: [{ message: '"Old" сan be removed' }],
},
{
code: 'scalar Old @deprecated(deletionDate: "bad")',
errors: [{ message: 'Deletion date must be in format "DD/MM/YYYY".' }],
errors: [{ message: 'Deletion date must be in format "DD/MM/YYYY"' }],
},
{
code: 'scalar Old @deprecated(deletionDate: "32/08/2021")',
errors: [{ message: 'Invalid "32/08/2021" deletion date.' }],
errors: [{ message: 'Invalid "32/08/2021" deletion date' }],
},
{
code: 'type Old { oldField: ID @deprecated }',
errors: [{ message: 'Directive "@deprecated" must have a deletion date' }],
}
],
});

0 comments on commit 578b890

Please sign in to comment.