Skip to content

Commit 430c9a6

Browse files
authored
Remove uses of var (#2506)
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
1 parent cd45bfa commit 430c9a6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+2671
-2645
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ docs/js/
77
docs/releases/
88
docs/_releases/
99
docs/assets/js/
10+
docs/vendor
1011

1112
# has linting of its own
1213
docs/release-source/release/examples

.prettierrc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This project uses Prettier defaults, the config file is present in
22
# order to trigger editor plugins to allow format on save behaviour
33
overrides:
4-
- files: "*.html"
5-
options:
6-
tabWidth: 2
4+
- files: "*.html"
5+
options:
6+
tabWidth: 2

lib/sinon.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
"use strict";
22

3-
var behavior = require("./sinon/behavior");
4-
var createSandbox = require("./sinon/create-sandbox");
5-
var extend = require("./sinon/util/core/extend");
6-
var fakeTimers = require("./sinon/util/fake-timers");
7-
var nise = require("nise");
8-
var Sandbox = require("./sinon/sandbox");
9-
var stub = require("./sinon/stub");
10-
var promise = require("./sinon/promise");
11-
12-
var apiMethods = {
3+
const behavior = require("./sinon/behavior");
4+
const createSandbox = require("./sinon/create-sandbox");
5+
const extend = require("./sinon/util/core/extend");
6+
const fakeTimers = require("./sinon/util/fake-timers");
7+
const nise = require("nise");
8+
const Sandbox = require("./sinon/sandbox");
9+
const stub = require("./sinon/stub");
10+
const promise = require("./sinon/promise");
11+
12+
const apiMethods = {
1313
createSandbox: createSandbox,
1414
assert: require("./sinon/assert"),
1515
match: require("@sinonjs/samsam").createMatcher,
@@ -41,8 +41,8 @@ var apiMethods = {
4141
promise: promise,
4242
};
4343

44-
var sandbox = new Sandbox();
44+
const sandbox = new Sandbox();
4545

46-
var api = extend(sandbox, apiMethods);
46+
const api = extend(sandbox, apiMethods);
4747

4848
module.exports = api;

lib/sinon/assert.js

Lines changed: 122 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,26 @@
11
"use strict";
22

3-
var arrayProto = require("@sinonjs/commons").prototypes.array;
4-
var calledInOrder = require("@sinonjs/commons").calledInOrder;
5-
var createMatcher = require("@sinonjs/samsam").createMatcher;
6-
var orderByFirstCall = require("@sinonjs/commons").orderByFirstCall;
7-
var timesInWords = require("./util/core/times-in-words");
8-
var inspect = require("util").inspect;
9-
var stringSlice = require("@sinonjs/commons").prototypes.string.slice;
10-
var globalObject = require("@sinonjs/commons").global;
11-
12-
var arraySlice = arrayProto.slice;
13-
var concat = arrayProto.concat;
14-
var forEach = arrayProto.forEach;
15-
var join = arrayProto.join;
16-
var splice = arrayProto.splice;
3+
const arrayProto = require("@sinonjs/commons").prototypes.array;
4+
const calledInOrder = require("@sinonjs/commons").calledInOrder;
5+
const createMatcher = require("@sinonjs/samsam").createMatcher;
6+
const orderByFirstCall = require("@sinonjs/commons").orderByFirstCall;
7+
const timesInWords = require("./util/core/times-in-words");
8+
const inspect = require("util").inspect;
9+
const stringSlice = require("@sinonjs/commons").prototypes.string.slice;
10+
const globalObject = require("@sinonjs/commons").global;
11+
12+
const arraySlice = arrayProto.slice;
13+
const concat = arrayProto.concat;
14+
const forEach = arrayProto.forEach;
15+
const join = arrayProto.join;
16+
const splice = arrayProto.splice;
1717

1818
function createAssertObject() {
19-
var assert;
20-
21-
function verifyIsStub() {
22-
var args = arraySlice(arguments);
23-
24-
forEach(args, function (method) {
25-
if (!method) {
26-
assert.fail("fake is not a spy");
27-
}
28-
29-
if (method.proxy && method.proxy.isSinonProxy) {
30-
verifyIsStub(method.proxy);
31-
} else {
32-
if (typeof method !== "function") {
33-
assert.fail(`${method} is not a function`);
34-
}
35-
36-
if (typeof method.getCall !== "function") {
37-
assert.fail(`${method} is not stubbed`);
38-
}
39-
}
40-
});
41-
}
42-
43-
function verifyIsValidAssertion(assertionMethod, assertionArgs) {
44-
switch (assertionMethod) {
45-
case "notCalled":
46-
case "called":
47-
case "calledOnce":
48-
case "calledTwice":
49-
case "calledThrice":
50-
if (assertionArgs.length !== 0) {
51-
assert.fail(
52-
`${assertionMethod} takes 1 argument but was called with ${
53-
assertionArgs.length + 1
54-
} arguments`
55-
);
56-
}
57-
break;
58-
default:
59-
break;
60-
}
61-
}
62-
63-
function failAssertion(object, msg) {
64-
var obj = object || globalObject;
65-
var failMethod = obj.fail || assert.fail;
66-
failMethod.call(obj, msg);
67-
}
68-
69-
function mirrorPropAsAssertion(name, method, message) {
70-
var msg = message;
71-
var meth = method;
72-
if (arguments.length === 2) {
73-
msg = method;
74-
meth = name;
75-
}
76-
77-
assert[name] = function (fake) {
78-
verifyIsStub(fake);
79-
80-
var args = arraySlice(arguments, 1);
81-
var failed = false;
82-
83-
verifyIsValidAssertion(name, args);
84-
85-
if (typeof meth === "function") {
86-
failed = !meth(fake);
87-
} else {
88-
failed =
89-
typeof fake[meth] === "function"
90-
? !fake[meth].apply(fake, args)
91-
: !fake[meth];
92-
}
93-
94-
if (failed) {
95-
failAssertion(
96-
this,
97-
(fake.printf || fake.proxy.printf).apply(
98-
fake,
99-
concat([msg], args)
100-
)
101-
);
102-
} else {
103-
assert.pass(name);
104-
}
105-
};
106-
}
107-
108-
function exposedName(prefix, prop) {
109-
return !prefix || /^fail/.test(prop)
110-
? prop
111-
: prefix +
112-
stringSlice(prop, 0, 1).toUpperCase() +
113-
stringSlice(prop, 1);
114-
}
115-
116-
assert = {
19+
const assert = {
11720
failException: "AssertError",
11821

11922
fail: function fail(message) {
120-
var error = new Error(message);
23+
const error = new Error(message);
12124
error.name = this.failException || assert.failException;
12225

12326
throw error;
@@ -129,14 +32,14 @@ function createAssertObject() {
12932

13033
callOrder: function assertCallOrder() {
13134
verifyIsStub.apply(null, arguments);
132-
var expected = "";
133-
var actual = "";
35+
let expected = "";
36+
let actual = "";
13437

13538
if (!calledInOrder(arguments)) {
13639
try {
13740
expected = join(arguments, ", ");
138-
var calls = arraySlice(arguments);
139-
var i = calls.length;
41+
const calls = arraySlice(arguments);
42+
let i = calls.length;
14043
while (i) {
14144
if (!calls[--i].called) {
14245
splice(calls, i, 1);
@@ -159,7 +62,7 @@ function createAssertObject() {
15962
callCount: function assertCallCount(method, count) {
16063
verifyIsStub(method);
16164

162-
var msg;
65+
let msg;
16366
if (typeof count !== "number") {
16467
msg =
16568
`expected ${inspect(count)} to be a number ` +
@@ -180,12 +83,12 @@ function createAssertObject() {
18083
throw new TypeError("target is null or undefined");
18184
}
18285

183-
var o = options || {};
184-
var prefix =
86+
const o = options || {};
87+
const prefix =
18588
(typeof o.prefix === "undefined" && "assert") || o.prefix;
186-
var includeFail =
89+
const includeFail =
18790
typeof o.includeFail === "undefined" || Boolean(o.includeFail);
188-
var instance = this;
91+
const instance = this;
18992

19093
forEach(Object.keys(instance), function (method) {
19194
if (
@@ -200,11 +103,11 @@ function createAssertObject() {
200103
},
201104

202105
match: function match(actual, expectation) {
203-
var matcher = createMatcher(expectation);
106+
const matcher = createMatcher(expectation);
204107
if (matcher.test(actual)) {
205108
assert.pass("match");
206109
} else {
207-
var formatted = [
110+
const formatted = [
208111
"expected value to match",
209112
` expected = ${inspect(expectation)}`,
210113
` actual = ${inspect(actual)}`,
@@ -215,6 +118,101 @@ function createAssertObject() {
215118
},
216119
};
217120

121+
function verifyIsStub() {
122+
const args = arraySlice(arguments);
123+
124+
forEach(args, function (method) {
125+
if (!method) {
126+
assert.fail("fake is not a spy");
127+
}
128+
129+
if (method.proxy && method.proxy.isSinonProxy) {
130+
verifyIsStub(method.proxy);
131+
} else {
132+
if (typeof method !== "function") {
133+
assert.fail(`${method} is not a function`);
134+
}
135+
136+
if (typeof method.getCall !== "function") {
137+
assert.fail(`${method} is not stubbed`);
138+
}
139+
}
140+
});
141+
}
142+
143+
function verifyIsValidAssertion(assertionMethod, assertionArgs) {
144+
switch (assertionMethod) {
145+
case "notCalled":
146+
case "called":
147+
case "calledOnce":
148+
case "calledTwice":
149+
case "calledThrice":
150+
if (assertionArgs.length !== 0) {
151+
assert.fail(
152+
`${assertionMethod} takes 1 argument but was called with ${
153+
assertionArgs.length + 1
154+
} arguments`
155+
);
156+
}
157+
break;
158+
default:
159+
break;
160+
}
161+
}
162+
163+
function failAssertion(object, msg) {
164+
const obj = object || globalObject;
165+
const failMethod = obj.fail || assert.fail;
166+
failMethod.call(obj, msg);
167+
}
168+
169+
function mirrorPropAsAssertion(name, method, message) {
170+
let msg = message;
171+
let meth = method;
172+
if (arguments.length === 2) {
173+
msg = method;
174+
meth = name;
175+
}
176+
177+
assert[name] = function (fake) {
178+
verifyIsStub(fake);
179+
180+
const args = arraySlice(arguments, 1);
181+
let failed = false;
182+
183+
verifyIsValidAssertion(name, args);
184+
185+
if (typeof meth === "function") {
186+
failed = !meth(fake);
187+
} else {
188+
failed =
189+
typeof fake[meth] === "function"
190+
? !fake[meth].apply(fake, args)
191+
: !fake[meth];
192+
}
193+
194+
if (failed) {
195+
failAssertion(
196+
this,
197+
(fake.printf || fake.proxy.printf).apply(
198+
fake,
199+
concat([msg], args)
200+
)
201+
);
202+
} else {
203+
assert.pass(name);
204+
}
205+
};
206+
}
207+
208+
function exposedName(prefix, prop) {
209+
return !prefix || /^fail/.test(prop)
210+
? prop
211+
: prefix +
212+
stringSlice(prop, 0, 1).toUpperCase() +
213+
stringSlice(prop, 1);
214+
}
215+
218216
mirrorPropAsAssertion(
219217
"called",
220218
"expected %n to have been called at least once but was never called"

0 commit comments

Comments
 (0)