Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggestionList: lexicographically sort options with the same lexical distance #2394

Merged
merged 1 commit into from
Jan 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/jsutils/__tests__/suggestionList-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,18 @@ describe('suggestionList', () => {
expect(suggestionList('input', [])).to.deep.equal([]);
});

it('Returns options sorted based on similarity', () => {
it('Returns options sorted based on lexical distance', () => {
expect(suggestionList('abc', ['a', 'ab', 'abc'])).to.deep.equal([
'abc',
'ab',
]);
});

it('Returns options with the same lexical distance sorted lexicographically', () => {
expect(suggestionList('a', ['az', 'ax', 'ay'])).to.deep.equal([
'ax',
'ay',
'az',
]);
});
});
7 changes: 4 additions & 3 deletions src/jsutils/suggestionList.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ export default function suggestionList(
optionsByDistance[option] = distance;
}
}
return Object.keys(optionsByDistance).sort(
(a, b) => optionsByDistance[a] - optionsByDistance[b],
);
return Object.keys(optionsByDistance).sort((a, b) => {
const diff = optionsByDistance[a] - optionsByDistance[b];
return diff !== 0 ? diff : a.localeCompare(b);
});
}

/**
Expand Down
22 changes: 11 additions & 11 deletions src/validation/__tests__/FieldsOnCorrectType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -293,34 +293,34 @@ describe('Validate: Fields on correct type', () => {
it('Works with no small numbers of field suggestions', () => {
const schema = buildSchema(`
type T {
z: String
y: String
z: String
}
type Query { t: T }
`);

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

it('Only shows one set of suggestions at a time, preferring types', () => {
const schema = buildSchema(`
interface T {
z: String
y: String
z: String
}
type Query { t: T }

type A implements T {
f: String
z: String
y: String
z: String
}
type B implements T {
f: String
z: String
y: String
z: String
}
`);

Expand Down Expand Up @@ -350,18 +350,18 @@ describe('Validate: Fields on correct type', () => {
it('Limits lots of field suggestions', () => {
const schema = buildSchema(`
type T {
z: String
y: String
x: String
w: String
v: String
u: String
v: String
w: String
x: String
y: String
z: String
}
type Query { t: T }
`);

expectErrorMessage(schema, '{ t { f } }').to.equal(
'Cannot query field "f" on type "T". Did you mean "z", "y", "x", "w", or "v"?',
'Cannot query field "f" on type "T". Did you mean "u", "v", "w", "x", or "y"?',
);
});
});
Expand Down
8 changes: 4 additions & 4 deletions src/validation/__tests__/KnownTypeNames-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ describe('Validate: Known type names', () => {
locations: [{ line: 5, column: 36 }],
},
{
message: 'Unknown type "D". Did you mean "ID", "A", or "B"?',
message: 'Unknown type "D". Did you mean "A", "B", or "ID"?',
locations: [{ line: 6, column: 16 }],
},
{
Expand All @@ -196,7 +196,7 @@ describe('Validate: Known type names', () => {
locations: [{ line: 12, column: 16 }],
},
{
message: 'Unknown type "I". Did you mean "ID", "A", or "B"?',
message: 'Unknown type "I". Did you mean "A", "B", or "ID"?',
locations: [{ line: 12, column: 20 }],
},
{
Expand Down Expand Up @@ -306,7 +306,7 @@ describe('Validate: Known type names', () => {
locations: [{ line: 4, column: 36 }],
},
{
message: 'Unknown type "D". Did you mean "ID", "A", or "B"?',
message: 'Unknown type "D". Did you mean "A", "B", or "ID"?',
locations: [{ line: 5, column: 16 }],
},
{
Expand All @@ -326,7 +326,7 @@ describe('Validate: Known type names', () => {
locations: [{ line: 11, column: 16 }],
},
{
message: 'Unknown type "I". Did you mean "ID", "A", or "B"?',
message: 'Unknown type "I". Did you mean "A", "B", or "ID"?',
locations: [{ line: 11, column: 20 }],
},
{
Expand Down
2 changes: 1 addition & 1 deletion src/validation/__tests__/ValuesOfCorrectType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,7 @@ describe('Validate: Values of correct type', () => {
`).to.deep.equal([
{
message:
'Field "unknownField" is not defined by type "ComplexInput". Did you mean "nonNullField", "intField", or "booleanField"?',
'Field "unknownField" is not defined by type "ComplexInput". Did you mean "booleanField", "intField", or "nonNullField"?',
locations: [{ line: 6, column: 15 }],
},
]);
Expand Down