@@ -15,16 +15,12 @@ import java.nio.charset.StandardCharsets
1515
1616import scala .build .EitherCps .{either , value }
1717import scala .build .Ops ._
18- import scala .build .errors .{
19- BuildException ,
20- CompositeBuildException ,
21- DependencyFormatError ,
22- UnusedDirectiveError
23- }
18+ import scala .build .errors ._
2419import scala .build .internal .{AmmUtil , Util }
2520import scala .build .options .{BuildOptions , BuildRequirements , ClassPathOptions , ShadowingSeq }
2621import scala .build .preprocessing .directives ._
2722import scala .build .{Inputs , Logger , Position , Positioned }
23+ import scala .collection .mutable
2824import scala .jdk .CollectionConverters ._
2925
3026case object ScalaPreprocessor extends Preprocessor {
@@ -362,76 +358,88 @@ case object ScalaPreprocessor extends Preprocessor {
362358 path : Either [String , os.Path ],
363359 logger : Logger
364360 ): Either [BuildException , ExtractedDirectives ] = {
361+ val errors = new mutable.ListBuffer [Diagnostic ]
362+ val reporter = CustomDirectivesReporter .create(path) { diag =>
363+ if (diag.severity == Severity .Warning )
364+ logger.log(Seq (diag))
365+ else
366+ errors += diag
367+ }
365368 val processor = {
366- val reporter = new DirectivesOutputStreamReporter (System .err) // TODO Get that via a logger
367369 val settings = new Settings
368370 settings.setAllowStartWithoutAt(true )
369371 settings.setAllowRequire(false )
370372 val context = new Context (reporter, settings)
371373 new UsingDirectivesProcessor (context)
372374 }
373375 val all = processor.extract(contentChars, true , true ).asScala
376+ if (errors.isEmpty) {
374377
375- def byKind (kind : UsingDirectiveKind ) = all.find(_.getKind == kind).get
378+ def byKind (kind : UsingDirectiveKind ) = all.find(_.getKind == kind).get
376379
377- def getDirectives (directives : UsingDirectives ) =
378- directives.getAst() match {
379- case ud : UsingDefs =>
380- ud.getUsingDefs().asScala
381- case _ =>
382- Nil
383- }
380+ def getDirectives (directives : UsingDirectives ) =
381+ directives.getAst() match {
382+ case ud : UsingDefs =>
383+ ud.getUsingDefs().asScala
384+ case _ =>
385+ Nil
386+ }
384387
385- val codeDirectives = byKind(UsingDirectiveKind .Code )
386- val specialCommentDirectives = byKind(UsingDirectiveKind .SpecialComment )
387- val plainCommentDirectives = byKind(UsingDirectiveKind .PlainComment )
388-
389- def reportWarning (msg : String , values : Seq [UsingDef ], before : Boolean = true ): Unit =
390- values.foreach { v =>
391- val astPos = v.getPosition()
392- val (start, end) =
393- if (before) (0 , astPos.getColumn())
394- else (astPos.getColumn(), astPos.getColumn() + v.getSyntax.getKeyword.size)
395- val position = Position .File (path, (astPos.getLine(), start), (astPos.getLine(), end))
396- logger.diagnostic(msg, positions = Seq (position))
397- }
388+ val codeDirectives = byKind(UsingDirectiveKind .Code )
389+ val specialCommentDirectives = byKind(UsingDirectiveKind .SpecialComment )
390+ val plainCommentDirectives = byKind(UsingDirectiveKind .PlainComment )
391+
392+ def reportWarning (msg : String , values : Seq [UsingDef ], before : Boolean = true ): Unit =
393+ values.foreach { v =>
394+ val astPos = v.getPosition()
395+ val (start, end) =
396+ if (before) (0 , astPos.getColumn())
397+ else (astPos.getColumn(), astPos.getColumn() + v.getSyntax.getKeyword.size)
398+ val position = Position .File (path, (astPos.getLine(), start), (astPos.getLine(), end))
399+ logger.diagnostic(msg, positions = Seq (position))
400+ }
398401
399- val usedDirectives =
400- if (! codeDirectives.getFlattenedMap().isEmpty()) {
401- val msg =
402- " This using directive is ignored. File contains directives outside comments and those have higher precedence."
403- reportWarning(
404- msg,
405- getDirectives(plainCommentDirectives) ++ getDirectives(specialCommentDirectives)
406- )
407- codeDirectives
408- }
409- else if (! specialCommentDirectives.getFlattenedMap().isEmpty()) {
410- val msg =
411- s " This using directive is ignored. $changeToSpecialCommentMsg"
412- reportWarning(msg, getDirectives(plainCommentDirectives))
413- specialCommentDirectives
414- }
415- else {
416- reportWarning(changeToSpecialCommentMsg, getDirectives(plainCommentDirectives))
417- plainCommentDirectives
418- }
402+ val usedDirectives =
403+ if (! codeDirectives.getFlattenedMap().isEmpty()) {
404+ val msg =
405+ " This using directive is ignored. File contains directives outside comments and those have higher precedence."
406+ reportWarning(
407+ msg,
408+ getDirectives(plainCommentDirectives) ++ getDirectives(specialCommentDirectives)
409+ )
410+ codeDirectives
411+ }
412+ else if (! specialCommentDirectives.getFlattenedMap().isEmpty()) {
413+ val msg =
414+ s " This using directive is ignored. $changeToSpecialCommentMsg"
415+ reportWarning(msg, getDirectives(plainCommentDirectives))
416+ specialCommentDirectives
417+ }
418+ else {
419+ reportWarning(changeToSpecialCommentMsg, getDirectives(plainCommentDirectives))
420+ plainCommentDirectives
421+ }
422+
423+ // All using directives should use just `using` keyword, no @using or require
424+ reportWarning(
425+ " Deprecated using directive syntax, please use keyword `using`." ,
426+ getDirectives(usedDirectives).filter(_.getSyntax() != UsingDirectiveSyntax .Using ),
427+ before = false
428+ )
429+
430+ val flattened = usedDirectives.getFlattenedMap.asScala.toSeq
431+ val strictDirectives =
432+ flattened.map { case (k, l) => StrictDirective (k.getPath.asScala.mkString(" ." ), l.asScala) }
419433
420- // All using directives should use just `using` keyword, no @using or require
421- reportWarning(
422- " Deprecated using directive syntax, please use keyword `using`." ,
423- getDirectives(usedDirectives).filter(_.getSyntax() != UsingDirectiveSyntax .Using ),
424- before = false
425- )
426-
427- val flattened = usedDirectives.getFlattenedMap.asScala.toSeq
428- val strictDirectives =
429- flattened.map { case (k, l) => StrictDirective (k.getPath.asScala.mkString(" ." ), l.asScala) }
430-
431- val offset =
432- if (usedDirectives.getKind() != UsingDirectiveKind .Code ) 0
433- else usedDirectives.getCodeOffset()
434- Right (ExtractedDirectives (offset, strictDirectives))
434+ val offset =
435+ if (usedDirectives.getKind() != UsingDirectiveKind .Code ) 0
436+ else usedDirectives.getCodeOffset()
437+ Right (ExtractedDirectives (offset, strictDirectives))
438+ }
439+ else {
440+ val errors0 = errors.map(diag => new MalformedDirectiveError (diag.message, diag.positions))
441+ Left (CompositeBuildException (errors0))
442+ }
435443 }
436444
437445 private def parseDependency (str : String , pos : Position ): Either [BuildException , AnyDependency ] =
0 commit comments