Skip to content

Adjustments for merging of contextual and lexical keywords #474

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

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1718,7 +1718,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
// this special exception for `async let` statements to avoid breaking prematurely between the
// `async` and `let` keywords.
let breakOrSpace: Token
if node.name.tokenKind == .contextualKeyword(.async) {
if node.name.tokenKind == .keyword(.async) {
breakOrSpace = .space
} else {
breakOrSpace = .break
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftFormatRules/AlwaysUseLowerCamelCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,9 @@ fileprivate func identifierDescription<NodeType: SyntaxProtocol>(for node: NodeT
case .enumCaseElement: return "enum case"
case .functionDecl: return "function"
case .optionalBindingCondition(let binding):
return binding.letOrVarKeyword.tokenKind == .varKeyword ? "variable" : "constant"
return binding.letOrVarKeyword.tokenKind == .keyword(.var) ? "variable" : "constant"
case .variableDecl(let variableDecl):
return variableDecl.letOrVarKeyword.tokenKind == .varKeyword ? "variable" : "constant"
return variableDecl.letOrVarKeyword.tokenKind == .keyword(.var) ? "variable" : "constant"
default:
return "identifier"
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftFormatRules/FileScopedDeclarationPrivacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ public final class FileScopedDeclarationPrivacy: SyntaxFormatRule {

switch context.configuration.fileScopedDeclarationPrivacy.accessLevel {
case .private:
invalidAccess = .fileprivateKeyword
validAccess = .privateKeyword
invalidAccess = .keyword(.fileprivate)
validAccess = .keyword(.private)
diagnostic = .replaceFileprivateWithPrivate
case .fileprivate:
invalidAccess = .privateKeyword
validAccess = .fileprivateKeyword
invalidAccess = .keyword(.private)
validAccess = .keyword(.fileprivate)
diagnostic = .replacePrivateWithFileprivate
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatRules/FullyIndirectEnum.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class FullyIndirectEnum: SyntaxFormatRule {
let leadingTrivia: Trivia
let newEnumDecl: EnumDeclSyntax

if firstTok.tokenKind == .enumKeyword {
if firstTok.tokenKind == .keyword(.enum) {
leadingTrivia = firstTok.leadingTrivia
newEnumDecl = replaceTrivia(
on: node, token: node.firstToken, leadingTrivia: [])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension ModifierListSyntax {
var accessLevelModifier: DeclModifierSyntax? {
for modifier in self {
switch modifier.name.tokenKind {
case .publicKeyword, .privateKeyword, .fileprivateKeyword, .internalKeyword:
case .keyword(.public), .keyword(.private), .keyword(.fileprivate), .keyword(.internal):
return modifier
default:
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ public final class NoAccessLevelOnExtensionDeclaration: SyntaxFormatRule {
let keywordKind = accessKeyword.name.tokenKind
switch keywordKind {
// Public, private, or fileprivate keywords need to be moved to members
case .publicKeyword, .privateKeyword, .fileprivateKeyword:
case .keyword(.public), .keyword(.private), .keyword(.fileprivate):
diagnose(.moveAccessKeyword(keyword: accessKeyword.name.text), on: accessKeyword)

// The effective access level of the members of a `private` extension is `fileprivate`, so
// we have to update the keyword to ensure that the result is correct.
let accessKeywordToAdd: DeclModifierSyntax
if keywordKind == .privateKeyword {
if keywordKind == .keyword(.private) {
accessKeywordToAdd
= accessKeyword.withName(accessKeyword.name.withKind(.fileprivateKeyword))
= accessKeyword.withName(accessKeyword.name.withKind(.keyword(.fileprivate)))
} else {
accessKeywordToAdd = accessKeyword
}
Expand All @@ -58,7 +58,7 @@ public final class NoAccessLevelOnExtensionDeclaration: SyntaxFormatRule {
return DeclSyntax(result)

// Internal keyword redundant, delete
case .internalKeyword:
case .keyword(.internal):
diagnose(
.removeRedundantAccessKeyword(name: node.extendedType.description),
on: accessKeyword)
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftFormatRules/UseEarlyExits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ public final class UseEarlyExits: SyntaxFormatRule {

let trueBlock = ifStatement.body

let guardKeyword = TokenSyntax.guardKeyword(
let guardKeyword = TokenSyntax.keyword(.guard,
leadingTrivia: ifStatement.ifKeyword.leadingTrivia,
trailingTrivia: .spaces(1))
let guardStatement = GuardStmtSyntax(
guardKeyword: guardKeyword,
conditions: ifStatement.conditions,
elseKeyword: TokenSyntax.elseKeyword(trailingTrivia: .spaces(1)),
elseKeyword: TokenSyntax.keyword(.else, trailingTrivia: .spaces(1)),
body: elseBody)

var items = [
Expand Down
14 changes: 7 additions & 7 deletions Sources/SwiftFormatRules/UseShorthandTypeNames.swift
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,12 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
// Look for accessors that indicate that this is a computed property. If none are found, then
// it is a stored property (e.g., having only observers like `willSet/didSet`).
switch accessorDecl.accessorKind.tokenKind {
case .contextualKeyword(.get),
.contextualKeyword(.set),
.contextualKeyword(.unsafeAddress),
.contextualKeyword(.unsafeMutableAddress),
.contextualKeyword(._read),
.contextualKeyword(._modify):
case .keyword(.get),
.keyword(.set),
.keyword(.unsafeAddress),
.keyword(.unsafeMutableAddress),
.keyword(._read),
.keyword(._modify):
return false
default:
return true
Expand All @@ -545,7 +545,7 @@ public final class UseShorthandTypeNames: SyntaxFormatRule {
isStoredProperty(patternBinding),
patternBinding.initializer == nil,
let variableDecl = nearestAncestor(of: patternBinding, type: VariableDeclSyntax.self),
variableDecl.letOrVarKeyword.tokenKind == .varKeyword
variableDecl.letOrVarKeyword.tokenKind == .keyword(.var)
{
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public final class UseSingleLinePropertyGetter: SyntaxFormatRule {
let acc = accessorBlock.accessors.first,
let body = acc.body,
accessorBlock.accessors.count == 1,
acc.accessorKind.tokenKind == .contextualKeyword(.get),
acc.accessorKind.tokenKind == .keyword(.get),
acc.attributes == nil,
acc.modifier == nil,
acc.asyncKeyword == nil,
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftFormatRules/UseSynthesizedInitializer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
switch synthesizedAccessLevel {
case .internal:
// No explicit access level or internal are equivalent.
return accessLevel == nil || accessLevel!.name.tokenKind == .internalKeyword
return accessLevel == nil || accessLevel!.name.tokenKind == .keyword(.internal)
case .fileprivate:
return accessLevel != nil && accessLevel!.name.tokenKind == .fileprivateKeyword
return accessLevel != nil && accessLevel!.name.tokenKind == .keyword(.fileprivate)
case .private:
return accessLevel != nil && accessLevel!.name.tokenKind == .privateKeyword
return accessLevel != nil && accessLevel!.name.tokenKind == .keyword(.private)
}
}

Expand All @@ -116,7 +116,7 @@ public final class UseSynthesizedInitializer: SyntaxLintRule {
// Ensure that parameters that correspond to properties declared using 'var' have a default
// argument that is identical to the property's default value. Otherwise, a default argument
// doesn't match the memberwise initializer.
let isVarDecl = property.letOrVarKeyword.tokenKind == .varKeyword
let isVarDecl = property.letOrVarKeyword.tokenKind == .keyword(.var)
if isVarDecl, let initializer = property.firstInitializer {
guard let defaultArg = parameter.defaultArgument else { return false }
guard initializer.value.description == defaultArg.value.description else { return false }
Expand Down Expand Up @@ -212,10 +212,10 @@ fileprivate func synthesizedInitAccessLevel(using properties: [VariableDeclSynta
guard let modifiers = property.modifiers else { continue }

// Private takes precedence, so finding 1 private property defines the access level.
if modifiers.contains(where: {$0.name.tokenKind == .privateKeyword && $0.detail == nil}) {
if modifiers.contains(where: {$0.name.tokenKind == .keyword(.private) && $0.detail == nil}) {
return .private
}
if modifiers.contains(where: {$0.name.tokenKind == .fileprivateKeyword && $0.detail == nil}) {
if modifiers.contains(where: {$0.name.tokenKind == .keyword(.fileprivate) && $0.detail == nil}) {
hasFileprivate = true
// Can't break here because a later property might be private.
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftFormatRules/UseWhereClausesInForLoops.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fileprivate func updateWithWhereCondition(
if lastToken?.trailingTrivia.containsSpaces == false {
whereLeadingTrivia = .spaces(1)
}
let whereKeyword = TokenSyntax.whereKeyword(
let whereKeyword = TokenSyntax.keyword(.where,
leadingTrivia: whereLeadingTrivia,
trailingTrivia: .spaces(1)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public final class ValidateDocumentationComments: SyntaxLintRule {
// If a function is marked as `rethrows`, it doesn't have any errors of its
// own that should be documented. So only require documentation for
// functions marked `throws`.
let needsThrowsDesc = throwsOrRethrowsKeyword?.tokenKind == .throwsKeyword
let needsThrowsDesc = throwsOrRethrowsKeyword?.tokenKind == .keyword(.throws)

if !needsThrowsDesc && throwsDesc != nil {
diagnose(.removeThrowsComment(funcName: name), on: throwsOrRethrowsKeyword ?? node.firstToken)
Expand Down