Skip to content

Commit

Permalink
Merge pull request #1851 from bjaglin/redundant-quoting
Browse files Browse the repository at this point in the history
RedundantSyntax: distinguish single and triple quotes
  • Loading branch information
bjaglin authored Sep 10, 2023
2 parents 89b843d + a6f99e8 commit f4dcc6f
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,29 @@ class RedundantSyntax(config: RedundantSyntaxConfig)
finalTok :: TokenList(o.tokens).trailingSpaces(finalTok).toList
}
}
case interpolator @ Term.Interpolate(
Term.Name(p),
Lit.String(v) :: Nil,
case Term.Interpolate(
interpolator,
lit :: Nil,
Nil
)
if config.stringInterpolator
&& (p == "s" || p == "f" || (p == "raw" && !v.contains('\\'))) =>
Patch.removeTokens(interpolator.prefix.tokens)
if config.stringInterpolator &&
!mustKeepInterpolator(interpolator, lit) =>
Patch.removeTokens(interpolator.tokens)
}
.map(_.atomic)
.asPatch

private def mustKeepInterpolator(interpolator: Tree, lit: Tree) = {
val escapedCharacter = lit.syntax.contains('\\')
// termInterpolate.syntax.contains("\"\"\"") does not work in scala 2.13 and scala 3
// as the syntax uses single quotes even if the litteral was defined with triple quotes
val tripleQuotes = lit.pos.start - interpolator.pos.end == 3
interpolator.syntax match {
case "s" | "f" =>
escapedCharacter && tripleQuotes
case "raw" =>
escapedCharacter && !tripleQuotes
case _ => true
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
rules = RedundantSyntax
RedundantSyntax.stringInterpolator = true
*/

package test.redundantSyntax

class StringInterpolator {

val a = 42d

var b = ""
b = "foo"
b = s"foo"
b = s"foo $a bar"
//b = s"my \"quoted\" string" does not compile as of 2.12.18 as https://github.com/scala/scala/pull/8830 was not backported

b = """foo"""
b =
s"""foo
|bar"""
b = s"""my \"quoted\" string"""
b = s"""foo $a bar"""
b = s"""$a"""

b = f"foo"
b = f"foo $a%2.2f"
b = f"foo \n bar"

b = raw"foo $a \nbar"
b = raw"""foo\nbar\\"""
b = raw"foo\nbar\\"
b = raw"foo bar"
b = raw"a\*b\+"

b = my"foo"
b = my"foo $a bar"

b = s"foo" // scalafix:ok

implicit class MyInterpolator(sc: StringContext) {
def my(subs: Any*): String = sc.toString + subs.mkString("")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@ class StringInterpolator {
b = "foo"
b = s"foo"
b = s"foo $a bar"
b = s"my \"quoted\" string"

b = """foo"""
b =
s"""foo
|bar"""
b = s"""my \"quoted\" string"""
b = s"""foo $a bar"""
b = s"""$a"""

b = f"foo"
b = f"foo $a%2.2f"
b = f"foo \n bar"

b = raw"foo $a \nbar"
b = raw"""foo\nbar\\"""
b = raw"foo\nbar\\"
b = raw"foo bar"
b = raw"a\*b\+"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
rules = RedundantSyntax
RedundantSyntax.stringInterpolator = true
*/

package test.redundantSyntax

class StringInterpolator {

val a = 42d

var b = ""
b = "foo"
b = s"foo"
b = s"foo $a bar"
b = s"my \"quoted\" string"

b = """foo"""
b =
s"""foo
|bar"""
b = s"""my \"quoted\" string"""
b = s"""foo $a bar"""
b = s"""$a"""

b = f"foo"
b = f"foo $a%2.2f"
b = f"foo \n bar"

b = raw"foo $a \nbar"
b = raw"""foo\nbar\\"""
b = raw"foo\nbar\\"
b = raw"foo bar"
b = raw"a\*b\+"

b = my"foo"
b = my"foo $a bar"

b = s"foo" // scalafix:ok

implicit class MyInterpolator(sc: StringContext) {
def my(subs: Any*): String = sc.toString + subs.mkString("")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test.redundantSyntax

class StringInterpolator {

val a = 42d

var b = ""
b = "foo"
b = "foo"
b = s"foo $a bar"
//b = s"my \"quoted\" string" does not compile as of 2.12.18 as https://github.com/scala/scala/pull/8830 was not backported

b = """foo"""
b =
"""foo
|bar"""
b = s"""my \"quoted\" string"""
b = s"""foo $a bar"""
b = s"""$a"""

b = "foo"
b = f"foo $a%2.2f"
b = "foo \n bar"

b = raw"foo $a \nbar"
b = """foo\nbar\\"""
b = raw"foo\nbar\\"
b = "foo bar"
b = raw"a\*b\+"

b = my"foo"
b = my"foo $a bar"

b = s"foo" // scalafix:ok

implicit class MyInterpolator(sc: StringContext) {
def my(subs: Any*): String = sc.toString + subs.mkString("")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,22 @@ class StringInterpolator {
b = "foo"
b = "foo"
b = s"foo $a bar"
b = "my \"quoted\" string"

b = """foo"""
b =
"""foo
|bar"""
b = s"""my \"quoted\" string"""
b = s"""foo $a bar"""
b = s"""$a"""

b = "foo"
b = f"foo $a%2.2f"
b = "foo \n bar"

b = raw"foo $a \nbar"
b = """foo\nbar\\"""
b = raw"foo\nbar\\"
b = "foo bar"
b = raw"a\*b\+"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test.redundantSyntax

class StringInterpolator {

val a = 42d

var b = ""
b = "foo"
b = "foo"
b = s"foo $a bar"
b = "my \"quoted\" string"

b = """foo"""
b =
"""foo
|bar"""
b = s"""my \"quoted\" string"""
b = s"""foo $a bar"""
b = s"""$a"""

b = "foo"
b = f"foo $a%2.2f"
b = "foo \n bar"

b = raw"foo $a \nbar"
b = """foo\nbar\\"""
b = raw"foo\nbar\\"
b = "foo bar"
b = raw"a\*b\+"

b = my"foo"
b = my"foo $a bar"

b = s"foo" // scalafix:ok

implicit class MyInterpolator(sc: StringContext) {
def my(subs: Any*): String = sc.toString + subs.mkString("")
}
}

0 comments on commit f4dcc6f

Please sign in to comment.