mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 15:29:22 +00:00
#14: Fixed issue with cast methods when being invocation targets
This commit is contained in:
parent
ed9b8c5cdc
commit
238e60bc3e
@ -246,37 +246,38 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "array")) {
|
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "array")) {
|
||||||
getPrinter().print(invocation.args.head);
|
printCastMethodInvocation(invocation);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "function")) {
|
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "function")) {
|
||||||
getPrinter().printArgList(invocation.args);
|
printCastMethodInvocation(invocation);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "string")) {
|
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "string")) {
|
||||||
getPrinter().printArgList(invocation.args);
|
printCastMethodInvocation(invocation);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "bool")) {
|
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "bool")) {
|
||||||
getPrinter().printArgList(invocation.args);
|
printCastMethodInvocation(invocation);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "number")) {
|
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "number")) {
|
||||||
getPrinter().printArgList(invocation.args);
|
printCastMethodInvocation(invocation);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "integer")) {
|
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "integer")) {
|
||||||
getPrinter().printArgList(invocation.args);
|
printCastMethodInvocation(invocation);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "object")) {
|
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "object")) {
|
||||||
getPrinter().printArgList(invocation.args);
|
printCastMethodInvocation(invocation);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "union")) {
|
if (matchesMethod(targetClassName, targetMethodName, UTIL_CLASSNAME, "union")) {
|
||||||
getPrinter().typeChecker.checkUnionTypeAssignment(getPrinter().getContext().types, getPrinter().getParent(), invocation);
|
getPrinter().typeChecker.checkUnionTypeAssignment(getPrinter().getContext().types, getPrinter().getParent(), invocation);
|
||||||
getPrinter().print("<any>");
|
getPrinter().print("(<any>");
|
||||||
getPrinter().printArgList(invocation.args);
|
printCastMethodInvocation(invocation);
|
||||||
|
getPrinter().print(")");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -418,6 +419,16 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter {
|
|||||||
return super.substituteMethodInvocation(invocation);
|
return super.substituteMethodInvocation(invocation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void printCastMethodInvocation(JCMethodInvocation invocation) {
|
||||||
|
if(getPrinter().getParent() instanceof JCMethodInvocation) {
|
||||||
|
getPrinter().print("(");
|
||||||
|
}
|
||||||
|
getPrinter().print(invocation.args.head);
|
||||||
|
if(getPrinter().getParent() instanceof JCMethodInvocation) {
|
||||||
|
getPrinter().print(")");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean substituteFieldAccess(JCFieldAccess fieldAccess) {
|
public boolean substituteFieldAccess(JCFieldAccess fieldAccess) {
|
||||||
String name = fieldAccess.name.toString();
|
String name = fieldAccess.name.toString();
|
||||||
|
|||||||
@ -29,6 +29,7 @@ import org.junit.Test;
|
|||||||
import source.syntax.AnnotationQualifiedNames;
|
import source.syntax.AnnotationQualifiedNames;
|
||||||
import source.syntax.FinalVariables;
|
import source.syntax.FinalVariables;
|
||||||
import source.syntax.FinalVariablesRuntime;
|
import source.syntax.FinalVariablesRuntime;
|
||||||
|
import source.syntax.GlobalsCastMethod;
|
||||||
import source.syntax.GlobalsInvocation;
|
import source.syntax.GlobalsInvocation;
|
||||||
import source.syntax.IndexedAccessInStaticScope;
|
import source.syntax.IndexedAccessInStaticScope;
|
||||||
import source.syntax.Keywords;
|
import source.syntax.Keywords;
|
||||||
@ -144,4 +145,12 @@ public class SyntaxTests extends AbstractTest {
|
|||||||
} , getSourceFile(ValidIndexedAccesses.class));
|
} , getSourceFile(ValidIndexedAccesses.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGlobalCastMethod() {
|
||||||
|
transpile((logHandler) -> {
|
||||||
|
logHandler.assertReportedProblems();
|
||||||
|
} , getSourceFile(GlobalsCastMethod.class));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
35
src/test/java/source/syntax/GlobalsCastMethod.java
Normal file
35
src/test/java/source/syntax/GlobalsCastMethod.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
/*
|
||||||
|
* JSweet - http://www.jsweet.org
|
||||||
|
* Copyright (C) 2015 CINCHEO SAS <renaud.pawlak@cincheo.fr>
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
package source.syntax;
|
||||||
|
|
||||||
|
import static jsweet.util.Globals.number;
|
||||||
|
|
||||||
|
public class GlobalsCastMethod {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
double n = 5;
|
||||||
|
|
||||||
|
// this invocation must preserve parentheses on target
|
||||||
|
number(n / 2).toFixed(0);
|
||||||
|
|
||||||
|
// this invocation does not
|
||||||
|
number(n / 2);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user