Skip to content

Commit eb4a851

Browse files
author
Dimitri POSTOLOV
authored
fix: adjust report location for require-description rule (#722)
1 parent a5c430a commit eb4a851

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

.changeset/twelve-grapes-provide.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-eslint/eslint-plugin': patch
3+
---
4+
5+
fix: adjust report location for `require-description` rule

packages/plugin/src/rules/avoid-operation-name-prefix.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,16 @@ const rule: GraphQLESLintRule<AvoidOperationNamePrefixConfig> = {
7878
const testName = caseSensitive ? node.name.value : node.name.value.toLowerCase();
7979

8080
if (testName.startsWith(testKeyword)) {
81+
const { start } = node.name.loc;
8182
context.report({
8283
loc: {
8384
start: {
84-
line: node.name.loc.start.line,
85-
column: node.name.loc.start.column - 1,
85+
line: start.line,
86+
column: start.column - 1,
8687
},
8788
end: {
88-
line: node.name.loc.start.line,
89-
column: node.name.loc.start.column + testKeyword.length - 1,
89+
line: start.line,
90+
column: start.column - 1 + testKeyword.length,
9091
},
9192
},
9293
data: {

packages/plugin/src/rules/require-description.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types';
1+
import { GraphQLESLintRule, GraphQLESLintRuleContext, ValueOf } from '../types';
22
import { GraphQLESTreeNode } from '../estree-parser/estree-ast';
3-
import { ASTNode, Kind, StringValueNode } from 'graphql';
3+
import { ASTKindToNode, Kind, StringValueNode } from 'graphql';
44

55
const REQUIRE_DESCRIPTION_ERROR = 'REQUIRE_DESCRIPTION_ERROR';
66
const DESCRIBABLE_NODES = [
@@ -17,23 +17,30 @@ const DESCRIBABLE_NODES = [
1717
];
1818
type RequireDescriptionRuleConfig = [{ on: typeof DESCRIBABLE_NODES }];
1919

20+
type AllowedKind = typeof DESCRIBABLE_NODES[number];
21+
type AllowedKindToNode = Pick<ASTKindToNode, AllowedKind>;
22+
2023
function verifyRule(
2124
context: GraphQLESLintRuleContext<RequireDescriptionRuleConfig>,
22-
node: GraphQLESTreeNode<ASTNode> & {
25+
node: GraphQLESTreeNode<ValueOf<AllowedKindToNode>> & {
2326
readonly description?: GraphQLESTreeNode<StringValueNode>;
2427
}
2528
) {
2629
if (node) {
2730
if (!node.description || !node.description.value || node.description.value.trim().length === 0) {
31+
const { start, end } = ('name' in node ? node.name : node).loc;
32+
2833
context.report({
2934
loc: {
3035
start: {
31-
line: node.loc.start.line,
32-
column: node.loc.start.column - 1,
36+
line: start.line,
37+
column: start.column - 1,
3338
},
3439
end: {
35-
line: node.loc.end.line,
36-
column: node.loc.end.column,
40+
line: end.line,
41+
column:
42+
// node.name don't exist on SchemaDefinition
43+
'name' in node ? end.column - 1 + node.name.value.length : end.column,
3744
},
3845
},
3946
messageId: REQUIRE_DESCRIPTION_ERROR,

packages/plugin/src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,5 @@ export type GraphQLESLintRule<Options = any[], WithTypeInfo extends boolean = fa
6666
create(context: GraphQLESLintRuleContext<Options>): GraphQLESLintRuleListener<WithTypeInfo>;
6767
meta: Rule.RuleMetaData & RuleDocsInfo<Options>;
6868
};
69+
70+
export type ValueOf<T> = T[keyof T];

0 commit comments

Comments
 (0)