Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OverlappingFieldsCanBeMerged: sort argument values before comparing (#…
- Loading branch information
1 parent
0a654cc
commit 40c160e
Showing
5 changed files
with
138 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { parseValue } from '../../language/parser'; | ||
import { print } from '../../language/printer'; | ||
|
||
import { sortValueNode } from '../sortValueNode'; | ||
|
||
describe('sortValueNode', () => { | ||
function expectSortedValue(source: string) { | ||
return expect(print(sortValueNode(parseValue(source)))); | ||
} | ||
|
||
it('do not change non-object values', () => { | ||
expectSortedValue('1').to.equal('1'); | ||
expectSortedValue('3.14').to.equal('3.14'); | ||
expectSortedValue('null').to.equal('null'); | ||
expectSortedValue('true').to.equal('true'); | ||
expectSortedValue('false').to.equal('false'); | ||
expectSortedValue('"cba"').to.equal('"cba"'); | ||
expectSortedValue('"""cba"""').to.equal('"""cba"""'); | ||
expectSortedValue('[1, 3.14, null, false, "cba"]').to.equal( | ||
'[1, 3.14, null, false, "cba"]', | ||
); | ||
expectSortedValue('[[1, 3.14, null, false, "cba"]]').to.equal( | ||
'[[1, 3.14, null, false, "cba"]]', | ||
); | ||
}); | ||
|
||
it('sort input object fields', () => { | ||
expectSortedValue('{ b: 2, a: 1 }').to.equal('{a: 1, b: 2}'); | ||
expectSortedValue('{ a: { c: 3, b: 2 } }').to.equal('{a: {b: 2, c: 3}}'); | ||
expectSortedValue('[{ b: 2, a: 1 }, { d: 4, c: 3}]').to.equal( | ||
'[{a: 1, b: 2}, {c: 3, d: 4}]', | ||
); | ||
expectSortedValue( | ||
'{ b: { g: 7, f: 6 }, c: 3 , a: { d: 4, e: 5 } }', | ||
).to.equal('{a: {d: 4, e: 5}, b: {f: 6, g: 7}, c: 3}'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { naturalCompare } from '../jsutils/naturalCompare'; | ||
|
||
import type { ObjectFieldNode, ValueNode } from '../language/ast'; | ||
import { Kind } from '../language/kinds'; | ||
|
||
/** | ||
* Sort ValueNode. | ||
* | ||
* This function returns a sorted copy of the given ValueNode. | ||
* | ||
* @internal | ||
*/ | ||
export function sortValueNode(valueNode: ValueNode): ValueNode { | ||
switch (valueNode.kind) { | ||
case Kind.OBJECT: | ||
return { | ||
...valueNode, | ||
fields: sortFields(valueNode.fields), | ||
}; | ||
case Kind.LIST: | ||
return { | ||
...valueNode, | ||
values: valueNode.values.map(sortValueNode), | ||
}; | ||
case Kind.INT: | ||
case Kind.FLOAT: | ||
case Kind.STRING: | ||
case Kind.BOOLEAN: | ||
case Kind.NULL: | ||
case Kind.ENUM: | ||
case Kind.VARIABLE: | ||
return valueNode; | ||
} | ||
} | ||
|
||
function sortFields( | ||
fields: ReadonlyArray<ObjectFieldNode>, | ||
): Array<ObjectFieldNode> { | ||
return fields | ||
.map((fieldNode) => ({ | ||
...fieldNode, | ||
value: sortValueNode(fieldNode.value), | ||
})) | ||
.sort((fieldA, fieldB) => | ||
naturalCompare(fieldA.name.value, fieldB.name.value), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters