Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simple refactoring #53

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
187 changes: 92 additions & 95 deletions index.js
@@ -1,97 +1,94 @@
function id(x) { return x; }
var PRIMITIVE = exports.PRIMITIVE = {};
var NODE = exports.NODE = {};
var LIST_NODE = exports.LIST_NODE = {};
var MAYBE_NODE = exports.MAYBE_NODE = {};
var LIST_MAYBE_NODE = exports.LIST_MAYBE_NODE = {};

exports.default = function (primitive, node, listNode, maybeNode, listMaybeNode) {
if (primitive == null) primitive = id;
if (node == null) node = id;
if (listNode == null) listNode = id;
if (maybeNode == null) maybeNode = id;
if (listMaybeNode == null) listMaybeNode = id;
return {
__proto__: null,
ArrayBinding: [listMaybeNode("elements"), maybeNode("restElement")],
ArrayExpression: [listMaybeNode("elements")],
ArrowExpression: [listNode("parameters"), maybeNode("restParameter"), node("body")],
AssignmentExpression: [primitive("operator"), node("binding"), node("expression")],
BinaryExpression: [primitive("operator"), node("left"), node("right")],
BindingIdentifier: [node("identifier")],
BindingPropertyIdentifier: [node("identifier"), maybeNode("init")],
BindingPropertyProperty: [node("name"), node("binding")],
BindingWithDefault: [node("binding"), node("init")],
Block: [listNode("statements")],
BlockStatement: [node("block")],
BreakStatement: [maybeNode("label")],
CallExpression: [node("callee"), listNode("arguments")],
CatchClause: [node("binding"), node("body")],
ClassDeclaration: [node("name"), maybeNode("super"), listNode("elements")],
ClassElement: [primitive("isStatic"), node("method")],
ClassExpression: [maybeNode("name"), maybeNode("super"), listNode("elements")],
ComputedMemberExpression: [node("object"), node("expression")],
ComputedPropertyName: [node("expression")],
ConditionalExpression: [node("test"), node("consequent"), node("alternate")],
ContinueStatement: [maybeNode("label")],
DataProperty: [node("name"), node("expression")],
DebuggerStatement: [],
Directive: [primitive("rawValue")],
DoWhileStatement: [node("body"), node("test")],
EmptyStatement: [],
Export: [node("declaration")],
ExportAllFrom: [primitive("moduleSpecifier")],
ExportDefault: [node("body")],
ExportFrom: [listNode("namedExports"), primitive("moduleSpecifier")],
ExportSpecifier: [maybeNode("name"), node("exportedName")],
ExpressionStatement: [node("expression")],
ForInStatement: [node("left"), node("right"), node("body")],
ForOfStatement: [node("left"), node("right"), node("body")],
ForStatement: [maybeNode("init"), maybeNode("test"), maybeNode("update"), node("body")],
FunctionBody: [listNode("directives"), listNode("statements")],
FunctionDeclaration: [primitive("isGenerator"), node("name"), listNode("parameters"), maybeNode("restParameter"), node("body")],
FunctionExpression: [primitive("isGenerator"), maybeNode("name"), listNode("parameters"), maybeNode("restParameter"), node("body")],
Getter: [node("name"), node("body")],
Identifier: [primitive("name")],
IdentifierExpression: [node("identifier")],
IfStatement: [node("test"), node("consequent"), maybeNode("alternate")],
Import: [maybeNode("defaultBinding"), listNode("namedImports"), primitive("moduleSpecifier")],
ImportNamespace: [maybeNode("defaultBinding"), node("namespaceBinding"), primitive("moduleSpecifier")],
ImportSpecifier: [maybeNode("name"), node("binding")],
LabeledStatement: [node("label"), node("body")],
LiteralBooleanExpression: [primitive("value")],
LiteralInfinityExpression: [],
LiteralNullExpression: [],
LiteralNumericExpression: [primitive("value")],
LiteralRegExpExpression: [primitive("pattern"), primitive("flags")],
LiteralStringExpression: [primitive("value")],
Method: [primitive("isGenerator"), node("name"), listNode("parameters"), maybeNode("restParameter"), node("body")],
Module: [listNode("items")],
NewExpression: [node("callee"), listNode("arguments")],
NewTargetExpression: [],
ObjectBinding: [listNode("properties")],
ObjectExpression: [listNode("properties")],
PostfixExpression: [node("operand"), primitive("operator")],
PrefixExpression: [primitive("operator"), node("operand")],
ReturnStatement: [maybeNode("expression")],
Script: [node("body")],
Setter: [node("name"), node("parameter"), node("body")],
ShorthandProperty: [node("name")],
SpreadElement: [node("expression")],
StaticMemberExpression: [node("object"), primitive("property")],
StaticPropertyName: [primitive("value")],
Super: [],
SwitchCase: [node("test"), listNode("consequent")],
SwitchDefault: [listNode("consequent")],
SwitchStatement: [node("discriminant"), listNode("cases")],
SwitchStatementWithDefault: [node("discriminant"), listNode("preDefaultCases"), node("defaultCase"), listNode("postDefaultCases")],
TemplateElement: [primitive("rawValue")],
TemplateExpression: [maybeNode("tag"), listNode("elements")],
ThisExpression: [],
ThrowStatement: [node("expression")],
TryCatchStatement: [node("body"), node("catchClause")],
TryFinallyStatement: [node("body"), maybeNode("catchClause"), node("finalizer")],
VariableDeclaration: [primitive("kind"), listNode("declarators")],
VariableDeclarationStatement: [node("declaration")],
VariableDeclarator: [node("binding"), maybeNode("init")],
WhileStatement: [node("test"), node("body")],
WithStatement: [node("object"), node("body")],
YieldExpression: [maybeNode("expression")],
YieldGeneratorExpression: [node("expression")],
};
exports.default = {
__proto__: null,
ArrayBinding: {elements: LIST_MAYBE_NODE, restElement: MAYBE_NODE},
ArrayExpression: {elements: LIST_MAYBE_NODE},
ArrowExpression: {parameters: LIST_NODE, restParameter: MAYBE_NODE, body: NODE},
AssignmentExpression: {operator: PRIMITIVE, binding: NODE, expression: NODE},
BinaryExpression: {operator: PRIMITIVE, left: NODE, right: NODE},
BindingIdentifier: {identifier: NODE},
BindingPropertyIdentifier: {identifier: NODE, init: MAYBE_NODE},
BindingPropertyProperty: {name: NODE, binding: NODE},
BindingWithDefault: {binding: NODE, init: NODE},
Block: {statements: LIST_NODE},
BlockStatement: {block: NODE},
BreakStatement: {label: MAYBE_NODE},
CallExpression: {callee: NODE, arguments: LIST_NODE},
CatchClause: {binding: NODE, body: NODE},
ClassDeclaration: {name: NODE, super: MAYBE_NODE, elements: LIST_NODE},
ClassElement: {isStatic: PRIMITIVE, method: NODE},
ClassExpression: {name: MAYBE_NODE, super: MAYBE_NODE, elements: LIST_NODE},
ComputedMemberExpression: {object: NODE, expression: NODE},
ComputedPropertyName: {expression: NODE},
ConditionalExpression: {test: NODE, consequent: NODE, alternate: NODE},
ContinueStatement: {label: MAYBE_NODE},
DataProperty: {name: NODE, expression: NODE},
DebuggerStatement: {},
Directive: {rawValue: PRIMITIVE},
DoWhileStatement: {body: NODE, test: NODE},
EmptyStatement: {},
Export: {declaration: NODE},
ExportAllFrom: {moduleSpecifier: PRIMITIVE},
ExportDefault: {body: NODE},
ExportFrom: {namedExports: LIST_NODE, moduleSpecifier: PRIMITIVE},
ExportSpecifier: {name: MAYBE_NODE, exportedName: NODE},
ExpressionStatement: {expression: NODE},
ForInStatement: {left: NODE, right: NODE, body: NODE},
ForOfStatement: {left: NODE, right: NODE, body: NODE},
ForStatement: {init: MAYBE_NODE, test: MAYBE_NODE, update: MAYBE_NODE, body: NODE},
FunctionBody: {directives: LIST_NODE, statements: LIST_NODE},
FunctionDeclaration: {isGenerator: PRIMITIVE, name: NODE, parameters: LIST_NODE, restParameter: MAYBE_NODE, body: NODE},
FunctionExpression: {isGenerator: PRIMITIVE, name: MAYBE_NODE, parameters: LIST_NODE, restParameter: MAYBE_NODE, body: NODE},
Getter: {name: NODE, body: NODE},
Identifier: {name: PRIMITIVE},
IdentifierExpression: {identifier: NODE},
IfStatement: {test: NODE, consequent: NODE, alternate: MAYBE_NODE},
Import: {defaultBinding: MAYBE_NODE, namedImports: LIST_NODE, moduleSpecifier: PRIMITIVE},
ImportNamespace: {defaultBinding: MAYBE_NODE, namespaceBinding: NODE, moduleSpecifier: PRIMITIVE},
ImportSpecifier: {name: MAYBE_NODE, binding: NODE},
LabeledStatement: {label: NODE, body: NODE},
LiteralBooleanExpression: {value: PRIMITIVE},
LiteralInfinityExpression: {},
LiteralNullExpression: {},
LiteralNumericExpression: {value: PRIMITIVE},
LiteralRegExpExpression: {pattern: PRIMITIVE, flags: PRIMITIVE},
LiteralStringExpression: {value: PRIMITIVE},
Method: {isGenerator: PRIMITIVE, name: NODE, parameters: LIST_NODE, restParameter: MAYBE_NODE, body: NODE},
Module: {items: LIST_NODE},
NewExpression: {callee: NODE, arguments: LIST_NODE},
NewTargetExpression: {},
ObjectBinding: {properties: LIST_NODE},
ObjectExpression: {properties: LIST_NODE},
PostfixExpression: {operand: NODE, operator: PRIMITIVE},
PrefixExpression: {operator: PRIMITIVE, operand: NODE},
ReturnStatement: {expression: MAYBE_NODE},
Script: {body: NODE},
Setter: {name: NODE, parameter: NODE, body: NODE},
ShorthandProperty: {name: NODE},
SpreadElement: {expression: NODE},
StaticMemberExpression: {object: NODE, property: PRIMITIVE},
StaticPropertyName: {value: PRIMITIVE},
Super: {},
SwitchCase: {test: NODE, consequent: LIST_NODE},
SwitchDefault: {consequent: LIST_NODE},
SwitchStatement: {discriminant: NODE, cases: LIST_NODE},
SwitchStatementWithDefault: {discriminant: NODE, preDefaultCases: LIST_NODE, defaultCase: NODE, postDefaultCases: LIST_NODE},
TemplateElement: {rawValue: PRIMITIVE},
TemplateExpression: {tag: MAYBE_NODE, elements: LIST_NODE},
ThisExpression: {},
ThrowStatement: {expression: NODE},
TryCatchStatement: {body: NODE, catchClause: NODE},
TryFinallyStatement: {body: NODE, catchClause: MAYBE_NODE, finalizer: NODE},
VariableDeclaration: {kind: PRIMITIVE, declarators: LIST_NODE},
VariableDeclarationStatement: {declaration: NODE},
VariableDeclarator: {binding: NODE, init: MAYBE_NODE},
WhileStatement: {test: NODE, body: NODE},
WithStatement: {object: NODE, body: NODE},
YieldExpression: {expression: MAYBE_NODE},
YieldGeneratorExpression: {expression: NODE},
};