[JS] Check the DECLARES_VARIABLE directive for all matching functions

Not just the first one.
This commit is contained in:
Sergej Jaskiewicz
2021-10-01 19:16:11 +03:00
committed by Space
parent 103be8f7bc
commit c84f9cde07
2 changed files with 28 additions and 10 deletions
@@ -24,18 +24,32 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.js.inline.util.CollectUtilsKt;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.jetbrains.kotlin.js.inline.util.CollectUtilsKt.collectNamedFunctions;
public class AstSearchUtil {
@NotNull
public static JsFunction getFunction(@NotNull JsNode searchRoot, String name) {
public static JsFunction getFunction(@NotNull JsNode searchRoot, @NotNull String name) {
JsFunction function = findByIdent(collectNamedFunctions(searchRoot), name);
assert function != null: "Function `" + name + "` was not found";
return function;
}
@NotNull
public static List<JsFunction> getFunctions(@NotNull JsNode searchRoot, @NotNull String name) {
Map<JsName, JsFunction> functions = collectNamedFunctions(searchRoot);
assert !functions.isEmpty() : "Function `" + name + "` was not found";
return functions
.entrySet()
.stream()
.filter(e -> e.getKey().getIdent().equals(name))
.map(Map.Entry::getValue)
.collect(Collectors.toList());
}
@NotNull
public static JsExpression getMetadataOrFunction(@NotNull JsNode searchRoot, @NotNull String name) {
JsExpression property = findByIdent(CollectUtilsKt.collectNamedFunctionsOrMetadata(searchRoot), name);
@@ -316,17 +316,21 @@ public class DirectiveTestUtils {
void processEntry(@NotNull JsNode ast, @NotNull ArgumentsHelper arguments) throws Exception {
String functionName = arguments.getNamedArgument("function");
String varName = arguments.getNamedArgument("name");
JsFunction function = AstSearchUtil.getFunction(ast, functionName);
List<JsFunction> functions = AstSearchUtil.getFunctions(ast, functionName);
boolean[] varDeclared = new boolean[1];
function.accept(new RecursiveJsVisitor() {
@Override
public void visit(@NotNull JsVars.JsVar x) {
super.visit(x);
if (x.getName().getIdent().equals(varName)) {
varDeclared[0] = true;
for (JsFunction function : functions) {
function.accept(new RecursiveJsVisitor() {
@Override
public void visit(@NotNull JsVars.JsVar x) {
super.visit(x);
if (x.getName().getIdent().equals(varName)) {
varDeclared[0] = true;
}
}
}
});
});
if (varDeclared[0])
break;
}
assertTrue("Function " + functionName + " does not declare variable " + varName, varDeclared[0]);
}