|
13 | 13 | import com.hubspot.jinjava.tree.parse.TagToken; |
14 | 14 | import com.hubspot.jinjava.util.EagerReconstructionUtils; |
15 | 15 | import java.io.IOException; |
16 | | -import java.util.HashSet; |
17 | 16 | import java.util.List; |
18 | 17 | import java.util.Map; |
| 18 | +import java.util.Map.Entry; |
19 | 19 | import java.util.Optional; |
20 | | -import java.util.Set; |
21 | 20 | import java.util.stream.Collectors; |
22 | 21 |
|
23 | 22 | @Beta |
@@ -100,11 +99,10 @@ public String getEagerTagImage(TagToken tagToken, JinjavaInterpreter interpreter |
100 | 99 | } |
101 | 100 |
|
102 | 101 | FromTag.integrateChild(imports, child, interpreter); |
103 | | - Map<String, String> newToOldImportNames = renameMacros(imports, interpreter) |
104 | | - .entrySet() |
105 | | - .stream() |
106 | | - .filter(e -> !e.getKey().equals(e.getValue())) |
107 | | - .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey)); |
| 102 | + Map<String, String> newToOldImportNames = getNewToOldWithoutMacros( |
| 103 | + imports, |
| 104 | + interpreter |
| 105 | + ); |
108 | 106 | if (child.getContext().getDeferredTokens().isEmpty() || output == null) { |
109 | 107 | return ""; |
110 | 108 | } else if (newToOldImportNames.size() > 0) { |
@@ -132,33 +130,19 @@ public String getEagerTagImage(TagToken tagToken, JinjavaInterpreter interpreter |
132 | 130 | } |
133 | 131 | } |
134 | 132 |
|
135 | | - private static Map<String, String> renameMacros( |
| 133 | + private static Map<String, String> getNewToOldWithoutMacros( |
136 | 134 | Map<String, String> oldToNewImportNames, |
137 | 135 | JinjavaInterpreter interpreter |
138 | 136 | ) { |
139 | | - Set<String> toRemove = new HashSet<>(); |
140 | | - Map<String, MacroFunction> macroFunctions = oldToNewImportNames |
| 137 | + return oldToNewImportNames |
141 | 138 | .entrySet() |
142 | 139 | .stream() |
| 140 | + .filter(e -> !e.getKey().equals(e.getValue())) |
143 | 141 | .filter( |
144 | 142 | e -> |
145 | | - !e.getKey().equals(e.getValue()) && |
146 | | - !interpreter.getContext().containsKey(e.getKey()) && |
147 | | - interpreter.getContext().isGlobalMacro(e.getKey()) |
| 143 | + interpreter.getContext().containsKey(e.getValue()) || |
| 144 | + !interpreter.getContext().isGlobalMacro(e.getValue()) |
148 | 145 | ) |
149 | | - .peek(entry -> toRemove.add(entry.getKey())) |
150 | | - .collect( |
151 | | - Collectors.toMap( |
152 | | - Map.Entry::getValue, |
153 | | - e -> interpreter.getContext().getGlobalMacro(e.getKey()) |
154 | | - ) |
155 | | - ); |
156 | | - |
157 | | - macroFunctions.forEach( |
158 | | - (key, value) -> |
159 | | - interpreter.getContext().addGlobalMacro(new MacroFunction(value, key)) |
160 | | - ); |
161 | | - toRemove.forEach(oldToNewImportNames::remove); |
162 | | - return oldToNewImportNames; |
| 146 | + .collect(Collectors.toMap(Entry::getValue, Entry::getKey)); // flip order |
163 | 147 | } |
164 | 148 | } |
0 commit comments