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

Fix #373: addLeft's implementation doesn't match its specification #378

Merged
merged 1 commit into from
Sep 29, 2017
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
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ case class RuleCtxImpl(tree: Tree, config: ScalafixConfig) extends RuleCtx {
toks(tree).lastOption.fold(Patch.empty)(addRight(_, toAdd))
def addLeft(tok: Token, toAdd: String): Patch = Add(tok, toAdd, "")
def addLeft(tree: Tree, toAdd: String): Patch =
toks(tree).lastOption.fold(Patch.empty)(addLeft(_, toAdd))
toks(tree).headOption.fold(Patch.empty)(addLeft(_, toAdd))

// Semantic patch ops.
def removeGlobalImport(symbol: Symbol)(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,18 @@ import scalafix.syntax._

import org.scalatest.FunSuiteLike

class SyntacticRuleSuite(rule: Rule) extends FunSuiteLike with DiffAssertions {
Copy link
Contributor

Choose a reason for hiding this comment

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

The migration manager (mima) is complaining because of this change, scalafix-testkit is a published artifact to help scalafix users develop custom rules in repositories outside of scalacenter/scalafix.

I think we can make this change without breaking compatibility by changing this parameter to defaultRule: Rule = Rule.empty.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

class SyntacticRuleSuite(defaultRule: Rule = Rule.empty)
extends FunSuiteLike
with DiffAssertions {
def check(name: String, original: String, expected: String): Unit = {
check(defaultRule, name, original, expected)
}

def check(
rule: Rule,
name: String,
original: String,
expected: String): Unit = {
test(name) {
import scala.meta._
val obtained = rule.apply(Input.String(original))
Expand All @@ -18,6 +28,10 @@ class SyntacticRuleSuite(rule: Rule) extends FunSuiteLike with DiffAssertions {
}

def checkDiff(original: Input, expected: String): Unit = {
checkDiff(defaultRule, original, expected)
}

def checkDiff(rule: Rule, original: Input, expected: String): Unit = {
test(original.label) {
val ctx = RuleCtx(original.parse[Source].get, ScalafixConfig.default)
val obtained = rule.diff(ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,27 @@ import java.nio.file.Files
import java.nio.file.Paths
import scalafix.internal.rule.ProcedureSyntax

class ErrorSuite extends SyntacticRuleSuite(ProcedureSyntax) {
class ErrorSuite extends SyntacticRuleSuite {
test("on parse error") {
intercept[ParseException] {
ProcedureSyntax.apply(Input.String("object A {"))
}
}
}
class PatchSuite
extends SyntacticRuleSuite(Rule.syntactic("PatchSuite")(ctx =>
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a nice cleanup, removing the rule definition in the constructor 👍

ctx.addRight(ctx.tree.tokens.find(_.is[Ident]).get, "bba"))) {
class PatchSuite extends SyntacticRuleSuite {

val original =
val original: String =
"""// Foobar
|object a {
| val x = 2
|}""".stripMargin

val addRightRule: Rule = Rule.syntactic("addRight") { (ctx) =>
ctx.addRight(ctx.tree.tokens.find(_.is[Ident]).get, "bba")
}

checkDiff(
addRightRule,
Input.String(original),
"""--- Input.String('<// Foobar...>')
|+++ Input.String('<// Foobar...>')
Expand All @@ -39,6 +42,7 @@ class PatchSuite
)

checkDiff(
addRightRule,
Input.VirtualFile("/label", original),
"""--- /label
|+++ /label
Expand All @@ -53,6 +57,7 @@ class PatchSuite
val file = File.createTempFile("foo", ".scala")
Files.write(Paths.get(file.toURI), original.getBytes)
checkDiff(
addRightRule,
Input.File(file),
s"""--- ${file.getAbsolutePath}
|+++ ${file.getAbsolutePath}
Expand All @@ -63,4 +68,19 @@ class PatchSuite
| val x = 2
| }""".stripMargin
)

val addLeftRule: Rule = Rule.syntactic("addLeft") { (ctx) =>
ctx.addLeft(ctx.tree, "object Foo {}\n")
}

check(
addLeftRule,
"addLeft adds the string before the first tree token",
original,
"""object Foo {}
|// Foobar
|object a {
| val x = 2
|}""".stripMargin
)
}