Repository: scalameta/scalameta
Pull request #3460 (2024-01-03 22:24:31)

Origin: add flag enabling unquotes to ParsedSource
Helps with #3425.


Patch:
diff --git a/scalameta/dialects/shared/src/main/scala/scala/meta/Dialect.scala b/scalameta/dialects/shared/src/main/scala/scala/meta/Dialect.scala
index 636772bf5..b01fc90cc 100644
--- a/scalameta/dialects/shared/src/main/scala/scala/meta/Dialect.scala
+++ b/scalameta/dialects/shared/src/main/scala/scala/meta/Dialect.scala
@@ -757,6 +757,13 @@ final class Dialect private[meta] (
     allowMultilinePrograms = true
   )
 
+  private[meta] def unquotesEnabled: Dialect =
+    privateCopy(
+      allowTermUnquotes = true,
+      allowMultilinePrograms = true,
+      allowTypeLambdas = true
+    )
+
   private[meta] def unquoteTerm(multiline: Boolean): Dialect = {
     require(!allowUnquotes)
     privateCopy(
diff --git a/scalameta/quasiquotes/shared/src/main/scala/scala/meta/internal/quasiquotes/ReificationMacros.scala b/scalameta/quasiquotes/shared/src/main/scala/scala/meta/internal/quasiquotes/ReificationMacros.scala
index fc81b86fa..be29216c6 100644
--- a/scalameta/quasiquotes/shared/src/main/scala/scala/meta/internal/quasiquotes/ReificationMacros.scala
+++ b/scalameta/quasiquotes/shared/src/main/scala/scala/meta/internal/quasiquotes/ReificationMacros.scala
@@ -61,7 +61,8 @@ class ReificationMacros(val c: Context) extends AstReflection with AdtLiftables
     val skeleton = parseSkeleton(parser, input, dialect)
     val sourceTree = q"""
       new _root_.scala.meta.internal.trees.Origin.ParsedSource(
-        _root_.scala.meta.inputs.Input.String(${input.text})
+        _root_.scala.meta.inputs.Input.String(${input.text}),
+        unquote = true
       )($dialectTree)
     """
     reifySkeleton(skeleton, mode, sourceTree)
diff --git a/scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/Origin.scala b/scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/Origin.scala
index 4af9a5db2..24513b5b3 100644
--- a/scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/Origin.scala
+++ b/scalameta/trees/shared/src/main/scala/scala/meta/internal/trees/Origin.scala
@@ -40,8 +40,9 @@ object Origin {
     @inline def dialect: Dialect = source.dialect
   }
 
-  class ParsedSource(val input: Input)(implicit val dialect: Dialect) {
-    lazy val tokenized = implicitly[Tokenize].apply(input, dialect)
+  class ParsedSource(val input: Input, unquote: Boolean = false)(implicit val dialect: Dialect) {
+    lazy val tokenized =
+      implicitly[Tokenize].apply(input, if (unquote) dialect.unquotesEnabled else dialect)
     @inline def tokens = tokenized.get
   }
 


Test patch:
diff --git a/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/SyntacticSuite.scala b/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/SyntacticSuite.scala
index 93bb46d23..2e8210f68 100644
--- a/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/SyntacticSuite.scala
+++ b/tests/shared/src/test/scala/scala/meta/tests/prettyprinters/SyntacticSuite.scala
@@ -1083,6 +1083,10 @@ class SyntacticSuite extends scala.meta.tests.parsers.ParseSuite {
     val source2 = new Origin.ParsedSource(input)
     val tree2 = tree1.withOrigin(new Origin.Parsed(source2, 0, numTokens))
     assertNotEquals(tree2.tokens.length, numTokens)
+
+    val source3 = new Origin.ParsedSource(input, unquote = true)
+    val tree3 = tree1.withOrigin(new Origin.Parsed(source3, 0, numTokens))
+    assertEquals(tree3.tokens.length, numTokens)
   }
 
   test("interpolator braces for plain identifiers") {