add alternate shallow equals option to types.is*

This commit is contained in:
Sebastian McKenzie
2014-11-08 11:59:54 +11:00
parent 08580edda8
commit 9ea4431ba3
9 changed files with 39 additions and 43 deletions

View File

@@ -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;

View File

@@ -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]);

View File

@@ -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");
}

View File

@@ -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) {

View File

@@ -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;
}
});

View File

@@ -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

View File

@@ -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());

View File

@@ -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")));

View File

@@ -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;
});
});