Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TypeSyntax util functions #2886

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions Sources/SwiftRefactor/SyntaxUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,3 @@ extension Trivia {
return Trivia(pieces: self.reversed().drop(while: \.isWhitespace).reversed())
}
}

extension TypeSyntax {
var isVoid: Bool {
switch self.as(TypeSyntaxEnum.self) {
case .identifierType(let identifierType) where identifierType.name.text == "Void": return true
case .tupleType(let tupleType) where tupleType.elements.isEmpty: return true
default: return false
}
}
}
26 changes: 26 additions & 0 deletions Sources/SwiftSyntax/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,29 @@ extension RawUnexpectedNodesSyntax {
self.init(raw: raw)
}
}

extension TypeSyntax {
hnrklssn marked this conversation as resolved.
Show resolved Hide resolved
public var isVoid: Bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide documentation comments for all public APIs. Once we settle on them, please add an entry to the release notes.

switch self.as(TypeSyntaxEnum.self) {
case .identifierType(let identifierType) where identifierType.name.text == "Void": return true
case .tupleType(let tupleType) where tupleType.elements.isEmpty: return true
case .memberType(let memberType) where memberType.name.text == "Void": return memberType.baseType.isSwiftCoreModule
default: return false
}
}

public var isSwiftInt: Bool {
switch self.as(TypeSyntaxEnum.self) {
case .identifierType(let identifierType) where identifierType.name.text == "Int": return true
case .memberType(let memberType) where memberType.name.text == "Int": return memberType.baseType.isSwiftCoreModule
default: return false
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is largely a copy of the isVoid implementation above. I'm concerned that this approach to the API doesn't really scale: are we going to end up growing isSwiftUInt, isSwiftBool, isSwiftSet, and others as separate properties? That doesn't seem like a desirable end state.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a way to generalise it to types where the canonical name only contains MemberTypeSyntax or IdentifierTypeSyntax. This doesn't include Void since it's a tuple, and there doesn't seem to be a good way to generalise meta types for tuples; I couldn't find anything like Tuple.Type, so handling tuple types would involve enumerating every arity. Not sure how to handle type shadowing either, since there doesn't seem to be a way to reify module<Swift>.self.


public var isSwiftCoreModule: Bool {
guard let identifierType = self.as(IdentifierTypeSyntax.self) else {
return false
}
return identifierType.name.text == "Swift"
}
}