mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-15 15:29:22 +00:00
Merge pull request #606 from wirthedv/develop
Support for string enums (typescript)
This commit is contained in:
commit
2d17010e88
41
core-lib/es6/src/main/java/jsweet/lang/StringEnum.java
Normal file
41
core-lib/es6/src/main/java/jsweet/lang/StringEnum.java
Normal 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 "";
|
||||
|
||||
}
|
||||
@ -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>
|
||||
|
||||
@ -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).
|
||||
*/
|
||||
|
||||
@ -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;
|
||||
@ -314,6 +315,8 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
|
||||
|
||||
private boolean enumWrapperClassScope = false;
|
||||
|
||||
private boolean enumString = false;
|
||||
|
||||
private boolean removedSuperclass = false;
|
||||
|
||||
private boolean declareClassScope;
|
||||
@ -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();
|
||||
|
||||
@ -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;
|
||||
@ -200,4 +201,8 @@ public class EnumTests extends AbstractTest {
|
||||
}, getSourceFile(EnumInOtherPackage.class), getSourceFile(EnumWrapper.class), getSourceFile(SwitchWithEnumWrapper.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStringEnum() {
|
||||
transpile(TestTranspilationHandler::assertNoProblems, getSourceFile(StringEnumType.class));
|
||||
}
|
||||
}
|
||||
|
||||
@ -0,0 +1,9 @@
|
||||
package source.enums;
|
||||
|
||||
|
||||
import jsweet.lang.StringEnum;
|
||||
|
||||
@StringEnum
|
||||
public enum StringEnumType {
|
||||
TEST1, TEST2, TEST3
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user