-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
Hashable
conformance to generated type (#110)
* Extract String(describing: key) helper into computed property for reuse * generate type with Hashable and Equatable conformance * Support older versions of Swift Syntax * Bump minimum version of swift-snapshot-testing to 1.17.0 * Remove redundant Equatable type from inheretence clause
- Loading branch information
1 parent
3bf9f3e
commit 3167c64
Showing
25 changed files
with
439 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
...gGenerator/Snippets/String/StringsTable/StringStringsTableComparisonFunctionSnippet.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
struct StringStringsTableComparisonFunctionSnippet: Snippet { | ||
let stringsTable: SourceFile.StringExtension.StringsTableStruct | ||
let leftArg: TokenSyntax = "lhs" | ||
let rightArg: TokenSyntax = "rhs" | ||
|
||
var syntax: some DeclSyntaxProtocol { | ||
FunctionDeclSyntax( | ||
modifiers: modifiers, | ||
name: .binaryOperator("=="), | ||
signature: FunctionSignatureSyntax( | ||
parameterClause: FunctionParameterClauseSyntax { | ||
FunctionParameterSyntax( | ||
firstName: leftArg, | ||
type: IdentifierTypeSyntax(name: stringsTable.type) | ||
) | ||
FunctionParameterSyntax( | ||
firstName: rightArg, | ||
type: IdentifierTypeSyntax(name: stringsTable.type) | ||
) | ||
}, | ||
returnClause: ReturnClauseSyntax(type: .identifier(.Bool)) | ||
), | ||
body: CodeBlockSyntax(statements: body) | ||
) | ||
} | ||
|
||
@DeclModifierListBuilder | ||
var modifiers: DeclModifierListSyntax { | ||
DeclModifierSyntax(name: stringsTable.accessLevel.token) | ||
DeclModifierSyntax(name: .keyword(.static)) | ||
} | ||
|
||
var body: CodeBlockItemListSyntax { | ||
// Array of `lhs.foo == rhs.foo` | ||
var comparisons = stringsTable.comparableProperties.map { property in | ||
InfixOperatorExprSyntax( | ||
leftOperand: MemberAccessExprSyntax(leftArg, property.name), | ||
operator: BinaryOperatorExprSyntax(operator: .binaryOperator("==")), | ||
rightOperand: MemberAccessExprSyntax(rightArg, property.name) | ||
) | ||
} | ||
|
||
var next = comparisons.removeLast() | ||
while !comparisons.isEmpty { | ||
let left = comparisons.removeLast() | ||
|
||
next = InfixOperatorExprSyntax( | ||
leftOperand: left, | ||
operator: BinaryOperatorExprSyntax(operator: .binaryOperator("&&")), | ||
rightOperand: next | ||
) | ||
} | ||
|
||
return [CodeBlockItemSyntax(item: .expr(ExprSyntax(next)))] | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
...ingGenerator/Snippets/String/StringsTable/StringStringsTableHashIntoFunctionSnippet.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
struct StringStringsTableHashIntoFunctionSnippet: Snippet { | ||
let stringsTable: SourceFile.StringExtension.StringsTableStruct | ||
let hasherToken: TokenSyntax = "hasher" | ||
|
||
var syntax: some DeclSyntaxProtocol { | ||
FunctionDeclSyntax( | ||
modifiers: modifiers, | ||
name: "hash", | ||
signature: FunctionSignatureSyntax( | ||
parameterClause: FunctionParameterClauseSyntax { | ||
FunctionParameterSyntax( | ||
firstName: "into", | ||
secondName: hasherToken, | ||
type: hasherType | ||
) | ||
} | ||
), | ||
body: CodeBlockSyntax(statements: body) | ||
) | ||
} | ||
|
||
var hasherType: AttributedTypeSyntax { | ||
#if canImport(SwiftSyntax600) | ||
AttributedTypeSyntax( | ||
specifiers: TypeSpecifierListSyntax { | ||
SimpleTypeSpecifierSyntax(specifier: .keyword(.inout)) | ||
}, | ||
baseType: IdentifierTypeSyntax(name: .type(.Hasher)) | ||
) | ||
#else | ||
AttributedTypeSyntax( | ||
specifier: .keyword(.inout), | ||
baseType: IdentifierTypeSyntax(name: .type(.Hasher)) | ||
) | ||
#endif | ||
} | ||
|
||
@DeclModifierListBuilder | ||
var modifiers: DeclModifierListSyntax { | ||
DeclModifierSyntax(name: stringsTable.accessLevel.token) | ||
} | ||
|
||
@CodeBlockItemListBuilder | ||
var body: CodeBlockItemListSyntax { | ||
for property in stringsTable.comparableProperties { | ||
FunctionCallExprSyntax(callee: MemberAccessExprSyntax(hasherToken, "combine")) { | ||
LabeledExprSyntax(expression: DeclReferenceExprSyntax(baseName: property.name)) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
...nippets/String/StringsTable/StringStringsTableUnderscoredKeyComputedPropertySnippet.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import SwiftSyntax | ||
import SwiftSyntaxBuilder | ||
|
||
struct StringStringsTableUnderscoredKeyComputedPropertySnippet: Snippet { | ||
let stringsTable: SourceFile.StringExtension.StringsTableStruct | ||
|
||
var syntax: some DeclSyntaxProtocol { | ||
// fileprivate var _key: String { ... } | ||
VariableDeclSyntax( | ||
modifiers: modifiers, | ||
bindingSpecifier: .keyword(.var) | ||
) { | ||
PatternBindingSyntax( | ||
pattern: IdentifierPatternSyntax(identifier: stringsTable._keyProperty.name), | ||
typeAnnotation: typeAnnotation, | ||
accessorBlock: AccessorBlockSyntax(accessors: .getter(body)) | ||
) | ||
} | ||
} | ||
|
||
@DeclModifierListBuilder | ||
var modifiers: DeclModifierListSyntax { | ||
DeclModifierSyntax(name: .keyword(.fileprivate)) | ||
} | ||
|
||
var typeAnnotation: TypeAnnotationSyntax { | ||
TypeAnnotationSyntax( | ||
type: stringsTable._keyProperty.type | ||
) | ||
} | ||
|
||
@CodeBlockItemListBuilder | ||
var body: CodeBlockItemListSyntax { | ||
// String(describing: key) | ||
FunctionCallExprSyntax( | ||
callee: DeclReferenceExprSyntax(baseName: .type(.String)) | ||
) { | ||
LabeledExprSyntax( | ||
label: "describing", | ||
expression: DeclReferenceExprSyntax(baseName: stringsTable.keyProperty.name) | ||
) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.