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(
String templateName,
String packageName,
PreambleNode preambleNode,
ClassNode mainClassNode,
WebViewComponentModuleNode moduleNode
@ -131,7 +130,8 @@ public class DefaultGroovyTranspiler implements GroovyTranspiler {
if (declarationsWithField.size() != preambleStatements.size()) {
logger.warn(
"{} 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.",
templateName
);
@ -199,7 +199,7 @@ public class DefaultGroovyTranspiler implements GroovyTranspiler {
// preamble
final PreambleNode preambleNode = compilationUnitNode.getPreambleNode();
if (preambleNode != null) {
this.handlePreamble(templateName, packageName, preambleNode, mainClassNode, moduleNode);
this.handlePreamble(templateName, preambleNode, mainClassNode, moduleNode);
}
// renderer

View File

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

View File

@ -1,11 +1,14 @@
package groowt.view.web;
import groovy.lang.Closure;
import groowt.view.component.*;
import groowt.view.web.runtime.WebViewComponentWriter;
import org.codehaus.groovy.control.CompilerConfiguration;
import org.junit.jupiter.api.Test;
import java.io.Reader;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;
import java.util.Objects;
@ -68,4 +71,28 @@ public class DefaultWebComponentTemplateCompilerTests {
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() {}
}