Skip to content

Commit

Permalink
fix error report for selection-set-depth rule and for graphql-js
Browse files Browse the repository at this point in the history
…rules (#747)
  • Loading branch information
Dimitri POSTOLOV committed Oct 31, 2021
1 parent cf3cc4f commit d081fcc
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 58 deletions.
5 changes: 5 additions & 0 deletions .changeset/purple-schools-complain.md
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix error report for `selection-set-depth` rule and for `graphql-js` rules
14 changes: 2 additions & 12 deletions packages/plugin/src/rules/avoid-typename-prefix.ts
Expand Up @@ -6,6 +6,7 @@ import {
} from 'graphql';
import { GraphQLESTreeNode } from '../estree-parser';
import { GraphQLESLintRule } from '../types';
import { getLocation } from '../utils';

const AVOID_TYPENAME_PREFIX = 'AVOID_TYPENAME_PREFIX';

Expand Down Expand Up @@ -55,24 +56,13 @@ const rule: GraphQLESLintRule = {
const fieldName = field.name.value;

if (fieldName.toLowerCase().startsWith(lowerTypeName)) {
const { start } = field.loc;

context.report({
data: {
fieldName,
typeName,
},
messageId: AVOID_TYPENAME_PREFIX,
loc: {
start: {
line: start.line,
column: start.column - 1,
},
end: {
line: start.line,
column: start.column - 1 + lowerTypeName.length,
},
},
loc: getLocation(field.loc, lowerTypeName),
});
}
}
Expand Down
5 changes: 2 additions & 3 deletions packages/plugin/src/rules/graphql-js-validation.ts
Expand Up @@ -4,7 +4,7 @@ import { parseImportLine, processImport } from '@graphql-tools/import';
import { existsSync } from 'fs';
import { join, dirname } from 'path';
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types';
import { requireGraphQLSchemaFromContext, requireSiblingsOperations } from '../utils';
import { getLocation, requireGraphQLSchemaFromContext, requireSiblingsOperations } from '../utils';
import { GraphQLESTreeNode } from '../estree-parser';

function extractRuleName(stack?: string): string | null {
Expand All @@ -26,9 +26,8 @@ export function validateDoc(

for (const error of validationErrors) {
const validateRuleName = ruleName || `[${extractRuleName(error.stack)}]`;

context.report({
loc: error.locations[0],
loc: getLocation({ start: error.locations[0] }),
message: ruleName ? error.message : `${validateRuleName} ${error.message}`,
});
}
Expand Down
30 changes: 9 additions & 21 deletions packages/plugin/src/rules/no-anonymous-operations.ts
@@ -1,4 +1,5 @@
import { GraphQLESLintRule } from '../types';
import { getLocation } from '../utils';

const NO_ANONYMOUS_OPERATIONS = 'NO_ANONYMOUS_OPERATIONS';

Expand Down Expand Up @@ -37,27 +38,14 @@ const rule: GraphQLESLintRule = {
},
create(context) {
return {
OperationDefinition(node) {
const isAnonymous = (node.name?.value || '').length === 0;
if (isAnonymous) {
const { start } = node.loc;
context.report({
loc: {
start: {
column: start.column - 1,
line: start.line,
},
end: {
column: start.column - 1 + node.operation.length,
line: start.line,
},
},
data: {
operation: node.operation,
},
messageId: NO_ANONYMOUS_OPERATIONS,
});
}
'OperationDefinition[name=undefined]'(node) {
context.report({
loc: getLocation(node.loc, node.operation),
data: {
operation: node.operation,
},
messageId: NO_ANONYMOUS_OPERATIONS,
});
},
};
},
Expand Down
16 changes: 2 additions & 14 deletions packages/plugin/src/rules/require-description.ts
@@ -1,6 +1,7 @@
import { GraphQLESLintRule, GraphQLESLintRuleContext, ValueOf } from '../types';
import { GraphQLESTreeNode } from '../estree-parser/estree-ast';
import { ASTKindToNode, Kind, StringValueNode } from 'graphql';
import { getLocation } from '../utils';

const REQUIRE_DESCRIPTION_ERROR = 'REQUIRE_DESCRIPTION_ERROR';
const DESCRIBABLE_NODES = [
Expand Down Expand Up @@ -28,21 +29,8 @@ function verifyRule(
) {
if (node) {
if (!node.description || !node.description.value || node.description.value.trim().length === 0) {
const { start, end } = ('name' in node ? node.name : node).loc;

context.report({
loc: {
start: {
line: start.line,
column: start.column - 1,
},
end: {
line: end.line,
column:
// node.name don't exist on SchemaDefinition
'name' in node ? end.column - 1 + node.name.value.length : end.column,
},
},
loc: getLocation(('name' in node ? node.name : node).loc, 'name' in node ? node.name.value : 'schema'),
messageId: REQUIRE_DESCRIPTION_ERROR,
data: {
nodeType: node.kind,
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/rules/selection-set-depth.ts
Expand Up @@ -2,7 +2,7 @@ import { GraphQLESLintRule } from '../types';
import depthLimit from 'graphql-depth-limit';
import { DocumentNode, FragmentDefinitionNode, GraphQLError, Kind, OperationDefinitionNode } from 'graphql';
import { GraphQLESTreeNode } from '../estree-parser';
import { requireSiblingsOperations } from '../utils';
import { getLocation, requireSiblingsOperations } from '../utils';
import { SiblingOperations } from '../sibling-operations';

type SelectionSetDepthRuleConfig = [{ maxDepth: number; ignore?: string[] }];
Expand Down Expand Up @@ -111,7 +111,7 @@ const rule: GraphQLESLintRule<SelectionSetDepthRuleConfig> = {
getDocument: () => document,
reportError: (error: GraphQLError) => {
context.report({
loc: error.locations[0],
loc: getLocation({ start: error.locations[0] }),
message: error.message,
});
},
Expand Down
Expand Up @@ -2,5 +2,5 @@

exports[` 1`] = `
> 1 | type Query { t: String }
| ^ The "Query" definition is not executable.
| ^ The "Query" definition is not executable.
`;
Expand Up @@ -24,7 +24,7 @@ exports[` 1`] = `
21 | }
22 |
> 23 | schema {
| ^ Must provide only one schema definition.
| ^ Must provide only one schema definition.
24 | query: RootQuery
25 | mutation: RootMutation
26 | }
Expand Down
Expand Up @@ -7,7 +7,7 @@ exports[` 1`] = `
4 | }
5 |
> 6 | extend type OtherUser {
| ^ Cannot extend type "OtherUser" because it is not defined.
| ^ Cannot extend type "OtherUser" because it is not defined.
7 | name: String!
8 | }
9 |
Expand Down
Expand Up @@ -6,7 +6,7 @@ exports[` 1`] = `
3 | viewer {
4 | albums {
> 5 | title
| ^ 'deep2' exceeds maximum operation depth of 1
| ^ 'deep2' exceeds maximum operation depth of 1
6 | }
7 | }
8 | }
Expand All @@ -19,7 +19,7 @@ exports[` 2`] = `
3 | viewer {
4 | albums {
> 5 | ...AlbumFields
| ^ 'deep2' exceeds maximum operation depth of 1
| ^ 'deep2' exceeds maximum operation depth of 1
6 | }
7 | }
8 | }
Expand Down
Expand Up @@ -3,7 +3,7 @@
exports[` 1`] = `
1 |
> 2 | type Query {
| ^ There can be only one type named "Query".
| ^ There can be only one type named "Query".
3 | foo: String
4 | }
5 |
Expand Down

0 comments on commit d081fcc

Please sign in to comment.