From 21b7f4120eabf1ae4887141bda35531b5889c26a Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Wed, 19 Nov 2014 12:09:54 +1100 Subject: [PATCH] add assertion checks into types --- lib/6to5/types/index.js | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index fac701132a..3e13d640e0 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -2,14 +2,25 @@ var _ = require("lodash"); var t = exports; +var addAssert = function (type, is) { + t["assert" + type] = function (node, opts) { + opts = opts || {}; + if (!is(node, opts)) { + throw new Error("Expected type " + JSON.stringify(type) + " with option " + JSON.stringify(opts)); + } + }; +}; + // t.VISITOR_KEYS = require("./visitor-keys"); _.each(t.VISITOR_KEYS, function (keys, type) { - t["is" + type] = function (node, opts) { + var is = t["is" + type] = function (node, opts) { return node && node.type === type && t.shallowEqual(node, opts); }; + + addAssert(type, is); }); // @@ -43,17 +54,21 @@ _.each(t.ALIAS_KEYS, function (aliases, type) { _.each(_aliases, function (types, type) { t[type.toUpperCase() + "_TYPES"] = types; - t["is" + type] = function (node, opts) { + var is = t["is" + type] = function (node, opts) { return node && _.contains(types, node.type) && t.shallowEqual(node, opts); }; + + addAssert(type, is); }); // t.isExpression = function (node) { - return !t.isDeclaration(node); + return !t.isStatement(node); }; +addAssert("Expression", t.isExpression); + // t.shallowEqual = function (actual, expected) {