Skip to content

Commit 08bccc6

Browse files
committed
abstract into function
1 parent 54e1e06 commit 08bccc6

File tree

1 file changed

+59
-68
lines changed

1 file changed

+59
-68
lines changed

src/validation/rules/OverlappingFieldsCanBeMergedRule.ts

+59-68
Original file line numberDiff line numberDiff line change
@@ -207,26 +207,15 @@ function findConflictsWithinSelectionSet(
207207
discoveredFragments,
208208
);
209209

210-
// (E) Then collect any conflicts between the provided collection of fields
211-
// and any fragment names found in the given fragment.
212-
while (discoveredFragments.length !== 0) {
213-
const item = discoveredFragments.pop();
214-
if (!item || comparedFragmentPairs.has(item[1], item[0], false)) {
215-
continue;
216-
}
217-
const [fragmentName, referencedFragmentName] = item;
218-
comparedFragmentPairs.add(referencedFragmentName, fragmentName, false);
219-
collectConflictsBetweenFieldsAndFragment(
220-
context,
221-
conflicts,
222-
cachedFieldsAndFragmentNames,
223-
comparedFragmentPairs,
224-
false,
225-
fieldMap,
226-
referencedFragmentName,
227-
discoveredFragments,
228-
);
229-
}
210+
processDiscoveredFragments(
211+
context,
212+
conflicts,
213+
cachedFieldsAndFragmentNames,
214+
comparedFragmentPairs,
215+
false,
216+
fieldMap,
217+
discoveredFragments,
218+
)
230219
// (C) Then compare this fragment with all other fragments found in this
231220
// selection set to collect conflicts between fragments spread together.
232221
// This compares each item in the list of fragment names to every other
@@ -439,33 +428,15 @@ function findConflictsBetweenSubSelectionSets(
439428
);
440429
}
441430

442-
// (E) Then collect any conflicts between the provided collection of fields
443-
// and any fragment names found in the given fragment.
444-
while (discoveredFragments.length !== 0) {
445-
const item = discoveredFragments.pop();
446-
if (
447-
!item ||
448-
comparedFragmentPairs.has(item[1], item[0], areMutuallyExclusive)
449-
) {
450-
continue;
451-
}
452-
const [fragmentName, referencedFragmentName] = item;
453-
comparedFragmentPairs.add(
454-
referencedFragmentName,
455-
fragmentName,
456-
areMutuallyExclusive,
457-
);
458-
collectConflictsBetweenFieldsAndFragment(
459-
context,
460-
conflicts,
461-
cachedFieldsAndFragmentNames,
462-
comparedFragmentPairs,
463-
areMutuallyExclusive,
464-
fieldMap1,
465-
referencedFragmentName,
466-
discoveredFragments,
467-
);
468-
}
431+
processDiscoveredFragments(
432+
context,
433+
conflicts,
434+
cachedFieldsAndFragmentNames,
435+
comparedFragmentPairs,
436+
areMutuallyExclusive,
437+
fieldMap1,
438+
discoveredFragments,
439+
)
469440

470441
// (I) Then collect conflicts between the second collection of fields and
471442
// those referenced by each fragment name associated with the first.
@@ -482,8 +453,46 @@ function findConflictsBetweenSubSelectionSets(
482453
);
483454
}
484455

485-
// (E) Then collect any conflicts between the provided collection of fields
486-
// and any fragment names found in the given fragment.
456+
processDiscoveredFragments(
457+
context,
458+
conflicts,
459+
cachedFieldsAndFragmentNames,
460+
comparedFragmentPairs,
461+
areMutuallyExclusive,
462+
fieldMap2,
463+
discoveredFragments,
464+
)
465+
466+
// (J) Also collect conflicts between any fragment names by the first and
467+
// fragment names by the second. This compares each item in the first set of
468+
// names to each item in the second set of names.
469+
for (const fragmentName1 of fragmentNames1) {
470+
for (const fragmentName2 of fragmentNames2) {
471+
collectConflictsBetweenFragments(
472+
context,
473+
conflicts,
474+
cachedFieldsAndFragmentNames,
475+
comparedFragmentPairs,
476+
areMutuallyExclusive,
477+
fragmentName1,
478+
fragmentName2,
479+
);
480+
}
481+
}
482+
return conflicts;
483+
}
484+
485+
// (E) Then collect any conflicts between the provided collection of fields
486+
// and any fragment names found in the given fragment.
487+
const processDiscoveredFragments = (
488+
context: ValidationContext,
489+
conflicts: Array<Conflict>,
490+
cachedFieldsAndFragmentNames: Map<SelectionSetNode, FieldsAndFragmentNames>,
491+
comparedFragmentPairs: PairSet,
492+
areMutuallyExclusive: boolean,
493+
fieldMap: NodeAndDefCollection,
494+
discoveredFragments: Array<Array<string>>,
495+
) => {
487496
while (discoveredFragments.length !== 0) {
488497
const item = discoveredFragments.pop();
489498
if (
@@ -504,29 +513,11 @@ function findConflictsBetweenSubSelectionSets(
504513
cachedFieldsAndFragmentNames,
505514
comparedFragmentPairs,
506515
areMutuallyExclusive,
507-
fieldMap2,
516+
fieldMap,
508517
referencedFragmentName,
509518
discoveredFragments,
510519
);
511520
}
512-
513-
// (J) Also collect conflicts between any fragment names by the first and
514-
// fragment names by the second. This compares each item in the first set of
515-
// names to each item in the second set of names.
516-
for (const fragmentName1 of fragmentNames1) {
517-
for (const fragmentName2 of fragmentNames2) {
518-
collectConflictsBetweenFragments(
519-
context,
520-
conflicts,
521-
cachedFieldsAndFragmentNames,
522-
comparedFragmentPairs,
523-
areMutuallyExclusive,
524-
fragmentName1,
525-
fragmentName2,
526-
);
527-
}
528-
}
529-
return conflicts;
530521
}
531522

532523
// Collect all Conflicts "within" one collection of fields.

0 commit comments

Comments
 (0)