Skip to content

Commit

Permalink
Merge pull request #154 from olafurpg/dead-fail
Browse files Browse the repository at this point in the history
Filter out non-section warnings in fail blocks, fixes #151.
  • Loading branch information
olafurpg committed Feb 21, 2019
2 parents dc7a4f6 + 628b4f6 commit 014548f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ class MarkdownCompiler(
new BatchSourceFile(filename, new String(input.chars))
}

def fail(original: Seq[Tree], input: Input): String = {
def fail(original: Seq[Tree], input: Input, sectionPos: Position): String = {
sreporter.reset()
val run = new global.Run
run.compileSources(List(toSource(input)))
Expand All @@ -149,9 +149,11 @@ class MarkdownCompiler(
case sreporter.Info(pos, msgOrNull, gseverity) =>
val msg = nullableMessage(msgOrNull)
val mpos = toMetaPosition(edit, pos)
val severity = gseverity.toString().toLowerCase
val formatted = PositionSyntax.formatMessage(mpos, severity, msg, includePath = false)
ps.println(formatted)
if (sectionPos.contains(mpos) || gseverity == sreporter.ERROR) {
val severity = gseverity.toString().toLowerCase
val formatted = PositionSyntax.formatMessage(mpos, severity, msg, includePath = false)
ps.println(formatted)
}
}
out.toString()
}
Expand Down
7 changes: 5 additions & 2 deletions mdoc/src/main/scala/mdoc/internal/markdown/Renderer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,11 @@ object Renderer {
sb.append('\n')
binder.value match {
case FailSection(instrumented, startLine, startColumn, endLine, endColumn) =>
val compiled =
compiler.fail(doc.sections.map(_.source), Input.String(instrumented))
val compiled = compiler.fail(
doc.sections.map(_.source),
Input.String(instrumented),
section.source.pos
)
if (compiled.isEmpty) {
val tpos = new RangePosition(startLine, startColumn, endLine, endColumn)
reporter.error(
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/src/test/scala/tests/cli/ScalacOptionsSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,47 @@ class ScalacOptionsSuite extends BaseCliSuite {
)
)

// see https://github.com/scalameta/mdoc/issues/151
checkCli(
"dead-fail",
"""
|/index.md
|```scala mdoc
|final case class Test(value: Int)
|
|val test = Test(123)
|
|test.value
|```
|
|```scala mdoc:fail
|val x: Int = "123"
|```
|""".stripMargin,
"""|/index.md
|```scala
|final case class Test(value: Int)
|
|val test = Test(123)
|// test: Test = Test(123)
|
|test.value
|// res0: Int = 123
|```
|
|```scala
|val x: Int = "123"
|// error: type mismatch;
|// found : String("123")
|// required: Int
|// val x: Int = "123"
|// ^^^^^
|```
|""".stripMargin,
extraArgs = Array(
"--scalac-options",
"-Ywarn-value-discard"
)
)

}

0 comments on commit 014548f

Please sign in to comment.