You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/reference/yaml-format-reference.md
+32-6Lines changed: 32 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,7 +16,7 @@ Note that values passed to declarative recipes are subject to YAML interpretatio
16
16
17
17
There are two places where you can define an OpenRewrite YAML file:
18
18
19
-
1. Within the `rewrite.yml` file of a project that applies rewrite recipes via the [rewrite-gradle-plugin](gradle-plugin-configuration.md) or [rewrite-maven-plugin](rewrite-maven-plugin.md)
19
+
1. Within the `rewrite.yml` file of a project that applies rewrite recipes via the [rewrite-gradle-plugin](./gradle-plugin-configuration.md) or [rewrite-maven-plugin](./rewrite-maven-plugin.md)
20
20
2. Inside the `META-INF/rewrite` folder of a JAR (such as in the [rewrite-testing-frameworks](https://github.com/openrewrite/rewrite-testing-frameworks/tree/main/src/main/resources/META-INF/rewrite))
21
21
22
22
If you define a recipe or style in the `rewrite.yml` file, they _will not_ be included in the JARs published from your project.
@@ -48,25 +48,32 @@ You can find the full recipe schema [here](https://github.com/openrewrite/rewrit
48
48
| tags | array of strings | A list of strings that help categorize this recipe |
49
49
| estimatedEffortPerOccurrence |[duration](https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-)| The expected amount of time saved each time this recipe fixes something |
50
50
| causesAnotherCycle | boolean | Whether or not this recipe can cause another cycle (defaults to false) |
51
-
|[recipeList](yaml-format-reference.md#recipe-list)| array of recipes | The list of recipes which comprise this recipe |
51
+
|[recipeList](#recipe-list)| array of recipes | The list of recipes which comprise this recipe |
52
52
53
53
### Preconditions
54
54
55
-
Preconditions are used to limit which source files a recipe is allowed to make edits to. In other words, they act as filters that allow you to target specific files, directories, or patterns.
55
+
Preconditions are used to limit which source files a recipe is allowed to make edits to. In other words, they act as filters that allow you to target specific files, directories, or patterns.
56
56
57
-
This is particularly useful when you want a recipe to run only on a subset of the codebase.
57
+
This is particularly useful when you want a recipe to run only on a subset of the codebase.
58
58
59
59
Technically, almost any recipe can serve as a precondition, but in practice, lightweight and fast recipes – often based on simple searches – are preferred. These ensure performance remains optimal during large-scale code transformations.
60
60
61
61
When a recipe is used as a precondition, it determines whether a source file should be considered eligible for transformation. In other words, preconditions don’t make changes themselves, they just decide if the targeted recipe(s) should be allowed to make changes to a file.
62
62
63
63
If a file does not satisfy the precondition, the recipe list is skipped for _that_ file entirely. When multiple recipes are used as preconditions, _all_ of them must make a change to the file for it to be considered to meet the precondition.
64
64
65
-
:::info
65
+
:::info
66
66
Changes made by preconditions are not included in the final result of the recipe.
67
67
Changes made by preconditions are used _only_ to determine if the recipe should edit a particular source file.
68
68
:::
69
69
70
+
It's important to understand that preconditions operate on **already-parsed** source files. OpenRewrite runs in two distinct phases:
71
+
72
+
1.**Parsing phase**: All source files (except those in `exclusions`) are parsed into LSTs
73
+
2.**Recipe execution phase**: Preconditions determine which parsed files the recipe should modify
74
+
75
+
This means preconditions cannot prevent files from being parsed - they only control whether recipes apply to files that have already been successfully parsed.
76
+
70
77
:::warning
71
78
Preconditions only apply to files that already exist in the source set. They cannot prevent the creation of new files.
72
79
@@ -75,6 +82,25 @@ If a recipe generates files during the `generate` phase, those files will always
75
82
To conditionally generate files, implement a custom scanning recipe. You can define logic in the scanning phase based on existing source files, and use that context in the generate phase to control whether a file should be created.
76
83
:::
77
84
85
+
#### Preconditions vs. Exclusions
86
+
87
+
Since preconditions work on already-parsed files, they're not the right tool if you need to skip parsing certain files. Below is a table that should help you make an informed decision on when to use one feature over another:
To create these top-level preconditions, you'll need to add the `preconditions` map to your declarative recipe's YAML. This object is a list of one or more recipes (formatted the same way as the [recipeList](#recipe-list)).
@@ -226,7 +252,7 @@ You can find the full style schema [here](https://github.com/openrewrite/rewrite
0 commit comments