Skip to content

Commit 5458111

Browse files
committed
Refactor: apply tell don't ask principle
1 parent 0d67b86 commit 5458111

File tree

4 files changed

+20
-28
lines changed

4 files changed

+20
-28
lines changed

modules/build/src/main/scala/scala/build/preprocessing/ExtractedDirectives.scala

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
package scala.build.preprocessing
22

33
import com.virtuslab.using_directives.config.Settings
4-
import com.virtuslab.using_directives.custom.model.{
5-
UsingDirectiveKind,
6-
UsingDirectiveSyntax,
7-
UsingDirectives
8-
}
4+
import com.virtuslab.using_directives.custom.model.{UsingDirectiveKind, UsingDirectiveSyntax, UsingDirectives}
95
import com.virtuslab.using_directives.custom.utils.ast.{UsingDef, UsingDefs}
106
import com.virtuslab.using_directives.{Context, UsingDirectivesProcessor}
117

@@ -17,8 +13,7 @@ import scala.jdk.CollectionConverters._
1713

1814
case class ExtractedDirectives(
1915
offset: Int,
20-
directives: Seq[StrictDirective],
21-
kind: UsingDirectiveKind
16+
directives: Seq[StrictDirective]
2217
)
2318

2419
object ExtractedDirectives {
@@ -29,7 +24,8 @@ object ExtractedDirectives {
2924
def from(
3025
contentChars: Array[Char],
3126
path: Either[String, os.Path],
32-
logger: Logger
27+
logger: Logger,
28+
supportedDirectives: Array[UsingDirectiveKind]
3329
): Either[BuildException, ExtractedDirectives] = {
3430
val errors = new mutable.ListBuffer[Diagnostic]
3531
val reporter = CustomDirectivesReporter.create(path) { diag =>
@@ -110,7 +106,10 @@ object ExtractedDirectives {
110106
val offset =
111107
if (usedDirectives.getKind() != UsingDirectiveKind.Code) 0
112108
else usedDirectives.getCodeOffset()
113-
Right(ExtractedDirectives(offset, strictDirectives, usedDirectives.getKind()))
109+
if (supportedDirectives.contains(usedDirectives.getKind()))
110+
Right(ExtractedDirectives(offset, strictDirectives))
111+
else
112+
Left(new DirectiveErrors(::("Unsupported directive", Nil)))
114113
}
115114
else {
116115
val errors0 = errors.map(diag => new MalformedDirectiveError(diag.message, diag.positions))

modules/build/src/main/scala/scala/build/preprocessing/JavaPreprocessor.scala

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import com.virtuslab.using_directives.custom.model.UsingDirectiveKind
44

55
import java.nio.charset.StandardCharsets
66
import scala.build.EitherCps.{either, value}
7-
import scala.build.errors.{BuildException, DirectiveErrors}
7+
import scala.build.errors.BuildException
88
import scala.build.options.BuildRequirements
99
import scala.build.preprocessing.ExtractedDirectives.from
1010
import scala.build.preprocessing.ScalaPreprocessor._
@@ -19,9 +19,13 @@ case object JavaPreprocessor extends Preprocessor {
1919
case j: Inputs.JavaFile => Some(either {
2020
val content = value(PreprocessingUtil.maybeRead(j.path))
2121
val scopePath = ScopePath.fromPath(j.path)
22-
val ExtractedDirectives(_, directives0, kind) =
23-
value(from(content.toCharArray, Right(j.path), logger))
24-
val _ = value(assertKindIsNotCode(kind))
22+
val ExtractedDirectives(_, directives0) =
23+
value(from(
24+
content.toCharArray,
25+
Right(j.path),
26+
logger,
27+
Array(UsingDirectiveKind.PlainComment, UsingDirectiveKind.SpecialComment)
28+
))
2529
val updatedOptions = value(DirectivesProcessor.process(
2630
directives0,
2731
usingDirectiveHandlers,
@@ -54,14 +58,4 @@ case object JavaPreprocessor extends Preprocessor {
5458

5559
case _ => None
5660
}
57-
58-
private def assertKindIsNotCode(kind: UsingDirectiveKind) =
59-
kind match {
60-
case UsingDirectiveKind.PlainComment => Right(())
61-
case UsingDirectiveKind.SpecialComment => Right(())
62-
case UsingDirectiveKind.Code => Left(new DirectiveErrors(::(
63-
"Java doesn't support 'using' directives in the code",
64-
Nil
65-
)))
66-
}
6761
}

modules/build/src/main/scala/scala/build/preprocessing/ScalaPreprocessor.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package scala.build.preprocessing
22

3+
import com.virtuslab.using_directives.custom.model.UsingDirectiveKind
34
import dependency.AnyDependency
45
import dependency.parser.DependencyParser
56

@@ -9,11 +10,8 @@ import scala.build.Ops._
910
import scala.build.errors._
1011
import scala.build.internal.{AmmUtil, Util}
1112
import scala.build.options.{BuildOptions, BuildRequirements, ClassPathOptions, ShadowingSeq}
12-
import scala.build.preprocessing.ExtractedDirectives.from
1313
import scala.build.preprocessing.directives._
1414
import scala.build.{Inputs, Logger, Position, Positioned}
15-
import scala.collection.mutable
16-
import scala.jdk.CollectionConverters._
1715

1816
case object ScalaPreprocessor extends Preprocessor {
1917

@@ -268,7 +266,7 @@ case object ScalaPreprocessor extends Preprocessor {
268266
logger: Logger
269267
): Either[BuildException, StrictDirectivesProcessingOutput] = either {
270268
val contentChars = content.toCharArray
271-
val ExtractedDirectives(codeOffset, directives0, _) = value(ExtractedDirectives.from(contentChars, path, logger))
269+
val ExtractedDirectives(codeOffset, directives0) = value(ExtractedDirectives.from(contentChars, path, logger, UsingDirectiveKind.values()))
272270

273271
val updatedOptions = value {
274272
DirectivesProcessor.process(

modules/build/src/test/scala/scala/build/tests/ScalaPreprocessorTests.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package scala.build
22
package tests
33

44
import com.eed3si9n.expecty.Expecty.{assert => expect}
5+
import com.virtuslab.using_directives.custom.model.UsingDirectiveKind
56

67
import scala.build.errors.Diagnostic
78
import scala.build.preprocessing.ExtractedDirectives
@@ -39,7 +40,7 @@ class ScalaPreprocessorTests extends munit.FunSuite {
3940
private def testWarnings(lines: String*)(expectedWarnings: Check*): Unit = {
4041
val persistentLogger = new PersistentDiagnosticLogger(Logger.nop)
4142
val code = lines.mkString("\n").toCharArray()
42-
val res = ExtractedDirectives.from(code, Right(path), persistentLogger)
43+
val res = ExtractedDirectives.from(code, Right(path), persistentLogger, UsingDirectiveKind.values())
4344
expect(res.isRight)
4445

4546
val diags = persistentLogger.diagnostics

0 commit comments

Comments
 (0)