Skip to content

Commit 16575a0

Browse files
committed
remove Variable fatalError
1 parent 26bbb1b commit 16575a0

File tree

10 files changed

+84
-40
lines changed

10 files changed

+84
-40
lines changed

Sources/SyntaxKit/Collections/Array+LiteralValue.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@
2929

3030
import Foundation
3131

32-
extension Array: LiteralValue where Element == String {
32+
extension Array: LiteralValue, CodeBlockable where Element == String {
3333
/// The Swift type name for an array of strings.
3434
public var typeName: String { "[String]" }
3535

36+
/// The code block representation of this array of strings.
37+
public var codeBlock: CodeBlock {
38+
Literal.array(self.map { .string($0) })
39+
}
40+
3641
/// Renders this array as a Swift literal string with proper escaping.
3742
public var literalString: String {
3843
let elements = self.map { element in

Sources/SyntaxKit/Collections/ArrayLiteral.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import Foundation
3131

3232
/// An array literal value that can be used as a literal.
33-
public struct ArrayLiteral: LiteralValue {
33+
public struct ArrayLiteral: LiteralValue, CodeBlockable {
3434
public let elements: [Literal]
3535

3636
/// Creates an array with the given elements.
@@ -39,6 +39,11 @@ public struct ArrayLiteral: LiteralValue {
3939
self.elements = elements
4040
}
4141

42+
/// The code block representation of this array literal.
43+
public var codeBlock: CodeBlock {
44+
Literal.array(elements)
45+
}
46+
4247
/// The Swift type name for this array.
4348
public var typeName: String {
4449
if elements.isEmpty {

Sources/SyntaxKit/Collections/Dictionary+LiteralValue.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,15 @@
2929

3030
import Foundation
3131

32-
extension Dictionary: LiteralValue where Key == Int, Value == String {
32+
extension Dictionary: LiteralValue, CodeBlockable where Key == Int, Value == String {
3333
/// The Swift type name for a dictionary mapping integers to strings.
3434
public var typeName: String { "[Int: String]" }
3535

36+
/// The code block representation of this dictionary.
37+
public var codeBlock: CodeBlock {
38+
Literal.dictionary(self.map { (.integer($0.key), .string($0.value)) })
39+
}
40+
3641
/// Renders this dictionary as a Swift literal string with proper escaping.
3742
public var literalString: String {
3843
let elements = self.map { key, value in

Sources/SyntaxKit/Collections/DictionaryExpr.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import SwiftSyntax
3131

3232
/// A dictionary expression that can contain both Literal types and CodeBlock types.
33-
public struct DictionaryExpr: CodeBlock, LiteralValue {
33+
public struct DictionaryExpr: CodeBlock, LiteralValue, CodeBlockable {
3434
private let elements: [(DictionaryValue, DictionaryValue)]
3535

3636
/// Creates a dictionary expression with the given key-value pairs.
@@ -39,6 +39,11 @@ public struct DictionaryExpr: CodeBlock, LiteralValue {
3939
self.elements = elements
4040
}
4141

42+
/// The code block representation of this dictionary expression.
43+
public var codeBlock: CodeBlock {
44+
self
45+
}
46+
4247
/// The Swift type name for this dictionary.
4348
public var typeName: String {
4449
if elements.isEmpty {

Sources/SyntaxKit/Collections/DictionaryLiteral.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import Foundation
3131

3232
/// A dictionary literal value that can be used as a literal.
33-
public struct DictionaryLiteral: LiteralValue {
33+
public struct DictionaryLiteral: LiteralValue, CodeBlockable {
3434
public let elements: [(Literal, Literal)]
3535

3636
/// Creates a dictionary with the given key-value pairs.
@@ -39,6 +39,11 @@ public struct DictionaryLiteral: LiteralValue {
3939
self.elements = elements
4040
}
4141

42+
/// The code block representation of this dictionary literal.
43+
public var codeBlock: CodeBlock {
44+
Literal.dictionary(elements)
45+
}
46+
4247
/// The Swift type name for this dictionary.
4348
public var typeName: String {
4449
if elements.isEmpty {

Sources/SyntaxKit/Collections/TupleLiteral.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import Foundation
3131

3232
/// A tuple literal value that can be used as a literal.
33-
public struct TupleLiteral: LiteralValue {
33+
public struct TupleLiteral: LiteralValue, CodeBlockable {
3434
public let elements: [Literal?]
3535

3636
/// Creates a tuple with the given elements.
@@ -39,6 +39,11 @@ public struct TupleLiteral: LiteralValue {
3939
self.elements = elements
4040
}
4141

42+
/// The code block representation of this tuple literal.
43+
public var codeBlock: CodeBlock {
44+
Literal.tuple(elements)
45+
}
46+
4247
/// The Swift type name for this tuple.
4348
public var typeName: String {
4449
let elementTypes = elements.map { element in

Sources/SyntaxKit/Declarations/Init.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import SwiftSyntax
3131

3232
/// An initializer expression.
33-
public struct Init: CodeBlock, ExprCodeBlock, LiteralValue {
33+
public struct Init: CodeBlock, ExprCodeBlock, LiteralValue, CodeBlockable {
3434
private let type: String
3535
private let parameters: [ParameterExp]
3636

@@ -52,6 +52,11 @@ public struct Init: CodeBlock, ExprCodeBlock, LiteralValue {
5252
self.parameters = try params()
5353
}
5454

55+
/// The code block representation of this initializer expression.
56+
public var codeBlock: CodeBlock {
57+
self
58+
}
59+
5560
public var exprSyntax: ExprSyntax {
5661
var args = parameters
5762
var trailingClosure: ClosureExprSyntax?

Sources/SyntaxKit/Expressions/Literal.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
import SwiftSyntax
3131

3232
/// A literal value.
33-
public enum Literal: CodeBlock {
33+
public enum Literal: CodeBlock, CodeBlockable {
3434
/// A string literal.
3535
case string(String)
3636
/// A floating-point literal.
@@ -50,6 +50,11 @@ public enum Literal: CodeBlock {
5050
/// A dictionary literal.
5151
case dictionary([(Literal, Literal)])
5252

53+
/// The code block representation of this literal.
54+
public var codeBlock: CodeBlock {
55+
self
56+
}
57+
5358
/// The Swift type name for this literal.
5459
public var typeName: String {
5560
switch self {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//
2+
// CodeBlockable.swift
3+
// SyntaxKit
4+
//
5+
// Created by Leo Dion.
6+
// Copyright © 2025 BrightDigit.
7+
//
8+
// Permission is hereby granted, free of charge, to any person
9+
// obtaining a copy of this software and associated documentation
10+
// files (the “Software”), to deal in the Software without
11+
// restriction, including without limitation the rights to use,
12+
// copy, modify, merge, publish, distribute, sublicense, and/or
13+
// sell copies of the Software, and to permit persons to whom the
14+
// Software is furnished to do so, subject to the following
15+
// conditions:
16+
//
17+
// The above copyright notice and this permission notice shall be
18+
// included in all copies or substantial portions of the Software.
19+
//
20+
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
21+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
// OTHER DEALINGS IN THE SOFTWARE.
28+
//
29+
30+
/// Can export a `CodeBlock`.
31+
public protocol CodeBlockable {
32+
/// Returns a `CodeBlock`.
33+
var codeBlock: CodeBlock { get }
34+
}

Sources/SyntaxKit/Variables/Variable+LiteralInitializers.swift

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,53 +32,23 @@ import Foundation
3232
// MARK: - Variable Literal Initializers
3333

3434
extension Variable {
35-
// swiftlint:disable cyclomatic_complexity
3635
/// Creates a `let` or `var` declaration with a literal value.
3736
/// - Parameters:
3837
/// - kind: The kind of variable, either ``VariableKind/let`` or ``VariableKind/var``.
3938
/// - name: The name of the variable.
4039
/// - equals: A literal value that conforms to ``LiteralValue``.
41-
public init<T: LiteralValue>(
40+
public init<T: CodeBlockable & LiteralValue>(
4241
_ kind: VariableKind, name: String, equals value: T
4342
) {
44-
let defaultValue: CodeBlock
45-
if let literal = value as? Literal {
46-
defaultValue = literal
47-
} else if let tuple = value as? TupleLiteral {
48-
defaultValue = Literal.tuple(tuple.elements)
49-
} else if let array = value as? ArrayLiteral {
50-
defaultValue = Literal.array(array.elements)
51-
} else if let dict = value as? DictionaryLiteral {
52-
defaultValue = Literal.dictionary(dict.elements)
53-
} else if let array = value as? [String] {
54-
defaultValue = Literal.array(array.map { .string($0) })
55-
} else if let dict = value as? [Int: String] {
56-
defaultValue = Literal.dictionary(dict.map { (.integer($0.key), .string($0.value)) })
57-
} else if let dictExpr = value as? DictionaryExpr {
58-
defaultValue = dictExpr
59-
} else if let initExpr = value as? Init {
60-
defaultValue = initExpr
61-
} else if let codeBlock = value as? CodeBlock {
62-
defaultValue = codeBlock
63-
} else {
64-
// For any other LiteralValue type that doesn't conform to CodeBlock,
65-
// create a fallback or throw an error
66-
fatalError(
67-
"Variable: Unsupported LiteralValue type that doesn't conform to CodeBlock: \(T.self)"
68-
)
69-
}
70-
7143
self.init(
7244
kind: kind,
7345
name: name,
7446
type: value.typeName,
75-
defaultValue: defaultValue,
47+
defaultValue: value.codeBlock,
7648
explicitType: false
7749
)
7850
}
7951

80-
// swiftlint:enable cyclomatic_complexity
81-
8252
/// Creates a `let` or `var` declaration with a string literal value.
8353
/// - Parameters:
8454
/// - kind: The kind of variable, either ``VariableKind/let`` or ``VariableKind/var``.

0 commit comments

Comments
 (0)