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 strict-id-in-types and naming-convention rule #744

Merged
merged 1 commit 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/silent-files-build.md
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix error report for `strict-id-in-types` and `naming-convention` rule
14 changes: 10 additions & 4 deletions packages/plugin/src/rules/naming-convention.ts
@@ -1,6 +1,6 @@
import { Kind } from 'graphql';
import { GraphQLESLintRule } from '../types';
import { isQueryType } from '../utils';
import { getLocation, isQueryType } from '../utils';

const formats = {
camelCase: /^[a-z][^_]*$/g,
Expand Down Expand Up @@ -246,7 +246,7 @@ const rule: GraphQLESLintRule<NamingConventionRuleConfig> = {
});
if (result.ok === false) {
context.report({
node,
loc: getLocation(node.loc, node.value),
message: result.errorMessage,
data: {
prefix,
Expand Down Expand Up @@ -275,10 +275,16 @@ const rule: GraphQLESLintRule<NamingConventionRuleConfig> = {
return {
Name: node => {
if (node.value.startsWith('_') && options.leadingUnderscore === 'forbid') {
context.report({ node, message: 'Leading underscores are not allowed' });
context.report({
loc: getLocation(node.loc, node.value),
message: 'Leading underscores are not allowed',
});
}
if (node.value.endsWith('_') && options.trailingUnderscore === 'forbid') {
context.report({ node, message: 'Trailing underscores are not allowed' });
context.report({
loc: getLocation(node.loc, node.value),
message: 'Trailing underscores are not allowed',
});
}
},
ObjectTypeDefinition: node => {
Expand Down
10 changes: 5 additions & 5 deletions packages/plugin/src/rules/strict-id-in-types.ts
@@ -1,6 +1,7 @@
import { Kind, ObjectTypeDefinitionNode } from 'graphql';
import { GraphQLESTreeNode } from '../estree-parser';
import { GraphQLESLintRule } from '../types';
import { getLocation } from '../utils';

export interface ExceptionRule {
types?: string[];
Expand Down Expand Up @@ -171,17 +172,16 @@ const rule: GraphQLESLintRule<StrictIdInTypesRuleConfig> = {

return isValidIdName && isValidIdType;
});

const typeName = node.name.value;
// Usually, there should be only one unique identifier field per type.
// Some clients allow multiple fields to be used. If more people need this,
// we can extend this rule later.
if (validIds.length !== 1) {
context.report({
node,
message:
'{{nodeName}} must have exactly one non-nullable unique identifier. Accepted name(s): {{acceptedNamesString}} ; Accepted type(s): {{acceptedTypesString}}',
loc: getLocation(node.name.loc, typeName),
message: `{{ typeName }} must have exactly one non-nullable unique identifier. Accepted name(s): {{ acceptedNamesString }} ; Accepted type(s): {{ acceptedTypesString }}`,
data: {
nodeName: node.name.value,
typeName,
acceptedNamesString: options.acceptedIdNames.join(','),
acceptedTypesString: options.acceptedIdTypes.join(','),
},
Expand Down