Mathias Panzenböck avatar Mathias Panzenböck committed fa1e7c7

changed Visitor and bugfixes

Comments (0)

Files changed (5)

lib/clonevisitor.js

 "use strict";
 
-var nodes = require("./lib/nodes.js");
-var Visitor = require("./lib/visitor.js").Visitor;
+var nodes = require("./nodes.js");
+var Visitor = require("./visitor.js").Visitor;
 var extend = require("extend");
 
 function CloneVisitor () {
 	conditionalExpression: function (test, consequent, alternate, loc) {
 		return new nodes.ConditionalExpression(test, consequent, alternate, loc);
 	},
-	newExpression: function (constructor, arguments, loc) {
-		return new nodes.NewExpression(constructor, arguments, loc);
+	newExpression: function (constructor, args, loc) {
+		return new nodes.NewExpression(constructor, args, loc);
 	},
-	callExpression: function (callee, arguments, loc) {
-		return new nodes.CallExpression(callee, arguments, loc);
+	callExpression: function (callee, args, loc) {
+		return new nodes.CallExpression(callee, args, loc);
 	},
 	memberExpression: function (object, property, computed, loc) {
 		return new nodes.MemberExpression(object, property, computed, loc);

lib/minvisitor.js

 "use strict";
 
-var nodes = require("./lib/nodes.js");
-var Visitor = require("./lib/visitor.js").Visitor;
+var nodes = require("./nodes.js");
+var Visitor = require("./visitor.js").Visitor;
 var extend = require("extend");
 
 function MinVisitor () {}
 		return label+":"+body;
 	},
 	breakStatement: function (label) {
-		return label ? "break "+label"+;" : "break;";
+		return label ? "break "+label+";" : "break;";
 	},
 	continueStatement: function (label) {
-		return label ? "continue "+label"+;" : "continue;";
+		return label ? "continue "+label+";" : "continue;";
 	},
 	withStatement: function (obj, body) {
 		return "with("+obj+")"+body;
 					code += " ";
 				}
 				code += op;
-				if (arg.charAt(0) === op) {
+				if (right.charAt(0) === op) {
 					code += " ";
 				}
 				code += right;
 			case '-':
 			case '/':
 				if (left.charAt(left.length-1) === c) {
-					return left+" "+op+right,
+					return left+" "+op+right;
 				}
 			default:
 				return left+op+right;
 	this.loc = loc;
 }
 
-function NewExpression (constructor, arguments, loc) {
+function NewExpression (constructor, args, loc) {
 	this.type = "NewExpression";
 	this.constructor = constructor;
-	this.arguments = arguments;
+	this.arguments = args;
 	this.loc = loc;
 }
 
-function CallExpression (callee, arguments, loc) {
+function CallExpression (callee, args, loc) {
 	this.type = "CallExpression";
 	this.callee = callee;
-	this.arguments = arguments;
+	this.arguments = args;
 	this.loc = loc;
 }
 
 	// TODO: add lots of helper functions
 };
 
-Satement.prototype = new Node();
+Statement.prototype = new Node();
 Expression.prototype = new Node();
-Declaration.prototype = new Satement();
+Declaration.prototype = new Statement();
 Program.prototype = new Node();
-EmptyStatement.prototype = new Satement();
-BlockStatement.prototype = new Satement();
-ExpressionStatement.prototype = new Satement();
-IfStatement.prototype = new Satement();
-LabeledStatement.prototype = new Satement();
-BreakStatement.prototype = new Satement();
-ContinueStatement.prototype = new Satement();
-WithStatement.prototype = new Satement();
-SwitchStatement.prototype = new Satement();
-ReturnStatement.prototype = new Satement();
-ThrowStatement.prototype = new Satement();
-TryStatement.prototype = new Satement();
-WhileStatement.prototype = new Satement();
-DoWhileStatement.prototype = new Satement();
-ForStatement.prototype = new Satement();
-ForInStatement.prototype = new Satement();
-DebuggerStatement.prototype = new Satement();
+EmptyStatement.prototype = new Statement();
+BlockStatement.prototype = new Statement();
+ExpressionStatement.prototype = new Statement();
+IfStatement.prototype = new Statement();
+LabeledStatement.prototype = new Statement();
+BreakStatement.prototype = new Statement();
+ContinueStatement.prototype = new Statement();
+WithStatement.prototype = new Statement();
+SwitchStatement.prototype = new Statement();
+ReturnStatement.prototype = new Statement();
+ThrowStatement.prototype = new Statement();
+TryStatement.prototype = new Statement();
+WhileStatement.prototype = new Statement();
+DoWhileStatement.prototype = new Statement();
+ForStatement.prototype = new Statement();
+ForInStatement.prototype = new Statement();
+DebuggerStatement.prototype = new Statement();
 FunctionDeclaration.prototype = new Declaration();
 VariableDeclaration.prototype = new Declaration();
 VariableDeclarator.prototype = new Node();

lib/optimizingvisitor.js

 "use strict";
 
-var nodes = require("./lib/nodes.js");
-var CloneVisitor = require("./lib/clonevisitor.js").CloneVisitor;
+var nodes = require("./nodes.js");
+var CloneVisitor = require("./clonevisitor.js").CloneVisitor;
 var extend = require("extend");
 
 // minimal optimizations
 		}
 		return new nodes.BlockStatement(body, loc);
 	},
-	binaryExpression: function (op, left, right) {
+	binaryExpression: function (op, left, right, loc) {
 		if (left.type === "Literal" && right.type === "Literal") {
 			switch (op) {
 				case '+':
 "use strict";
 
-var nodes = require("./lib/nodes.js");
+var nodes = require("./nodes.js");
 var extend = require("extend");
 
-var TypeMap = {
-	Program: function (node) {
+function Visitor () {}
+
+Visitor.prototype = {
+	visit: function (node) {
+		return this['visit'+node.type](node);
+	},
+	visitProgram: function (node) {
 		return this.program(
 			node.body.map(this.visit.bind(this)),
 			node.loc);
 	},
-	EmptyStatement: function (node) {
+	visitEmptyStatement: function (node) {
 		return this.emptyStatement(node.loc);
 	},
-	BlockStatement: function (node) {
+	visitBlockStatement: function (node) {
 		return this.program(
 			node.body.map(this.visit.bind(this)),
 			node.loc);
 	},
-	ExpressionStatement: function (node) {
+	visitExpressionStatement: function (node) {
 		return this.expressionStatement(
 			this.visit(node.expression),
 			node.loc);
 	},
-	IfStatement: function (node) {
+	visitIfStatement: function (node) {
 		return this.ifStatement(
 			this.visit(node.test),
 			this.visit(node.consequent),
 			node.alternate ? this.visit(node.alternate) : null,
 			node.loc);
 	},
-	LabeledStatement: function (node) {
+	visitLabeledStatement: function (node) {
 		return this.labeledStatement(
 			this.visit(node.label),
 			this.visit(node.body),
 			node.loc);
 	},
-	BreakStatement: function (node) {
+	visitBreakStatement: function (node) {
 		return this.breakStatement(
 			node.label ? this.visit(node.label) : null,
 			node.loc);
 	},
-	ContinueStatement: function (node) {
+	visitContinueStatement: function (node) {
 		return this.breakStatement(
 			node.label ? this.visit(node.label) : null,
 			node.loc);
 	},
-	WithStatement: function (node) {
+	visitWithStatement: function (node) {
 		return this.withStatement(
 			this.visit(node.object),
 			this.visit(node.body),
 			node.loc);
 	},
-	SwitchStatement: function (node) {
+	visitSwitchStatement: function (node) {
 		return this.switchStatement(
 			this.visit(node.discriminant),
 			node.cases.map(this.visit.bind(this)),
 			node.loc);
 	},
-	ReturnStatement: function (node) {
+	visitReturnStatement: function (node) {
 		return this.returnStatement(
 			node.argument ? this.visit(node.argument) : null,
 			node.loc);
 	},
-	ThrowStatement: function (node) {
+	visitThrowStatement: function (node) {
 		return this.throwStatement(
 			this.visit(node.argument),
 			node.loc);
 	},
-	TryStatement: function (node) {
+	visitTryStatement: function (node) {
 		return this.tryStatement(
 			this.visit(node.block),
 			node.handlers.map(this.visit.bind(this)),
 			node.finalizer ? this.visit(node.finalizer) : null,
 			node.loc);
 	},
-	WhileStatement: function (node) {
+	visitWhileStatement: function (node) {
 		return this.whileStatement(
 			this.visit(node.test),
 			this.visit(node.body),
 			node.loc);
 	},
-	DoWhileStatement: function (node) {
+	visitDoWhileStatement: function (node) {
 		return this.doWhileStatement(
 			this.visit(node.body),
 			this.visit(node.test),
 			node.loc);
 	},
-	ForStatement: function (node) {
+	visitForStatement: function (node) {
 		return this.forStatement(
 			node.init ? this.visit(node.init) : null,
 			node.test ? this.visit(node.test) : null,
 			this.visit(body),
 			node.loc);
 	},
-	ForInStatement: function (node) {
+	visitForInStatement: function (node) {
 		return this.forInStatement(
 			this.visit(node.left),
 			this.visit(node.right),
 			this.visit(node.body),
 			node.loc);
 	},
-	DebuggerStatement: function (node) {
+	visitDebuggerStatement: function (node) {
 		return this.debuggerStatement(node.loc);
 	},
-	FunctionDeclaration: function (node) {
+	visitFunctionDeclaration: function (node) {
 		return this.functionDeclaration(
 			this.visit(node.id),
 			node.params.map(this.visit.bind(this)),
 			this.visit(node.body),
 			node.loc);
 	},
-	VariableDeclaration: function (node) {
+	visitVariableDeclaration: function (node) {
 		return this.variableDeclaration(
 			node.declarations.map(this.visit.bind(this)),
 			node.kind,
 			node.loc);
 	},
-	VariableDeclarator: function (node) {
+	visitVariableDeclarator: function (node) {
 		return this.variableDeclarator(
 			this.visit(node.id),
 			node.init ? this.visit(node.init) : null,
 			node.loc);
 	},
-	ThisExpression: function (node) {
+	visitThisExpression: function (node) {
 		return this.thisExpression(node.loc);
 	},
-	ArrayExpression: function (node) {
+	visitArrayExpression: function (node) {
 		return this.arrayExpression(
 			node.elements.map(this.visit.bind(this)),
 			node.loc);
 	},
-	ObjectExpression: function (node) {
+	visitObjectExpression: function (node) {
 		return this.objectExpression(
 			node.properties.map(this.visit.bind(this)),
 			node.loc);
 	},
-	FunctionExpression: function (node) {
+	visitFunctionExpression: function (node) {
 		return this.functionExpression(
 			node.id ? this.visit(node.id) : null,
 			node.params.map(this.visit.bind(this)),
 			this.visit(node.body),
 			node.loc);
 	},
-	SequenceExpression: function (node) {
+	visitSequenceExpression: function (node) {
 		return this.sequenceExpression(
 			node.expressions.map(this.visit.bind(this)),
 			node.loc);
 	},
-	UnaryExpression: function (node) {
+	visitUnaryExpression: function (node) {
 		return this.unaryExpression(
 			node.operator,
 			this.visit(node.argument),
 			node.loc);
 	},
-	BinaryExpression: function (node) {
+	visitBinaryExpression: function (node) {
 		return this.binaryExpression(
 			node.operator,
 			this.visit(node.left),
 			this.visit(node.right),
 			node.loc);
 	},
-	AssignmentExpression: function (node) {
+	visitAssignmentExpression: function (node) {
 		return this.assignmentExpression(
 			node.operator,
 			this.visit(node.left),
 			this.visit(node.right),
 			node.loc);
 	},
-	UpdateExpression: function (node) {
+	visitUpdateExpression: function (node) {
 		return this.updateExpression(
 			node.operator,
 			this.visit(node.argument),
 			node.prefix,
 			node.loc);
 	},
-	LogicalExpression: function (node) {
+	visitLogicalExpression: function (node) {
 		return this.logicalExpression(
 			node.operator,
 			this.visit(node.left),
 			this.visit(node.right),
 			node.loc);
 	},
-	ConditionalExpression: function (node) {
+	visitConditionalExpression: function (node) {
 		return this.conditionalExpression(
 			this.visit(node.test),
 			this.visit(node.consequent),
 			this.visit(node.alternate),
 			node.loc);
 	},
-	NewExpression: function (node) {
+	visitNewExpression: function (node) {
 		return this.newExpression(
 			this.visit(node.constructor),
 			node.arguments ? node.arguments.map(this.visit.bind(this)) : null,
 			node.loc);
 	},
-	CallExpression: function (node) {
+	visitCallExpression: function (node) {
 		return this.callExpression(
 			this.visit(node.callee),
 			node.arguments.map(this.visit.bind(this)),
 			node.loc);
 	},
-	MemberExpression: function (node) {
+	visitMemberExpression: function (node) {
 		return this.memberExpression(
 			this.visit(node.object),
 			this.visit(node.property),
 			node.computed,
 			node.loc);
 	},
-	SwitchCase: function (node) {
+	visitSwitchCase: function (node) {
 		return this.switchCase(
 			node.test ? this.visit(node.test) : null,
 			node.consequent.map(this.visit.bind(this)),
 			node.loc);
 	},
-	CatchClause: function (node) {
+	visitCatchClause: function (node) {
 		return this.catchClause(
 			this.visit(node.param),
 			this.visit(node.body),
 			node.loc);
 			
 	},
-	Identifier: function (node) {
+	visitIdentifier: function (node) {
 		return this.identifier(
 			node.name,
 			node.loc);
 	},
-	Literal: function (node) {
-		return this.value(
+	visitLiteral: function (node) {
+		return this.literal(
 			node.value,
 			node.loc);
 	},
-	Property: function (node) {
+	visitProperty: function (node) {
 		return this.property(
 			node.kind,
 			this.visit(node.key),
 			this.visit(node.value),
 			node.loc);
-	}
-};
+	},
 
-function Visitor () {}
-
-Visitor.prototype = {
-	visit: function (node) {
-		return TypeMap[node.type].call(this,node);
-	},
-	
 	program: function (body, loc) {},
 	emptyStatement: function (loc) {},
 	blockStatement: function (body, loc) {},
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.