IntelliJ 的搜索模板语言是否有办法识别未被 if 语句包装的函数调用

Does IntelliJ's search template language have a way to identify function calls not wrapped by an if statement

我正在查看 PhpStorm 中的一些遗留代码,代码库中有一些实例,其中特定函数调用被 if 语句中的另一个函数调用包装,例如

if (thisIsTrue($param1, $param2)) {
    // possibly some function calls above
    callThisFunction($paramA, $paramB);
    // possibly some function calls below
}

我仅使用基本的 Linux find/grep 命令来突出显示感兴趣的文件就取得了一些成功,但我希望能够创建一个结构搜索检查来查找 callThisFunction 没有被特定的 if 语句包装。

有谁知道使用 vanilla IntelliJ 搜索模板功能是否可行?

简短的回答是(仍然)不。

如果您复制现有模板“Logging without if”(在运算符类别中)或导入此 XML:[=15,则可以定义“不在 if 语句内”模板约束=]

<searchConfiguration name="Logging without if" text="LOG.debug($Argument$);" recursive="false" caseInsensitive="true" type="JAVA" pattern_context="default">
  <constraint name="__context__" negateWithin="true" within="statement in if" contains="" />
  <constraint name="Argument" minCount="0" maxCount="2147483647" within="" contains="" />
</searchConfiguration>

但是,当 if 主体有多个语句时它不匹配,当我将文本修改为 $Statement1$; log.tracef($Argument$); $Statement2$; 时我的 IntelliJ 挂起,语句的计数约束与参数的计数约束相同。

您可以改为 运行 2 次搜索:一次仅 callThisFunction($paramA, $paramB); 一次搜索整个模板,然后比较结果。

如果将结构搜索转换为检查,然后从命令行 运行 这些检查 (https://www.jetbrains.com/help/idea/command-line-code-inspector.html)

,比较结果应该会更容易

有关“范围内”约束的详细信息,请参阅 https://ijnspector.wordpress.com/2020/06/11/contained-in-constraints/