Skip to content

Commit

Permalink
✨ feat: Add condition to JaspilerContract.Change
Browse files Browse the repository at this point in the history
  • Loading branch information
caoccao committed Jun 30, 2023
1 parent 380334c commit 58270e1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion scripts/node/jaspiler/jaspiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ function evaluateAnnotationAttribute(annotation, context, attributeName, default
function getChangeInstructionByAnnotations(annotations, context) {
if (annotations) {
const annotation = annotations.find(annotation => annotation.annotationType.toString() == 'JaspilerContract.Change');
return evaluateAnnotationAttribute(annotation, context, 'instruction', undefined, undefined);
if (evaluateAnnotationAttribute(annotation, context, 'condition', true, false)) {
return evaluateAnnotationAttribute(annotation, context, 'instruction', undefined, undefined);
}
}
return undefined;
}
Expand Down
7 changes: 6 additions & 1 deletion scripts/node/tutorials/03_builtin_annotations.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,15 @@ const result = jaspiler.transformSync(
System.out.println(str); // This method is cleared.
}
@JaspilerContract.Change(instruction = "options = { type: 'clear' }")
@JaspilerContract.Change(condition = "x == 1", instruction = "options = { type: 'clear' }")
public int f(int x, int y) {
return x + y; // This method is cleared.
}
@JaspilerContract.Change(condition = "x == 2", instruction = "options = { type: 'clear' }")
public int g(int x, int y) {
return x + y; // This method is not cleared.
}
}
@JaspilerContract.Ignore
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/caoccao/jaspiler/JaspilerContract.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ public boolean isNoChange() {
*/
@Retention(RetentionPolicy.SOURCE)
public @interface Change {
/**
* Condition is a JavaScript expression / statement / statements that returns a boolean
* indicating whether certain condition is true or false.
*
* @return the condition string
* @since 0.1.0
*/
String condition() default "";

/**
* Instruction is a JavaScript expression / statement / statements that returns anything
* instructing the plugins how to make the changes.
Expand Down

0 comments on commit 58270e1

Please sign in to comment.