com.prezi.spaghetti.javascript.AbstractJavaScriptGeneratorVisitor Maven / Gradle / Ivy
package com.prezi.spaghetti.javascript;
import com.google.common.base.Strings;
import com.prezi.spaghetti.ast.QualifiedNode;
import com.prezi.spaghetti.ast.StringModuleVisitorBase;
import java.util.Arrays;
import java.util.List;
abstract public class AbstractJavaScriptGeneratorVisitor extends StringModuleVisitorBase {
protected static String embedInPackageStructure(T node, PackagedGenerator generator) {
List levels = Arrays.asList(node.getQualifiedName().namespace.split("\\."));
String topLevel = levels.get(0);
return "var " + topLevel + ";\n"
+ "(function (" + topLevel + ") {\n"
+ defineLevel(node, 1, topLevel, levels.subList(1, levels.size()), generator)
+ "})(" + topLevel + " || (" + topLevel + " = {}));\n";
}
private static String defineLevel(T node, int indentLevel, String parent, List remainingLevels, PackagedGenerator generator) {
final String indent = Strings.repeat("\t", indentLevel);
String result;
if (remainingLevels.isEmpty()) {
result = generator.generate(node, indentLevel, parent);
} else {
String level = remainingLevels.get(0);
result = indent + "(function(" + level + ") {\n"
+ defineLevel(node, indentLevel + 1, level, remainingLevels.subList(1, remainingLevels.size()), generator)
+ indent + "})(" + parent + "." + level + " || (" + parent + "." + level + " = {}));\n"
+ indent + "var " + level + " = " + parent + "." + level + ";\n";
}
return result;
}
protected static interface PackagedGenerator {
String generate(T node, int indentLevel, String parent);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy