diff --git a/lib/6to5/generators/expressions.js b/lib/6to5/generators/expressions.js index e8909f6643..6e17fa83fa 100644 --- a/lib/6to5/generators/expressions.js +++ b/lib/6to5/generators/expressions.js @@ -1,10 +1,11 @@ +var t = require("../types"); var _ = require("lodash"); exports.UnaryExpression = function (node, print) { this.push(node.operator); var arg = node.argument; - if (/[a-z]$/.test(node.operator) || arg.type === "UpdateExpression" || arg.type === "UnaryExpression") { + if (/[a-z]$/.test(node.operator) || t.isUpdateExpression(arg) || t.isUnaryExpression(arg)) { this.push(" "); } diff --git a/lib/6to5/generators/jsx.js b/lib/6to5/generators/jsx.js index fb0aadf93f..3544da04ac 100644 --- a/lib/6to5/generators/jsx.js +++ b/lib/6to5/generators/jsx.js @@ -1,3 +1,4 @@ +var t = require("../types"); var _ = require("lodash"); exports.XJSAttribute = function (node, print) { @@ -45,7 +46,7 @@ exports.XJSElement = function (node, print) { this.indent(); _.each(node.children, function (child) { - if (child.type === "Literal" && typeof child.value === "string") { + if (t.isLiteral(child) && typeof child.value === "string") { if (/\S/.test(child.value)) { return self.push(child.value.replace(/^\s+|\s+$/g, "")); } else if (/\n/.test(child.value)) { diff --git a/lib/6to5/generators/methods.js b/lib/6to5/generators/methods.js index b387ded065..e35d329622 100644 --- a/lib/6to5/generators/methods.js +++ b/lib/6to5/generators/methods.js @@ -1,3 +1,5 @@ +var t = require("../types"); + exports._params = function (node, print) { var self = this; @@ -79,7 +81,7 @@ exports.FunctionExpression = function (node, print) { }; exports.ArrowFunctionExpression = function (node, print) { - if (node.params.length === 1 && !node.defaults.length && !node.rest && node.params[0].type === "Identifier") { + if (node.params.length === 1 && !node.defaults.length && !node.rest && t.isIdentifier(node.params[0])) { print(node.params[0]); } else { this._params(node, print); diff --git a/lib/6to5/generators/modules.js b/lib/6to5/generators/modules.js index 1f6cecce9d..ed6c068b70 100644 --- a/lib/6to5/generators/modules.js +++ b/lib/6to5/generators/modules.js @@ -1,3 +1,4 @@ +var t = require("../types"); var _ = require("lodash"); exports.ImportSpecifier = @@ -21,7 +22,7 @@ exports.ExportDeclaration = function (node, print) { if (node.default) { this.push(" default"); } else if (specifiers && specifiers.length > 0) { - if (specifiers.length === 1 && specifiers[0].type === "ExportBatchSpecifier") { + if (specifiers.length === 1 && t.isExportBatchSpecifier(specifiers[0])) { this.push(" *"); } else { this.push(" { "); diff --git a/lib/6to5/modules/amd.js b/lib/6to5/modules/amd.js index 5cc573f7ca..65e23f157c 100644 --- a/lib/6to5/modules/amd.js +++ b/lib/6to5/modules/amd.js @@ -62,7 +62,7 @@ AMDFormatter.prototype.importSpecifier = function (specifier, node, nodes) { var ref; - if (specifier.type === "ImportBatchSpecifier") { + if (t.isImportBatchSpecifier(specifier)) { // import * as bar from "foo"; ref = this._push(node); } else { diff --git a/lib/6to5/modules/common.js b/lib/6to5/modules/common.js index 551968768c..6fe4131d4e 100644 --- a/lib/6to5/modules/common.js +++ b/lib/6to5/modules/common.js @@ -53,7 +53,7 @@ CommonJSFormatter.prototype.export = function (node, nodes) { }, true)); } else { var id = declar.id; - if (declar.type === "VariableDeclaration") { + if (t.isVariableDeclaration(declar)) { id = declar.declarations[0].id; } @@ -64,7 +64,7 @@ CommonJSFormatter.prototype.export = function (node, nodes) { nodes.push(declar); - if (declar.type === "FunctionDeclaration") { + if (t.isFunctionDeclaration(declar)) { assign._blockHoist = true; } @@ -76,7 +76,7 @@ CommonJSFormatter.prototype._exportSpecifier = function (getRef, specifier, node var variableName = t.getSpecifierName(specifier); if (node.source) { - if (specifier.type === "ExportBatchSpecifier") { + if (t.isExportBatchSpecifier(specifier)) { // export * from "foo"; nodes.push(util.template("exports-wildcard", { OBJECT: getRef() diff --git a/lib/6to5/transformers/_alias-functions.js b/lib/6to5/transformers/_alias-functions.js index d1435f47c2..174e9355c0 100644 --- a/lib/6to5/transformers/_alias-functions.js +++ b/lib/6to5/transformers/_alias-functions.js @@ -39,9 +39,9 @@ var go = function (getBody, node, file) { var getId; - if (node.type === "Identifier" && node.name === "arguments") { + if (t.isIdentifier(node) && node.name === "arguments") { getId = getArgumentsId; - } else if (node.type === "ThisExpression") { + } else if (t.isThisExpression(node)) { getId = getThisId; } else { return; diff --git a/lib/6to5/transformers/classes.js b/lib/6to5/transformers/classes.js index 64e1298ebe..1ab7f74326 100644 --- a/lib/6to5/transformers/classes.js +++ b/lib/6to5/transformers/classes.js @@ -15,7 +15,7 @@ exports.ClassExpression = function (node, parent, file) { }; var getMemberExpressionObject = function (node) { - while (node.type === "MemberExpression") { + while (t.isMemberExpression(node)) { node = node.object; } return node; @@ -29,9 +29,9 @@ var buildClass = function (node, file) { var superClassCallee = node.superClass; if (superName) { - if (superName.type === "MemberExpression") { + if (t.isMemberExpression(superName)) { superClassArgument = superClassCallee = getMemberExpressionObject(superName); - } else if (superName.type !== "Identifier") { + } else if (t.isIdentifier(superName)) { superClassArgument = superName; superClassCallee = superName = b.identifier(file.generateUid("ref")); } @@ -138,7 +138,7 @@ var buildClassBody = function (file, construct, body, className, superName, node var superIdentifier = function (superName, methodNode, methodName, node, parent) { if (parent.property === node) { return; - } else if (parent.type === "CallExpression" && parent.callee === node) { + } else if (t.isCallExpression(parent) && parent.callee === node) { // super(); -> ClassName.prototype.MethodName.call(this); parent.arguments.unshift(b.thisExpression()); @@ -156,7 +156,7 @@ var superIdentifier = function (superName, methodNode, methodName, node, parent) node = b.memberExpression(node, b.identifier(methodName)); return b.memberExpression(node, b.identifier("call")); } - } else if (parent.type === "MemberExpression" && !methodNode.static) { + } else if (t.isMemberExpression(parent) && !methodNode.static) { // super.test -> ClassName.prototype.test return b.memberExpression(superName, b.identifier("prototype")); } else { @@ -168,11 +168,11 @@ var replaceInstanceSuperReferences = function (superName, method, methodNode, me superName = superName || b.identifier("Function"); traverse(method, function (node, parent) { - if (node.type === "Identifier" && node.name === "super") { + if (t.isIdentifier(node) && node.name === "super") { return superIdentifier(superName, methodNode, methodName, node, parent); - } else if (node.type === "CallExpression") { + } else if (t.isCallExpression(node)) { var callee = node.callee; - if (callee.type !== "MemberExpression") return; + if (!t.isMemberExpression(callee)) return; if (callee.object.name !== "super") return; // super.test(); -> ClassName.prototype.MethodName.call(this); diff --git a/lib/6to5/transformers/constants.js b/lib/6to5/transformers/constants.js index 12af051e38..529d6f69f9 100644 --- a/lib/6to5/transformers/constants.js +++ b/lib/6to5/transformers/constants.js @@ -1,5 +1,6 @@ var traverse = require("../traverse"); var util = require("../util"); +var t = require("../types"); var _ = require("lodash"); exports.Program = @@ -16,7 +17,7 @@ exports.ForStatement = function (node, parent, file) { }; _.each(node.body, function (child) { - if (child && child.type === "VariableDeclaration" && child.kind === "const") { + if (child && t.isVariableDeclaration(child) && child.kind === "const") { _.each(child.declarations, function (declar) { _.each(util.getIds(declar.id), function (name) { check(declar, name); @@ -34,11 +35,9 @@ exports.ForStatement = function (node, parent, file) { traverse(node, function (child) { if (child._ignoreConstant) return; - if (child.type === "VariableDeclarator" || - child.type === "FunctionDeclaration" || - child.type === "ClassDeclaration") { + if (t.isVariableDeclarator(child) || t.isFunctionDeclaration(child) || t.isClassDeclaration(child)) { check(child, child.id.name); - } else if (child.type === "AssignmentExpression") { + } else if (t.isAssignmentExpression(child)) { check(child, child.left.name); } }); diff --git a/lib/6to5/transformers/destructuring.js b/lib/6to5/transformers/destructuring.js index b1513dea50..3e987d602f 100644 --- a/lib/6to5/transformers/destructuring.js +++ b/lib/6to5/transformers/destructuring.js @@ -13,11 +13,11 @@ var buildVariableAssign = function (kind, id, init) { }; var push = function (kind, nodes, elem, parentId) { - if (elem.type === "ObjectPattern") { + if (t.isObjectPattern(elem)) { pushObjectPattern(kind, nodes, elem, parentId); - } else if (elem.type === "ArrayPattern") { + } else if (t.isArrayPattern(elem)) { pushArrayPattern(kind, nodes, elem, parentId); - } else if (elem.type === "MemberExpression") { + } else if (t.isMemberExpression(elem)) { nodes.push(buildVariableAssign(false, elem, parentId)); } else { nodes.push(buildVariableAssign(kind, elem, parentId)); @@ -124,7 +124,7 @@ exports.ExpressionStatement = function (node, parent, file) { }; exports.VariableDeclaration = function (node, parent, file) { - if (parent.type === "ForInStatement") return; + if (t.isForInStatement(parent)) return; var nodes = []; diff --git a/lib/6to5/transformers/for-of.js b/lib/6to5/transformers/for-of.js index d051d07dd5..587a06716b 100644 --- a/lib/6to5/transformers/for-of.js +++ b/lib/6to5/transformers/for-of.js @@ -9,9 +9,9 @@ exports.ForOfStatement = function (node, parent, file) { var stepKey = b.identifier(file.generateUid("step")); var stepValueId = b.memberExpression(stepKey, b.identifier("value")); - if (left.type === "Identifier") { + if (t.isIdentifier(left)) { declar = b.expressionStatement(b.assignmentExpression("=", left, stepValueId)); - } else if (left.type === "VariableDeclaration") { + } else if (t.isVariableDeclaration(left)) { declar = b.variableDeclaration(left.kind, [ b.variableDeclarator(left.declarations[0].id, stepValueId) ]); diff --git a/lib/6to5/transformers/jsx.js b/lib/6to5/transformers/jsx.js index b4e881b34e..d4510b03be 100644 --- a/lib/6to5/transformers/jsx.js +++ b/lib/6to5/transformers/jsx.js @@ -37,7 +37,7 @@ exports.XJSNamespacedName = function () { exports.XJSMemberExpression = { exit: function (node) { - node.computed = node.property.type === "Literal"; + node.computed = t.isLiteral(node.property); node.type = "MemberExpression"; } }; @@ -64,7 +64,7 @@ exports.XJSOpeningElement = { exit: function (node, parent, file) { var tagExpr = node.name; - if (tagExpr.type === "Identifier") { + if (t.isIdentifier(tagExpr)) { var tagName = tagExpr.name; if (/[a-z]/.exec(tagName[0]) || _.contains(tagName, "-")) { diff --git a/lib/6to5/transformers/let-scoping.js b/lib/6to5/transformers/let-scoping.js index 439bde7bcd..157e46abf7 100644 --- a/lib/6to5/transformers/let-scoping.js +++ b/lib/6to5/transformers/let-scoping.js @@ -27,7 +27,7 @@ exports.VariableDeclaration = function (node, parent, file) { if (t.isReferenced(node, parent)) return id; }; - var isProgram = parent.type === "Program"; + var isProgram = t.isProgram(parent); var replace = function (node, parent) { if (!isProgram && _.contains(t.FUNCTION_TYPES, node.type)) { @@ -40,7 +40,7 @@ exports.VariableDeclaration = function (node, parent, file) { }); if (letReferences.length) { - if (node.type === "FunctionDeclaration") { + if (t.isFunctionDeclaration(node)) { throw new Error("`FunctionDeclaration`s that use `let` and `constant` references aren't allowed outside of the root scope"); } else { var func = b.functionExpression(null, letReferences, b.blockStatement([ diff --git a/lib/6to5/transformers/react.js b/lib/6to5/transformers/react.js index e527088882..f98b0cc28d 100644 --- a/lib/6to5/transformers/react.js +++ b/lib/6to5/transformers/react.js @@ -1,4 +1,5 @@ var b = require("../builders"); +var t = require("../types"); var _ = require("lodash"); var addDisplayName = function (id, call) { @@ -42,22 +43,22 @@ exports.Property = exports.VariableDeclarator = function (node) { var left, right; - if (node.type === "AssignmentExpression") { + if (t.isAssignmentExpression(node)) { left = node.left; right = node.right; - } else if (node.type === "Property") { + } else if (t.isProperty(node)) { left = node.key; right = node.value; - } else if (node.type === "VariableDeclarator") { + } else if (t.isVariableDeclarator(node)) { left = node.id; right = node.init; } - if (left && left.type === "MemberExpression") { + if (t.isMemberExpression(left)) { left = left.property; } - if (left && left.type === "Identifier") { + if (t.isIdentifier(left)) { addDisplayName(left.name, right); } }; diff --git a/lib/6to5/transformers/spread.js b/lib/6to5/transformers/spread.js index dcb9cee196..01100cb48b 100644 --- a/lib/6to5/transformers/spread.js +++ b/lib/6to5/transformers/spread.js @@ -1,5 +1,6 @@ var util = require("../util"); var b = require("../builders"); +var t = require("../types"); var _ = require("lodash"); var getSpreadLiteral = function (spread, file) { @@ -16,7 +17,7 @@ var getSpreadLiteral = function (spread, file) { var hasSpread = function (nodes) { var has = false; _.each(nodes, function (node) { - if (node.type === "SpreadElement") { + if (t.isSpreadElement(node)) { has = true; return false; } @@ -36,7 +37,7 @@ var build = function (props, file) { }; _.each(props, function (prop) { - if (prop.type === "SpreadElement") { + if (t.isSpreadElement(prop)) { push(); nodes.push(getSpreadLiteral(prop, file)); } else { @@ -80,7 +81,7 @@ exports.CallExpression = function (node, parent, file) { var callee = node.callee; - if (callee.type === "MemberExpression") { + if (t.isMemberExpression(callee)) { contextLiteral = callee.object; if (callee.computed) { diff --git a/lib/6to5/transformers/template-literals.js b/lib/6to5/transformers/template-literals.js index ecfba6057e..869a42ed64 100644 --- a/lib/6to5/transformers/template-literals.js +++ b/lib/6to5/transformers/template-literals.js @@ -1,3 +1,4 @@ +var t = require("../types"); var b = require("../builders"); var _ = require("lodash"); @@ -34,7 +35,7 @@ exports.TemplateLiteral = function (node) { if (nodes.length > 1) { // remove redundant '' at the end of the expression var last = _.last(nodes); - if (last.type === "Literal" && last.value === "") nodes.pop(); + if (t.isLiteral(last) && last.value === "") nodes.pop(); var root = buildBinaryExpression(nodes.shift(), nodes.shift()); diff --git a/lib/6to5/types.js b/lib/6to5/types.js index d8f73a0b22..f42d6d9159 100644 --- a/lib/6to5/types.js +++ b/lib/6to5/types.js @@ -196,11 +196,11 @@ t.needsParans = function (node, parent) { if (t.isYieldExpression(node)) { return t.isBinary(parent) || t.isUnaryLike(parent) - || parent.type === "CallExpression" - || parent.type === "MemberExpression" - || parent.type === "NewExpression" - || parent.type === "ConditionalExpression" - || parent.type === "YieldExpression"; + || t.isCallExpression(parent) + || t.isMemberExpression(parent) + || t.isNewExpression(parent) + || t.isConditionalExpression(parent) + || t.isYieldExpression(parent); } if (t.isNewExpression(parent) && parent.callee === node) { diff --git a/lib/6to5/util.js b/lib/6to5/util.js index 6e93d50c41..5e1eb5ebac 100644 --- a/lib/6to5/util.js +++ b/lib/6to5/util.js @@ -4,6 +4,7 @@ var acorn = require("acorn-6to5"); var path = require("path"); var util = require("util"); var fs = require("fs"); +var t = require("./types"); var b = require("./builders"); var _ = require("lodash"); @@ -26,17 +27,15 @@ exports.list = function (val) { exports.getUid = function (parent, file) { var node; - if (parent.type === "AssignmentExpression") { + if (t.isAssignmentExpression(parent)) { node = parent.left; - } else if (parent.type === "VariableDeclarator") { + } else if (t.isVariableDeclarator(parent)) { node = parent.id; } var id = "ref"; - if (node && node.type === "Identifier") { - id = node.name; - } + if (t.isIdentifier(node)) id = node.name; return b.identifier(file.generateUid(id)); }; @@ -55,13 +54,13 @@ exports.getIds = function (node) { while (search.length) { var id = search.shift(); - if (id.type === "Identifier") { + if (t.isIdentifier(id)) { ids.push(id.name); - } else if (id.type === "ArrayPattern") { + } else if (t.isArrayPattern(id)) { _.each(id.elements, function (elem) { search.push(elem); }); - } else if (id.type === "ObjectPattern") { + } else if (t.isObjectPattern(id)) { _.each(id.properties, function (prop) { search.push(prop.value); }); @@ -110,7 +109,7 @@ exports.buildDefineProperties = function (mutatorMap) { _.each(map, function (node, key) { node = _.clone(node); - if (node.type === "MethodDefinition") node = node.value; + if (t.isMethodDefinition(node)) node = node.value; mapNode.properties.push(b.property("init", b.identifier(key), node)); }); @@ -128,7 +127,7 @@ exports.template = function (name, nodes, keepExpression) { if (!_.isEmpty(nodes)) { traverse(template, function (node) { - if (node.type === "Identifier" && _.has(nodes, node.name)) { + if (t.isIdentifier(node) && _.has(nodes, node.name)) { var newNode = nodes[node.name]; if (_.isString(newNode)) { node.name = newNode; @@ -141,7 +140,7 @@ exports.template = function (name, nodes, keepExpression) { var node = template.body[0]; - if (!keepExpression && node.type === "ExpressionStatement") { + if (!keepExpression && t.isExpressionStatement(node)) { return node.expression; } else { return node;