From 190dc23f7063787e145b1cf1c061259829681504 Mon Sep 17 00:00:00 2001 From: Eugene Yokota Date: Wed, 11 Apr 2018 01:31:38 -0400 Subject: [PATCH] Fixes linter that detects missing .value Fixes #4079 #3216 introduced a linter that checks against missing `.value`, but the tree only checked for `Ident`. This doesn't work because in reality the symbols of build.sbt are transformed to `$somehash.npmInstallTask` where `somehash` is the wrapper object we create. Similarly for the built-in keys, they are presented as `sbt.Keys.compile`. With this change unused task will fail to load the build with the following message: ``` /sbt-4079/build.sbt:26: error: The key `compile` is not being invoked inside the task definition. Problem: Keys missing `.value` are not initialized and their dependency is not registered. Solution: Replace `compile` by `compile.value` or remove it if unused. compile ^ /sbt-4079/build.sbt:27: error: The key `npmInstallTask` is not being invoked inside the task definition. Problem: Keys missing `.value` are not initialized and their dependency is not registered. Solution: Replace `npmInstallTask` by `npmInstallTask.value` or remove it if unused. npmInstallTask ^ ``` --- .../main/scala/sbt/std/TaskLinterDSL.scala | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/main-settings/src/main/scala/sbt/std/TaskLinterDSL.scala b/main-settings/src/main/scala/sbt/std/TaskLinterDSL.scala index a328cee70..1c47216fa 100644 --- a/main-settings/src/main/scala/sbt/std/TaskLinterDSL.scala +++ b/main-settings/src/main/scala/sbt/std/TaskLinterDSL.scala @@ -27,6 +27,7 @@ abstract class BaseTaskLinterDSL extends LinterDSL { private val taskKeyType = typeOf[sbt.TaskKey[_]] private val settingKeyType = typeOf[sbt.SettingKey[_]] private val inputKeyType = typeOf[sbt.InputKey[_]] + private val initializeType = typeOf[sbt.Def.Initialize[_]] private val uncheckedWrappers = MutableSet.empty[Tree] var insideIf: Boolean = false var insideAnon: Boolean = false @@ -57,7 +58,7 @@ abstract class BaseTaskLinterDSL extends LinterDSL { } @inline def isKey(tpe: Type): Boolean = - tpe <:< taskKeyType || tpe <:< settingKeyType || tpe <:< inputKeyType + tpe <:< initializeType || tpe <:< taskKeyType || tpe <:< settingKeyType || tpe <:< inputKeyType def detectAndErrorOnKeyMissingValue(i: Ident): Unit = { if (isKey(i.tpe)) { @@ -66,6 +67,13 @@ abstract class BaseTaskLinterDSL extends LinterDSL { } else () } + def detectAndErrorOnKeyMissingValue(s: Select): Unit = { + if (isKey(s.tpe)) { + val keyName = s.name.decodedName.toString + ctx.error(s.pos, TaskLinterDSLFeedback.missingValueForKey(keyName)) + } else () + } + override def traverse(tree: ctx.universe.Tree): Unit = { tree match { case ap @ Apply(TypeApply(Select(_, nme), tpe :: Nil), qual :: Nil) => @@ -118,11 +126,13 @@ abstract class BaseTaskLinterDSL extends LinterDSL { // TODO: Consider using unused names analysis to be able to report on more cases case ValDef(_, valName, _, rhs) if valName == termNames.WILDCARD => rhs match { - case i: Ident => detectAndErrorOnKeyMissingValue(i) - case _ => () + case i: Ident => detectAndErrorOnKeyMissingValue(i) + case s: Select => detectAndErrorOnKeyMissingValue(s) + case _ => () } - case i: Ident => detectAndErrorOnKeyMissingValue(i) - case _ => () + case i: Ident => detectAndErrorOnKeyMissingValue(i) + case s: Select => detectAndErrorOnKeyMissingValue(s) + case t => () } } traverseTrees(stmts)