Imports seem to be working.

This commit is contained in:
JesseBrault0709 2024-05-03 20:33:33 +02:00
parent 5c3d973c4c
commit b66ecd89b7
4 changed files with 52 additions and 60 deletions

View File

@ -103,7 +103,6 @@ public class DefaultGroovyTranspiler implements GroovyTranspiler {
protected void handlePreamble( protected void handlePreamble(
String templateName, String templateName,
String packageName,
PreambleNode preambleNode, PreambleNode preambleNode,
ClassNode mainClassNode, ClassNode mainClassNode,
WebViewComponentModuleNode moduleNode WebViewComponentModuleNode moduleNode
@ -131,7 +130,8 @@ public class DefaultGroovyTranspiler implements GroovyTranspiler {
if (declarationsWithField.size() != preambleStatements.size()) { if (declarationsWithField.size() != preambleStatements.size()) {
logger.warn( logger.warn(
"{} contains script statements which are not supported. " + "{} contains script statements which are not supported. " +
"Currently, only classes, methods, and field declarations (marked with @Field) " + "Currently, only classes, methods, and field declarations " +
"(marked with @groovy.transform.Field) " +
"are supported. The rest will be ignored.", "are supported. The rest will be ignored.",
templateName templateName
); );
@ -199,7 +199,7 @@ public class DefaultGroovyTranspiler implements GroovyTranspiler {
// preamble // preamble
final PreambleNode preambleNode = compilationUnitNode.getPreambleNode(); final PreambleNode preambleNode = compilationUnitNode.getPreambleNode();
if (preambleNode != null) { if (preambleNode != null) {
this.handlePreamble(templateName, packageName, preambleNode, mainClassNode, moduleNode); this.handlePreamble(templateName, preambleNode, mainClassNode, moduleNode);
} }
// renderer // renderer

View File

@ -1,15 +1,12 @@
package groowt.view.web.transpile; package groowt.view.web.transpile;
import org.codehaus.groovy.ast.ClassNode; import org.codehaus.groovy.ast.ClassNode;
import org.codehaus.groovy.ast.CompileUnit;
import org.codehaus.groovy.ast.ImportNode; import org.codehaus.groovy.ast.ImportNode;
import org.codehaus.groovy.ast.ModuleNode; import org.codehaus.groovy.ast.ModuleNode;
import org.codehaus.groovy.control.SourceUnit; import org.codehaus.groovy.control.SourceUnit;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.*; import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
public class WebViewComponentModuleNode extends ModuleNode { public class WebViewComponentModuleNode extends ModuleNode {
@ -25,95 +22,70 @@ public class WebViewComponentModuleNode extends ModuleNode {
protected final Map<String, ImportNode> staticImports = new LinkedHashMap<>(); protected final Map<String, ImportNode> staticImports = new LinkedHashMap<>();
protected final Map<String, ImportNode> staticStarImports = new LinkedHashMap<>(); protected final Map<String, ImportNode> staticStarImports = new LinkedHashMap<>();
protected final Map<String, ImportNode> allImports = new LinkedHashMap<>();
public WebViewComponentModuleNode(SourceUnit context) { public WebViewComponentModuleNode(SourceUnit context) {
super(context); super(context);
} }
@Override @Override
public List<ImportNode> getImports() { public List<ImportNode> getImports() {
final List<ImportNode> r = new ArrayList<>(super.getImports()); return new ArrayList<>(this.imports);
r.addAll(this.imports);
return r;
} }
@Override @Override
public List<ImportNode> getStarImports() { public List<ImportNode> getStarImports() {
final List<ImportNode> r = new ArrayList<>(super.getStarImports()); return new ArrayList<>(this.starImports);
r.addAll(this.starImports);
return r;
} }
@Override @Override
public Map<String, ImportNode> getStaticImports() { public Map<String, ImportNode> getStaticImports() {
final Map<String, ImportNode> r = new HashMap<>(super.getStaticImports()); return new HashMap<>(this.staticImports);
r.putAll(this.staticImports);
return r;
} }
@Override @Override
public Map<String, ImportNode> getStaticStarImports() { public Map<String, ImportNode> getStaticStarImports() {
final Map<String, ImportNode> r = new HashMap<>(super.getStaticStarImports()); return new HashMap<>(this.staticStarImports);
r.putAll(this.staticStarImports);
return r;
} }
@Override @Override
public ClassNode getImportType(String alias) { public @Nullable ClassNode getImportType(String alias) {
final ClassNode superResult = super.getImportType(alias); return Optional.ofNullable(this.getImport(alias))
if (superResult != null) { .map(ImportNode::getType)
return superResult; .orElse(null);
} else {
return Optional.ofNullable(this.getImport(alias)).map(ImportNode::getType).orElse(null);
}
} }
@Override @Override
public @Nullable ImportNode getImport(String alias) { public @Nullable ImportNode getImport(String alias) {
final ImportNode superResult = super.getImport(alias); return this.allImports.get(alias);
if (superResult != null) {
return superResult;
} else {
final Map<String, ImportNode> aliases = this.getNodeMetaData("import.aliases", x -> {
return this.imports.stream()
.collect(Collectors.toMap(ImportNode::getAlias,
Function.identity(),
(first, second) -> second
));
});
return aliases.get(alias);
}
} }
// Copied from super protected void putToAll(String alias, ImportNode importNode) {
protected void storeLastAddedImportNode(ImportNode importNode) { this.allImports.put(alias, importNode);
if (this.getNodeMetaData(ImportNode.class) == ImportNode.class) { }
this.putNodeMetaData(ImportNode.class, importNode);
} protected final void putToAll(ImportNode importNode) {
this.putToAll(importNode.getAlias(), importNode);
} }
public void addImport(ImportNode importNode) { public void addImport(ImportNode importNode) {
this.imports.add(importNode); this.imports.add(importNode);
this.removeNodeMetaData("import.aliases"); this.putToAll(importNode);
this.storeLastAddedImportNode(importNode);
} }
public void addStarImport(ImportNode importNode) { public void addStarImport(ImportNode importNode) {
this.starImports.add(importNode); this.starImports.add(importNode);
this.storeLastAddedImportNode(importNode); this.putToAll(importNode);
} }
public void addStaticImport(String alias, ImportNode importNode) { public void addStaticImport(String alias, ImportNode importNode) {
final ImportNode prev = this.staticImports.put(alias, importNode); this.staticImports.put(alias, importNode);
if (prev != null) { this.putToAll(alias, importNode);
this.staticImports.put(prev.toString(), prev);
this.staticImports.put(alias, this.staticImports.remove(alias));
}
this.storeLastAddedImportNode(importNode);
} }
public void addStaticStarImport(String alias, ImportNode importNode) { public void addStaticStarImport(String alias, ImportNode importNode) {
this.staticStarImports.put(alias, importNode); this.staticStarImports.put(alias, importNode);
this.storeLastAddedImportNode(importNode); this.putToAll(alias, importNode);
} }
} }

View File

@ -1,11 +1,14 @@
package groowt.view.web; package groowt.view.web;
import groovy.lang.Closure;
import groowt.view.component.*; import groowt.view.component.*;
import groowt.view.web.runtime.WebViewComponentWriter;
import org.codehaus.groovy.control.CompilerConfiguration; import org.codehaus.groovy.control.CompilerConfiguration;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.Reader; import java.io.Reader;
import java.io.StringReader; import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
@ -68,4 +71,28 @@ public class DefaultWebComponentTemplateCompilerTests {
assertEquals("Hello, World!", usingGreeter.render()); assertEquals("Hello, World!", usingGreeter.render());
} }
@Test
public void withPreambleImport() {
final ComponentTemplate template = doCompile(DefaultWebViewComponent.class,
"""
---
import groovy.transform.Field
@Field
String greeting = 'Hello, World!'
---
$greeting
""".stripIndent()
);
final var context = new DefaultComponentContext();
context.pushDefaultScope();
final var sw = new StringWriter();
final var out = new WebViewComponentWriter(sw);
final Closure<?> renderer = template.getRenderer();
renderer.call(context, out);
assertEquals("Hello, World!", sw.toString());
}
} }

View File

@ -1,7 +0,0 @@
package groowt.view.web.transpiler;
public final class TranspilerTestsUtil {
private TranspilerTestsUtil() {}
}