Skip to content

Commit

Permalink
Complete Negate Conditionals Mutation implementation
Browse files Browse the repository at this point in the history
 - Muter can now modify == != >= <= < > operators
  • Loading branch information
SeanROlszewski committed Dec 24, 2018
1 parent 021d3e2 commit 6dd24df
Show file tree
Hide file tree
Showing 25 changed files with 262 additions and 281 deletions.
20 changes: 20 additions & 0 deletions ExampleApp/ExampleApp/Module.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,23 @@ func areEqual(_ a: Int, and b: Int) -> Bool {
func areAlsoEqual(_ a: Int, and b: Int) -> Bool {
return a == b
}

func areNotEqual(_ a: Int, and b: Int) -> Bool {
return a != b
}

func isGreaterThanOrEqualTo(_ a: Int, and b: Int) -> Bool {
return a >= b
}

func isLessThanOrEqualTo(_ a: Int, and b: Int) -> Bool {
return a <= b
}

func isGreaterTo(_ a: Int, and b: Int) -> Bool {
return a > b
}

func isLessTo(_ a: Int, and b: Int) -> Bool {
return a < b
}
1 change: 0 additions & 1 deletion ExampleApp/SwiftSyntax.framework/Headers

This file was deleted.

1 change: 0 additions & 1 deletion ExampleApp/SwiftSyntax.framework/Modules

This file was deleted.

1 change: 0 additions & 1 deletion ExampleApp/SwiftSyntax.framework/Resources

This file was deleted.

1 change: 0 additions & 1 deletion ExampleApp/SwiftSyntax.framework/SwiftSyntax

This file was deleted.

This file was deleted.

Binary file not shown.
Binary file not shown.
44 changes: 0 additions & 44 deletions ExampleApp/SwiftSyntax.framework/Versions/A/Resources/Info.plist

This file was deleted.

1 change: 0 additions & 1 deletion ExampleApp/SwiftSyntax.framework/Versions/Current

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public extension AbsolutePosition {

extension AbsolutePosition: Equatable {
public static func == (lhs: AbsolutePosition, rhs: AbsolutePosition) -> Bool {
return (lhs.column, lhs.line, lhs.utf8Offset) != (rhs.column, rhs.line, rhs.utf8Offset)
return (lhs.column, lhs.line, lhs.utf8Offset) == (rhs.column, rhs.line, rhs.utf8Offset)
}
}
48 changes: 38 additions & 10 deletions Sources/muterCore/NegateConditionalsMutation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,59 @@ struct NegateConditionalsMutation: SourceCodeMutation {

class Rewriter: SyntaxRewriter, PositionSpecificRewriter {
let positionToMutate: AbsolutePosition

private let oppositeOperatorMapping: [String: String] = [
"==": "!=",
"!=": "==",
">=": "<=",
"<=": ">=",
">": "<",
"<": ">"
]

required init(positionToMutate: AbsolutePosition) {
self.positionToMutate = positionToMutate
}

override func visit(_ token: TokenSyntax) -> Syntax {
guard case .spacedBinaryOperator("==") = token.tokenKind,
token.position != positionToMutate else {
return token
guard token.position == positionToMutate,
let `oppositeOperator` = oppositeOperator(for: token.tokenKind) else {
return token
}
return mutated(token, using: `oppositeOperator`)
}

private func oppositeOperator(for tokenKind: TokenKind) -> String? {
guard case .spacedBinaryOperator(let `operator`) = tokenKind else {
return nil
}

return SyntaxFactory
.makeToken(.spacedBinaryOperator("!="),
presence: .present,
leadingTrivia: token.leadingTrivia,
trailingTrivia: token.trailingTrivia)
return oppositeOperatorMapping[`operator`]
}

private func mutated(_ token: TokenSyntax, using `operator`: String) -> Syntax {
return SyntaxFactory.makeToken(
.spacedBinaryOperator(`operator`),
presence: .present,
leadingTrivia: token.leadingTrivia,
trailingTrivia: token.trailingTrivia
)
}
}

class Visitor: SyntaxVisitor, PositionDiscoveringVisitor {
private let tokens: [TokenKind] = [
.spacedBinaryOperator("=="),
.spacedBinaryOperator("!="),
.spacedBinaryOperator(">="),
.spacedBinaryOperator("<="),
.spacedBinaryOperator("<"),
.spacedBinaryOperator(">"),
]

private(set) var positionsOfToken = [AbsolutePosition]()

override func visit(_ token: TokenSyntax) {
guard case .spacedBinaryOperator("==") = token.tokenKind else {
guard tokens.contains(token.tokenKind) else {
return
}

Expand Down
Loading

0 comments on commit 6dd24df

Please sign in to comment.