Skip to content

Commit 9b06e3b

Browse files
authored
Merge pull request #821 from HubSpot/revert-820-handle-nested-import-file-paths
Revert "Handle nested import file paths"
2 parents 3cc0df2 + bb526b1 commit 9b06e3b

9 files changed

Lines changed: 9 additions & 92 deletions

File tree

src/main/java/com/hubspot/jinjava/lib/fn/MacroFunction.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,20 +182,14 @@ public boolean equals(Object o) {
182182
MacroFunction that = (MacroFunction) o;
183183
return (
184184
caller == that.caller &&
185-
Objects.equals(getName(), that.getName()) &&
186-
Objects.equals(
187-
localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY),
188-
that.localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY)
189-
)
185+
definitionLineNumber == that.definitionLineNumber &&
186+
definitionStartPosition == that.definitionStartPosition &&
187+
content.equals(that.content)
190188
);
191189
}
192190

193191
@Override
194192
public int hashCode() {
195-
return Objects.hash(
196-
getName(),
197-
localContextScope.get(Context.IMPORT_RESOURCE_PATH_KEY),
198-
caller
199-
);
193+
return Objects.hash(content, caller, definitionLineNumber, definitionStartPosition);
200194
}
201195
}

src/main/java/com/hubspot/jinjava/lib/fn/eager/EagerMacroFunction.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import com.hubspot.jinjava.el.ext.AstMacroFunction;
66
import com.hubspot.jinjava.interpret.Context;
77
import com.hubspot.jinjava.interpret.DeferredMacroValueImpl;
8-
import com.hubspot.jinjava.interpret.DeferredValue;
98
import com.hubspot.jinjava.interpret.DeferredValueException;
109
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
1110
import com.hubspot.jinjava.interpret.JinjavaInterpreter.InterpreterScopeClosable;
@@ -107,15 +106,11 @@ public String reconstructImage() {
107106
String prefix = "";
108107
String suffix = "";
109108
Optional<String> importFile = macroFunction.getImportFile(interpreter);
110-
Object currentDeferredImportResource = null;
111109
if (importFile.isPresent()) {
112110
interpreter.getContext().getCurrentPathStack().pop();
113-
currentDeferredImportResource =
114-
interpreter.getContext().get(Context.DEFERRED_IMPORT_RESOURCE_PATH_KEY);
115-
if (currentDeferredImportResource instanceof DeferredValue) {
116-
currentDeferredImportResource =
117-
((DeferredValue) currentDeferredImportResource).getOriginalValue();
118-
}
111+
String currentDeferredImportResource = (String) interpreter
112+
.getContext()
113+
.get(Context.DEFERRED_IMPORT_RESOURCE_PATH_KEY);
119114
prefix =
120115
EagerReconstructionUtils.buildSetTag(
121116
ImmutableMap.of(
@@ -125,9 +120,6 @@ public String reconstructImage() {
125120
interpreter,
126121
false
127122
);
128-
interpreter
129-
.getContext()
130-
.put(Context.DEFERRED_IMPORT_RESOURCE_PATH_KEY, importFile.get());
131123
suffix =
132124
EagerReconstructionUtils.buildSetTag(
133125
ImmutableMap.of(
@@ -158,10 +150,6 @@ public String reconstructImage() {
158150
.map(arg -> DeferredMacroValueImpl.instance())
159151
.toArray()
160152
);
161-
162-
interpreter
163-
.getContext()
164-
.put(Context.DEFERRED_IMPORT_RESOURCE_PATH_KEY, currentDeferredImportResource);
165153
if (interpreter.getContext().getEagerTokens().size() > numEagerTokensStart) {
166154
evaluation =
167155
(String) evaluate(

src/main/java/com/hubspot/jinjava/lib/tag/ImportTag.java

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525
import java.io.IOException;
2626
import java.util.List;
2727
import java.util.Map;
28-
import java.util.Map.Entry;
2928
import java.util.Optional;
30-
import java.util.stream.Collectors;
3129
import org.apache.commons.lang3.StringUtils;
3230

3331
/**
@@ -153,9 +151,7 @@ public static void integrateChild(
153151
parent.getContext().addGlobalMacro(macro);
154152
}
155153
childBindings.remove(Context.GLOBAL_MACROS_SCOPE_KEY);
156-
parent
157-
.getContext()
158-
.putAll(getChildBindingsWithoutImportResourcePath(childBindings));
154+
parent.getContext().putAll(childBindings);
159155
} else {
160156
for (Map.Entry<String, MacroFunction> macro : child
161157
.getContext()
@@ -168,17 +164,6 @@ public static void integrateChild(
168164
}
169165
}
170166

171-
public static Map<String, Object> getChildBindingsWithoutImportResourcePath(
172-
Map<String, Object> childBindings
173-
) {
174-
// Don't remove them from childBindings, because it is needed in a macro function's localContextScope
175-
return childBindings
176-
.entrySet()
177-
.stream()
178-
.filter(entry -> !entry.getKey().equals(Context.IMPORT_RESOURCE_PATH_KEY))
179-
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
180-
}
181-
182167
public static void handleDeferredNodesDuringImport(
183168
Node node,
184169
String contextVar,

src/main/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTag.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,7 @@ public static void integrateChild(
338338
}
339339
childBindings.remove(Context.GLOBAL_MACROS_SCOPE_KEY);
340340
childBindings.remove(Context.IMPORT_RESOURCE_ALIAS_KEY);
341-
parent
342-
.getContext()
343-
.putAll(ImportTag.getChildBindingsWithoutImportResourcePath(childBindings));
341+
parent.getContext().putAll(childBindings);
344342
} else {
345343
Map<String, MacroFunction> globalMacros = child.getContext().getGlobalMacros();
346344
for (Map.Entry<String, MacroFunction> macro : globalMacros.entrySet()) {

src/test/java/com/hubspot/jinjava/lib/tag/ImportTagTest.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.hubspot.jinjava.interpret.JinjavaInterpreter;
1212
import com.hubspot.jinjava.interpret.RenderResult;
1313
import com.hubspot.jinjava.lib.fn.MacroFunction;
14-
import com.hubspot.jinjava.lib.tag.eager.EagerImportTagTest.PrintPathFilter;
1514
import com.hubspot.jinjava.loader.LocationResolver;
1615
import com.hubspot.jinjava.loader.RelativePathResolver;
1716
import com.hubspot.jinjava.loader.ResourceLocator;
@@ -40,7 +39,6 @@ public void setup() {
4039
);
4140

4241
context.put("padding", 42);
43-
context.registerFilter(new PrintPathFilter());
4442
}
4543

4644
@Test
@@ -354,17 +352,6 @@ public void itSetsErrorLineNumbersCorrectlyForImportedMacros() throws IOExceptio
354352
.isEqualTo("tags/importtag/errors/macro-with-error.jinja");
355353
}
356354

357-
@Test
358-
public void itCorrectlySetsNestedPaths() {
359-
context.put("foo", "foo");
360-
assertThat(
361-
interpreter.render(
362-
"{% import 'double-import-macro.jinja' %}{{ print_path_macro2(foo) }}"
363-
)
364-
)
365-
.isEqualTo("double-import-macro.jinja\n\nimport-macro.jinja\nfoo\n");
366-
}
367-
368355
private String fixture(String name) {
369356
try {
370357
return interpreter.renderFlat(

src/test/java/com/hubspot/jinjava/lib/tag/eager/EagerImportTagTest.java

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -535,27 +535,6 @@ public void itCorrectlySetsPathForSecondPass() {
535535
.isEqualTo("import-macro.jinja\nfoo");
536536
}
537537

538-
@Test
539-
public void itCorrectlySetsNestedPathsForSecondPass() {
540-
setupResourceLocator();
541-
context.put("foo", DeferredValue.instance());
542-
String firstPassResult = interpreter.render(
543-
"{% import 'double-import-macro.jinja' %}{{ print_path_macro2(foo) }}"
544-
);
545-
assertThat(firstPassResult)
546-
.isEqualTo(
547-
"{% set deferred_import_resource_path = 'double-import-macro.jinja' %}{% macro print_path_macro2(var) %}{{ filter:print_path.filter(var, ____int3rpr3t3r____) }}\n" +
548-
"{% set deferred_import_resource_path = 'import-macro.jinja' %}{% macro print_path_macro(var) %}\n" +
549-
"{{ filter:print_path.filter(var, ____int3rpr3t3r____) }}\n" +
550-
"{{ var }}\n" +
551-
"{% endmacro %}{% set deferred_import_resource_path = null %}{{ print_path_macro(var) }}{% endmacro %}{% set deferred_import_resource_path = null %}{{ print_path_macro2(foo) }}"
552-
);
553-
context.put("foo", "foo");
554-
context.put(Context.GLOBAL_MACROS_SCOPE_KEY, null);
555-
assertThat(interpreter.render(firstPassResult).trim())
556-
.isEqualTo("double-import-macro.jinja\n\nimport-macro.jinja\nfoo");
557-
}
558-
559538
@Test
560539
public void itImportsDoublyNamed() {
561540
setupResourceLocator();

src/test/resources/tags/eager/importtag/double-import-macro.jinja

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/test/resources/tags/macrotag/double-import-macro.jinja

Lines changed: 0 additions & 5 deletions
This file was deleted.

src/test/resources/tags/macrotag/import-macro.jinja

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)