Skip to content

Commit

Permalink
Remove uses of var (#2506)
Browse files Browse the repository at this point in the history
Replace var with const where possible in /lib and /test.

Modified the let codemod to be a  codemod.
Took about half an hour with --watch running
  • Loading branch information
fatso83 authored Mar 30, 2023
1 parent cd45bfa commit 430c9a6
Show file tree
Hide file tree
Showing 70 changed files with 2,671 additions and 2,645 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ docs/js/
docs/releases/
docs/_releases/
docs/assets/js/
docs/vendor

# has linting of its own
docs/release-source/release/examples
Expand Down
6 changes: 3 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This project uses Prettier defaults, the config file is present in
# order to trigger editor plugins to allow format on save behaviour
overrides:
- files: "*.html"
options:
tabWidth: 2
- files: "*.html"
options:
tabWidth: 2
24 changes: 12 additions & 12 deletions lib/sinon.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"use strict";

var behavior = require("./sinon/behavior");
var createSandbox = require("./sinon/create-sandbox");
var extend = require("./sinon/util/core/extend");
var fakeTimers = require("./sinon/util/fake-timers");
var nise = require("nise");
var Sandbox = require("./sinon/sandbox");
var stub = require("./sinon/stub");
var promise = require("./sinon/promise");

var apiMethods = {
const behavior = require("./sinon/behavior");
const createSandbox = require("./sinon/create-sandbox");
const extend = require("./sinon/util/core/extend");
const fakeTimers = require("./sinon/util/fake-timers");
const nise = require("nise");
const Sandbox = require("./sinon/sandbox");
const stub = require("./sinon/stub");
const promise = require("./sinon/promise");

const apiMethods = {
createSandbox: createSandbox,
assert: require("./sinon/assert"),
match: require("@sinonjs/samsam").createMatcher,
Expand Down Expand Up @@ -41,8 +41,8 @@ var apiMethods = {
promise: promise,
};

var sandbox = new Sandbox();
const sandbox = new Sandbox();

var api = extend(sandbox, apiMethods);
const api = extend(sandbox, apiMethods);

module.exports = api;
246 changes: 122 additions & 124 deletions lib/sinon/assert.js
Original file line number Diff line number Diff line change
@@ -1,123 +1,26 @@
"use strict";

var arrayProto = require("@sinonjs/commons").prototypes.array;
var calledInOrder = require("@sinonjs/commons").calledInOrder;
var createMatcher = require("@sinonjs/samsam").createMatcher;
var orderByFirstCall = require("@sinonjs/commons").orderByFirstCall;
var timesInWords = require("./util/core/times-in-words");
var inspect = require("util").inspect;
var stringSlice = require("@sinonjs/commons").prototypes.string.slice;
var globalObject = require("@sinonjs/commons").global;

var arraySlice = arrayProto.slice;
var concat = arrayProto.concat;
var forEach = arrayProto.forEach;
var join = arrayProto.join;
var splice = arrayProto.splice;
const arrayProto = require("@sinonjs/commons").prototypes.array;
const calledInOrder = require("@sinonjs/commons").calledInOrder;
const createMatcher = require("@sinonjs/samsam").createMatcher;
const orderByFirstCall = require("@sinonjs/commons").orderByFirstCall;
const timesInWords = require("./util/core/times-in-words");
const inspect = require("util").inspect;
const stringSlice = require("@sinonjs/commons").prototypes.string.slice;
const globalObject = require("@sinonjs/commons").global;

const arraySlice = arrayProto.slice;
const concat = arrayProto.concat;
const forEach = arrayProto.forEach;
const join = arrayProto.join;
const splice = arrayProto.splice;

function createAssertObject() {
var assert;

function verifyIsStub() {
var args = arraySlice(arguments);

forEach(args, function (method) {
if (!method) {
assert.fail("fake is not a spy");
}

if (method.proxy && method.proxy.isSinonProxy) {
verifyIsStub(method.proxy);
} else {
if (typeof method !== "function") {
assert.fail(`${method} is not a function`);
}

if (typeof method.getCall !== "function") {
assert.fail(`${method} is not stubbed`);
}
}
});
}

function verifyIsValidAssertion(assertionMethod, assertionArgs) {
switch (assertionMethod) {
case "notCalled":
case "called":
case "calledOnce":
case "calledTwice":
case "calledThrice":
if (assertionArgs.length !== 0) {
assert.fail(
`${assertionMethod} takes 1 argument but was called with ${
assertionArgs.length + 1
} arguments`
);
}
break;
default:
break;
}
}

function failAssertion(object, msg) {
var obj = object || globalObject;
var failMethod = obj.fail || assert.fail;
failMethod.call(obj, msg);
}

function mirrorPropAsAssertion(name, method, message) {
var msg = message;
var meth = method;
if (arguments.length === 2) {
msg = method;
meth = name;
}

assert[name] = function (fake) {
verifyIsStub(fake);

var args = arraySlice(arguments, 1);
var failed = false;

verifyIsValidAssertion(name, args);

if (typeof meth === "function") {
failed = !meth(fake);
} else {
failed =
typeof fake[meth] === "function"
? !fake[meth].apply(fake, args)
: !fake[meth];
}

if (failed) {
failAssertion(
this,
(fake.printf || fake.proxy.printf).apply(
fake,
concat([msg], args)
)
);
} else {
assert.pass(name);
}
};
}

function exposedName(prefix, prop) {
return !prefix || /^fail/.test(prop)
? prop
: prefix +
stringSlice(prop, 0, 1).toUpperCase() +
stringSlice(prop, 1);
}

assert = {
const assert = {
failException: "AssertError",

fail: function fail(message) {
var error = new Error(message);
const error = new Error(message);
error.name = this.failException || assert.failException;

throw error;
Expand All @@ -129,14 +32,14 @@ function createAssertObject() {

callOrder: function assertCallOrder() {
verifyIsStub.apply(null, arguments);
var expected = "";
var actual = "";
let expected = "";
let actual = "";

if (!calledInOrder(arguments)) {
try {
expected = join(arguments, ", ");
var calls = arraySlice(arguments);
var i = calls.length;
const calls = arraySlice(arguments);
let i = calls.length;
while (i) {
if (!calls[--i].called) {
splice(calls, i, 1);
Expand All @@ -159,7 +62,7 @@ function createAssertObject() {
callCount: function assertCallCount(method, count) {
verifyIsStub(method);

var msg;
let msg;
if (typeof count !== "number") {
msg =
`expected ${inspect(count)} to be a number ` +
Expand All @@ -180,12 +83,12 @@ function createAssertObject() {
throw new TypeError("target is null or undefined");
}

var o = options || {};
var prefix =
const o = options || {};
const prefix =
(typeof o.prefix === "undefined" && "assert") || o.prefix;
var includeFail =
const includeFail =
typeof o.includeFail === "undefined" || Boolean(o.includeFail);
var instance = this;
const instance = this;

forEach(Object.keys(instance), function (method) {
if (
Expand All @@ -200,11 +103,11 @@ function createAssertObject() {
},

match: function match(actual, expectation) {
var matcher = createMatcher(expectation);
const matcher = createMatcher(expectation);
if (matcher.test(actual)) {
assert.pass("match");
} else {
var formatted = [
const formatted = [
"expected value to match",
` expected = ${inspect(expectation)}`,
` actual = ${inspect(actual)}`,
Expand All @@ -215,6 +118,101 @@ function createAssertObject() {
},
};

function verifyIsStub() {
const args = arraySlice(arguments);

forEach(args, function (method) {
if (!method) {
assert.fail("fake is not a spy");
}

if (method.proxy && method.proxy.isSinonProxy) {
verifyIsStub(method.proxy);
} else {
if (typeof method !== "function") {
assert.fail(`${method} is not a function`);
}

if (typeof method.getCall !== "function") {
assert.fail(`${method} is not stubbed`);
}
}
});
}

function verifyIsValidAssertion(assertionMethod, assertionArgs) {
switch (assertionMethod) {
case "notCalled":
case "called":
case "calledOnce":
case "calledTwice":
case "calledThrice":
if (assertionArgs.length !== 0) {
assert.fail(
`${assertionMethod} takes 1 argument but was called with ${
assertionArgs.length + 1
} arguments`
);
}
break;
default:
break;
}
}

function failAssertion(object, msg) {
const obj = object || globalObject;
const failMethod = obj.fail || assert.fail;
failMethod.call(obj, msg);
}

function mirrorPropAsAssertion(name, method, message) {
let msg = message;
let meth = method;
if (arguments.length === 2) {
msg = method;
meth = name;
}

assert[name] = function (fake) {
verifyIsStub(fake);

const args = arraySlice(arguments, 1);
let failed = false;

verifyIsValidAssertion(name, args);

if (typeof meth === "function") {
failed = !meth(fake);
} else {
failed =
typeof fake[meth] === "function"
? !fake[meth].apply(fake, args)
: !fake[meth];
}

if (failed) {
failAssertion(
this,
(fake.printf || fake.proxy.printf).apply(
fake,
concat([msg], args)
)
);
} else {
assert.pass(name);
}
};
}

function exposedName(prefix, prop) {
return !prefix || /^fail/.test(prop)
? prop
: prefix +
stringSlice(prop, 0, 1).toUpperCase() +
stringSlice(prop, 1);
}

mirrorPropAsAssertion(
"called",
"expected %n to have been called at least once but was never called"
Expand Down
Loading

0 comments on commit 430c9a6

Please sign in to comment.