Commits

Stephen McKamey committed 095e7d7

- reducing bulk of client-side generated namespaces

  • Participants
  • Parent commits 9911619

Comments (0)

Files changed (2)

src/org/duelengine/duel/DataEncoder.java

 		output.append('\"');
 	}
 
+	/**
+	 * Produces more compact namespace declarations.
+	 * @param output
+	 * @param namespaces
+	 * @param ident
+	 * @throws IOException
+	 */
 	public void writeNamespace(Appendable output, List<String> namespaces, String ident)
 		throws IOException {
 
 			}
 			namespaces.add(ns);
 
+			this.writeln(output, 0);
+			if (isRoot) {
+				output.append("var ");
+				isRoot = false;
+			}
+
+			output.append(ns);
+			output.append(" = ");
+			output.append(ns);
+			output.append(" || {};");
+
+			// next iteration
+			nextDot = ident.indexOf('.', nextDot+1);
+			needsNewline = true;
+		}
+
+		if (needsNewline) {
+			this.writeln(output, 0);
+		}
+	}
+
+	/**
+	 * Produces more verbose but technically more correct namespace declarations
+	 * @param output
+	 * @param namespaces
+	 * @param ident
+	 * @throws IOException
+	 */
+	@Deprecated
+	public void writeNamespaceAlt(Appendable output, List<String> namespaces, String ident)
+		throws IOException {
+
+		if (!JSUtility.isValidIdentifier(ident, true)) {
+			throw new IllegalArgumentException("Invalid identifier: "+ident);
+		}
+
+		boolean needsNewline = false;
+		boolean isRoot = true;
+		int nextDot = ident.indexOf('.');
+		while (nextDot > -1) {
+			String ns = ident.substring(0, nextDot);
+
+			// check if already exists
+			if ((isRoot && JSUtility.isGlobalIdent(ns)) || namespaces.contains(ns)) {
+				// next iteration
+				nextDot = ident.indexOf('.', nextDot+1);
+				isRoot = false;
+				continue;
+			}
+			namespaces.add(ns);
+
 			if (isRoot) {
 				this.writeln(output, 0);
 				output.append("var ");

test/org/duelengine/duel/codegen/ClientCodeGenTests.java

 
 		String expected =
 			"/*global duel */\n\n"+
-			"var foo;\n"+
-			"if (typeof foo === \"undefined\") {\n"+
-			"\tfoo = {};\n"+
-			"}\n"+
-			"if (typeof foo.bar === \"undefined\") {\n"+
-			"\tfoo.bar = {};\n"+
-			"}\n\n"+
+			"var foo = foo || {};\n"+
+			"foo.bar = foo.bar || {};\n\n"+
 			"foo.bar.Blah = duel([\"div\"]);\n";
 
 		StringBuilder output = new StringBuilder();
 	public void namespaceRepeatedTest() throws IOException {
 		String expected =
 			"/*global duel */\n\n"+
-			"var foo;\n"+
-			"if (typeof foo === \"undefined\") {\n"+
-			"\tfoo = {};\n"+
-			"}\n"+
-			"if (typeof foo.bar === \"undefined\") {\n"+
-			"\tfoo.bar = {};\n"+
-			"}\n\n"+
+			"var foo = foo || {};\n"+
+			"foo.bar = foo.bar || {};\n\n"+
 			"foo.bar.Blah = duel(\"First View\");\n\n"+
 			"foo.bar.Yada = duel(\"Second View\");\n";
 
 	public void namespacesOverlappingTest() throws IOException {
 		String expected =
 			"/*global duel */\n\n"+
-			"var foo;\n"+
-			"if (typeof foo === \"undefined\") {\n"+
-			"\tfoo = {};\n"+
-			"}\n"+
-			"if (typeof foo.bar === \"undefined\") {\n"+
-			"\tfoo.bar = {};\n"+
-			"}\n"+
-			"if (typeof foo.bar.one === \"undefined\") {\n"+
-			"\tfoo.bar.one = {};\n"+
-			"}\n\n"+
+			"var foo = foo || {};\n"+
+			"foo.bar = foo.bar || {};\n"+
+			"foo.bar.one = foo.bar.one || {};\n\n"+
 			"foo.bar.one.Blah = duel(\"First View\");\n\n"+
-			"if (typeof foo.bar.two === \"undefined\") {\n"+
-			"\tfoo.bar.two = {};\n"+
-			"}\n\n"+
+			"foo.bar.two = foo.bar.two || {};\n\n"+
 			"foo.bar.two.Yada = duel(\"Second View\");\n";
 
 		StringBuilder output = new StringBuilder();
 	public void namespacesDistinctTest() throws IOException {
 		String expected =
 			"/*global duel */\n\n"+
-			"var foo;\n"+
-			"if (typeof foo === \"undefined\") {\n"+
-			"\tfoo = {};\n"+
-			"}\n"+
-			"if (typeof foo.bar === \"undefined\") {\n"+
-			"\tfoo.bar = {};\n"+
-			"}\n\n"+
+			"var foo = foo || {};\n"+
+			"foo.bar = foo.bar || {};\n\n"+
 			"foo.bar.Blah = duel(\"First View\");\n\n"+
-			"var com;\n"+
-			"if (typeof com === \"undefined\") {\n"+
-			"\tcom = {};\n"+
-			"}\n"+
-			"if (typeof com.example === \"undefined\") {\n"+
-			"\tcom.example = {};\n"+
-			"}\n\n"+
+			"var com = com || {};\n"+
+			"com.example = com.example || {};\n\n"+
 			"com.example.Yada = duel(\"Second View\");\n";
 
 		StringBuilder output = new StringBuilder();
 
 		String expected =
 			"/*global duel */\n\n"+
-			"var foo;\n"+
-			"if (typeof foo === \"undefined\") {\n"+
-			"\tfoo = {};\n"+
-			"}\n"+
-			"if (typeof foo.bar === \"undefined\") {\n"+
-			"\tfoo.bar = {};\n"+
-			"}\n\n"+
+			"var foo = foo || {};\n"+
+			"foo.bar = foo.bar || {};\n\n"+
 			"foo.bar.Blah = duel([\"$call\", {\n"+
 			"\t\t\"view\" : function() { return (foo.bar.Yada); },\n"+
 			"\t\t\"data\" : function(data) { return (data.foo); }\n"+
 
 		String expected =
 			"/*global duel */\n\n"+
-			"var foo;\n"+
-			"if (typeof foo === \"undefined\") {\n"+
-			"\tfoo = {};\n"+
-			"}\n"+
-			"if (typeof foo.bar === \"undefined\") {\n"+
-			"\tfoo.bar = {};\n"+
-			"}\n\n"+
+			"var foo = foo || {};\n"+
+			"foo.bar = foo.bar || {};\n\n"+
 			"foo.bar.Blah = duel(\n"+
 			"\t[\"$call\", {\n"+
 			"\t\t\t\"view\" : function() { return (foo.bar.Yada); },\n"+
 
 		String expected =
 			"/*global duel */\n\n"+
-			"var foo;\n"+
-			"if (typeof foo === \"undefined\") {\n"+
-			"\tfoo = {};\n"+
-			"}\n"+
-			"if (typeof foo.bar === \"undefined\") {\n"+
-			"\tfoo.bar = {};\n"+
-			"}\n\n"+
+			"var foo = foo || {};\n"+
+			"foo.bar = foo.bar || {};\n\n"+
 			"foo.bar.Blah = duel(\n"+
 			"\t[\"div\", { \"class\" : \"dialog\" },\n"+
 			"\t\t[\"$part\", { \"name\" : \"header\" },\n"+