add a switch parameter to the @Decorator annotation so that the decorator function can be made optional by the developer

This commit is contained in:
Renaud Pawlak 2020-08-09 09:35:51 +02:00
parent 49f67a25e3
commit 8e276782e8
5 changed files with 32 additions and 17 deletions

View File

@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.jsweet</groupId>
<artifactId>jsweet-core</artifactId>
<version>6.0.5</version>
<version>6.0.6</version>
<name>JSweet Core Lib</name>
<description>JavaScript API for JSweet</description>
<url>http://www.jsweet.org</url>

View File

@ -58,4 +58,12 @@ import java.lang.annotation.Target;
@Target({ ElementType.ANNOTATION_TYPE })
@Documented
public @interface Decorator {
/**
* Tells if the corresponding decorator function is expected to be found in the
* transpiled source code. If the decorator function is declared externally, set
* this parameter to false.
*
* @return true by default
*/
boolean value() default true;
}

View File

@ -260,7 +260,7 @@
<dependency>
<groupId>org.jsweet</groupId>
<artifactId>jsweet-core</artifactId>
<version>6.0.5</version>
<version>6.0.6</version>
<scope>test</scope>
<optional>true</optional>
</dependency>

View File

@ -205,7 +205,7 @@ public class JSweetContext extends Context {
}
/**
* Registers a decorator annotation in the context.
* Looks up a decorator annotation in the context.
*/
public JCClassDecl lookupDecoratorAnnotation(String fullyQualifiedName) {
return decoratorAnnotations.get(fullyQualifiedName);

View File

@ -1446,20 +1446,27 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
}
} else {
if (context.lookupDecoratorAnnotation(classdecl.sym.getQualifiedName().toString()) != null) {
JCTree[] globalDecoratorFunction = context
.lookupGlobalMethod(classdecl.sym.getQualifiedName().toString());
if (globalDecoratorFunction == null) {
report(classdecl, JSweetProblem.CANNOT_FIND_GLOBAL_DECORATOR_FUNCTION,
classdecl.sym.getQualifiedName());
} else {
getScope().decoratorScope = true;
enter(globalDecoratorFunction[0]);
print(globalDecoratorFunction[1]);
exit();
getScope().decoratorScope = false;
}
exitScope();
return;
boolean requiresDecoratorFunction = context.getAnnotationValue(classdecl.sym,
JSweetConfig.ANNOTATION_DECORATOR, Boolean.class, true);
boolean errorReported = false;
if (requiresDecoratorFunction) {
JCTree[] globalDecoratorFunction = context
.lookupGlobalMethod(classdecl.sym.getQualifiedName().toString());
if (globalDecoratorFunction == null) {
report(classdecl, JSweetProblem.CANNOT_FIND_GLOBAL_DECORATOR_FUNCTION,
classdecl.sym.getQualifiedName());
errorReported = true;
}
if (!errorReported) {
getScope().decoratorScope = true;
enter(globalDecoratorFunction[0]);
print(globalDecoratorFunction[1]);
exit();
getScope().decoratorScope = false;
}
exitScope();
return;
}
}
}