test: replace function with arrow function
1. Among the list of Code and Learn, I solved the unfinished task of replacing function with arrow function: https://github.com/nodejs/code-and-learn/issues/72#issuecomment-345667395 2. Replace arrow function with shorter property syntax Arrow function makes `this` lexical scope. But toString expects evaluate `this` in runtime. 3. Replace this with null makeBlock does not need `this`. update `this` with `null` to clarify the intent. PR-URL: https://github.com/nodejs/node/pull/17345 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Jon Moss <me@jonathanmoss.me> Reviewed-By: Yosuke Furukawa <yosuke.furukawa@gmail.com>
This commit is contained in:
parent
597b3d1941
commit
3c62f33d7b
@ -26,8 +26,8 @@ const a = assert;
|
|||||||
|
|
||||||
function makeBlock(f) {
|
function makeBlock(f) {
|
||||||
const args = Array.prototype.slice.call(arguments, 1);
|
const args = Array.prototype.slice.call(arguments, 1);
|
||||||
return function() {
|
return () => {
|
||||||
return f.apply(this, args);
|
return f.apply(null, args);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -183,7 +183,7 @@ assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2));
|
|||||||
|
|
||||||
// having an identical prototype property
|
// having an identical prototype property
|
||||||
const nbRoot = {
|
const nbRoot = {
|
||||||
toString: function() { return `${this.first} ${this.last}`; }
|
toString() { return `${this.first} ${this.last}`; }
|
||||||
};
|
};
|
||||||
|
|
||||||
function nameBuilder(first, last) {
|
function nameBuilder(first, last) {
|
||||||
@ -458,10 +458,10 @@ assert.throws(makeBlock(thrower, TypeError));
|
|||||||
'a.doesNotThrow is not catching type matching errors');
|
'a.doesNotThrow is not catching type matching errors');
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.throws(function() { assert.ifError(new Error('test error')); },
|
assert.throws(() => { assert.ifError(new Error('test error')); },
|
||||||
/^Error: test error$/);
|
/^Error: test error$/);
|
||||||
assert.doesNotThrow(function() { assert.ifError(null); });
|
assert.doesNotThrow(() => { assert.ifError(null); });
|
||||||
assert.doesNotThrow(function() { assert.ifError(); });
|
assert.doesNotThrow(() => { assert.ifError(); });
|
||||||
|
|
||||||
assert.throws(() => {
|
assert.throws(() => {
|
||||||
assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
|
assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
|
||||||
@ -501,7 +501,7 @@ assert.throws(() => {
|
|||||||
let threw = false;
|
let threw = false;
|
||||||
try {
|
try {
|
||||||
assert.throws(
|
assert.throws(
|
||||||
function() {
|
() => {
|
||||||
throw ({}); // eslint-disable-line no-throw-literal
|
throw ({}); // eslint-disable-line no-throw-literal
|
||||||
},
|
},
|
||||||
Array
|
Array
|
||||||
@ -516,7 +516,7 @@ assert.throws(() => {
|
|||||||
a.throws(makeBlock(thrower, TypeError), /\[object Object\]/);
|
a.throws(makeBlock(thrower, TypeError), /\[object Object\]/);
|
||||||
|
|
||||||
// use a fn to validate error object
|
// use a fn to validate error object
|
||||||
a.throws(makeBlock(thrower, TypeError), function(err) {
|
a.throws(makeBlock(thrower, TypeError), (err) => {
|
||||||
if ((err instanceof TypeError) && /\[object Object\]/.test(err)) {
|
if ((err instanceof TypeError) && /\[object Object\]/.test(err)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -619,7 +619,7 @@ testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
|
|||||||
let threw = false;
|
let threw = false;
|
||||||
try {
|
try {
|
||||||
// eslint-disable-next-line no-restricted-syntax
|
// eslint-disable-next-line no-restricted-syntax
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
assert.ifError(null);
|
assert.ifError(null);
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -9,8 +9,8 @@ const domain = require('domain');
|
|||||||
*/
|
*/
|
||||||
const d = domain.create();
|
const d = domain.create();
|
||||||
|
|
||||||
d.on('error', common.mustCall(function() {
|
d.on('error', common.mustCall(() => {
|
||||||
process.nextTick(function() {
|
process.nextTick(() => {
|
||||||
// Scheduling a callback with process.nextTick will enter a _new_ domain,
|
// Scheduling a callback with process.nextTick will enter a _new_ domain,
|
||||||
// and the callback will be called after the domain that handled the error
|
// and the callback will be called after the domain that handled the error
|
||||||
// was exited. So there should be only one domain on the domains stack if
|
// was exited. So there should be only one domain on the domains stack if
|
||||||
@ -29,6 +29,6 @@ d.on('error', common.mustCall(function() {
|
|||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
|
||||||
d.run(function() {
|
d.run(() => {
|
||||||
throw new Error('Error from domain');
|
throw new Error('Error from domain');
|
||||||
});
|
});
|
||||||
|
@ -129,7 +129,7 @@ const qsWeirdObjects = [
|
|||||||
[{ regexp: /./g }, 'regexp=', { 'regexp': '' }],
|
[{ regexp: /./g }, 'regexp=', { 'regexp': '' }],
|
||||||
// eslint-disable-next-line no-unescaped-regexp-dot
|
// eslint-disable-next-line no-unescaped-regexp-dot
|
||||||
[{ regexp: new RegExp('.', 'g') }, 'regexp=', { 'regexp': '' }],
|
[{ regexp: new RegExp('.', 'g') }, 'regexp=', { 'regexp': '' }],
|
||||||
[{ fn: function() {} }, 'fn=', { 'fn': '' }],
|
[{ fn: () => {} }, 'fn=', { 'fn': '' }],
|
||||||
[{ fn: new Function('') }, 'fn=', { 'fn': '' }],
|
[{ fn: new Function('') }, 'fn=', { 'fn': '' }],
|
||||||
[{ math: Math }, 'math=', { 'math': '' }],
|
[{ math: Math }, 'math=', { 'math': '' }],
|
||||||
[{ e: extendedFunction }, 'e=', { 'e': '' }],
|
[{ e: extendedFunction }, 'e=', { 'e': '' }],
|
||||||
@ -192,7 +192,7 @@ function check(actual, expected, input) {
|
|||||||
`Expected keys: ${inspect(expectedKeys)}`;
|
`Expected keys: ${inspect(expectedKeys)}`;
|
||||||
}
|
}
|
||||||
assert.deepStrictEqual(actualKeys, expectedKeys, msg);
|
assert.deepStrictEqual(actualKeys, expectedKeys, msg);
|
||||||
expectedKeys.forEach(function(key) {
|
expectedKeys.forEach((key) => {
|
||||||
if (typeof input === 'string') {
|
if (typeof input === 'string') {
|
||||||
msg = `Input: ${inspect(input)}\n` +
|
msg = `Input: ${inspect(input)}\n` +
|
||||||
`Key: ${inspect(key)}\n` +
|
`Key: ${inspect(key)}\n` +
|
||||||
@ -206,21 +206,21 @@ function check(actual, expected, input) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// test that the canonical qs is parsed properly.
|
// test that the canonical qs is parsed properly.
|
||||||
qsTestCases.forEach(function(testCase) {
|
qsTestCases.forEach((testCase) => {
|
||||||
check(qs.parse(testCase[0]), testCase[2], testCase[0]);
|
check(qs.parse(testCase[0]), testCase[2], testCase[0]);
|
||||||
});
|
});
|
||||||
|
|
||||||
// test that the colon test cases can do the same
|
// test that the colon test cases can do the same
|
||||||
qsColonTestCases.forEach(function(testCase) {
|
qsColonTestCases.forEach((testCase) => {
|
||||||
check(qs.parse(testCase[0], ';', ':'), testCase[2], testCase[0]);
|
check(qs.parse(testCase[0], ';', ':'), testCase[2], testCase[0]);
|
||||||
});
|
});
|
||||||
|
|
||||||
// test the weird objects, that they get parsed properly
|
// test the weird objects, that they get parsed properly
|
||||||
qsWeirdObjects.forEach(function(testCase) {
|
qsWeirdObjects.forEach((testCase) => {
|
||||||
check(qs.parse(testCase[1]), testCase[2], testCase[1]);
|
check(qs.parse(testCase[1]), testCase[2], testCase[1]);
|
||||||
});
|
});
|
||||||
|
|
||||||
qsNoMungeTestCases.forEach(function(testCase) {
|
qsNoMungeTestCases.forEach((testCase) => {
|
||||||
assert.deepStrictEqual(testCase[0], qs.stringify(testCase[1], '&', '='));
|
assert.deepStrictEqual(testCase[0], qs.stringify(testCase[1], '&', '='));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -258,15 +258,15 @@ qsNoMungeTestCases.forEach(function(testCase) {
|
|||||||
// now test stringifying
|
// now test stringifying
|
||||||
|
|
||||||
// basic
|
// basic
|
||||||
qsTestCases.forEach(function(testCase) {
|
qsTestCases.forEach((testCase) => {
|
||||||
assert.strictEqual(testCase[1], qs.stringify(testCase[2]));
|
assert.strictEqual(testCase[1], qs.stringify(testCase[2]));
|
||||||
});
|
});
|
||||||
|
|
||||||
qsColonTestCases.forEach(function(testCase) {
|
qsColonTestCases.forEach((testCase) => {
|
||||||
assert.strictEqual(testCase[1], qs.stringify(testCase[2], ';', ':'));
|
assert.strictEqual(testCase[1], qs.stringify(testCase[2], ';', ':'));
|
||||||
});
|
});
|
||||||
|
|
||||||
qsWeirdObjects.forEach(function(testCase) {
|
qsWeirdObjects.forEach((testCase) => {
|
||||||
assert.strictEqual(testCase[1], qs.stringify(testCase[0]));
|
assert.strictEqual(testCase[1], qs.stringify(testCase[0]));
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -300,7 +300,7 @@ assert.strictEqual('foo=', qs.stringify({ foo: Infinity }));
|
|||||||
assert.strictEqual(f, 'a=b&q=x%3Dy%26y%3Dz');
|
assert.strictEqual(f, 'a=b&q=x%3Dy%26y%3Dz');
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.doesNotThrow(function() {
|
assert.doesNotThrow(() => {
|
||||||
qs.parse(undefined);
|
qs.parse(undefined);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -432,7 +432,7 @@ check(qs.parse('%\u0100=%\u0101'), { '%Ā': '%ā' });
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Test QueryString.unescapeBuffer
|
// Test QueryString.unescapeBuffer
|
||||||
qsUnescapeTestCases.forEach(function(testCase) {
|
qsUnescapeTestCases.forEach((testCase) => {
|
||||||
assert.strictEqual(qs.unescape(testCase[0]), testCase[1]);
|
assert.strictEqual(qs.unescape(testCase[0]), testCase[1]);
|
||||||
assert.strictEqual(qs.unescapeBuffer(testCase[0]).toString(), testCase[1]);
|
assert.strictEqual(qs.unescapeBuffer(testCase[0]).toString(), testCase[1]);
|
||||||
});
|
});
|
||||||
@ -440,7 +440,7 @@ qsUnescapeTestCases.forEach(function(testCase) {
|
|||||||
// test overriding .unescape
|
// test overriding .unescape
|
||||||
{
|
{
|
||||||
const prevUnescape = qs.unescape;
|
const prevUnescape = qs.unescape;
|
||||||
qs.unescape = function(str) {
|
qs.unescape = (str) => {
|
||||||
return str.replace(/o/g, '_');
|
return str.replace(/o/g, '_');
|
||||||
};
|
};
|
||||||
check(
|
check(
|
||||||
|
@ -41,10 +41,10 @@ function test8(clazz) {
|
|||||||
assert.strictEqual(0xfb, buffer[1]);
|
assert.strictEqual(0xfb, buffer[1]);
|
||||||
|
|
||||||
/* Make sure we handle truncation correctly */
|
/* Make sure we handle truncation correctly */
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
buffer.writeInt8(0xabc, 0);
|
buffer.writeInt8(0xabc, 0);
|
||||||
}, errorOutOfBounds);
|
}, errorOutOfBounds);
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
buffer.writeInt8(0xabc, 0);
|
buffer.writeInt8(0xabc, 0);
|
||||||
}, errorOutOfBounds);
|
}, errorOutOfBounds);
|
||||||
|
|
||||||
@ -54,10 +54,10 @@ function test8(clazz) {
|
|||||||
|
|
||||||
assert.strictEqual(0x7f, buffer[0]);
|
assert.strictEqual(0x7f, buffer[0]);
|
||||||
assert.strictEqual(0x80, buffer[1]);
|
assert.strictEqual(0x80, buffer[1]);
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
buffer.writeInt8(0x7f + 1, 0);
|
buffer.writeInt8(0x7f + 1, 0);
|
||||||
}, errorOutOfBounds);
|
}, errorOutOfBounds);
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
buffer.writeInt8(-0x80 - 1, 0);
|
buffer.writeInt8(-0x80 - 1, 0);
|
||||||
}, errorOutOfBounds);
|
}, errorOutOfBounds);
|
||||||
}
|
}
|
||||||
@ -94,10 +94,10 @@ function test16(clazz) {
|
|||||||
assert.strictEqual(0xff, buffer[1]);
|
assert.strictEqual(0xff, buffer[1]);
|
||||||
assert.strictEqual(0x80, buffer[2]);
|
assert.strictEqual(0x80, buffer[2]);
|
||||||
assert.strictEqual(0x00, buffer[3]);
|
assert.strictEqual(0x00, buffer[3]);
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
buffer.writeInt16BE(0x7fff + 1, 0);
|
buffer.writeInt16BE(0x7fff + 1, 0);
|
||||||
}, errorOutOfBounds);
|
}, errorOutOfBounds);
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
buffer.writeInt16BE(-0x8000 - 1, 0);
|
buffer.writeInt16BE(-0x8000 - 1, 0);
|
||||||
}, errorOutOfBounds);
|
}, errorOutOfBounds);
|
||||||
|
|
||||||
@ -107,10 +107,10 @@ function test16(clazz) {
|
|||||||
assert.strictEqual(0x7f, buffer[1]);
|
assert.strictEqual(0x7f, buffer[1]);
|
||||||
assert.strictEqual(0x00, buffer[2]);
|
assert.strictEqual(0x00, buffer[2]);
|
||||||
assert.strictEqual(0x80, buffer[3]);
|
assert.strictEqual(0x80, buffer[3]);
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
buffer.writeInt16LE(0x7fff + 1, 0);
|
buffer.writeInt16LE(0x7fff + 1, 0);
|
||||||
}, errorOutOfBounds);
|
}, errorOutOfBounds);
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
buffer.writeInt16LE(-0x8000 - 1, 0);
|
buffer.writeInt16LE(-0x8000 - 1, 0);
|
||||||
}, errorOutOfBounds);
|
}, errorOutOfBounds);
|
||||||
}
|
}
|
||||||
@ -163,10 +163,10 @@ function test32(clazz) {
|
|||||||
assert.strictEqual(0x00, buffer[5]);
|
assert.strictEqual(0x00, buffer[5]);
|
||||||
assert.strictEqual(0x00, buffer[6]);
|
assert.strictEqual(0x00, buffer[6]);
|
||||||
assert.strictEqual(0x00, buffer[7]);
|
assert.strictEqual(0x00, buffer[7]);
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
buffer.writeInt32BE(0x7fffffff + 1, 0);
|
buffer.writeInt32BE(0x7fffffff + 1, 0);
|
||||||
}, errorOutOfBounds);
|
}, errorOutOfBounds);
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
buffer.writeInt32BE(-0x80000000 - 1, 0);
|
buffer.writeInt32BE(-0x80000000 - 1, 0);
|
||||||
}, errorOutOfBounds);
|
}, errorOutOfBounds);
|
||||||
|
|
||||||
@ -180,10 +180,10 @@ function test32(clazz) {
|
|||||||
assert.strictEqual(0x00, buffer[5]);
|
assert.strictEqual(0x00, buffer[5]);
|
||||||
assert.strictEqual(0x00, buffer[6]);
|
assert.strictEqual(0x00, buffer[6]);
|
||||||
assert.strictEqual(0x80, buffer[7]);
|
assert.strictEqual(0x80, buffer[7]);
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
buffer.writeInt32LE(0x7fffffff + 1, 0);
|
buffer.writeInt32LE(0x7fffffff + 1, 0);
|
||||||
}, errorOutOfBounds);
|
}, errorOutOfBounds);
|
||||||
assert.throws(function() {
|
assert.throws(() => {
|
||||||
buffer.writeInt32LE(-0x80000000 - 1, 0);
|
buffer.writeInt32LE(-0x80000000 - 1, 0);
|
||||||
}, errorOutOfBounds);
|
}, errorOutOfBounds);
|
||||||
}
|
}
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
const common = require('../common');
|
const common = require('../common');
|
||||||
const http = require('http');
|
const http = require('http');
|
||||||
|
|
||||||
const server = http.createServer(function(req, res) {
|
const server = http.createServer((req, res) => {
|
||||||
const buffer = Buffer.alloc(0);
|
const buffer = Buffer.alloc(0);
|
||||||
// FIXME: WTF gjslint want this?
|
// FIXME: WTF gjslint want this?
|
||||||
res.writeHead(200, { 'Content-Type': 'text/html',
|
res.writeHead(200, { 'Content-Type': 'text/html',
|
||||||
@ -12,12 +12,12 @@ const server = http.createServer(function(req, res) {
|
|||||||
res.end(buffer);
|
res.end(buffer);
|
||||||
});
|
});
|
||||||
|
|
||||||
server.listen(0, common.mustCall(function() {
|
server.listen(0, common.mustCall(() => {
|
||||||
http.get({ port: this.address().port }, common.mustCall(function(res) {
|
http.get({ port: server.address().port }, common.mustCall((res) => {
|
||||||
|
|
||||||
res.on('data', common.mustNotCall());
|
res.on('data', common.mustNotCall());
|
||||||
|
|
||||||
res.on('end', function(d) {
|
res.on('end', (d) => {
|
||||||
server.close();
|
server.close();
|
||||||
});
|
});
|
||||||
}));
|
}));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user