Skip to content

Commit

Permalink
More checks for clashing source languages
Browse files Browse the repository at this point in the history
  • Loading branch information
anferbui committed Nov 19, 2024
1 parent 30623c5 commit bc6df51
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
37 changes: 36 additions & 1 deletion Sources/SwiftDocC/Infrastructure/DocumentationContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3136,12 +3136,14 @@ extension DocumentationContext {
for node in topicGraph.nodes.values {
guard let entity = try? self.entity(with: node.reference) else { continue }

var sourceLanguageToReference: [SourceLanguage: AlternateDeclaration] = [:]
for alternateDeclaration in entity.metadata?.alternateDeclarations ?? [] {
guard case .resolved(.success(let counterpartReference)) = alternateDeclaration.counterpart,
let counterpartEntity = try? self.entity(with: counterpartReference) else {
continue
}

// Case where the original symbol already was defined in the languages of the counterpart symbol.
let duplicateSourceLanguages = counterpartEntity.availableSourceLanguages.intersection(entity.availableSourceLanguages)
duplicateSourceLanguages.forEach { duplicateSourceLanguage in
problems
Expand All @@ -3151,13 +3153,46 @@ extension DocumentationContext {
source: alternateDeclaration.originalMarkup.range?.source,
severity: .warning,
range: alternateDeclaration.originalMarkup.range,
identifier: "org.swift.docc.AlternateDeclaration.DuplicateSourceLanguage",
identifier: "org.swift.docc.AlternateDeclaration.DuplicateLanguageDefinition",
summary: "This node already has a declaration in \(duplicateSourceLanguage.name)",
explanation: "This node is already available in \(entity.availableSourceLanguages.map { $0.name }.joined(separator: ", ")).",
),
possibleSolutions: [Solution(summary: "Replace the counterpart link with a node which isn't available in \(entity.availableSourceLanguages.map { $0.name }.joined(separator: ", "))", replacements: [])]
)
)
}

let duplicateCounterpartLanguages = Set(sourceLanguageToReference.keys).intersection(counterpartEntity.availableSourceLanguages)
duplicateCounterpartLanguages.forEach { duplicateSourceLanguage in
let replacements = alternateDeclaration.originalMarkup.range.flatMap { [Replacement(range: $0, replacement: "")] } ?? []
let notes: [DiagnosticNote] = sourceLanguageToReference[duplicateSourceLanguage].flatMap {
guard let range = $0.originalMarkup.range, let source = range.source else { return nil }

return [
DiagnosticNote(source: source, range: range, message: """
An alternate declaration for \(duplicateSourceLanguage.name) has already been defined by an @\(AlternateDeclaration.self) directive.
""")
]
} ?? []
problems
.append(
Problem(
diagnostic: Diagnostic(
source: alternateDeclaration.originalMarkup.range?.source,
severity: .warning,
range: alternateDeclaration.originalMarkup.range,
identifier: "org.swift.docc.AlternateDeclaration.DuplicateLanguageDefinition",
summary: "An alternate declaration for \(duplicateSourceLanguage.name) already exists",
explanation: "This node is already available in \(sourceLanguageToReference.keys.map { $0.name }.joined(separator: ", ")).",
notes: notes
),
possibleSolutions: [Solution(summary: "Remove this alternate declaration", replacements: replacements)]
)
)
}

// Update mapping from source language to alternate declaration, for diagnostic purposes
counterpartEntity.availableSourceLanguages.forEach { sourceLanguageToReference[$0] = alternateDeclaration }
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftDocC/Semantics/Metadata/Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public final class Metadata: Semantic, AutomaticDirectiveConvertible {

validateDuplicates(in: pageImages, uniqueBy: \.purpose, keyName: "purpose", problems: &problems)
validateDuplicates(in: availability, uniqueBy: \.platform, keyName: "platform", problems: &problems)
validateDuplicates(in: alternateDeclarations, uniqueBy: \.counterpart, keyName: "language", problems: &problems)
validateDuplicates(in: alternateDeclarations, uniqueBy: \.counterpart, keyName: "counterpart", problems: &problems)

return true
}
Expand Down

0 comments on commit bc6df51

Please sign in to comment.