Replaced var with let and const in PrettyPrinter, DiffBuilder, and friends

This commit is contained in:
Steve Gravrock
2022-05-12 17:15:50 -07:00
parent bb4d18f959
commit 2fd76c954c
5 changed files with 146 additions and 198 deletions
+21 -26
View File
@@ -1,10 +1,11 @@
getJasmineRequireObj().DiffBuilder = function(j$) {
return function DiffBuilder(config) {
var prettyPrinter = (config || {}).prettyPrinter || j$.makePrettyPrinter(),
mismatches = new j$.MismatchTree(),
path = new j$.ObjectPath(),
actualRoot = undefined,
expectedRoot = undefined;
const prettyPrinter =
(config || {}).prettyPrinter || j$.makePrettyPrinter();
const mismatches = new j$.MismatchTree();
let path = new j$.ObjectPath();
let actualRoot = undefined;
let expectedRoot = undefined;
return {
setRoots: function(actual, expected) {
@@ -17,29 +18,24 @@ getJasmineRequireObj().DiffBuilder = function(j$) {
},
getMessage: function() {
var messages = [];
const messages = [];
mismatches.traverse(function(path, isLeaf, formatter) {
var actualCustom,
expectedCustom,
useCustom,
derefResult = dereferencePath(
path,
actualRoot,
expectedRoot,
prettyPrinter
),
actual = derefResult.actual,
expected = derefResult.expected;
const { actual, expected } = dereferencePath(
path,
actualRoot,
expectedRoot,
prettyPrinter
);
if (formatter) {
messages.push(formatter(actual, expected, path, prettyPrinter));
return true;
}
actualCustom = prettyPrinter.customFormat_(actual);
expectedCustom = prettyPrinter.customFormat_(expected);
useCustom = !(
const actualCustom = prettyPrinter.customFormat_(actual);
const expectedCustom = prettyPrinter.customFormat_(expected);
const useCustom = !(
j$.util.isUndefined(actualCustom) &&
j$.util.isUndefined(expectedCustom)
);
@@ -64,7 +60,7 @@ getJasmineRequireObj().DiffBuilder = function(j$) {
},
withPath: function(pathComponent, block) {
var oldPath = path;
const oldPath = path;
path = path.add(pathComponent);
block();
path = oldPath;
@@ -98,18 +94,17 @@ getJasmineRequireObj().DiffBuilder = function(j$) {
j$.isAsymmetricEqualityTester_(expected) &&
j$.isFunction_(expected.valuesForDiff_)
) {
var asymmetricResult = expected.valuesForDiff_(actual, pp);
const asymmetricResult = expected.valuesForDiff_(actual, pp);
expected = asymmetricResult.self;
actual = asymmetricResult.other;
}
}
var i;
handleAsymmetricExpected();
for (i = 0; i < objectPath.components.length; i++) {
actual = actual[objectPath.components[i]];
expected = expected[objectPath.components[i]];
for (const pc of objectPath.components) {
actual = actual[pc];
expected = expected[pc];
handleAsymmetricExpected();
}
+9 -16
View File
@@ -14,15 +14,13 @@ getJasmineRequireObj().MismatchTree = function(j$) {
}
MismatchTree.prototype.add = function(path, formatter) {
var key, child;
if (path.depth() === 0) {
this.formatter = formatter;
this.isMismatch = true;
} else {
key = path.components[0];
const key = path.components[0];
path = path.shift();
child = this.child(key);
let child = this.child(key);
if (!child) {
child = new MismatchTree(this.path.add(key));
@@ -34,27 +32,22 @@ getJasmineRequireObj().MismatchTree = function(j$) {
};
MismatchTree.prototype.traverse = function(visit) {
var i,
hasChildren = this.children.length > 0;
const hasChildren = this.children.length > 0;
if (this.isMismatch || hasChildren) {
if (visit(this.path, !hasChildren, this.formatter)) {
for (i = 0; i < this.children.length; i++) {
this.children[i].traverse(visit);
for (const child of this.children) {
child.traverse(visit);
}
}
}
};
MismatchTree.prototype.child = function(key) {
var i, pathEls;
for (i = 0; i < this.children.length; i++) {
pathEls = this.children[i].path.components;
if (pathEls[pathEls.length - 1] === key) {
return this.children[i];
}
}
return this.children.find(child => {
const pathEls = child.path.components;
return pathEls[pathEls.length - 1] === key;
});
};
return MismatchTree;
+1 -9
View File
@@ -5,7 +5,7 @@ getJasmineRequireObj().ObjectPath = function(j$) {
ObjectPath.prototype.toString = function() {
if (this.components.length) {
return '$' + map(this.components, formatPropertyAccess).join('');
return '$' + this.components.map(formatPropertyAccess).join('');
} else {
return '';
}
@@ -35,14 +35,6 @@ getJasmineRequireObj().ObjectPath = function(j$) {
return "['" + prop + "']";
}
function map(array, fn) {
var results = [];
for (var i = 0; i < array.length; i++) {
results.push(fn(array[i]));
}
return results;
}
function isValidIdentifier(string) {
return /^[A-Za-z\$_][A-Za-z0-9\$_]*$/.test(string);
}