Skip to content

Commit

Permalink
fix: adjust report location for require-description rule (#722)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitri POSTOLOV committed Oct 27, 2021
1 parent a5c430a commit eb4a851
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/twelve-grapes-provide.md
@@ -0,0 +1,5 @@
---
'@graphql-eslint/eslint-plugin': patch
---

fix: adjust report location for `require-description` rule
9 changes: 5 additions & 4 deletions packages/plugin/src/rules/avoid-operation-name-prefix.ts
Expand Up @@ -78,15 +78,16 @@ const rule: GraphQLESLintRule<AvoidOperationNamePrefixConfig> = {
const testName = caseSensitive ? node.name.value : node.name.value.toLowerCase();

if (testName.startsWith(testKeyword)) {
const { start } = node.name.loc;
context.report({
loc: {
start: {
line: node.name.loc.start.line,
column: node.name.loc.start.column - 1,
line: start.line,
column: start.column - 1,
},
end: {
line: node.name.loc.start.line,
column: node.name.loc.start.column + testKeyword.length - 1,
line: start.line,
column: start.column - 1 + testKeyword.length,
},
},
data: {
Expand Down
21 changes: 14 additions & 7 deletions packages/plugin/src/rules/require-description.ts
@@ -1,6 +1,6 @@
import { GraphQLESLintRule, GraphQLESLintRuleContext } from '../types';
import { GraphQLESLintRule, GraphQLESLintRuleContext, ValueOf } from '../types';
import { GraphQLESTreeNode } from '../estree-parser/estree-ast';
import { ASTNode, Kind, StringValueNode } from 'graphql';
import { ASTKindToNode, Kind, StringValueNode } from 'graphql';

const REQUIRE_DESCRIPTION_ERROR = 'REQUIRE_DESCRIPTION_ERROR';
const DESCRIBABLE_NODES = [
Expand All @@ -17,23 +17,30 @@ const DESCRIBABLE_NODES = [
];
type RequireDescriptionRuleConfig = [{ on: typeof DESCRIBABLE_NODES }];

type AllowedKind = typeof DESCRIBABLE_NODES[number];
type AllowedKindToNode = Pick<ASTKindToNode, AllowedKind>;

function verifyRule(
context: GraphQLESLintRuleContext<RequireDescriptionRuleConfig>,
node: GraphQLESTreeNode<ASTNode> & {
node: GraphQLESTreeNode<ValueOf<AllowedKindToNode>> & {
readonly description?: GraphQLESTreeNode<StringValueNode>;
}
) {
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: node.loc.start.line,
column: node.loc.start.column - 1,
line: start.line,
column: start.column - 1,
},
end: {
line: node.loc.end.line,
column: node.loc.end.column,
line: end.line,
column:
// node.name don't exist on SchemaDefinition
'name' in node ? end.column - 1 + node.name.value.length : end.column,
},
},
messageId: REQUIRE_DESCRIPTION_ERROR,
Expand Down
2 changes: 2 additions & 0 deletions packages/plugin/src/types.ts
Expand Up @@ -66,3 +66,5 @@ export type GraphQLESLintRule<Options = any[], WithTypeInfo extends boolean = fa
create(context: GraphQLESLintRuleContext<Options>): GraphQLESLintRuleListener<WithTypeInfo>;
meta: Rule.RuleMetaData & RuleDocsInfo<Options>;
};

export type ValueOf<T> = T[keyof T];

0 comments on commit eb4a851

Please sign in to comment.