Merge pull request #606 from wirthedv/develop

Support for string enums (typescript)
This commit is contained in:
Renaud Pawlak 2020-07-25 10:18:20 +02:00 committed by GitHub
commit 2d17010e88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 100 additions and 27 deletions

View File

@ -0,0 +1,41 @@
/*
* 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 jsweet.lang;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* This annotation is used to annotate enums to generate a TS string enum.
*
* @author Sebastian Schneider
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE })
@Documented
public @interface StringEnum {
/**
* Defines the actual value of the string type, to be used in case the
* string is not a valid Java identifier.
*/
String value() default "";
}

View File

@ -147,7 +147,7 @@
<excludePackageNames>org.jsweet.transpiler.model.support</excludePackageNames>
<fixTags>all</fixTags>
<failOnError>false</failOnError>
<!-- leave these options unchanged to place the Javadoc at the right
<!-- leave these options unchanged to place the Javadoc at the right
place for jsweet.org -->
<reportOutputDirectory>/var/www/apidocs/org/jsweet</reportOutputDirectory>
<destDir>jsweet-transpiler-${project.version}</destDir>
@ -248,7 +248,7 @@
<version>1.4.0.1</version>
</dependency>
<!-- Compile-time only dependency (JSweet looks up installed JDK at runtime
<!-- Compile-time only dependency (JSweet looks up installed JDK at runtime
to conform to legal obligations) -->
<dependency>
<groupId>com.sun</groupId>
@ -260,7 +260,7 @@
<dependency>
<groupId>org.jsweet</groupId>
<artifactId>jsweet-core</artifactId>
<version>6.0.3</version>
<version>6.0.4</version>
<scope>test</scope>
<optional>true</optional>
</dependency>

View File

@ -1,12 +1,12 @@
/*
/*
* JSweet transpiler - http://www.jsweet.org
* Copyright (C) 2015 CINCHEO SAS <renaud.pawlak@cincheo.fr>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -32,7 +32,7 @@ import org.apache.log4j.Logger;
/**
* This class contains static constants and utilities.
*
*
* @author Renaud Pawlak
*/
@SuppressWarnings("serial")
@ -73,7 +73,7 @@ public abstract class JSweetConfig {
/**
* Initialize the classpath to include tools.jar.
*
*
* @param jdkHome
* the jdkHome option value (if not set or not found, fall back
* to the JAVA_HOME environment variable)
@ -295,6 +295,10 @@ public abstract class JSweetConfig {
* Fully-qualified name for the JSweet <code>@StringType</code> annotation (see JSweet core API).
*/
public static final String ANNOTATION_STRING_TYPE = JSweetConfig.LANG_PACKAGE + ".StringType";
/**
* Fully-qualified name for the JSweet <code>@StringEnum</code> annotation (see JSweet core API).
*/
public static final String ANNOTATION_STRING_ENUM = JSweetConfig.LANG_PACKAGE + ".StringEnum";
/**
* Fully-qualified name for the JSweet <code>@Root</code> annotation (see JSweet core API).
*/

View File

@ -1,12 +1,12 @@
/*
/*
* JSweet transpiler - http://www.jsweet.org
* Copyright (C) 2015 CINCHEO SAS <renaud.pawlak@cincheo.fr>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
@ -20,6 +20,7 @@ package org.jsweet.transpiler;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.jsweet.JSweetConfig.ANNOTATION_ERASED;
import static org.jsweet.JSweetConfig.ANNOTATION_STRING_ENUM;
import static org.jsweet.JSweetConfig.UTIL_CLASSNAME;
import static org.jsweet.JSweetConfig.ANNOTATION_FUNCTIONAL_INTERFACE;
import static org.jsweet.JSweetConfig.ANNOTATION_OBJECT_TYPE;
@ -162,7 +163,7 @@ import com.sun.tools.javac.util.Name;
/**
* This is a TypeScript printer for translating the Java AST to a TypeScript
* program.
*
*
* @author Renaud Pawlak
*/
public class Java2TypeScriptTranslator extends AbstractTreePrinter {
@ -248,7 +249,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
/**
* A state flag indicating the comparison mode to be used by this printer for
* printing comparison operators.
*
*
* @author Renaud Pawlak
*/
public static enum ComparisonMode {
@ -273,7 +274,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
/**
* Selects a comparison mode for subsequently printed comparison operators.
*
*
* @see #exitComparisonMode()
*/
public void enterComparisonMode(ComparisonMode comparisonMode) {
@ -282,7 +283,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
/**
* Exits a comparison mode and go back to the previous one.
*
*
* @see #enterComparisonMode(ComparisonMode)
*/
public void exitComparisonMode() {
@ -314,6 +315,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
private boolean enumWrapperClassScope = false;
private boolean enumString = false;
private boolean removedSuperclass = false;
private boolean declareClassScope;
@ -497,7 +500,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
/**
* Enters a new class scope.
*
*
* @see #exitScope()
*/
public void enterScope() {
@ -506,7 +509,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
/**
* Exits a class scope.
*
*
* @see #enterScope()
*/
public void exitScope() {
@ -515,7 +518,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
/**
* Creates a new TypeScript translator.
*
*
* @param adapter an object that can tune various aspects of the
* TypeScript code generation
* @param logHandler the handler for logging and error reporting
@ -1501,6 +1504,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
} else {
print("enum ");
getScope().enumScope = true;
if (context.hasAnnotationType(classdecl.sym, ANNOTATION_STRING_ENUM)) {
getScope().enumString = true;
getScope().enumWrapperClassScope = false;
getScope().isComplexEnum = false;
}
}
} else {
if (getScope().declareClassScope && !(getIndent() != 0 && isDefinitionScope)) {
@ -1774,6 +1782,12 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
}
int pos = getCurrentPosition();
print(def);
if (getScope().enumScope && JCTree.Tag.VARDEF == def.getTag() && getScope().enumString) {
print(" = \"");
print(def);
print("\"");
}
if (getCurrentPosition() == pos) {
if (!getScope().enumScope) {
removeLastIndent();
@ -2695,7 +2709,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
/**
* Print async keyword for given method if relevant. Prints nothing if method
* shouldn't be async
*
*
*/
protected void printAsyncKeyword(JCMethodDecl methodDecl) {
if (getScope().declareClassScope || getScope().interfaceScope) {
@ -5812,7 +5826,7 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
/**
* Prints either a string, or the tree if the the string is null.
*
*
* @param exprStr a string to be printed as is if not null
* @param expr a tree to be printed if exprStr is null
*/

View File

@ -1,13 +1,13 @@
/*
/*
* 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.
@ -40,6 +40,7 @@ import source.enums.EnumsImplementingInterfaces;
import source.enums.EnumsReflection;
import source.enums.ErasedEnum;
import source.enums.MyComplexEnum2;
import source.enums.StringEnumType;
import source.enums.StringEnums;
import source.enums.SwitchWithEnumWrapper;
import source.enums.other.ComplexEnumsAccess;
@ -101,7 +102,7 @@ public class EnumTests extends AbstractTest {
Assert.assertEquals(">", r.get("trace"));
}, getSourceFile(EnumsReflection.class));
}
@Test
public void testComplexEnumsAccess() {
eval(ModuleKind.commonjs, (logHandler, r) -> {
@ -131,7 +132,7 @@ public class EnumTests extends AbstractTest {
Assert.assertEquals(">ok1,ok2", r.get("trace"));
}, getSourceFile(ComplexEnumWithAbstractMethods.class));
transpilerTest().getTranspiler().setBundle(false);
TranspilerTestRunner transpilerTest = new TranspilerTestRunner(getCurrentTestOutDir(), new AddRootFactory());
transpilerTest.eval((logHandler, r) -> {
assertEquals("There should be no errors", 0, logHandler.reportedProblems.size());
@ -199,5 +200,9 @@ public class EnumTests extends AbstractTest {
logHandler.assertNoProblems();
}, getSourceFile(EnumInOtherPackage.class), getSourceFile(EnumWrapper.class), getSourceFile(SwitchWithEnumWrapper.class));
}
@Test
public void testStringEnum() {
transpile(TestTranspilationHandler::assertNoProblems, getSourceFile(StringEnumType.class));
}
}

View File

@ -0,0 +1,9 @@
package source.enums;
import jsweet.lang.StringEnum;
@StringEnum
public enum StringEnumType {
TEST1, TEST2, TEST3
}