From ba1283fe8e56558f7191ffd278742614fe4cda48 Mon Sep 17 00:00:00 2001 From: gh-action-runner Date: Thu, 9 Nov 2023 05:48:55 +0000 Subject: [PATCH] Squashed 'apollo-ios-codegen/' changes from 10a0738e..8e730238 8e730238 Alphabetize fragment ordering in source text (apollographql/apollo-ios-dev#130) git-subtree-dir: apollo-ios-codegen git-subtree-split: 8e7302381016d6b1422d3fd843766c84c5516cf1 --- .../OperationDescriptor.swift | 50 ++++++++++--------- Sources/IR/IR+RootFieldBuilder.swift | 4 ++ 2 files changed, 30 insertions(+), 24 deletions(-) diff --git a/Sources/ApolloCodegenLib/OperationDescriptor.swift b/Sources/ApolloCodegenLib/OperationDescriptor.swift index 1b39743f4..8e14b211a 100644 --- a/Sources/ApolloCodegenLib/OperationDescriptor.swift +++ b/Sources/ApolloCodegenLib/OperationDescriptor.swift @@ -62,22 +62,38 @@ public struct OperationDescriptor: Sendable { } func sourceText(withFormat format: SourceFormat) -> String { - format.formatted(underlyingDefinition) + format.formatted(self) } - + + fileprivate var allReferencedFragments: [CompilationResult.FragmentDefinition] { + func insertAllFragments( + from fragment: CompilationResult.FragmentDefinition, + into set: inout Set + ) { + set.insert(fragment) + for referencedFragment in fragment.referencedFragments { + insertAllFragments(from: referencedFragment, into: &set) + } + } + + var fragmentSet = Set() + for fragment in underlyingDefinition.referencedFragments { + insertAllFragments(from: fragment, into: &fragmentSet) + } + return fragmentSet.sorted { $0.name < $1.name } + } + } // MARK: - Formatting fileprivate extension OperationDescriptor.SourceFormat { - func formatted(_ operation: CompilationResult.OperationDefinition) -> String { - var source = operation.source.convertedToSingleLine() - var set = Set() - append( - to: &source, - set: &set, - fragments: operation.referencedFragments - ) + func formatted(_ operation: OperationDescriptor) -> String { + var source = operation.underlyingDefinition.source.convertedToSingleLine() + for fragment in operation.allReferencedFragments { + source += formatted(fragment) + } + switch self { case .rawSource: return source @@ -86,20 +102,6 @@ fileprivate extension OperationDescriptor.SourceFormat { } } - private func append( - to source: inout String, - set: inout Set, - fragments: [CompilationResult.FragmentDefinition] - ) { - for fragment in fragments { - if !set.contains(fragment.name) { - set.insert(fragment.name) - source += formatted(fragment) - append(to: &source, set: &set, fragments: fragment.referencedFragments) - } - } - } - private func formatted(_ fragment: CompilationResult.FragmentDefinition) -> String { switch self { case .rawSource: diff --git a/Sources/IR/IR+RootFieldBuilder.swift b/Sources/IR/IR+RootFieldBuilder.swift index 2b7d5c9ea..932d154e6 100644 --- a/Sources/IR/IR+RootFieldBuilder.swift +++ b/Sources/IR/IR+RootFieldBuilder.swift @@ -121,6 +121,10 @@ class RootFieldBuilder { from: rootSelectionSet ) + referencedFragments.sort(by: { + $0.name < $1.name + }) + return Result( rootField: EntityField(rootField, selectionSet: rootIrSelectionSet), referencedFragments: referencedFragments,