diff --git a/core-lib/es5/src/main/java/jsweet/lang/Replace.java b/core-lib/es5/src/main/java/jsweet/lang/Replace.java
index cec11ae0..194b7746 100644
--- a/core-lib/es5/src/main/java/jsweet/lang/Replace.java
+++ b/core-lib/es5/src/main/java/jsweet/lang/Replace.java
@@ -30,6 +30,24 @@ import java.lang.annotation.Target;
* The annotation's value contains TypeScript which is generated as is by the
* JSweet transpiler. The code will be checked by the TypeScript transpiler.
*
+ *
+ * The replacing code can contain variables substituted using a mustache-like
+ * convention ({{variableName}}). Note that no space is allowed between the
+ * variable name and the mustaches. Here is the list of supported variables:
+ *
+ *
+ * - {{className}}: the current class.
+ * - {{methodName}}: the current method name.
+ * - {{body}}: the body of the current method. A typical use of this variable
+ * is to wrap the original behavior in a lambda. For instance:
+ *
[before code] let _result = () => { {{body}} }(); [after code] return result;
+ *
+ * - {{baseIndent}}: the indentation of the replaced method. Can be used to
+ * generate well-formatted code.
+ * - {{indent}}: substituted with an indentation. Can be used to generate
+ * well-formatted code.
+ *
+ *
* @author Renaud Pawlak
*/
@Retention(RetentionPolicy.RUNTIME)
@@ -37,10 +55,10 @@ import java.lang.annotation.Target;
@Documented
public @interface Replace {
- /**
- * The code that will be generated by the transpiler in place of the
- * annotated method body.
- */
- java.lang.String value();
+ /**
+ * The code that will be generated by the transpiler in place of the
+ * annotated method body.
+ */
+ java.lang.String value();
}
diff --git a/core-lib/es6/src/main/java/jsweet/lang/Replace.java b/core-lib/es6/src/main/java/jsweet/lang/Replace.java
index e2e1490e..655614b3 100644
--- a/core-lib/es6/src/main/java/jsweet/lang/Replace.java
+++ b/core-lib/es6/src/main/java/jsweet/lang/Replace.java
@@ -30,6 +30,24 @@ import java.lang.annotation.Target;
* The annotation's value contains TypeScript which is generated as is by the
* JSweet transpiler. The code will be checked by the TypeScript transpiler.
*
+ *
+ * The replacing code can contain variables substituted using a mustache-like
+ * convention ({{variableName}}). Note that no space is allowed between the
+ * variable name and the mustaches. Here is the list of supported variables:
+ *
+ *
+ * - {{className}}: the current class.
+ * - {{methodName}}: the current method name.
+ * - {{body}}: the body of the current method. A typical use of this variable
+ * is to wrap the original behavior in a lambda. For instance:
+ *
[before code] let _result = () => { {{body}} }(); [after code] return result;
+ *
+ * - {{baseIndent}}: the indentation of the replaced method. Can be used to
+ * generate well-formatted code.
+ * - {{indent}}: substituted with an indentation. Can be used to generate
+ * well-formatted code.
+ *
+ *
* @author Renaud Pawlak
*/
@Retention(RetentionPolicy.RUNTIME)
@@ -37,10 +55,10 @@ import java.lang.annotation.Target;
@Documented
public @interface Replace {
- /**
- * The code that will be generated by the transpiler in place of the
- * annotated method body.
- */
- java.lang.String value();
+ /**
+ * The code that will be generated by the transpiler in place of the
+ * annotated method body.
+ */
+ java.lang.String value();
}
diff --git a/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java b/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java
index f9af13b5..1bb89e6c 100644
--- a/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java
+++ b/transpiler/src/main/java/org/jsweet/transpiler/Java2TypeScriptTranslator.java
@@ -160,11 +160,11 @@ public class Java2TypeScriptTranslator extends AbstractTreePrinter {
public static final String ENUM_WRAPPER_CLASS_NAME = "_$name";
public static final String ENUM_WRAPPER_CLASS_ORDINAL = "_$ordinal";
public static final String VAR_DECL_KEYWORD = "let";
- public static final String BODY_MARKER = "#BODY#";
- public static final String BASE_INDENT_MARKER = "#BASEINDENT#";
- public static final String INDENT_MARKER = "#INDENT#";
- public static final String METHOD_NAME_MARKER = "#METHODNAME#";
- public static final String CLASS_NAME_MARKER = "#CLASSNAME#";
+ public static final String BODY_MARKER = "{{body}}";
+ public static final String BASE_INDENT_MARKER = "{{baseIndent}}";
+ public static final String INDENT_MARKER = "{{indent}}";
+ public static final String METHOD_NAME_MARKER = "{{methodName}}";
+ public static final String CLASS_NAME_MARKER = "{{className}}";
public static final String GENERATOR_PREFIX = "__generator_";
protected static Logger logger = Logger.getLogger(Java2TypeScriptTranslator.class);
diff --git a/transpiler/src/test/java/org/jsweet/test/transpiler/StructuralTests.java b/transpiler/src/test/java/org/jsweet/test/transpiler/StructuralTests.java
index d724815e..7aa42350 100644
--- a/transpiler/src/test/java/org/jsweet/test/transpiler/StructuralTests.java
+++ b/transpiler/src/test/java/org/jsweet/test/transpiler/StructuralTests.java
@@ -331,7 +331,7 @@ public class StructuralTests extends AbstractTest {
context.addAnnotation("@Replace('return (this.i + 2)')",
"source.structural.ReplaceAnnotation.m2()");
context.addAnnotation(
- "@Replace('let result = (() => { #BODY# })(); return (result + '#METHODNAME#'.length);')",
+ "@Replace('let result = (() => { {{body}} })(); return (result + '{{methodName}}'.length);')",
"source.structural.ReplaceAnnotation.m4()");
}
};