handle hidden global function with fully qualified names

This commit is contained in:
Renaud Pawlak 2016-01-06 15:29:38 +01:00
parent 5e17a74bae
commit 1e93140e24
3 changed files with 17 additions and 3 deletions

View File

@ -61,12 +61,14 @@ import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Symbol.MethodSymbol;
import com.sun.tools.javac.code.Symbol.PackageSymbol;
import com.sun.tools.javac.code.Symbol.TypeSymbol;
import com.sun.tools.javac.code.Symbol.VarSymbol;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.tree.JCTree.JCClassDecl;
import com.sun.tools.javac.tree.JCTree.JCExpression;
import com.sun.tools.javac.tree.JCTree.JCFieldAccess;
import com.sun.tools.javac.tree.JCTree.JCIdent;
import com.sun.tools.javac.tree.JCTree.JCImport;
import com.sun.tools.javac.tree.JCTree.JCMethodDecl;
import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;
import com.sun.tools.javac.tree.JCTree.JCNewClass;
import com.sun.tools.javac.tree.JCTree.JCTypeApply;
@ -389,6 +391,11 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter {
getPrinter().print(JSweetConfig.GLOBALS_PACKAGE_NAME).print(".");
}
}
Map<String, VarSymbol> vars = new HashMap<>();
Util.fillAllVariablesInScope(vars, getPrinter().getStack(), invocation, getPrinter().getParent(JCMethodDecl.class));
if(vars.containsKey(targetMethodName)) {
report(invocation, JSweetProblem.HIDDEN_INVOCATION, targetMethodName);
}
getPrinter().printIdentifier(targetMethodName).print("(").printArgList(invocation.args).print(")");
return true;
}

View File

@ -69,7 +69,7 @@ public class StructuralTests extends AbstractTest {
@Test
public void testVariableMethodNameClashes() {
transpile(logHandler -> {
logHandler.assertReportedProblems(JSweetProblem.HIDDEN_INVOCATION, JSweetProblem.HIDDEN_INVOCATION);
logHandler.assertReportedProblems(JSweetProblem.HIDDEN_INVOCATION, JSweetProblem.HIDDEN_INVOCATION, JSweetProblem.HIDDEN_INVOCATION);
} , getSourceFile(NameClashesWithMethodInvocations.class));
}

View File

@ -32,10 +32,17 @@ public class NameClashesWithMethodInvocations {
}
public void m3() {
// name clash between local variable and method call
// not a name clash
@SuppressWarnings("unused")
String m2 = "test";
m2();
}
public void m4(boolean alert) {
// name clash between local variable and qualified method call
//String alert = "test";
jsweet.dom.Globals.alert(alert);
}
}