remove invalid node types from generator and add todo ones

This commit is contained in:
Sebastian McKenzie 2014-10-30 17:11:31 +11:00
parent 976e6782a2
commit 11dd13b7e0

View File

@ -3,6 +3,8 @@ module.exports = function (ast, opts) {
return gen.generate(ast, opts); return gen.generate(ast, opts);
}; };
module.exports.CodeGenerator = CodeGenerator;
var assert = require("assert"); var assert = require("assert");
var _ = require("lodash"); var _ = require("lodash");
@ -98,18 +100,11 @@ CodeGenerator.prototype.MemberExpression = function (node, print) {
return code; return code;
}; };
CodeGenerator.prototype.Path = function (node, print) {
return "." + print(node.body);
};
CodeGenerator.prototype.Identifier = function (node) { CodeGenerator.prototype.Identifier = function (node) {
return node.name; return node.name;
}; };
CodeGenerator.prototype.SpreadElement = CodeGenerator.prototype.SpreadElement = function (node, print) {
CodeGenerator.prototype.SpreadElementPattern =
CodeGenerator.prototype.SpreadProperty =
CodeGenerator.prototype.SpreadPropertyPattern = function (node, print) {
return "..." + print(node.argument); return "..." + print(node.argument);
}; };
@ -120,7 +115,7 @@ CodeGenerator.prototype.FunctionExpression = function (node, print) {
code += "function"; code += "function";
if (node.generator) code += "*"; if (node.generator) code += "*";
if (node.id) code += " " + print(node.id); if (node.id) code += " " + print(node.id);
code += "(" + node.params.map(print).join(", ") + ")"; code += this._params(node, print);
code += " " + print(node.body); code += " " + print(node.body);
return code; return code;
}; };
@ -131,17 +126,13 @@ CodeGenerator.prototype.ArrowFunctionExpression = function (node, print) {
if (node.params.length === 1) { if (node.params.length === 1) {
code += print(node.params[0]); code += print(node.params[0]);
} else { } else {
code += "(" + node.params.map(this.buildPrint(node)).join(", ") + ")"; code += this._params(node, print);
} }
code += " => "; code += " => ";
code += print(node.body); code += print(node.body);
return code; return code;
}; };
CodeGenerator.prototype.MethodDefinition = function () {
throw new Error("MethodDefinition");
};
CodeGenerator.prototype.YieldExpression = function (node, print) { CodeGenerator.prototype.YieldExpression = function (node, print) {
var code = "yield"; var code = "yield";
if (node.delegate) code += "*"; if (node.delegate) code += "*";
@ -149,23 +140,6 @@ CodeGenerator.prototype.YieldExpression = function (node, print) {
return code; return code;
}; };
CodeGenerator.prototype.AwaitExpression = function (node, print) {
var code = "await";
if (node.all) code += "*";
if (node.argument) code += print(node.argument);
return code;
};
CodeGenerator.prototype.ModuleDeclaration = function (node, print) {
var code = "module " + print(node.id);
if (node.source) {
code += " from " + print(node.source);
} else {
code += print(node.body);
}
return code;
};
CodeGenerator.prototype.ImportSpecifier = CodeGenerator.prototype.ImportSpecifier =
CodeGenerator.prototype.ExportSpecifier = function (node, print) { CodeGenerator.prototype.ExportSpecifier = function (node, print) {
var code = print(node.id); var code = print(node.id);
@ -222,37 +196,27 @@ CodeGenerator.prototype.CallExpression = function (node, print) {
CodeGenerator.prototype.ObjectExpression = CodeGenerator.prototype.ObjectExpression =
CodeGenerator.prototype.ObjectPattern = function (node, print) { CodeGenerator.prototype.ObjectPattern = function (node, print) {
var allowBreak = false; var props = node.properties;
var len = props.length;
if (!len) return "{}";
var indent = this.indent; var indent = this.indent;
var parts = [len > 0 ? "{\n" : "{"]; var parts = ["{\n"];
var len = node.properties.length;
_.each(node.properties, function (prop, i) { _.each(props, function (prop, i) {
var lines = indent(print(prop)); var part = indent(print(prop));
var multiLine = lines.length > 1;
if (multiLine && allowBreak) {
// Similar to the logic for BlockStatement.
parts.push("\n");
}
parts.push(lines);
if (i < len - 1) { if (i < len - 1) {
// Add an extra line break if the previous object property // not the last item
// had a multi-line value. part += ",\n";
parts.push(multiLine ? ",\n\n" : ",\n");
allowBreak = !multiLine;
} }
parts.push(part);
}); });
parts.push(len > 0 ? "\n}" : "}"); parts.push("\n}");
return parts.join("\n"); return parts.join("");
};
CodeGenerator.prototype.PropertyPattern = function (node, print) {
return print(node.key) + ": " + print(node.pattern);
}; };
CodeGenerator.prototype.Property = function (node, print) { CodeGenerator.prototype.Property = function (node, print) {
@ -318,10 +282,6 @@ CodeGenerator.prototype.Literal = function (node) {
} }
}; };
CodeGenerator.prototype.ModuleSpecifier = function (node) {
return "\"" + node.value + "\"";
};
CodeGenerator.prototype.UnaryExpression = function (node, print) { CodeGenerator.prototype.UnaryExpression = function (node, print) {
var code = node.operator; var code = node.operator;
if (/[a-z]$/.test(node.operator)) code += " "; if (/[a-z]$/.test(node.operator)) code += " ";
@ -523,6 +483,38 @@ CodeGenerator.prototype.ClassDeclaration = function (node, print) {
return parts.join(""); return parts.join("");
}; };
CodeGenerator.prototype.ComprehensionBlock = function (node, print) {
throw new Error("ComprehensionBlock");
};
CodeGenerator.prototype.ComprehensionExpression = function (node, print) {
throw new Error("ComprehensionExpression");
};
CodeGenerator.prototype.ForOfStatement = function (node, print) {
throw new Error("ForOfStatement");
};
CodeGenerator.prototype.ImportBatchSpecifier = function (node, print) {
throw new Error("ImportBatchSpecifier");
};
CodeGenerator.prototype.ParenthesizedExpression = function (node, print) {
throw new Error("ParenthesizedExpression");
};
CodeGenerator.prototype.TaggedTemplateExpression = function (node, print) {
throw new Error("TaggedTemplateExpression");
};
CodeGenerator.prototype.TemplateElement = function (node, print) {
throw new Error("TemplateElement");
};
CodeGenerator.prototype.TemplateLiteral = function (node, print) {
throw new Error("TemplateLiteral");
};
CodeGenerator.prototype.ClassBody = function (node, print) { CodeGenerator.prototype.ClassBody = function (node, print) {
if (node.body.length === 0) { if (node.body.length === 0) {
return "{}"; return "{}";
@ -535,6 +527,10 @@ CodeGenerator.prototype.ClassBody = function (node, print) {
].join(""); ].join("");
}; };
CodeGenerator.prototype._params = function (node, print) {
return "(" + node.params.map(print).join(", ") + ")";
};
CodeGenerator.prototype._method = function (kind, key, value, print) { CodeGenerator.prototype._method = function (kind, key, value, print) {
var parts = []; var parts = [];
@ -553,7 +549,7 @@ CodeGenerator.prototype._method = function (kind, key, value, print) {
parts.push( parts.push(
print(key), print(key),
"(" + value.params.map(print).join(", ") + ")", this._params(value, print),
print(value.body) print(value.body)
); );
@ -620,10 +616,6 @@ CodeGenerator.prototype.XJSClosingElement = function (node) {
return "</" + node.name + ">"; return "</" + node.name + ">";
}; };
CodeGenerator.prototype.XJSText = function (node) {
return node.value;
};
CodeGenerator.prototype.XJSEmptyExpression = function () { CodeGenerator.prototype.XJSEmptyExpression = function () {
return ""; return "";
}; };