Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix error report for require-deprecation-date rule #740

Merged
merged 2 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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' }],
}
],
});