mirror of
https://github.com/cincheo/jsweet.git
synced 2025-12-14 23:09:22 +00:00
Added support for typscript string enums.
This commit is contained in:
parent
7d42942707
commit
8bb9b89243
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 "";
|
||||
|
||||
}
|
||||
@ -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).
|
||||
*/
|
||||
|
||||
@ -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
|
||||
*/
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user