From 8d70ab5853b8e43a622d829a212b2d80b2abc435 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 15 Jul 2015 13:51:47 +0100 Subject: [PATCH] switch node definitions to a DSL --- packages/babel/src/patch.js | 2 - packages/babel/src/types/alias-keys.json | 125 --------- packages/babel/src/types/builder-keys.json | 185 ------------- packages/babel/src/types/definitions/core.js | 251 ++++++++++++++++++ .../babel/src/types/definitions/es2015.js | 123 +++++++++ .../src/types/definitions/experimental.js | 34 +++ packages/babel/src/types/definitions/flow.js | 164 ++++++++++++ packages/babel/src/types/definitions/index.js | 21 ++ packages/babel/src/types/definitions/init.js | 7 + packages/babel/src/types/definitions/jsx.js | 49 ++++ packages/babel/src/types/definitions/misc.js | 10 + packages/babel/src/types/index.js | 6 +- packages/babel/src/types/visitor-keys.json | 126 --------- packages/babylon/README.md | 2 - 14 files changed, 662 insertions(+), 443 deletions(-) delete mode 100644 packages/babel/src/types/alias-keys.json delete mode 100644 packages/babel/src/types/builder-keys.json create mode 100644 packages/babel/src/types/definitions/core.js create mode 100644 packages/babel/src/types/definitions/es2015.js create mode 100644 packages/babel/src/types/definitions/experimental.js create mode 100644 packages/babel/src/types/definitions/flow.js create mode 100644 packages/babel/src/types/definitions/index.js create mode 100644 packages/babel/src/types/definitions/init.js create mode 100644 packages/babel/src/types/definitions/jsx.js create mode 100644 packages/babel/src/types/definitions/misc.js delete mode 100644 packages/babel/src/types/visitor-keys.json diff --git a/packages/babel/src/patch.js b/packages/babel/src/patch.js index 2ec3f48111..1d07060336 100644 --- a/packages/babel/src/patch.js +++ b/packages/babel/src/patch.js @@ -2,6 +2,4 @@ import estraverse from "estraverse"; import extend from "lodash/object/extend"; import * as t from "./types"; -// estraverse - extend(estraverse.VisitorKeys, t.VISITOR_KEYS); diff --git a/packages/babel/src/types/alias-keys.json b/packages/babel/src/types/alias-keys.json deleted file mode 100644 index 7c1d27be6d..0000000000 --- a/packages/babel/src/types/alias-keys.json +++ /dev/null @@ -1,125 +0,0 @@ -{ - "ExpressionStatement": ["Statement"], - "DebuggerStatement": ["Statement"], - "IfStatement": ["Statement"], - "TryStatement": ["Statement"], - "WithStatement": ["Statement"], - "EmptyStatement": ["Statement"], - "LabeledStatement": ["Statement"], - "VariableDeclaration": ["Statement", "Declaration"], - - "BreakStatement": ["Statement", "Terminatorless", "CompletionStatement"], - "ContinueStatement": ["Statement", "Terminatorless", "CompletionStatement"], - "ReturnStatement": ["Statement", "Terminatorless", "CompletionStatement"], - "ThrowStatement": ["Statement", "Terminatorless", "CompletionStatement"], - - "DoWhileStatement": ["Statement", "BlockParent", "Loop", "While", "Scopable"], - "WhileStatement": ["Statement", "BlockParent", "Loop", "While", "Scopable"], - "SwitchStatement": ["Statement", "BlockParent", "Scopable"], - - "ImportSpecifier": ["ModuleSpecifier"], - "ExportSpecifier": ["ModuleSpecifier"], - "ImportDefaultSpecifier": ["ModuleSpecifier"], - "ExportDefaultSpecifier": ["ModuleSpecifier"], - "ExportNamespaceSpecifier": ["ModuleSpecifier"], - "ExportDefaultFromSpecifier": ["ModuleSpecifier"], - "ExportAllDeclaration": ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"], - "ExportDefaultDeclaration": ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"], - "ExportNamedDeclaration": ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"], - "ImportDeclaration": ["Statement", "Declaration", "ModuleDeclaration"], - - "ArrowFunctionExpression": ["Scopable", "Function", "Func", "BlockParent", "FunctionParent", "Expression", "Pure"], - "FunctionDeclaration": ["Scopable", "Function", "Func", "BlockParent", "FunctionParent", "Statement", "Pure", "Declaration"], - "FunctionExpression": ["Scopable", "Function", "Func", "BlockParent", "FunctionParent", "Expression", "Pure"], - - "BlockStatement": ["Scopable", "BlockParent", "Block", "Statement"], - "Program": ["Scopable", "BlockParent", "Block", "FunctionParent"], - "CatchClause": ["Scopable"], - - "LogicalExpression": ["Binary", "Expression"], - "BinaryExpression": ["Binary", "Expression"], - - "UnaryExpression": ["UnaryLike", "Expression"], - "SpreadProperty": ["UnaryLike"], - "SpreadElement": ["UnaryLike"], - - "ClassDeclaration": ["Scopable", "Class", "Statement", "Declaration"], - "ClassExpression": ["Scopable", "Class", "Expression"], - - "ForOfStatement": ["Scopable", "Statement", "For", "BlockParent", "Loop", "ForXStatement"], - "ForInStatement": ["Scopable", "Statement", "For", "BlockParent", "Loop", "ForXStatement"], - "ForStatement": ["Scopable", "Statement", "For", "BlockParent", "Loop"], - - "ObjectPattern": ["Pattern"], - "ArrayPattern": ["Pattern"], - "AssignmentPattern": ["Pattern"], - - "Property": ["UserWhitespacable"], - - "AwaitExpression": ["Expression", "Terminatorless"], - "YieldExpression": ["Expression", "Terminatorless"], - - "ArrayExpression": ["Expression"], - "AssignmentExpression": ["Expression"], - "CallExpression": ["Expression"], - "ComprehensionExpression": ["Expression", "Scopable"], - "ConditionalExpression": ["Expression"], - "DoExpression": ["Expression"], - "Identifier": ["Expression"], - "Literal": ["Expression", "Pure"], - "MemberExpression": ["Expression"], - "MetaProperty": ["Expression"], - "NewExpression": ["Expression"], - "ObjectExpression": ["Expression"], - "ParenthesizedExpression": ["Expression"], - "SequenceExpression": ["Expression"], - "TaggedTemplateExpression": ["Expression"], - "ThisExpression": ["Expression"], - "Super": ["Expression"], - "UpdateExpression": ["Expression"], - - "AnyTypeAnnotation": ["Flow", "FlowBaseAnnotation"], - "ArrayTypeAnnotation": ["Flow"], - "BooleanTypeAnnotation": ["Flow", "FlowBaseAnnotation"], - "ClassImplements": ["Flow"], - "DeclareClass": ["Flow", "FlowDeclaration", "Statement", "Declaration"], - "DeclareFunction": ["Flow", "FlowDeclaration", "Statement", "Declaration"], - "DeclareModule": ["Flow", "FlowDeclaration", "Statement"], - "DeclareVariable": ["Flow", "FlowDeclaration", "Statement", "Declaration"], - "FunctionTypeAnnotation": ["Flow"], - "FunctionTypeParam": ["Flow"], - "GenericTypeAnnotation": ["Flow"], - "InterfaceExtends": ["Flow"], - "InterfaceDeclaration": ["Flow", "FlowDeclaration", "Statement", "Declaration"], - "IntersectionTypeAnnotation": ["Flow"], - "MixedTypeAnnotation": ["Flow", "FlowBaseAnnotation"], - "NullableTypeAnnotation": ["Flow"], - "NumberTypeAnnotation": ["Flow", "FlowBaseAnnotation"], - "StringLiteralTypeAnnotation": ["Flow"], - "StringTypeAnnotation": ["Flow", "FlowBaseAnnotation"], - "TupleTypeAnnotation": ["Flow"], - "TypeofTypeAnnotation": ["Flow"], - "TypeAlias": ["Flow", "FlowDeclaration", "Statement", "Declaration"], - "TypeAnnotation": ["Flow"], - "TypeCastExpression": ["Flow"], - "TypeParameterDeclaration": ["Flow"], - "TypeParameterInstantiation": ["Flow"], - "ObjectTypeAnnotation": ["Flow"], - "ObjectTypeCallProperty": ["Flow", "UserWhitespacable"], - "ObjectTypeIndexer": ["Flow", "UserWhitespacable"], - "ObjectTypeProperty": ["Flow", "UserWhitespacable"], - "QualifiedTypeIdentifier": ["Flow"], - "UnionTypeAnnotation": ["Flow"], - "VoidTypeAnnotation": ["Flow", "FlowBaseAnnotation"], - - "JSXAttribute": ["JSX", "Immutable"], - "JSXClosingElement": ["JSX", "Immutable"], - "JSXElement": ["JSX", "Immutable", "Expression"], - "JSXEmptyExpression": ["JSX", "Immutable", "Expression"], - "JSXExpressionContainer": ["JSX", "Immutable"], - "JSXIdentifier": ["JSX"], - "JSXMemberExpression": ["JSX", "Expression"], - "JSXNamespacedName": ["JSX"], - "JSXOpeningElement": ["JSX", "Immutable"], - "JSXSpreadAttribute": ["JSX"] -} diff --git a/packages/babel/src/types/builder-keys.json b/packages/babel/src/types/builder-keys.json deleted file mode 100644 index 8213e78235..0000000000 --- a/packages/babel/src/types/builder-keys.json +++ /dev/null @@ -1,185 +0,0 @@ -{ - "ArrayExpression": { - "elements": null - }, - - "ArrowFunctionExpression": { - "params": null, - "body": null - }, - - "AssignmentExpression": { - "operator": null, - "left": null, - "right": null - }, - - "BinaryExpression": { - "operator": null, - "left": null, - "right": null - }, - - "BindExpression": { - "object": null, - "callee": 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, - "async": false - }, - - "FunctionDeclaration": { - "id": null, - "params": null, - "body": null, - "generator": false, - "async": false - }, - - "GenericTypeAnnotation": { - "id": null, - "typeParameters": null - }, - - "Identifier": { - "name": null - }, - - "IfStatement": { - "test": null, - "consequent": null, - "alternate": null - }, - - "ImportDeclaration": { - "specifiers": null, - "source": null - }, - - "ImportSpecifier": { - "local": null, - "imported": null - }, - - "LabeledStatement": { - "label": null, - "body": null - }, - - "Literal": { - "value": null - }, - - "LogicalExpression": { - "operator": null, - "left": null, - "right": null - }, - - "MemberExpression": { - "object": null, - "property": null, - "computed": false - }, - - "MethodDefinition": { - "key": null, - "value": null, - "kind": "method", - "computed": false, - "static": false - }, - - "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 - }, - - "TemplateLiteral": { - "quasis": null, - "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 - } -} diff --git a/packages/babel/src/types/definitions/core.js b/packages/babel/src/types/definitions/core.js new file mode 100644 index 0000000000..3080232998 --- /dev/null +++ b/packages/babel/src/types/definitions/core.js @@ -0,0 +1,251 @@ +import define from "./index"; + +define("ArrayExpression", { + visitor: ["elements"], + aliases: ["Expression"] +}); + +define("AssignmentExpression", { + builder: ["operator", "left", "right"], + visitor: ["left", "right"], + aliases: ["Expression"] +}); + +define("BinaryExpression", { + builder: ["operator", "left", "right"], + visitor: ["left", "right"], + aliases: ["Binary", "Expression"] +}); + +define("BlockStatement", { + visitor: ["body"], + aliases: ["Scopable", "BlockParent", "Block", "Statement"] +}); + +define("BreakStatement", { + visitor: ["label"], + aliases: ["Statement", "Terminatorless", "CompletionStatement"] +}); + +define("CallExpression", { + visitor: ["callee", "arguments"], + aliases: ["Expression"] +}); + +define("CatchClause", { + visitor: ["param", "body"], + aliases: ["Scopable"] +}); + +define("ConditionalExpression", { + visitor: ["test", "consequent", "alternate"], + aliases: ["Expression"] +}); + +define("ContinueStatement", { + visitor: ["label"], + aliases: ["Statement", "Terminatorless", "CompletionStatement"] +}); + +define("DebuggerStatement", { + aliases: ["Statement"] +}); + +define("DoWhileStatement", { + visitor: ["body", "test"], + aliases: ["Statement", "BlockParent", "Loop", "While", "Scopable"] +}); + +define("EmptyStatement", { + aliases: ["Statement"] +}); + +define("ExpressionStatement", { + visitor: ["expression"], + aliases: ["Statement"] +}); + +define("File", { + builder: ["program", "comments", "tokens"], + visitor: ["program"] +}); + +define("ForInStatement", { + visitor: ["left", "right", "body"], + aliases: ["Scopable", "Statement", "For", "BlockParent", "Loop", "ForXStatement"] +}); + +define("ForStatement", { + visitor: ["init", "test", "update", "body"], + aliases: ["Scopable", "Statement", "For", "BlockParent", "Loop"] +}); + +define("FunctionDeclaration", { + builder: { + id: null, + params: null, + body: null, + generator: false, + async: false + }, + visitor: ["id", "params", "body", "returnType", "typeParameters"], + aliases: ["Scopable", "Function", "Func", "BlockParent", "FunctionParent", "Statement", "Pure", "Declaration"] +}); + +define("FunctionExpression", { + builder: { + id: null, + params: null, + body: null, + generator: false, + async: false + }, + visitor: ["id", "params", "body", "returnType", "typeParameters"], + aliases: ["Scopable", "Function", "Func", "BlockParent", "FunctionParent", "Expression", "Pure"] +}); + +define("Identifier", { + builder: ["name"], + visitor: ["typeAnnotation"], + aliases: ["Expression"] +}); + +define("IfStatement", { + visitor: ["test", "consequent", "alternate"], + aliases: ["Statement"] +}); + +define("LabeledStatement", { + visitor: ["label", "body"], + aliases: ["Statement"] +}); + +define("Literal", { + builder: ["value"], + aliases: ["Expression", "Pure"] +}); + +define("LogicalExpression", { + builder: ["operator", "left", "right"], + visitor: ["left", "right"], + aliases: ["Binary", "Expression"] +}); + +define("MemberExpression", { + builder: { + object: null, + property: null, + computed: false + }, + visitor: ["object", "property"], + aliases: ["Expression"] +}); + +define("MethodDefinition", { + builder: { + key: null, + value: null, + kind: "method", + computed: false, + static: false + }, + visitor: ["key", "value", "decorators"] +}); + +define("NewExpression", { + visitor: ["callee", "arguments"], + aliases: ["Expression"] +}); + +define("ObjectExpression", { + visitor: ["properties"], + aliases: ["Expression"] +}); + +define("Program", { + visitor: ["body"], + aliases: ["Scopable", "BlockParent", "Block", "FunctionParent"] +}); + +define("Property", { + builder: { + kind: null, + key: null, + value: null, + computed: false + }, + visitor: ["key", "value", "decorators"], + aliases: ["UserWhitespacable"] +}); + +define("RestElement", { + visitor: ["argument", "typeAnnotation"] +}); + +define("ReturnStatement", { + visitor: ["argument"], + aliases: ["Statement", "Terminatorless", "CompletionStatement"] +}); + +define("SequenceExpression", { + visitor: ["expressions"], + aliases: ["Expression"] +}); + +define("SwitchCase", { + visitor: ["test", "consequent"] +}); + +define("SwitchStatement", { + visitor: ["discriminant", "cases"], + aliases: ["Statement", "BlockParent", "Scopable"] +}); + +define("ThisExpression", { + aliases: ["Expression"] +}); + +define("ThrowStatement", { + visitor: ["argument"], + aliases: ["Statement", "Terminatorless", "CompletionStatement"] +}); + +define("TryStatement", { + visitor: ["block", "handlers", "handler", "guardedHandlers", "finalizer"], + aliases: ["Statement"] +}); + +define("UnaryExpression", { + builder: { + operator: null, + argument: null, + prefix: false + }, + visitor: ["argument"], + aliases: ["UnaryLike", "Expression"] +}); + +define("UpdateExpression", { + visitor: ["argument"], + aliases: ["Expression"] +}); + +define("VariableDeclaration", { + builder: ["kind", "declarations"], + visitor: ["declarations"], + aliases: ["Statement", "Declaration"] +}); + +define("VariableDeclarator", { + visitor: ["id", "init"] +}); + +define("WhileStatement", { + visitor: ["test", "body"], + aliases: ["Statement", "BlockParent", "Loop", "While", "Scopable"] +}); + +define("WithStatement", { + visitor: ["object", "body"], + aliases: ["Statement"] +}); diff --git a/packages/babel/src/types/definitions/es2015.js b/packages/babel/src/types/definitions/es2015.js new file mode 100644 index 0000000000..45214fe5ee --- /dev/null +++ b/packages/babel/src/types/definitions/es2015.js @@ -0,0 +1,123 @@ +import define from "./index"; + +define("AssignmentPattern", { + visitor: ["left", "right"], + aliases: ["Pattern"] +}); + +define("ArrayPattern", { + visitor: ["elements", "typeAnnotation"], + aliases: ["Pattern"] +}); + +define("ArrowFunctionExpression", { + builder: ["params", "body", "async"], + visitor: ["params", "body", "returnType"], + aliases: ["Scopable", "Function", "Func", "BlockParent", "FunctionParent", "Expression", "Pure"] +}); + +define("ClassBody", { + visitor: ["body"] +}); + +define("ClassDeclaration", { + visitor: ["id", "body", "superClass", "typeParameters", "superTypeParameters", "implements", "decorators"], + aliases: ["Scopable", "Class", "Statement", "Declaration"] +}); + +define("ClassExpression", { + visitor: ["id", "body", "superClass", "typeParameters", "superTypeParameters", "implements", "decorators"], + aliases: ["Scopable", "Class", "Expression"] +}); + +define("ExportAllDeclaration", { + visitor: ["source", "exported"], + aliases: ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"] +}); + +define("ExportDefaultDeclaration", { + visitor: ["declaration"], + aliases: ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"] +}); + +define("ExportNamedDeclaration", { + visitor: ["declaration", "specifiers", "source"], + aliases: ["Statement", "Declaration", "ModuleDeclaration", "ExportDeclaration"] +}); + +define("ExportDefaultSpecifier", { + visitor: ["exported"], + aliases: ["ModuleSpecifier"] +}); + +define("ExportNamespaceSpecifier", { + visitor: ["exported"], + aliases: ["ModuleSpecifier"] +}); + +define("ExportSpecifier", { + visitor: ["local", "exported"], + aliases: ["ModuleSpecifier"] +}); + +define("ForOfStatement", { + visitor: ["left", "right", "body"], + aliases: ["Scopable", "Statement", "For", "BlockParent", "Loop", "ForXStatement"] +}); + +define("ImportDeclaration", { + visitor: ["specifiers", "source"], + aliases: ["Statement", "Declaration", "ModuleDeclaration"] +}); + +define("ImportDefaultSpecifier", { + visitor: ["local"], + aliases: ["ModuleSpecifier"] +}); + +define("ImportNamespaceSpecifier", { + visitor: ["local"], + aliases: ["ModuleSpecifier"] +}); + +define("ImportSpecifier", { + visitor: ["local", "imported"], + aliases: ["ModuleSpecifier"] +}); + +define("MetaProperty", { + visitor: ["meta", "property"], + aliases: ["Expression"] +}); + +define("ObjectPattern", { + visitor: ["properties", "typeAnnotation"], + aliases: ["Pattern"] +}); + +define("SpreadElement", { + visitor: ["argument"], + aliases: ["UnaryLike"] +}); + +define("Super", { + aliases: ["Expression"] +}); + +define("TaggedTemplateExpression", { + visitor: ["tag", "quasi"], + aliases: ["Expression"] +}); + +define("TemplateElement"); + +define("TemplateLiteral", { + visitor: ["quasis", "expressions"], + aliases: ["Expression"] +}); + +define("YieldExpression", { + builder: ["argument", "delegate"], + visitor: ["argument"], + aliases: ["Expression", "Terminatorless"] +}); diff --git a/packages/babel/src/types/definitions/experimental.js b/packages/babel/src/types/definitions/experimental.js new file mode 100644 index 0000000000..6af58866b9 --- /dev/null +++ b/packages/babel/src/types/definitions/experimental.js @@ -0,0 +1,34 @@ +import define from "./index"; + +define("AwaitExpression", { + builder: ["argument", "all"], + visitor: ["argument"], + aliases: ["Expression", "Terminatorless"] +}); + +define("BindExpression", { + visitor: ["object", "callee"] +}); + +define("ComprehensionBlock", { + visitor: ["left", "right"] +}); + +define("ComprehensionExpression", { + visitor: ["filter", "blocks", "body"], + aliases: ["Expression", "Scopable"] +}); + +define("Decorator", { + visitor: ["expression"] +}); + +define("DoExpression", { + visitor: ["body"], + aliases: ["Expression"] +}); + +define("SpreadProperty", { + visitor: ["argument"], + aliases: ["UnaryLike"] +}); diff --git a/packages/babel/src/types/definitions/flow.js b/packages/babel/src/types/definitions/flow.js new file mode 100644 index 0000000000..448300e123 --- /dev/null +++ b/packages/babel/src/types/definitions/flow.js @@ -0,0 +1,164 @@ +import define from "./index"; + +define("AnyTypeAnnotation", { + aliases: ["Flow", "FlowBaseAnnotation"] +}); + +define("ArrayTypeAnnotation", { + visitor: ["elementType"], + aliases: ["Flow"] +}); + +define("BooleanTypeAnnotation", { + aliases: ["Flow", "FlowBaseAnnotation"] +}); + +define("ClassImplements", { + visitor: ["id", "typeParameters"], + aliases: ["Flow"] +}); + +define("ClassProperty", { + visitor: ["key", "value", "typeAnnotation", "decorators"], + aliases: ["Flow"] +}); + +define("DeclareClass", { + visitor: ["id", "typeParameters", "extends", "body"], + aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"] +}); + +define("DeclareFunction", { + visitor: ["id"], + aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"] +}); + +define("DeclareModule", { + visitor: ["id", "body"], + aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"] +}); + +define("DeclareVariable", { + visitor: ["id"], + aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"] +}); + +define("FunctionTypeAnnotation", { + visitor: ["typeParameters", "params", "rest", "returnType"], + aliases: ["Flow"] +}); + +define("FunctionTypeParam", { + visitor: ["name", "typeAnnotation"], + aliases: ["Flow"] +}); + +define("GenericTypeAnnotation", { + visitor: ["id", "typeParameters"], + aliases: ["Flow"] +}); + +define("InterfaceExtends", { + visitor: ["id", "typeParameters"], + aliases: ["Flow"] +}); + +define("InterfaceDeclaration", { + visitor: ["id", "typeParameters", "extends", "body"], + aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"] +}); + +define("IntersectionTypeAnnotation", { + visitor: ["types"], + aliases: ["Flow"] +}); + +define("MixedTypeAnnotation", { + aliases: ["Flow", "FlowBaseAnnotation"] +}); + +define("NullableTypeAnnotation", { + visitor: ["typeAnnotation"], + aliases: ["Flow"] +}); + +define("NumberTypeAnnotation", { + aliases: ["Flow", "FlowBaseAnnotation"] +}); + +define("StringLiteralTypeAnnotation", { + aliases: ["Flow"] +}); + +define("StringTypeAnnotation", { + aliases: ["Flow", "FlowBaseAnnotation"] +}); + +define("TupleTypeAnnotation", { + visitor: ["types"], + aliases: ["Flow"] +}); + +define("TypeofTypeAnnotation", { + visitor: ["argument"], + aliases: ["Flow"] +}); + +define("TypeAlias", { + visitor: ["id", "typeParameters", "right"], + aliases: ["Flow", "FlowDeclaration", "Statement", "Declaration"] +}); + +define("TypeAnnotation", { + visitor: ["typeAnnotation"], + aliases: ["Flow"] +}); + +define("TypeCastExpression", { + visitor: ["expression", "typeAnnotation"], + aliases: ["Flow"] +}); + +define("TypeParameterDeclaration", { + visitor: ["params"], + aliases: ["Flow"] +}); + +define("TypeParameterInstantiation", { + visitor: ["params"], + aliases: ["Flow"] +}); + +define("ObjectTypeAnnotation", { + visitor: ["properties", "indexers", "callProperties"], + aliases: ["Flow"] +}); + +define("ObjectTypeCallProperty", { + visitor: ["value"], + aliases: ["Flow", "UserWhitespacable"] +}); + +define("ObjectTypeIndexer", { + visitor: ["id", "key", "value"], + aliases: ["Flow", "UserWhitespacable"] +}); + +define("ObjectTypeProperty", { + visitor: ["key", "value"], + aliases: ["Flow", "UserWhitespacable"] +}); + +define("QualifiedTypeIdentifier", { + visitor: ["id", "qualification"], + aliases: ["Flow"] +}); + +define("UnionTypeAnnotation", { + visitor: ["types"], + aliases: ["Flow"] +}); + +define("VoidTypeAnnotation", { + aliases: ["Flow", "FlowBaseAnnotation"] +}); diff --git a/packages/babel/src/types/definitions/index.js b/packages/babel/src/types/definitions/index.js new file mode 100644 index 0000000000..4633ae2999 --- /dev/null +++ b/packages/babel/src/types/definitions/index.js @@ -0,0 +1,21 @@ +export var VISITOR_KEYS = {}; +export var ALIAS_KEYS = {}; +export var BUILDER_KEYS = {}; + +function builderFromArray(arr) { + var builder = {}; + for (var key of (arr: Array)) builder[key] = null; + return builder; +} + +export default function define(type, opts = {}) { + opts.visitor = opts.visitor || []; + opts.aliases = opts.aliases || []; + + if (!opts.builder) opts.builder = builderFromArray(opts.visitor); + if (Array.isArray(opts.builder)) opts.builder = builderFromArray(opts.builder); + + VISITOR_KEYS[type] = opts.visitor; + ALIAS_KEYS[type] = opts.aliases; + BUILDER_KEYS[type] = opts.builder; +} diff --git a/packages/babel/src/types/definitions/init.js b/packages/babel/src/types/definitions/init.js new file mode 100644 index 0000000000..4a82f02e92 --- /dev/null +++ b/packages/babel/src/types/definitions/init.js @@ -0,0 +1,7 @@ +import "./index"; +import "./core"; +import "./es2015"; +import "./flow"; +import "./jsx"; +import "./misc"; +import "./experimental"; diff --git a/packages/babel/src/types/definitions/jsx.js b/packages/babel/src/types/definitions/jsx.js new file mode 100644 index 0000000000..e3c2ed04fe --- /dev/null +++ b/packages/babel/src/types/definitions/jsx.js @@ -0,0 +1,49 @@ +import define from "./index"; + +define("JSXAttribute", { + visitor: ["name", "value"], + aliases: ["JSX", "Immutable"] +}); + +define("JSXClosingElement", { + visitor: ["name"], + aliases: ["JSX", "Immutable"] +}); + +define("JSXElement", { + visitor: ["openingElement", "closingElement", "children"], + aliases: ["JSX", "Immutable", "Expression"] +}); + +define("JSXEmptyExpression", { + aliases: ["JSX", "Expression"] +}); + +define("JSXExpressionContainer", { + visitor: ["expression"], + aliases: ["JSX", "Immutable"] +}); + +define("JSXIdentifier", { + aliases: ["JSX", "Expression"] +}); + +define("JSXMemberExpression", { + visitor: ["object", "property"], + aliases: ["JSX", "Expression"] +}); + +define("JSXNamespacedName", { + visitor: ["namespace", "name"], + aliases: ["JSX"] +}); + +define("JSXOpeningElement", { + visitor: ["name", "attributes"], + aliases: ["JSX", "Immutable"] +}); + +define("JSXSpreadAttribute", { + visitor: ["argument"], + aliases: ["JSX"] +}); diff --git a/packages/babel/src/types/definitions/misc.js b/packages/babel/src/types/definitions/misc.js new file mode 100644 index 0000000000..28730499f1 --- /dev/null +++ b/packages/babel/src/types/definitions/misc.js @@ -0,0 +1,10 @@ +import define from "./index"; + +define("Noop", { + visitor: [] +}); + +define("ParenthesizedExpression", { + visitor: ["expression"], + aliases: ["Expression"] +}); diff --git a/packages/babel/src/types/index.js b/packages/babel/src/types/index.js index 8528af9e42..f08bc0efc2 100644 --- a/packages/babel/src/types/index.js +++ b/packages/babel/src/types/index.js @@ -42,9 +42,9 @@ export const BOOLEAN_UNARY_OPERATORS = ["delete", "!"]; export const NUMBER_UNARY_OPERATORS = ["+", "-", "++", "--", "~"]; export const STRING_UNARY_OPERATORS = ["typeof"]; -export const VISITOR_KEYS = require("./visitor-keys"); -export const BUILDER_KEYS = require("./builder-keys"); -export const ALIAS_KEYS = require("./alias-keys"); +import "./definitions/init"; +import { VISITOR_KEYS, BUILDER_KEYS, ALIAS_KEYS } from "./definitions"; +export { VISITOR_KEYS, BUILDER_KEYS, ALIAS_KEYS }; /** * Registers `is[Type]` and `assert[Type]` for all types. diff --git a/packages/babel/src/types/visitor-keys.json b/packages/babel/src/types/visitor-keys.json deleted file mode 100644 index 8d99ae7918..0000000000 --- a/packages/babel/src/types/visitor-keys.json +++ /dev/null @@ -1,126 +0,0 @@ -{ - "ArrayExpression": ["elements"], - "ArrayPattern": ["elements", "typeAnnotation"], - "ArrowFunctionExpression": ["params", "body", "returnType"], - "AssignmentExpression": ["left", "right"], - "AssignmentPattern": ["left", "right"], - "AwaitExpression": ["argument"], - "BinaryExpression": ["left", "right"], - "BindExpression": ["object", "callee"], - "BlockStatement": ["body"], - "BreakStatement": ["label"], - "CallExpression": ["callee", "arguments"], - "CatchClause": ["param", "body"], - "ClassBody": ["body"], - "ClassDeclaration": ["id", "body", "superClass", "typeParameters", "superTypeParameters", "implements", "decorators"], - "ClassExpression": ["id", "body", "superClass", "typeParameters", "superTypeParameters", "implements", "decorators"], - "ComprehensionBlock": ["left", "right"], - "ComprehensionExpression": ["filter", "blocks", "body"], - "ConditionalExpression": ["test", "consequent", "alternate"], - "ContinueStatement": ["label"], - "Decorator": ["expression"], - "DebuggerStatement": [], - "DoWhileStatement": ["body", "test"], - "DoExpression": ["body"], - "EmptyStatement": [], - "ExpressionStatement": ["expression"], - "File": ["program"], - "ForInStatement": ["left", "right", "body"], - "ForOfStatement": ["left", "right", "body"], - "ForStatement": ["init", "test", "update", "body"], - "FunctionDeclaration": ["id", "params", "body", "returnType", "typeParameters"], - "FunctionExpression": ["id", "params", "body", "returnType", "typeParameters"], - "Identifier": ["typeAnnotation"], - "IfStatement": ["test", "consequent", "alternate"], - "ImportDefaultSpecifier": ["local"], - "ImportNamespaceSpecifier": ["local"], - "ImportDeclaration": ["specifiers", "source"], - "ImportSpecifier": ["imported", "local"], - "LabeledStatement": ["label", "body"], - "Literal": [], - "LogicalExpression": ["left", "right"], - "MemberExpression": ["object", "property"], - "MetaProperty": ["meta", "property"], - "MethodDefinition": ["key", "value", "decorators"], - "NewExpression": ["callee", "arguments"], - "Noop": [], - "ObjectExpression": ["properties"], - "ObjectPattern": ["properties", "typeAnnotation"], - "ParenthesizedExpression": ["expression"], - "Program": ["body"], - "Property": ["key", "value", "decorators"], - "RestElement": ["argument", "typeAnnotation"], - "ReturnStatement": ["argument"], - "SequenceExpression": ["expressions"], - "SpreadElement": ["argument"], - "SpreadProperty": ["argument"], - "Super": [], - "SwitchCase": ["test", "consequent"], - "SwitchStatement": ["discriminant", "cases"], - "TaggedTemplateExpression": ["tag", "quasi"], - "TemplateElement": [], - "TemplateLiteral": ["quasis", "expressions"], - "ThisExpression": [], - "ThrowStatement": ["argument"], - "TryStatement": ["block", "handlers", "handler", "guardedHandlers", "finalizer"], - "UnaryExpression": ["argument"], - "UpdateExpression": ["argument"], - "VariableDeclaration": ["declarations"], - "VariableDeclarator": ["id", "init"], - "WhileStatement": ["test", "body"], - "WithStatement": ["object", "body"], - "YieldExpression": ["argument"], - - "ExportAllDeclaration": ["source", "exported"], - "ExportDefaultDeclaration": ["declaration"], - "ExportNamedDeclaration": ["declaration", "specifiers", "source"], - "ExportDefaultSpecifier": ["exported"], - "ExportNamespaceSpecifier": ["exported"], - "ExportSpecifier": ["local", "exported"], - - "AnyTypeAnnotation": [], - "ArrayTypeAnnotation": ["elementType"], - "BooleanTypeAnnotation": [], - "ClassImplements": ["id", "typeParameters"], - "ClassProperty": ["key", "value", "typeAnnotation", "decorators"], - "DeclareClass": ["id", "typeParameters", "extends", "body"], - "DeclareFunction": ["id"], - "DeclareModule": ["id", "body"], - "DeclareVariable": ["id"], - "FunctionTypeAnnotation": ["typeParameters", "params", "rest", "returnType"], - "FunctionTypeParam": ["name", "typeAnnotation"], - "GenericTypeAnnotation": ["id", "typeParameters"], - "InterfaceExtends": ["id", "typeParameters"], - "InterfaceDeclaration": ["id", "typeParameters", "extends", "body"], - "IntersectionTypeAnnotation": ["types"], - "MixedTypeAnnotation": [], - "NullableTypeAnnotation": ["typeAnnotation"], - "NumberTypeAnnotation": [], - "StringLiteralTypeAnnotation": [], - "StringTypeAnnotation": [], - "TupleTypeAnnotation": ["types"], - "TypeofTypeAnnotation": ["argument"], - "TypeAlias": ["id", "typeParameters", "right"], - "TypeAnnotation": ["typeAnnotation"], - "TypeCastExpression": ["expression", "typeAnnotation"], - "TypeParameterDeclaration": ["params"], - "TypeParameterInstantiation": ["params"], - "ObjectTypeAnnotation": ["properties", "indexers", "callProperties"], - "ObjectTypeCallProperty": ["value"], - "ObjectTypeIndexer": ["id", "key", "value"], - "ObjectTypeProperty": ["key", "value"], - "QualifiedTypeIdentifier": ["id", "qualification"], - "UnionTypeAnnotation": ["types"], - "VoidTypeAnnotation": [], - - "JSXAttribute": ["name", "value"], - "JSXClosingElement": ["name"], - "JSXElement": ["openingElement", "closingElement", "children"], - "JSXEmptyExpression": [], - "JSXExpressionContainer": ["expression"], - "JSXIdentifier": [], - "JSXMemberExpression": ["object", "property"], - "JSXNamespacedName": ["namespace", "name"], - "JSXOpeningElement": ["name", "attributes"], - "JSXSpreadAttribute": ["argument"] -} diff --git a/packages/babylon/README.md b/packages/babylon/README.md index a08fb65a0c..25edcd9a74 100644 --- a/packages/babylon/README.md +++ b/packages/babylon/README.md @@ -6,8 +6,6 @@ Babylon is a JavaScript parser used in Babel.

----- - ## Credits Heavily based on [acorn](https://github.com/marijnh/acorn) and [acorn-jsx](https://github.com/RReverser/acorn-jsx),