diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 9956aff41b..b3e7434bb5 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -64,7 +64,14 @@ _.each(t.FLIPPED_ALIAS_KEYS, function (types, type) { addAssert(type, is); }); -// +/* + * Shallowly checks to see if the passed `node` will evaluate to a + * falsy. This is if `node` is a `Literal` and `value` is falsy or + * `node` is an `Identifier` with a name of `undefiend`. + * + * @param {Object} node + * @returns {Boolean} + */ t.isFalsyExpression = function (node) { if (t.isLiteral(node)) { @@ -75,7 +82,14 @@ t.isFalsyExpression = function (node) { return false; }; -// +/** + * Turn an array of statement `nodes` into a `SequenceExpression`. + * + * Variable declarations are turned into simple assignments and their + * declarations hoisted to the top of the current scope. + * + * Expression statements are just resolved to their standard expression. + */ t.toSequenceExpression = function (nodes, scope) { var exprs = []; @@ -116,7 +130,14 @@ t.shallowEqual = function (actual, expected) { return same; }; -// +/** + * Description + * + * @param {Object} member + * @param {Object} append + * @param {Boolean} [computed] + * @returns {Object} member + */ t.appendToMemberExpression = function (member, append, computed) { member.object = t.memberExpression(member.object, member.property, member.computed); @@ -125,12 +146,25 @@ t.appendToMemberExpression = function (member, append, computed) { return member; }; +/** + * Description + * + * @param {Object} member + * @param {Object} append + * @returns {Object} member + */ + t.prependToMemberExpression = function (member, append) { member.object = t.memberExpression(append, member.object); return member; }; -// +/* + * Description + * + * @param {Object} node + * @returns {Boolean} + */ t.isDynamic = function (node) { if (t.isExpressionStatement(node)) { @@ -144,6 +178,14 @@ t.isDynamic = function (node) { } }; +/** + * Description + * + * @param {Object} node + * @param {Object} parent + * @returns {Boolean} + */ + t.isReferenced = function (node, parent) { // we're a property key and we aren't computed so we aren't referenced if (t.isProperty(parent) && parent.key === node && !parent.computed) return false; @@ -165,11 +207,23 @@ t.isReferenced = function (node, parent) { return false; }; +/** + * Description + * + * @param {String} name + * @returns {Boolean} + */ + t.isValidIdentifier = function (name) { return _.isString(name) && esutils.keyword.isIdentifierName(name) && !esutils.keyword.isKeywordES6(name, true); }; -// +/* + * Description + * + * @param {String} name + * @returns {String} + */ t.toIdentifier = function (name) { if (t.isIdentifier(name)) return name.name; @@ -191,11 +245,26 @@ t.toIdentifier = function (name) { return name || '_'; }; +/** + * Description + * + * @param {Object} node + * @param {String} key + */ + t.ensureBlock = function (node, key) { key = key || "body"; node[key] = t.toBlock(node[key], node); }; +/** + * Description + * + * @param {Object} node + * @param {Boolean} [ignore] + * @returns {Object|Boolean} + */ + t.toStatement = function (node, ignore) { if (t.isStatement(node)) { return node; @@ -229,6 +298,14 @@ t.toStatement = function (node, ignore) { return node; }; +/** + * Description + * + * @param {Object} node + * @param {Object} parent + * @returns {Object} + */ + t.toBlock = function (node, parent) { if (t.isBlockStatement(node)) { return node; @@ -249,7 +326,14 @@ t.toBlock = function (node, parent) { return t.blockStatement(node); }; -// +/* + * Description + * + * @param {Object} parent + * @param {File} file + * @param {Scope} scope + * @returns {Object} + */ t.getUid = function (parent, file, scope) { var node = parent; @@ -294,6 +378,15 @@ t.getUid = function (parent, file, scope) { return file.generateUidIdentifier(id, scope); }; +/** + * Description + * + * @param {Object} node + * @param {Boolean} [map] + * @param {Array} [ignoreTypes] + * @returns {Array|Object} + */ + t.getIds = function (node, map, ignoreTypes) { ignoreTypes = ignoreTypes || []; @@ -344,12 +437,24 @@ t.getIds.arrays = { ObjectPattern: ["properties"] }; -// +/** + * Description + * + * @param {Object} node + * @returns {Boolean} + */ t.isLet = function (node) { return t.isVariableDeclaration(node) && (node.kind !== "var" || node._let); }; +/** + * Description + * + * @param {Object} node + * @returns {Boolean} + */ + t.isVar = function (node) { return t.isVariableDeclaration(node, { kind: "var" }) && !node._let; }; @@ -358,6 +463,13 @@ t.isVar = function (node) { t.COMMENT_KEYS = ["leadingComments", "trailingComments"]; +/** + * Description + * + * @param {Object} child + * @returns {Object} child + */ + t.removeComments = function (child) { _.each(t.COMMENT_KEYS, function (key) { delete child[key]; @@ -365,6 +477,14 @@ t.removeComments = function (child) { return child; }; +/** + * Description + * + * @param {Object} child + * @param {Object} parent + * @returns {Object} child + */ + t.inheritsComments = function (child, parent) { _.each(t.COMMENT_KEYS, function (key) { child[key] = _.uniq(_.compact([].concat(child[key], parent[key]))); @@ -372,7 +492,13 @@ t.inheritsComments = function (child, parent) { return child; }; -// +/** + * Description + * + * @param {Object} child + * @param {Object} parent + * @returns {Object} child + */ t.inherits = function (child, parent) { child.loc = parent.loc; @@ -383,10 +509,24 @@ t.inherits = function (child, parent) { return child; }; +/** + * Description + * + * @param {Object} specifier + * @returns {String} + */ + t.getSpecifierName = function (specifier) { return specifier.name || specifier.id; }; +/** + * Description + * + * @param {Object} specifier + * @returns {Boolean} + */ + t.isSpecifierDefault = function (specifier) { return t.isIdentifier(specifier.id) && specifier.id.name === "default"; }; diff --git a/test/types.js b/test/types.js new file mode 100644 index 0000000000..8a00081e68 --- /dev/null +++ b/test/types.js @@ -0,0 +1,55 @@ +var assert = require("assert"); +var t = require("../lib/6to5/types"); + +suite("types", function () { + test("isFalsyExpression", function () { + assert.ok(t.isFalsyExpression(t.literal(""))); + assert.ok(t.isFalsyExpression(t.literal(null))); + assert.ok(t.isFalsyExpression(t.literal(0))); + assert.ok(t.isFalsyExpression(t.identifier("undefined"))); + + assert.ok(!t.isFalsyExpression(t.literal("foobar"))); + assert.ok(!t.isFalsyExpression(t.literal(5))); + assert.ok(!t.isFalsyExpression(t.identifier("foobar"))); + }); + + test("toSequenceExpression"); + + test("shallowEqual"); + + test("appendToMemberExpression"); + + test("prependToMemberExpression"); + + test("isDynamic"); + + test("isReferenced"); + + test("isValidIdentifier"); + + test("toIdentifier"); + + test("ensureBlock"); + + test("toStatement"); + + test("toBlock"); + + test("getUid"); + + test("getIds"); + + test("isLet"); + + test("isVar"); + + test("removeComments"); + + test("inheritsComments"); + + test("inherits"); + + test("getSpecifierName"); + + test("isSpecifierDefault"); +});