mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 15:29:22 +00:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
commit
f9e05f56cd
@ -262,8 +262,16 @@ public enum JSweetProblem {
|
||||
/**
|
||||
* Raised when using a candy which was generated for an older / newer version of the transpiler
|
||||
*/
|
||||
CANDY_VERSION_DISCREPANCY(Severity.WARNING);
|
||||
|
||||
CANDY_VERSION_DISCREPANCY(Severity.WARNING),
|
||||
/**
|
||||
* Raised when a Globals class declares a non static member.
|
||||
*/
|
||||
GLOBALS_CAN_ONLY_HAVE_STATIC_MEMBERS(Severity.ERROR),
|
||||
/**
|
||||
* Raised when a Globals class declares a superclass.
|
||||
*/
|
||||
GLOBALS_CLASS_CANNOT_HAVE_SUPERCLASS(Severity.ERROR);
|
||||
|
||||
private Severity severity;
|
||||
|
||||
/**
|
||||
@ -394,6 +402,10 @@ public enum JSweetProblem {
|
||||
return String.format("wrong use of @Ambient on %s: only types and globals can be declared as ambients", params);
|
||||
case CANDY_VERSION_DISCREPANCY:
|
||||
return String.format("candy %s:%s was generated for an older / newer version of the transpiler. current:%s previous:%s", params);
|
||||
case GLOBALS_CAN_ONLY_HAVE_STATIC_MEMBERS:
|
||||
return String.format("Globals classes can only define static members", params);
|
||||
case GLOBALS_CLASS_CANNOT_HAVE_SUPERCLASS:
|
||||
return String.format("Globals classes cannot extend any class", params);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@ -289,7 +289,7 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter {
|
||||
|
||||
|
||||
if (matchesMethod(targetClassName, targetMethodName, null, INDEXED_GET_FUCTION_NAME)) {
|
||||
if (isWithinGlobals(targetClassName)) {
|
||||
if (invocation.getArguments().size()==1 && isWithinGlobals(targetClassName)) {
|
||||
report(invocation, JSweetProblem.GLOBAL_INDEXER_GET);
|
||||
return true;
|
||||
}
|
||||
@ -306,7 +306,7 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter {
|
||||
return true;
|
||||
}
|
||||
if (matchesMethod(targetClassName, targetMethodName, null, INDEXED_GET_STATIC_FUCTION_NAME)) {
|
||||
if (isWithinGlobals(targetClassName)) {
|
||||
if (invocation.getArguments().size()==1 && isWithinGlobals(targetClassName)) {
|
||||
report(invocation, JSweetProblem.GLOBAL_INDEXER_GET);
|
||||
return true;
|
||||
}
|
||||
@ -317,7 +317,7 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter {
|
||||
|
||||
if (matchesMethod(targetClassName, targetMethodName, null, INDEXED_SET_FUCTION_NAME)) {
|
||||
|
||||
if (isWithinGlobals(targetClassName)) {
|
||||
if (invocation.getArguments().size()==2 && isWithinGlobals(targetClassName)) {
|
||||
report(invocation, JSweetProblem.GLOBAL_INDEXER_SET);
|
||||
return true;
|
||||
}
|
||||
@ -349,7 +349,7 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter {
|
||||
|
||||
if (matchesMethod(targetClassName, targetMethodName, null, INDEXED_SET_STATIC_FUCTION_NAME)) {
|
||||
|
||||
if (isWithinGlobals(targetClassName)) {
|
||||
if (invocation.getArguments().size()==2 && isWithinGlobals(targetClassName)) {
|
||||
report(invocation, JSweetProblem.GLOBAL_INDEXER_SET);
|
||||
return true;
|
||||
}
|
||||
@ -359,7 +359,7 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter {
|
||||
}
|
||||
|
||||
if (matchesMethod(targetClassName, targetMethodName, null, INDEXED_DELETE_FUCTION_NAME)) {
|
||||
if (isWithinGlobals(targetClassName)) {
|
||||
if (invocation.getArguments().size()==1 && isWithinGlobals(targetClassName)) {
|
||||
report(invocation, JSweetProblem.GLOBAL_DELETE);
|
||||
return true;
|
||||
}
|
||||
@ -373,7 +373,7 @@ public class Java2TypeScriptAdapter extends AbstractPrinterAdapter {
|
||||
}
|
||||
|
||||
if (matchesMethod(targetClassName, targetMethodName, null, INDEXED_DELETE_STATIC_FUCTION_NAME)) {
|
||||
if (isWithinGlobals(targetClassName)) {
|
||||
if (invocation.getArguments().size()==1 && isWithinGlobals(targetClassName)) {
|
||||
report(invocation, JSweetProblem.GLOBAL_DELETE);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -493,6 +493,9 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
||||
removedSuperclass = false;
|
||||
enumScope = false;
|
||||
boolean globals = JSweetConfig.GLOBALS_CLASS_NAME.equals(classdecl.name.toString());
|
||||
if(globals && classdecl.extending!=null) {
|
||||
report(classdecl, JSweetProblem.GLOBALS_CLASS_CANNOT_HAVE_SUPERCLASS);
|
||||
}
|
||||
if (!globals) {
|
||||
printDocComment(classdecl, false);
|
||||
if (!globalModule) {
|
||||
@ -708,6 +711,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!methodDecl.mods.getFlags().contains(Modifier.STATIC)) {
|
||||
report(methodDecl, methodDecl.name, JSweetProblem.GLOBALS_CAN_ONLY_HAVE_STATIC_MEMBERS);
|
||||
return;
|
||||
}
|
||||
|
||||
if (context.useModules) {
|
||||
if (!methodDecl.mods.getFlags().contains(Modifier.PRIVATE)) {
|
||||
print("export ");
|
||||
@ -878,6 +886,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
||||
|
||||
boolean globals = (parent instanceof JCClassDecl) && JSweetConfig.GLOBALS_CLASS_NAME.equals(((JCClassDecl) parent).name.toString());
|
||||
|
||||
if (globals && !varDecl.mods.getFlags().contains(Modifier.STATIC)) {
|
||||
report(varDecl, varDecl.name, JSweetProblem.GLOBALS_CAN_ONLY_HAVE_STATIC_MEMBERS);
|
||||
return;
|
||||
}
|
||||
|
||||
printDocComment(varDecl, false);
|
||||
if (!globals && parent instanceof JCClassDecl) {
|
||||
if (varDecl.mods.getFlags().contains(Modifier.PUBLIC)) {
|
||||
|
||||
@ -26,6 +26,7 @@ import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.jsweet.transpiler.JSweetProblem;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
import source.structural.AbstractClass;
|
||||
@ -47,11 +48,9 @@ import source.structural.WrongConstructsInEnums;
|
||||
import source.structural.WrongConstructsInInterfaces;
|
||||
import source.structural.globalclasses.Globals;
|
||||
import source.structural.globalclasses.a.GlobalsConstructor;
|
||||
import source.structural.globalclasses.b.GlobalFunctionStaticGetSet;
|
||||
import source.structural.globalclasses.c.GlobalFunctionGetSet;
|
||||
import source.structural.globalclasses.d.GlobalFunctionAccessFromMain;
|
||||
import source.structural.globalclasses.f.GlobalFunctionStaticDelete;
|
||||
import source.structural.globalclasses.g.GlobalFunctionDelete;
|
||||
|
||||
public class StructuralTests extends AbstractTest {
|
||||
|
||||
@ -178,17 +177,11 @@ public class StructuralTests extends AbstractTest {
|
||||
@Test
|
||||
public void testNoGetSetInGlobalFunction() {
|
||||
transpile(logHandler -> {
|
||||
logHandler.assertReportedProblems(GLOBAL_INDEXER_GET, GLOBAL_INDEXER_SET, GLOBAL_INDEXER_GET, GLOBAL_INDEXER_SET);
|
||||
logHandler.assertReportedProblems();
|
||||
} , getSourceFile(GlobalFunctionGetSet.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoStaticGetSetInGlobalFunction() {
|
||||
transpile(logHandler -> {
|
||||
logHandler.assertReportedProblems(GLOBAL_INDEXER_GET, GLOBAL_INDEXER_SET);
|
||||
} , getSourceFile(GlobalFunctionStaticGetSet.class));
|
||||
}
|
||||
|
||||
@Ignore
|
||||
@Test
|
||||
public void testNoStaticDeleteInGlobalFunction() {
|
||||
transpile(logHandler -> {
|
||||
@ -196,13 +189,6 @@ public class StructuralTests extends AbstractTest {
|
||||
} , getSourceFile(GlobalFunctionStaticDelete.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoDeleteInGlobalFunction() {
|
||||
transpile(logHandler -> {
|
||||
logHandler.assertReportedProblems(GLOBAL_DELETE);
|
||||
} , getSourceFile(GlobalFunctionDelete.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGlobalFunctionAccessFromMain() {
|
||||
eval((logHandler, r) -> {
|
||||
@ -247,4 +233,12 @@ public class StructuralTests extends AbstractTest {
|
||||
} , getSourceFile(GlobalsAccess.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWrongGlobals() {
|
||||
transpile(logHandler -> {
|
||||
logHandler.assertReportedProblems(JSweetProblem.GLOBALS_CLASS_CANNOT_HAVE_SUPERCLASS, JSweetProblem.GLOBAL_CONSTRUCTOR_DEF,
|
||||
JSweetProblem.GLOBALS_CAN_ONLY_HAVE_STATIC_MEMBERS, JSweetProblem.GLOBALS_CAN_ONLY_HAVE_STATIC_MEMBERS);
|
||||
} , getSourceFile(source.structural.wrongglobals.Globals.class));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -20,10 +20,10 @@ import static jsweet.util.Globals.$set;
|
||||
|
||||
class Globals {
|
||||
|
||||
public int a;
|
||||
public static int a;
|
||||
|
||||
public Globals() {
|
||||
this.a = 3;
|
||||
a = 3;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,35 +0,0 @@
|
||||
/*
|
||||
* 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.structural.globalclasses.b;
|
||||
|
||||
import static jsweet.util.Globals.$get;
|
||||
import static jsweet.util.Globals.$set;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
class Globals {
|
||||
|
||||
public void test() {
|
||||
Object val = $get(this, "ttest");
|
||||
$set(this, "ttest", val);
|
||||
}
|
||||
}
|
||||
|
||||
public class GlobalFunctionStaticGetSet {
|
||||
|
||||
public void main(String[] args) {
|
||||
}
|
||||
}
|
||||
@ -16,15 +16,21 @@
|
||||
*/
|
||||
package source.structural.globalclasses.c;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
class Globals extends jsweet.lang.Object {
|
||||
import static jsweet.util.Globals.$get;
|
||||
import static jsweet.util.Globals.$set;
|
||||
|
||||
public void test() {
|
||||
// invalid
|
||||
Object val = this.$get("ttest");
|
||||
this.$set("ttest", val);
|
||||
val = $get("ttest");
|
||||
$set("ttest", val);
|
||||
@SuppressWarnings("all")
|
||||
class Globals {
|
||||
|
||||
public static void test() {
|
||||
Object val;
|
||||
// TODO: invalid
|
||||
//val = $get(Globals.class, "ttest");
|
||||
//$set(Globals.class, "ttest", val);
|
||||
val = $get(new GlobalFunctionGetSet(), "ttest");
|
||||
$set(new GlobalFunctionGetSet(), "ttest", val);
|
||||
val = $get(GlobalFunctionGetSet.class, "ttest");
|
||||
$set(GlobalFunctionGetSet.class, "ttest", val);
|
||||
|
||||
// valid
|
||||
jsweet.lang.Object otherObject = null;
|
||||
|
||||
@ -21,8 +21,8 @@ import static jsweet.util.Globals.$delete;
|
||||
@SuppressWarnings("all")
|
||||
class Globals {
|
||||
|
||||
public void test() {
|
||||
$delete(this, "key2s");
|
||||
public static void test() {
|
||||
$delete(Globals.class, "key2s");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,32 +0,0 @@
|
||||
/*
|
||||
* 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.structural.globalclasses.g;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
class Globals extends jsweet.lang.Object {
|
||||
|
||||
public void test() {
|
||||
// invalid
|
||||
this.$delete("ttest");
|
||||
}
|
||||
}
|
||||
|
||||
public class GlobalFunctionDelete {
|
||||
|
||||
public void main(String[] args) {
|
||||
}
|
||||
}
|
||||
26
src/test/java/source/structural/wrongglobals/Globals.java
Normal file
26
src/test/java/source/structural/wrongglobals/Globals.java
Normal file
@ -0,0 +1,26 @@
|
||||
package source.structural.wrongglobals;
|
||||
|
||||
import static jsweet.util.Globals.$get;
|
||||
|
||||
public class Globals extends SuperClass {
|
||||
|
||||
public Globals() {
|
||||
}
|
||||
|
||||
public void m() {
|
||||
}
|
||||
|
||||
public boolean b = true;
|
||||
|
||||
public static void staticM() {
|
||||
$get(B, "p");
|
||||
}
|
||||
|
||||
public static boolean B = true;
|
||||
|
||||
}
|
||||
|
||||
class SuperClass {
|
||||
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user