proper optional builder keys

This commit is contained in:
Sebastian McKenzie
2015-02-11 00:13:27 +11:00
parent 7b8118d8bd
commit 50f903caf7
2 changed files with 175 additions and 37 deletions

View File

@@ -1,33 +1,162 @@
{
"ArrayExpression": ["elements"],
"ArrowFunctionExpression": ["params", "body"],
"AssignmentExpression": ["operator", "left", "right"],
"BinaryExpression": ["operator", "left", "right"],
"BlockStatement": ["body"],
"CallExpression": ["callee", "arguments"],
"ConditionalExpression": ["test", "consequent", "alternate"],
"ExpressionStatement": ["expression"],
"File": ["program", "comments", "tokens"],
"FunctionExpression": ["id", "params", "body", "generator"],
"FunctionDeclaration": ["id", "params", "body", "generator"],
"Identifier": ["name"],
"IfStatement": ["test", "consequent", "alternate"],
"ImportDeclaration": ["specifiers", "source"],
"ImportSpecifier": ["id", "name"],
"Literal": ["value"],
"LogicalExpression": ["operator", "left", "right"],
"MemberExpression": ["object", "property", "computed"],
"MethodDefinition": ["key", "value", "computed", "kind"],
"NewExpression": ["callee", "arguments"],
"ObjectExpression": ["properties"],
"Program": ["body"],
"Property": ["kind", "key", "value", "computed"],
"ReturnStatement": ["argument"],
"SequenceExpression": ["expressions"],
"ThrowExpression": ["argument"],
"UnaryExpression": ["operator", "argument", "prefix"],
"VariableDeclaration": ["kind", "declarations"],
"VariableDeclarator": ["id", "init"],
"WithStatement": ["object", "body"],
"YieldExpression": ["argument", "delegate"]
"ArrayExpression": {
"elements": null
},
"ArrowFunctionExpression": {
"params": null,
"body": null
},
"AssignmentExpression": {
"operator": null,
"left": null,
"right": null
},
"BinaryExpression": {
"operator": null,
"left": null,
"right": null
},
"BlockStatement": {
"body": null
},
"CallExpression": {
"callee": null,
"arguments": null
},
"ConditionalExpression": {
"test": null,
"consequent": null,
"alternate": null
},
"ExpressionStatement": {
"expression": null
},
"File": {
"program": null,
"comments": null,
"tokens": null
},
"FunctionExpression": {
"id": null,
"params": null,
"body": null,
"generator": false
},
"FunctionDeclaration": {
"id": null,
"params": null,
"body": null,
"generator": false
},
"Identifier": {
"name": null
},
"IfStatement": {
"test": null,
"consequent": null,
"alternate": null
},
"ImportDeclaration": {
"specifiers": null,
"source": null
},
"ImportSpecifier": {
"id": null,
"name": null
},
"Literal": {
"value": null
},
"LogicalExpression": {
"operator": null,
"left": null,
"right": null
},
"MemberExpression": {
"object": null,
"property": null,
"computed": false
},
"MethodDefinition": {
"key": null,
"value": null,
"computed": false,
"kind": null
},
"NewExpression": {
"callee": null,
"arguments": null
},
"ObjectExpression": {
"properties": null
},
"Program": {
"body": null
},
"Property": {
"kind": null,
"key": null,
"value": null,
"computed": false
},
"ReturnStatement": {
"argument": null
},
"SequenceExpression": {
"expressions": null
},
"ThrowExpression": {
"argument": null
},
"UnaryExpression": {
"operator": null,
"argument": null,
"prefix": null
},
"VariableDeclaration": {
"kind": null,
"declarations": null
},
"VariableDeclarator": {
"id": null,
"init": null
},
"WithStatement": {
"object": null,
"body": null
},
"YieldExpression": {
"argument": null,
"delegate": null
}
}

View File

@@ -97,20 +97,29 @@ t.is = function (type, node, opts, skipAliasCheck) {
//
t.BUILDER_KEYS = defaults(require("./builder-keys"), t.VISITOR_KEYS);
t.BUILDER_KEYS = require("./builder-keys");
each(t.VISITOR_KEYS, function (keys, type) {
if (t.BUILDER_KEYS[type]) return;
var defs = {};
each(keys, function (key) {
defs[key] = null;
});
t.BUILDER_KEYS[type] = defs;
});
each(t.BUILDER_KEYS, function (keys, type) {
t[type[0].toLowerCase() + type.slice(1)] = function () {
var args = arguments;
var node = new Node;
node.start = null;
node.type = type;
for (var i = 0; i < keys.length; i++) {
var arg = args[i];
if (arg === undefined) arg = false;
var i = 0;
var key = keys[i];
for (var key in keys) {
var arg = arguments[i++];
if (arg === undefined) arg = keys[key];
node[key] = arg;
}