From 6a6ec8785b6a377c45bb534a98854866a77b8bd9 Mon Sep 17 00:00:00 2001 From: Henry Zhu Date: Tue, 15 Nov 2016 14:24:20 -0500 Subject: [PATCH] Start babel-types tests, add isNodesEquivalent (#3553) --- packages/babel-types/package.json | 3 ++ packages/babel-types/src/index.js | 3 +- packages/babel-types/src/validators.js | 44 +++++++++++++++++++++++++ packages/babel-types/test/validators.js | 30 +++++++++++++++++ 4 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 packages/babel-types/test/validators.js diff --git a/packages/babel-types/package.json b/packages/babel-types/package.json index d95ef62273..21946370ff 100644 --- a/packages/babel-types/package.json +++ b/packages/babel-types/package.json @@ -12,5 +12,8 @@ "esutils": "^2.0.2", "lodash": "^4.2.0", "to-fast-properties": "^1.0.1" + }, + "devDependencies": { + "babylon": "^6.8.2" } } diff --git a/packages/babel-types/src/index.js b/packages/babel-types/src/index.js index e87118b29b..1cc4b02bec 100644 --- a/packages/babel-types/src/index.js +++ b/packages/babel-types/src/index.js @@ -529,7 +529,8 @@ export { isVar, isSpecifierDefault, isScope, - isImmutable + isImmutable, + isNodesEquivalent } from "./validators"; export { diff --git a/packages/babel-types/src/validators.js b/packages/babel-types/src/validators.js index 46b2c81fba..78ea84cb40 100644 --- a/packages/babel-types/src/validators.js +++ b/packages/babel-types/src/validators.js @@ -238,3 +238,47 @@ export function isImmutable(node: Object): boolean { return false; } + +/** + * Check if two nodes are equivalent + */ + +export function isNodesEquivalent(a, b) { + if (typeof a !== "object" || typeof a !== "object" || a == null || b == null) { + return a === b; + } + + if (a.type !== b.type) { + return false; + } + + const fields = Object.keys(t.NODE_FIELDS[a.type] || a.type); + + for (let field of fields) { + if (typeof a[field] !== typeof b[field]) { + return false; + } + + if (Array.isArray(a[field])) { + if (!Array.isArray(b[field])) { + return false; + } + if (a[field].length !== b[field].length) { + return false; + } + + for (let i = 0; i < a[field].length; i++) { + if (!isNodesEquivalent(a[field][i], b[field][i])) { + return false; + } + } + continue; + } + + if (!isNodesEquivalent(a[field], b[field])) { + return false; + } + } + + return true; +} diff --git a/packages/babel-types/test/validators.js b/packages/babel-types/test/validators.js new file mode 100644 index 0000000000..5aa73dda30 --- /dev/null +++ b/packages/babel-types/test/validators.js @@ -0,0 +1,30 @@ +var t = require("../lib"); +var assert = require("assert"); +var parse = require("babylon").parse; + +suite("validators", function () { + suite("isNodesEquivalent", function () { + it("should handle simple cases", function () { + var mem = t.memberExpression(t.identifier("a"), t.identifier("b")); + assert(t.isNodesEquivalent(mem, mem) === true); + + var mem2 = t.memberExpression(t.identifier("a"), t.identifier("c")); + assert(t.isNodesEquivalent(mem, mem2) === false); + }); + + it("should handle full programs", function () { + assert(t.isNodesEquivalent(parse("1 + 1"), parse("1+1")) === true); + assert(t.isNodesEquivalent(parse("1 + 1"), parse("1+2")) === false); + }); + + it("should handle complex programs", function () { + var program = "'use strict'; function lol() { wow();return 1; }"; + + assert(t.isNodesEquivalent(parse(program), parse(program)) === true); + + var program2 = "'use strict'; function lol() { wow();return -1; }"; + + assert(t.isNodesEquivalent(parse(program), parse(program2)) === false); + }); + }); +});