Skip to content

Commit 1b639e3

Browse files
committed
Improve validation error for unused variable
1 parent 71b7b4a commit 1b639e3

File tree

2 files changed

+22
-16
lines changed

2 files changed

+22
-16
lines changed

src/validation/__tests__/NoUnusedVariables.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ import {
1515
} from '../rules/NoUnusedVariables';
1616

1717

18-
function unusedVar(varName, line, column) {
18+
function unusedVar(varName, opName, line, column) {
1919
return {
20-
message: unusedVariableMessage(varName),
20+
message: unusedVariableMessage(varName, opName),
2121
locations: [ { line, column } ],
2222
};
2323
}
@@ -26,7 +26,7 @@ describe('Validate: No unused variables', () => {
2626

2727
it('uses all variables', () => {
2828
expectPassesRule(NoUnusedVariables, `
29-
query Foo($a: String, $b: String, $c: String) {
29+
query ($a: String, $b: String, $c: String) {
3030
field(a: $a, b: $b, c: $c)
3131
}
3232
`);
@@ -113,11 +113,11 @@ describe('Validate: No unused variables', () => {
113113

114114
it('variable not used', () => {
115115
expectFailsRule(NoUnusedVariables, `
116-
query Foo($a: String, $b: String, $c: String) {
116+
query ($a: String, $b: String, $c: String) {
117117
field(a: $a, b: $b)
118118
}
119119
`, [
120-
unusedVar('c', 2, 41)
120+
unusedVar('c', null, 2, 38)
121121
]);
122122
});
123123

@@ -127,8 +127,8 @@ describe('Validate: No unused variables', () => {
127127
field(b: $b)
128128
}
129129
`, [
130-
unusedVar('a', 2, 17),
131-
unusedVar('c', 2, 41)
130+
unusedVar('a', 'Foo', 2, 17),
131+
unusedVar('c', 'Foo', 2, 41)
132132
]);
133133
});
134134

@@ -151,7 +151,7 @@ describe('Validate: No unused variables', () => {
151151
field
152152
}
153153
`, [
154-
unusedVar('c', 2, 41)
154+
unusedVar('c', 'Foo', 2, 41)
155155
]);
156156
});
157157

@@ -174,8 +174,8 @@ describe('Validate: No unused variables', () => {
174174
field
175175
}
176176
`, [
177-
unusedVar('a', 2, 17),
178-
unusedVar('c', 2, 41)
177+
unusedVar('a', 'Foo', 2, 17),
178+
unusedVar('c', 'Foo', 2, 41)
179179
]);
180180
});
181181

@@ -191,7 +191,7 @@ describe('Validate: No unused variables', () => {
191191
field(b: $b)
192192
}
193193
`, [
194-
unusedVar('b', 2, 17)
194+
unusedVar('b', 'Foo', 2, 17)
195195
]);
196196
});
197197

@@ -210,8 +210,8 @@ describe('Validate: No unused variables', () => {
210210
field(b: $b)
211211
}
212212
`, [
213-
unusedVar('b', 2, 17),
214-
unusedVar('a', 5, 17)
213+
unusedVar('b', 'Foo', 2, 17),
214+
unusedVar('a', 'Bar', 5, 17)
215215
]);
216216
});
217217

src/validation/rules/NoUnusedVariables.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ import type { ValidationContext } from '../index';
1212
import { GraphQLError } from '../../error';
1313

1414

15-
export function unusedVariableMessage(varName: string): string {
16-
return `Variable "$${varName}" is never used.`;
15+
export function unusedVariableMessage(
16+
varName: string,
17+
opName: ?string
18+
): string {
19+
return opName ?
20+
`Variable "$${varName}" is never used in operation "${opName}".` :
21+
`Variable "$${varName}" is never used.`;
1722
}
1823

1924
/**
@@ -33,6 +38,7 @@ export function NoUnusedVariables(context: ValidationContext): any {
3338
leave(operation) {
3439
const variableNameUsed = Object.create(null);
3540
const usages = context.getRecursiveVariableUsages(operation);
41+
const opName = operation.name ? operation.name.value : null;
3642

3743
usages.forEach(({ node }) => {
3844
variableNameUsed[node.name.value] = true;
@@ -42,7 +48,7 @@ export function NoUnusedVariables(context: ValidationContext): any {
4248
const variableName = variableDef.variable.name.value;
4349
if (variableNameUsed[variableName] !== true) {
4450
context.reportError(new GraphQLError(
45-
unusedVariableMessage(variableName),
51+
unusedVariableMessage(variableName, opName),
4652
[ variableDef ]
4753
));
4854
}

0 commit comments

Comments
 (0)