Skip to content

Commit cdf9c83

Browse files
suggestionList: lexicographically sort options with the same le… (#2394)
1 parent 744c20d commit cdf9c83

File tree

5 files changed

+29
-20
lines changed

5 files changed

+29
-20
lines changed

src/jsutils/__tests__/suggestionList-test.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,18 @@ describe('suggestionList', () => {
1414
expect(suggestionList('input', [])).to.deep.equal([]);
1515
});
1616

17-
it('Returns options sorted based on similarity', () => {
17+
it('Returns options sorted based on lexical distance', () => {
1818
expect(suggestionList('abc', ['a', 'ab', 'abc'])).to.deep.equal([
1919
'abc',
2020
'ab',
2121
]);
2222
});
23+
24+
it('Returns options with the same lexical distance sorted lexicographically', () => {
25+
expect(suggestionList('a', ['az', 'ax', 'ay'])).to.deep.equal([
26+
'ax',
27+
'ay',
28+
'az',
29+
]);
30+
});
2331
});

src/jsutils/suggestionList.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ export default function suggestionList(
1717
optionsByDistance[option] = distance;
1818
}
1919
}
20-
return Object.keys(optionsByDistance).sort(
21-
(a, b) => optionsByDistance[a] - optionsByDistance[b],
22-
);
20+
return Object.keys(optionsByDistance).sort((a, b) => {
21+
const diff = optionsByDistance[a] - optionsByDistance[b];
22+
return diff !== 0 ? diff : a.localeCompare(b);
23+
});
2324
}
2425

2526
/**

src/validation/__tests__/FieldsOnCorrectType-test.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -293,34 +293,34 @@ describe('Validate: Fields on correct type', () => {
293293
it('Works with no small numbers of field suggestions', () => {
294294
const schema = buildSchema(`
295295
type T {
296-
z: String
297296
y: String
297+
z: String
298298
}
299299
type Query { t: T }
300300
`);
301301

302302
expectErrorMessage(schema, '{ t { f } }').to.equal(
303-
'Cannot query field "f" on type "T". Did you mean "z" or "y"?',
303+
'Cannot query field "f" on type "T". Did you mean "y" or "z"?',
304304
);
305305
});
306306

307307
it('Only shows one set of suggestions at a time, preferring types', () => {
308308
const schema = buildSchema(`
309309
interface T {
310-
z: String
311310
y: String
311+
z: String
312312
}
313313
type Query { t: T }
314314
315315
type A implements T {
316316
f: String
317-
z: String
318317
y: String
318+
z: String
319319
}
320320
type B implements T {
321321
f: String
322-
z: String
323322
y: String
323+
z: String
324324
}
325325
`);
326326

@@ -350,18 +350,18 @@ describe('Validate: Fields on correct type', () => {
350350
it('Limits lots of field suggestions', () => {
351351
const schema = buildSchema(`
352352
type T {
353-
z: String
354-
y: String
355-
x: String
356-
w: String
357-
v: String
358353
u: String
354+
v: String
355+
w: String
356+
x: String
357+
y: String
358+
z: String
359359
}
360360
type Query { t: T }
361361
`);
362362

363363
expectErrorMessage(schema, '{ t { f } }').to.equal(
364-
'Cannot query field "f" on type "T". Did you mean "z", "y", "x", "w", or "v"?',
364+
'Cannot query field "f" on type "T". Did you mean "u", "v", "w", "x", or "y"?',
365365
);
366366
});
367367
});

src/validation/__tests__/KnownTypeNames-test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ describe('Validate: Known type names', () => {
176176
locations: [{ line: 5, column: 36 }],
177177
},
178178
{
179-
message: 'Unknown type "D". Did you mean "ID", "A", or "B"?',
179+
message: 'Unknown type "D". Did you mean "A", "B", or "ID"?',
180180
locations: [{ line: 6, column: 16 }],
181181
},
182182
{
@@ -196,7 +196,7 @@ describe('Validate: Known type names', () => {
196196
locations: [{ line: 12, column: 16 }],
197197
},
198198
{
199-
message: 'Unknown type "I". Did you mean "ID", "A", or "B"?',
199+
message: 'Unknown type "I". Did you mean "A", "B", or "ID"?',
200200
locations: [{ line: 12, column: 20 }],
201201
},
202202
{
@@ -306,7 +306,7 @@ describe('Validate: Known type names', () => {
306306
locations: [{ line: 4, column: 36 }],
307307
},
308308
{
309-
message: 'Unknown type "D". Did you mean "ID", "A", or "B"?',
309+
message: 'Unknown type "D". Did you mean "A", "B", or "ID"?',
310310
locations: [{ line: 5, column: 16 }],
311311
},
312312
{
@@ -326,7 +326,7 @@ describe('Validate: Known type names', () => {
326326
locations: [{ line: 11, column: 16 }],
327327
},
328328
{
329-
message: 'Unknown type "I". Did you mean "ID", "A", or "B"?',
329+
message: 'Unknown type "I". Did you mean "A", "B", or "ID"?',
330330
locations: [{ line: 11, column: 20 }],
331331
},
332332
{

src/validation/__tests__/ValuesOfCorrectType-test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,7 @@ describe('Validate: Values of correct type', () => {
930930
`).to.deep.equal([
931931
{
932932
message:
933-
'Field "unknownField" is not defined by type "ComplexInput". Did you mean "nonNullField", "intField", or "booleanField"?',
933+
'Field "unknownField" is not defined by type "ComplexInput". Did you mean "booleanField", "intField", or "nonNullField"?',
934934
locations: [{ line: 6, column: 15 }],
935935
},
936936
]);

0 commit comments

Comments
 (0)