From b97dc4778b59526385bde7d65d7f34f1cbe96076 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Tue, 21 Jul 2015 01:14:19 +0100 Subject: [PATCH] add Flow virtual type --- packages/babel/src/traversal/path/index.js | 3 +- .../src/traversal/path/lib/virtual-types.js | 33 +++++++++++++++++++ packages/babel/src/traversal/visitors.js | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/packages/babel/src/traversal/path/index.js b/packages/babel/src/traversal/path/index.js index 9b3f2fb541..7d3b0ee90a 100644 --- a/packages/babel/src/traversal/path/index.js +++ b/packages/babel/src/traversal/path/index.js @@ -145,8 +145,7 @@ for (let type of (t.TYPES: Array)) { for (let type in virtualTypes) { if (type[0] === "_") continue; - - t.TYPES.push(type); + if (t.TYPES.indexOf(type) < 0) t.TYPES.push(type); NodePath.prototype[`is${type}`] = function (opts) { return virtualTypes[type].checkPath(this, opts); diff --git a/packages/babel/src/traversal/path/lib/virtual-types.js b/packages/babel/src/traversal/path/lib/virtual-types.js index 98e45b4091..b8e83103f8 100644 --- a/packages/babel/src/traversal/path/lib/virtual-types.js +++ b/packages/babel/src/traversal/path/lib/virtual-types.js @@ -110,6 +110,10 @@ export var Var = { } }; +/** + * [Please add a description.] + */ + export var DirectiveLiteral = { types: ["Literal"], checkPath(path) { @@ -117,6 +121,10 @@ export var DirectiveLiteral = { } }; +/** + * [Please add a description.] + */ + export var Directive = { types: ["ExpressionStatement"], checkPath(path) { @@ -124,14 +132,39 @@ export var Directive = { } }; +/** + * [Please add a description.] + */ + export var User = { checkPath(path) { return path.node && !!path.node.loc; } }; +/** + * [Please add a description.] + */ + export var Generated = { checkPath(path) { return !path.isUser(); } }; + +/** + * [Please add a description.] + */ + +export var Flow = { + types: ["Flow", "ImportDeclaration"], + checkPath({ node }) { + if (t.isFlow(node)) { + return true; + } else if (t.isImportDeclaration(node)) { + return node.importKind === "type" || node.importKind === "typeof"; + } else { + return false; + } + } +}; diff --git a/packages/babel/src/traversal/visitors.js b/packages/babel/src/traversal/visitors.js index 0f5ef2b2ab..5e60c32976 100644 --- a/packages/babel/src/traversal/visitors.js +++ b/packages/babel/src/traversal/visitors.js @@ -40,7 +40,7 @@ export function explode(visitor) { ensureCallbackArrays(visitor); // add type wrappers - for (let nodeType in visitor) { + for (let nodeType of (Object.keys(visitor): Array)) { if (shouldIgnoreKey(nodeType)) continue; var wrapper = virtualTypes[nodeType];