OverlappingFieldsCanBeMerged.mjs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. import find from '../../polyfills/find';
  2. import objectEntries from '../../polyfills/objectEntries';
  3. import inspect from '../../jsutils/inspect';
  4. import { GraphQLError } from '../../error/GraphQLError';
  5. import { Kind } from '../../language/kinds';
  6. import { print } from '../../language/printer';
  7. import { getNamedType, isNonNullType, isLeafType, isObjectType, isListType, isInterfaceType } from '../../type/definition';
  8. import { typeFromAST } from '../../utilities/typeFromAST';
  9. export function fieldsConflictMessage(responseName, reason) {
  10. return "Fields \"".concat(responseName, "\" conflict because ").concat(reasonMessage(reason), ". ") + 'Use different aliases on the fields to fetch both if this was intentional.';
  11. }
  12. function reasonMessage(reason) {
  13. if (Array.isArray(reason)) {
  14. return reason.map(function (_ref) {
  15. var responseName = _ref[0],
  16. subreason = _ref[1];
  17. return "subfields \"".concat(responseName, "\" conflict because ").concat(reasonMessage(subreason));
  18. }).join(' and ');
  19. }
  20. return reason;
  21. }
  22. /**
  23. * Overlapping fields can be merged
  24. *
  25. * A selection set is only valid if all fields (including spreading any
  26. * fragments) either correspond to distinct response names or can be merged
  27. * without ambiguity.
  28. */
  29. export function OverlappingFieldsCanBeMerged(context) {
  30. // A memoization for when two fragments are compared "between" each other for
  31. // conflicts. Two fragments may be compared many times, so memoizing this can
  32. // dramatically improve the performance of this validator.
  33. var comparedFragmentPairs = new PairSet(); // A cache for the "field map" and list of fragment names found in any given
  34. // selection set. Selection sets may be asked for this information multiple
  35. // times, so this improves the performance of this validator.
  36. var cachedFieldsAndFragmentNames = new Map();
  37. return {
  38. SelectionSet: function SelectionSet(selectionSet) {
  39. var conflicts = findConflictsWithinSelectionSet(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, context.getParentType(), selectionSet);
  40. for (var _i2 = 0; _i2 < conflicts.length; _i2++) {
  41. var _ref3 = conflicts[_i2];
  42. var _ref2$ = _ref3[0];
  43. var responseName = _ref2$[0];
  44. var reason = _ref2$[1];
  45. var fields1 = _ref3[1];
  46. var fields2 = _ref3[2];
  47. context.reportError(new GraphQLError(fieldsConflictMessage(responseName, reason), fields1.concat(fields2)));
  48. }
  49. }
  50. };
  51. }
  52. /**
  53. * Algorithm:
  54. *
  55. * Conflicts occur when two fields exist in a query which will produce the same
  56. * response name, but represent differing values, thus creating a conflict.
  57. * The algorithm below finds all conflicts via making a series of comparisons
  58. * between fields. In order to compare as few fields as possible, this makes
  59. * a series of comparisons "within" sets of fields and "between" sets of fields.
  60. *
  61. * Given any selection set, a collection produces both a set of fields by
  62. * also including all inline fragments, as well as a list of fragments
  63. * referenced by fragment spreads.
  64. *
  65. * A) Each selection set represented in the document first compares "within" its
  66. * collected set of fields, finding any conflicts between every pair of
  67. * overlapping fields.
  68. * Note: This is the *only time* that a the fields "within" a set are compared
  69. * to each other. After this only fields "between" sets are compared.
  70. *
  71. * B) Also, if any fragment is referenced in a selection set, then a
  72. * comparison is made "between" the original set of fields and the
  73. * referenced fragment.
  74. *
  75. * C) Also, if multiple fragments are referenced, then comparisons
  76. * are made "between" each referenced fragment.
  77. *
  78. * D) When comparing "between" a set of fields and a referenced fragment, first
  79. * a comparison is made between each field in the original set of fields and
  80. * each field in the the referenced set of fields.
  81. *
  82. * E) Also, if any fragment is referenced in the referenced selection set,
  83. * then a comparison is made "between" the original set of fields and the
  84. * referenced fragment (recursively referring to step D).
  85. *
  86. * F) When comparing "between" two fragments, first a comparison is made between
  87. * each field in the first referenced set of fields and each field in the the
  88. * second referenced set of fields.
  89. *
  90. * G) Also, any fragments referenced by the first must be compared to the
  91. * second, and any fragments referenced by the second must be compared to the
  92. * first (recursively referring to step F).
  93. *
  94. * H) When comparing two fields, if both have selection sets, then a comparison
  95. * is made "between" both selection sets, first comparing the set of fields in
  96. * the first selection set with the set of fields in the second.
  97. *
  98. * I) Also, if any fragment is referenced in either selection set, then a
  99. * comparison is made "between" the other set of fields and the
  100. * referenced fragment.
  101. *
  102. * J) Also, if two fragments are referenced in both selection sets, then a
  103. * comparison is made "between" the two fragments.
  104. *
  105. */
  106. // Find all conflicts found "within" a selection set, including those found
  107. // via spreading in fragments. Called when visiting each SelectionSet in the
  108. // GraphQL Document.
  109. function findConflictsWithinSelectionSet(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentType, selectionSet) {
  110. var conflicts = [];
  111. var _getFieldsAndFragment = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType, selectionSet),
  112. fieldMap = _getFieldsAndFragment[0],
  113. fragmentNames = _getFieldsAndFragment[1]; // (A) Find find all conflicts "within" the fields of this selection set.
  114. // Note: this is the *only place* `collectConflictsWithin` is called.
  115. collectConflictsWithin(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, fieldMap);
  116. if (fragmentNames.length !== 0) {
  117. // (B) Then collect conflicts between these fields and those represented by
  118. // each spread fragment name found.
  119. var comparedFragments = Object.create(null);
  120. for (var i = 0; i < fragmentNames.length; i++) {
  121. collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragments, comparedFragmentPairs, false, fieldMap, fragmentNames[i]); // (C) Then compare this fragment with all other fragments found in this
  122. // selection set to collect conflicts between fragments spread together.
  123. // This compares each item in the list of fragment names to every other
  124. // item in that same list (except for itself).
  125. for (var j = i + 1; j < fragmentNames.length; j++) {
  126. collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, false, fragmentNames[i], fragmentNames[j]);
  127. }
  128. }
  129. }
  130. return conflicts;
  131. } // Collect all conflicts found between a set of fields and a fragment reference
  132. // including via spreading in any nested fragments.
  133. function collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragments, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fragmentName) {
  134. // Memoize so a fragment is not compared for conflicts more than once.
  135. if (comparedFragments[fragmentName]) {
  136. return;
  137. }
  138. comparedFragments[fragmentName] = true;
  139. var fragment = context.getFragment(fragmentName);
  140. if (!fragment) {
  141. return;
  142. }
  143. var _getReferencedFieldsA = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment),
  144. fieldMap2 = _getReferencedFieldsA[0],
  145. fragmentNames2 = _getReferencedFieldsA[1]; // Do not compare a fragment's fieldMap to itself.
  146. if (fieldMap === fieldMap2) {
  147. return;
  148. } // (D) First collect any conflicts between the provided collection of fields
  149. // and the collection of fields represented by the given fragment.
  150. collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fieldMap2); // (E) Then collect any conflicts between the provided collection of fields
  151. // and any fragment names found in the given fragment.
  152. for (var i = 0; i < fragmentNames2.length; i++) {
  153. collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragments, comparedFragmentPairs, areMutuallyExclusive, fieldMap, fragmentNames2[i]);
  154. }
  155. } // Collect all conflicts found between two fragments, including via spreading in
  156. // any nested fragments.
  157. function collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentName1, fragmentName2) {
  158. // No need to compare a fragment to itself.
  159. if (fragmentName1 === fragmentName2) {
  160. return;
  161. } // Memoize so two fragments are not compared for conflicts more than once.
  162. if (comparedFragmentPairs.has(fragmentName1, fragmentName2, areMutuallyExclusive)) {
  163. return;
  164. }
  165. comparedFragmentPairs.add(fragmentName1, fragmentName2, areMutuallyExclusive);
  166. var fragment1 = context.getFragment(fragmentName1);
  167. var fragment2 = context.getFragment(fragmentName2);
  168. if (!fragment1 || !fragment2) {
  169. return;
  170. }
  171. var _getReferencedFieldsA2 = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment1),
  172. fieldMap1 = _getReferencedFieldsA2[0],
  173. fragmentNames1 = _getReferencedFieldsA2[1];
  174. var _getReferencedFieldsA3 = getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment2),
  175. fieldMap2 = _getReferencedFieldsA3[0],
  176. fragmentNames2 = _getReferencedFieldsA3[1]; // (F) First, collect all conflicts between these two collections of fields
  177. // (not including any nested fragments).
  178. collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fieldMap2); // (G) Then collect conflicts between the first fragment and any nested
  179. // fragments spread in the second fragment.
  180. for (var j = 0; j < fragmentNames2.length; j++) {
  181. collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentName1, fragmentNames2[j]);
  182. } // (G) Then collect conflicts between the second fragment and any nested
  183. // fragments spread in the first fragment.
  184. for (var i = 0; i < fragmentNames1.length; i++) {
  185. collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentNames1[i], fragmentName2);
  186. }
  187. } // Find all conflicts found between two selection sets, including those found
  188. // via spreading in fragments. Called when determining if conflicts exist
  189. // between the sub-fields of two overlapping fields.
  190. function findConflictsBetweenSubSelectionSets(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, parentType1, selectionSet1, parentType2, selectionSet2) {
  191. var conflicts = [];
  192. var _getFieldsAndFragment2 = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType1, selectionSet1),
  193. fieldMap1 = _getFieldsAndFragment2[0],
  194. fragmentNames1 = _getFieldsAndFragment2[1];
  195. var _getFieldsAndFragment3 = getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType2, selectionSet2),
  196. fieldMap2 = _getFieldsAndFragment3[0],
  197. fragmentNames2 = _getFieldsAndFragment3[1]; // (H) First, collect all conflicts between these two collections of field.
  198. collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fieldMap2); // (I) Then collect conflicts between the first collection of fields and
  199. // those referenced by each fragment name associated with the second.
  200. if (fragmentNames2.length !== 0) {
  201. var comparedFragments = Object.create(null);
  202. for (var j = 0; j < fragmentNames2.length; j++) {
  203. collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, comparedFragments, comparedFragmentPairs, areMutuallyExclusive, fieldMap1, fragmentNames2[j]);
  204. }
  205. } // (I) Then collect conflicts between the second collection of fields and
  206. // those referenced by each fragment name associated with the first.
  207. if (fragmentNames1.length !== 0) {
  208. var _comparedFragments = Object.create(null);
  209. for (var i = 0; i < fragmentNames1.length; i++) {
  210. collectConflictsBetweenFieldsAndFragment(context, conflicts, cachedFieldsAndFragmentNames, _comparedFragments, comparedFragmentPairs, areMutuallyExclusive, fieldMap2, fragmentNames1[i]);
  211. }
  212. } // (J) Also collect conflicts between any fragment names by the first and
  213. // fragment names by the second. This compares each item in the first set of
  214. // names to each item in the second set of names.
  215. for (var _i3 = 0; _i3 < fragmentNames1.length; _i3++) {
  216. for (var _j = 0; _j < fragmentNames2.length; _j++) {
  217. collectConflictsBetweenFragments(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, fragmentNames1[_i3], fragmentNames2[_j]);
  218. }
  219. }
  220. return conflicts;
  221. } // Collect all Conflicts "within" one collection of fields.
  222. function collectConflictsWithin(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, fieldMap) {
  223. // A field map is a keyed collection, where each key represents a response
  224. // name and the value at that key is a list of all fields which provide that
  225. // response name. For every response name, if there are multiple fields, they
  226. // must be compared to find a potential conflict.
  227. for (var _i5 = 0, _objectEntries2 = objectEntries(fieldMap); _i5 < _objectEntries2.length; _i5++) {
  228. var _ref5 = _objectEntries2[_i5];
  229. var responseName = _ref5[0];
  230. var fields = _ref5[1];
  231. // This compares every field in the list to every other field in this list
  232. // (except to itself). If the list only has one item, nothing needs to
  233. // be compared.
  234. if (fields.length > 1) {
  235. for (var i = 0; i < fields.length; i++) {
  236. for (var j = i + 1; j < fields.length; j++) {
  237. var conflict = findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, false, // within one collection is never mutually exclusive
  238. responseName, fields[i], fields[j]);
  239. if (conflict) {
  240. conflicts.push(conflict);
  241. }
  242. }
  243. }
  244. }
  245. }
  246. } // Collect all Conflicts between two collections of fields. This is similar to,
  247. // but different from the `collectConflictsWithin` function above. This check
  248. // assumes that `collectConflictsWithin` has already been called on each
  249. // provided collection of fields. This is true because this validator traverses
  250. // each individual selection set.
  251. function collectConflictsBetween(context, conflicts, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, fieldMap1, fieldMap2) {
  252. // A field map is a keyed collection, where each key represents a response
  253. // name and the value at that key is a list of all fields which provide that
  254. // response name. For any response name which appears in both provided field
  255. // maps, each field from the first field map must be compared to every field
  256. // in the second field map to find potential conflicts.
  257. for (var _i7 = 0, _Object$keys2 = Object.keys(fieldMap1); _i7 < _Object$keys2.length; _i7++) {
  258. var responseName = _Object$keys2[_i7];
  259. var fields2 = fieldMap2[responseName];
  260. if (fields2) {
  261. var fields1 = fieldMap1[responseName];
  262. for (var i = 0; i < fields1.length; i++) {
  263. for (var j = 0; j < fields2.length; j++) {
  264. var conflict = findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, responseName, fields1[i], fields2[j]);
  265. if (conflict) {
  266. conflicts.push(conflict);
  267. }
  268. }
  269. }
  270. }
  271. }
  272. } // Determines if there is a conflict between two particular fields, including
  273. // comparing their sub-fields.
  274. function findConflict(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, parentFieldsAreMutuallyExclusive, responseName, field1, field2) {
  275. var parentType1 = field1[0],
  276. node1 = field1[1],
  277. def1 = field1[2];
  278. var parentType2 = field2[0],
  279. node2 = field2[1],
  280. def2 = field2[2]; // If it is known that two fields could not possibly apply at the same
  281. // time, due to the parent types, then it is safe to permit them to diverge
  282. // in aliased field or arguments used as they will not present any ambiguity
  283. // by differing.
  284. // It is known that two parent types could never overlap if they are
  285. // different Object types. Interface or Union types might overlap - if not
  286. // in the current state of the schema, then perhaps in some future version,
  287. // thus may not safely diverge.
  288. var areMutuallyExclusive = parentFieldsAreMutuallyExclusive || parentType1 !== parentType2 && isObjectType(parentType1) && isObjectType(parentType2); // The return type for each field.
  289. var type1 = def1 && def1.type;
  290. var type2 = def2 && def2.type;
  291. if (!areMutuallyExclusive) {
  292. // Two aliases must refer to the same field.
  293. var name1 = node1.name.value;
  294. var name2 = node2.name.value;
  295. if (name1 !== name2) {
  296. return [[responseName, "".concat(name1, " and ").concat(name2, " are different fields")], [node1], [node2]];
  297. } // Two field calls must have the same arguments.
  298. if (!sameArguments(node1.arguments || [], node2.arguments || [])) {
  299. return [[responseName, 'they have differing arguments'], [node1], [node2]];
  300. }
  301. }
  302. if (type1 && type2 && doTypesConflict(type1, type2)) {
  303. return [[responseName, "they return conflicting types ".concat(inspect(type1), " and ").concat(inspect(type2))], [node1], [node2]];
  304. } // Collect and compare sub-fields. Use the same "visited fragment names" list
  305. // for both collections so fields in a fragment reference are never
  306. // compared to themselves.
  307. var selectionSet1 = node1.selectionSet;
  308. var selectionSet2 = node2.selectionSet;
  309. if (selectionSet1 && selectionSet2) {
  310. var conflicts = findConflictsBetweenSubSelectionSets(context, cachedFieldsAndFragmentNames, comparedFragmentPairs, areMutuallyExclusive, getNamedType(type1), selectionSet1, getNamedType(type2), selectionSet2);
  311. return subfieldConflicts(conflicts, responseName, node1, node2);
  312. }
  313. }
  314. function sameArguments(arguments1, arguments2) {
  315. if (arguments1.length !== arguments2.length) {
  316. return false;
  317. }
  318. return arguments1.every(function (argument1) {
  319. var argument2 = find(arguments2, function (argument) {
  320. return argument.name.value === argument1.name.value;
  321. });
  322. if (!argument2) {
  323. return false;
  324. }
  325. return sameValue(argument1.value, argument2.value);
  326. });
  327. }
  328. function sameValue(value1, value2) {
  329. return !value1 && !value2 || print(value1) === print(value2);
  330. } // Two types conflict if both types could not apply to a value simultaneously.
  331. // Composite types are ignored as their individual field types will be compared
  332. // later recursively. However List and Non-Null types must match.
  333. function doTypesConflict(type1, type2) {
  334. if (isListType(type1)) {
  335. return isListType(type2) ? doTypesConflict(type1.ofType, type2.ofType) : true;
  336. }
  337. if (isListType(type2)) {
  338. return true;
  339. }
  340. if (isNonNullType(type1)) {
  341. return isNonNullType(type2) ? doTypesConflict(type1.ofType, type2.ofType) : true;
  342. }
  343. if (isNonNullType(type2)) {
  344. return true;
  345. }
  346. if (isLeafType(type1) || isLeafType(type2)) {
  347. return type1 !== type2;
  348. }
  349. return false;
  350. } // Given a selection set, return the collection of fields (a mapping of response
  351. // name to field nodes and definitions) as well as a list of fragment names
  352. // referenced via fragment spreads.
  353. function getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, parentType, selectionSet) {
  354. var cached = cachedFieldsAndFragmentNames.get(selectionSet);
  355. if (!cached) {
  356. var nodeAndDefs = Object.create(null);
  357. var fragmentNames = Object.create(null);
  358. _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeAndDefs, fragmentNames);
  359. cached = [nodeAndDefs, Object.keys(fragmentNames)];
  360. cachedFieldsAndFragmentNames.set(selectionSet, cached);
  361. }
  362. return cached;
  363. } // Given a reference to a fragment, return the represented collection of fields
  364. // as well as a list of nested fragment names referenced via fragment spreads.
  365. function getReferencedFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragment) {
  366. // Short-circuit building a type from the node if possible.
  367. var cached = cachedFieldsAndFragmentNames.get(fragment.selectionSet);
  368. if (cached) {
  369. return cached;
  370. }
  371. var fragmentType = typeFromAST(context.getSchema(), fragment.typeCondition);
  372. return getFieldsAndFragmentNames(context, cachedFieldsAndFragmentNames, fragmentType, fragment.selectionSet);
  373. }
  374. function _collectFieldsAndFragmentNames(context, parentType, selectionSet, nodeAndDefs, fragmentNames) {
  375. for (var _i9 = 0, _selectionSet$selecti2 = selectionSet.selections; _i9 < _selectionSet$selecti2.length; _i9++) {
  376. var selection = _selectionSet$selecti2[_i9];
  377. switch (selection.kind) {
  378. case Kind.FIELD:
  379. {
  380. var fieldName = selection.name.value;
  381. var fieldDef = void 0;
  382. if (isObjectType(parentType) || isInterfaceType(parentType)) {
  383. fieldDef = parentType.getFields()[fieldName];
  384. }
  385. var responseName = selection.alias ? selection.alias.value : fieldName;
  386. if (!nodeAndDefs[responseName]) {
  387. nodeAndDefs[responseName] = [];
  388. }
  389. nodeAndDefs[responseName].push([parentType, selection, fieldDef]);
  390. break;
  391. }
  392. case Kind.FRAGMENT_SPREAD:
  393. fragmentNames[selection.name.value] = true;
  394. break;
  395. case Kind.INLINE_FRAGMENT:
  396. {
  397. var typeCondition = selection.typeCondition;
  398. var inlineFragmentType = typeCondition ? typeFromAST(context.getSchema(), typeCondition) : parentType;
  399. _collectFieldsAndFragmentNames(context, inlineFragmentType, selection.selectionSet, nodeAndDefs, fragmentNames);
  400. break;
  401. }
  402. }
  403. }
  404. } // Given a series of Conflicts which occurred between two sub-fields, generate
  405. // a single Conflict.
  406. function subfieldConflicts(conflicts, responseName, node1, node2) {
  407. if (conflicts.length > 0) {
  408. return [[responseName, conflicts.map(function (_ref6) {
  409. var reason = _ref6[0];
  410. return reason;
  411. })], conflicts.reduce(function (allFields, _ref7) {
  412. var fields1 = _ref7[1];
  413. return allFields.concat(fields1);
  414. }, [node1]), conflicts.reduce(function (allFields, _ref8) {
  415. var fields2 = _ref8[2];
  416. return allFields.concat(fields2);
  417. }, [node2])];
  418. }
  419. }
  420. /**
  421. * A way to keep track of pairs of things when the ordering of the pair does
  422. * not matter. We do this by maintaining a sort of double adjacency sets.
  423. */
  424. var PairSet =
  425. /*#__PURE__*/
  426. function () {
  427. function PairSet() {
  428. this._data = Object.create(null);
  429. }
  430. var _proto = PairSet.prototype;
  431. _proto.has = function has(a, b, areMutuallyExclusive) {
  432. var first = this._data[a];
  433. var result = first && first[b];
  434. if (result === undefined) {
  435. return false;
  436. } // areMutuallyExclusive being false is a superset of being true,
  437. // hence if we want to know if this PairSet "has" these two with no
  438. // exclusivity, we have to ensure it was added as such.
  439. if (areMutuallyExclusive === false) {
  440. return result === false;
  441. }
  442. return true;
  443. };
  444. _proto.add = function add(a, b, areMutuallyExclusive) {
  445. _pairSetAdd(this._data, a, b, areMutuallyExclusive);
  446. _pairSetAdd(this._data, b, a, areMutuallyExclusive);
  447. };
  448. return PairSet;
  449. }();
  450. function _pairSetAdd(data, a, b, areMutuallyExclusive) {
  451. var map = data[a];
  452. if (!map) {
  453. map = Object.create(null);
  454. data[a] = map;
  455. }
  456. map[b] = areMutuallyExclusive;
  457. }