diff --git a/lib/6to5/transformation/transformers/classes.js b/lib/6to5/transformation/transformers/classes.js index 6b7f51a58f..a563572621 100644 --- a/lib/6to5/transformation/transformers/classes.js +++ b/lib/6to5/transformation/transformers/classes.js @@ -153,7 +153,7 @@ var superIdentifier = function (superName, methodNode, node, parent) { if (parent.property === node) { return; - } else if (t.isCallExpression(parent) && parent.callee === node) { + } else if (t.isCallExpression(parent, { callee: node })) { // super(); -> ClassName.prototype.MethodName.call(this); parent.arguments.unshift(t.thisExpression()); @@ -186,7 +186,7 @@ var replaceInstanceSuperReferences = function (superName, methodNode) { superName = superName || t.identifier("Function"); traverse(method, function (node, parent) { - if (t.isIdentifier(node) && node.name === "super") { + if (t.isIdentifier(node, { name: "super" })) { return superIdentifier(superName, methodNode, node, parent); } else if (t.isCallExpression(node)) { var callee = node.callee; diff --git a/lib/6to5/transformation/transformers/constants.js b/lib/6to5/transformation/transformers/constants.js index ec7ad31f42..b173f06b0b 100644 --- a/lib/6to5/transformation/transformers/constants.js +++ b/lib/6to5/transformation/transformers/constants.js @@ -18,7 +18,7 @@ exports.ForStatement = function (node, parent, file) { }; _.each(node.body, function (child) { - if (child && t.isVariableDeclaration(child) && child.kind === "const") { + if (child && t.isVariableDeclaration(child, { kind: "const" })) { _.each(child.declarations, function (declar) { _.each(t.getIds(declar), function (name) { check(declar, [name]); diff --git a/lib/6to5/transformation/transformers/destructuring.js b/lib/6to5/transformation/transformers/destructuring.js index 86825ade8d..67690661e5 100644 --- a/lib/6to5/transformation/transformers/destructuring.js +++ b/lib/6to5/transformation/transformers/destructuring.js @@ -61,7 +61,7 @@ var pushPattern = function (opts) { var file = opts.file; var scope = opts.scope; - if (parentId.type !== "MemberExpression" && parentId.type !== "Identifier") { + if (!t.isMemberExpression(parentId) && !t.isIdentifier(parentId)) { var key = t.identifier(file.generateUid("ref", scope)); nodes.push(t.variableDeclaration("var", [ @@ -77,7 +77,7 @@ var pushPattern = function (opts) { exports.ForInStatement = exports.ForOfStatement = function (node, parent, file, scope) { var declar = node.left; - if (declar.type !== "VariableDeclaration") return; + if (!t.isVariableDeclaration(declar)) return; var pattern = declar.declarations[0].id; if (!t.isPattern(pattern)) return; @@ -147,7 +147,7 @@ exports.ExpressionStatement = function (node, parent, file, scope) { }; exports.VariableDeclaration = function (node, parent, file, scope) { - if (t.isForInStatement(parent)) return; + if (t.isForInStatement(parent) || t.isForOfStatement(parent)) return; var nodes = []; @@ -177,13 +177,13 @@ exports.VariableDeclaration = function (node, parent, file, scope) { } }); - if (parent.type !== "Program" && parent.type !== "BlockStatement") { + if (!t.isProgram(parent) && !t.isBlockStatement(parent)) { var declar; _.each(nodes, function (node) { declar = declar || t.variableDeclaration(node.kind, []); - if (node.type !== "VariableDeclaration" && declar.kind !== node.kind) { + if (!t.isVariableDeclaration(node) && declar.kind !== node.kind) { throw file.errorWithNode(node, "Cannot use this node within the current parent"); } diff --git a/lib/6to5/transformation/transformers/let-scoping.js b/lib/6to5/transformation/transformers/let-scoping.js index 413125d41f..ffe1d7dc17 100644 --- a/lib/6to5/transformation/transformers/let-scoping.js +++ b/lib/6to5/transformation/transformers/let-scoping.js @@ -13,7 +13,7 @@ var isLet = function (node) { }; var isVar = function (node) { - return t.isVariableDeclaration(node) && node.kind === "var" && !isLet(node); + return t.isVariableDeclaration(node, { kind: "var" }) && !isLet(node); }; exports.VariableDeclaration = function (node) { diff --git a/lib/6to5/transformation/transformers/react.js b/lib/6to5/transformation/transformers/react.js index b7df808d16..8b9af5894a 100644 --- a/lib/6to5/transformation/transformers/react.js +++ b/lib/6to5/transformation/transformers/react.js @@ -2,18 +2,18 @@ var t = require("../../types"); var _ = require("lodash"); var addDisplayName = function (id, call) { - if (!call || call.type !== "CallExpression") return; + if (!call || !t.isCallExpression(call)) return; var callee = call.callee; - if (callee.type !== "MemberExpression") return; + if (!t.isMemberExpression(callee)) return; // not React var obj = callee.object; - if (obj.type !== "Identifier" || obj.name !== "React") return; + if (!t.isIdentifier(obj, { name: "React" })) return; // not createClass var prop = callee.property; - if (prop.type !== "Identifier" || prop.name !== "createClass") return; + if (!t.isIdentifier(prop, { name: "createClass" })) return; // no arguments var args = call.arguments; @@ -21,13 +21,14 @@ var addDisplayName = function (id, call) { // not an object var first = args[0]; - if (first.type !== "ObjectExpression") return; + if (!t.isObjectExpression(first)) return; var props = first.properties; var safe = true; _.each(props, function (prop) { - if (prop.key.name === "displayName") { + var key = prop.key; + if (t.isIdentifier(prop.key, { name: "displayName" })) { return safe = false; } }); diff --git a/lib/6to5/transformation/transformers/spread.js b/lib/6to5/transformation/transformers/spread.js index 23094d296e..a3f11826b5 100644 --- a/lib/6to5/transformation/transformers/spread.js +++ b/lib/6to5/transformation/transformers/spread.js @@ -4,7 +4,7 @@ var _ = require("lodash"); var getSpreadLiteral = function (spread, file) { var literal = spread.argument; - if (literal.type !== "ArrayExpression") { + if (!t.isArrayExpression(literal)) { literal = util.template("call", { OBJECT: file.addDeclaration("slice"), CONTEXT: literal diff --git a/lib/6to5/transformation/transformers/template-literals.js b/lib/6to5/transformation/transformers/template-literals.js index e89add41d6..c9b923f277 100644 --- a/lib/6to5/transformation/transformers/template-literals.js +++ b/lib/6to5/transformation/transformers/template-literals.js @@ -34,7 +34,7 @@ exports.TemplateLiteral = function (node) { if (nodes.length > 1) { // remove redundant '' at the end of the expression var last = _.last(nodes); - if (t.isLiteral(last) && last.value === "") nodes.pop(); + if (t.isLiteral(last, { value: "" })) nodes.pop(); var root = buildBinaryExpression(nodes.shift(), nodes.shift()); diff --git a/lib/6to5/transformation/transformers/use-strict.js b/lib/6to5/transformation/transformers/use-strict.js index 729421fbaa..c4b352cc20 100644 --- a/lib/6to5/transformation/transformers/use-strict.js +++ b/lib/6to5/transformation/transformers/use-strict.js @@ -4,7 +4,7 @@ module.exports = function (ast) { var body = ast.program.body; var first = body[0]; - var noStrict = !first || first.type !== "ExpressionStatement" || first.expression.type !== "Literal" || first.expression.value !== "use strict"; + var noStrict = !first || !t.isExpressionStatement(first) || !t.isLiteral(first.expression) || first.expression.value !== "use strict"; if (noStrict) { body.unshift(t.expressionStatement(t.literal("use strict"))); diff --git a/lib/6to5/types/index.js b/lib/6to5/types/index.js index 571021739f..af6f2fe0d2 100644 --- a/lib/6to5/types/index.js +++ b/lib/6to5/types/index.js @@ -9,8 +9,8 @@ var t = exports; t.VISITOR_KEYS = require("./visitor-keys"); _.each(t.VISITOR_KEYS, function (keys, type) { - t["is" + type] = function (node) { - return node && node.type === type; + t["is" + type] = function (node, opts) { + return node && node.type === type && t.shallowEqual(node, opts); }; }); @@ -45,13 +45,29 @@ _.each(t.ALIAS_KEYS, function (aliases, type) { _.each(_aliases, function (types, type) { t[type.toUpperCase() + "_TYPES"] = types; - t["is" + type] = function (node) { - return node && _.contains(types, node.type); + t["is" + type] = function (node, opts) { + return node && _.contains(types, node.type) && t.shallowEqual(node, opts); }; }); // +t.shallowEqual = function (actual, expected) { + var same = true; + + if (expected) { + _.each(expected, function (val, key) { + if (actual[key] !== val) { + return same = false; + } + }); + } + + return same; +}; + +// + t.isReferenced = function (node, parent) { // we're a property key so we aren't referenced if (t.isProperty(parent) && parent.key === node) return false; @@ -172,24 +188,3 @@ t.inherits = function (child, parent) { t.getSpecifierName = function (specifier) { return specifier.name || specifier.id; }; - -// - -t.PRECEDENCE = {}; - -_.each([ - ["||"], - ["&&"], - ["|"], - ["^"], - ["&"], - ["==", "===", "!=", "!=="], - ["<", ">", "<=", ">=", "in", "instanceof"], - [">>", "<<", ">>>"], - ["+", "-"], - ["*", "/", "%"] -], function (tier, i) { - _.each(tier, function (op) { - t.PRECEDENCE[op] = i; - }); -});