Skip to content

Commit 5ca9f45

Browse files
committed
Merge remote-tracking branch 'origin/amqplib-modernisation-5-noVar'
2 parents 5c9e3db + ce370fa commit 5ca9f45

29 files changed

+683
-681
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- Add npm format script for manual code formatting
99
- Enable noUnusedFunctionParameters lint rule and fix all violations
1010
- Enable noUnusedVariables lint rule and remove all unused variables from codebase
11+
- Replace all var declarations with let/const for modern JavaScript standards
1112

1213
## v0.10.9
1314
- Add support for IPv6 urls

bin/generate-defs.js

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
var format = require('util').format;
1+
const format = require('util').format;
22

3-
var defs = require('./amqp-rabbitmq-0.9.1.json');
3+
const defs = require('./amqp-rabbitmq-0.9.1.json');
44

5-
var FRAME_OVERHEAD = 8; // type + channel + size + frame-end
5+
const FRAME_OVERHEAD = 8; // type + channel + size + frame-end
66

7-
var METHOD_OVERHEAD = FRAME_OVERHEAD + 4;
7+
const METHOD_OVERHEAD = FRAME_OVERHEAD + 4;
88
// F_O + classId + methodId
99

10-
var PROPERTIES_OVERHEAD = FRAME_OVERHEAD + 4 + 8 + 2;
10+
const PROPERTIES_OVERHEAD = FRAME_OVERHEAD + 4 + 8 + 2;
1111
// F_O + classId + weight + content size + flags
1212

13-
var out = process.stdout;
13+
const out = process.stdout;
1414

1515
function printf() {
1616
out.write(format.apply(format, arguments), 'utf8');
@@ -24,11 +24,11 @@ function println() {
2424
nl();
2525
}
2626

27-
var constants = {};
28-
var constant_strs = {};
27+
const constants = {};
28+
const constant_strs = {};
2929

30-
for (var i = 0, len = defs.constants.length; i < len; i++) {
31-
var cdef = defs.constants[i];
30+
for (let i = 0, len = defs.constants.length; i < len; i++) {
31+
const cdef = defs.constants[i];
3232
constants[constantName(cdef)] = cdef.value;
3333
constant_strs[cdef.value] = cdef.name;
3434
}
@@ -42,7 +42,7 @@ function methodName(clazz, method) {
4242
}
4343

4444
function propertyName(dashed) {
45-
var parts = dashed.split('-');
45+
const parts = dashed.split('-');
4646
return parts[0] + parts.slice(1).map(initial).join('');
4747
}
4848

@@ -51,26 +51,26 @@ function initial(part) {
5151
}
5252

5353
function argument(a) {
54-
var type = a.type || domains[a.domain];
55-
var friendlyName = propertyName(a.name);
54+
const type = a.type || domains[a.domain];
55+
const friendlyName = propertyName(a.name);
5656
return {type: type, name: friendlyName, default: a['default-value']};
5757
}
5858

59-
var domains = {};
60-
for (var i = 0, len = defs.domains.length; i < len; i++) {
61-
var dom = defs.domains[i];
59+
const domains = {};
60+
for (let i = 0, len = defs.domains.length; i < len; i++) {
61+
const dom = defs.domains[i];
6262
domains[dom[0]] = dom[1];
6363
}
6464

65-
var methods = {};
66-
var propertieses = {};
65+
const methods = {};
66+
const propertieses = {};
6767

68-
for (var i = 0, len = defs.classes.length; i < len; i++) {
69-
var clazz = defs.classes[i];
70-
for (var j = 0, num = clazz.methods.length; j < num; j++) {
71-
var method = clazz.methods[j];
72-
var name = methodName(clazz, method);
73-
var info = 'methodInfo' + name;
68+
for (let i = 0, len = defs.classes.length; i < len; i++) {
69+
const clazz = defs.classes[i];
70+
for (let j = 0, num = clazz.methods.length; j < num; j++) {
71+
const method = clazz.methods[j];
72+
const name = methodName(clazz, method);
73+
const info = 'methodInfo' + name;
7474

7575
methods[name] = {
7676
id: methodId(clazz, method),
@@ -86,8 +86,8 @@ for (var i = 0, len = defs.classes.length; i < len; i++) {
8686
};
8787
}
8888
if (clazz.properties && clazz.properties.length > 0) {
89-
var name = propertiesName(clazz);
90-
var props = clazz.properties;
89+
const name = propertiesName(clazz);
90+
const props = clazz.properties;
9191
propertieses[name] = {
9292
id: clazz.id,
9393
name: name,
@@ -130,12 +130,12 @@ nl();
130130

131131
println('module.exports.decode = function(id, buf) {');
132132
println('switch (id) {');
133-
for (var m in methods) {
134-
var method = methods[m];
133+
for (const m in methods) {
134+
const method = methods[m];
135135
println('case %d: return %s(buf);', method.id, method.decoder);
136136
}
137-
for (var p in propertieses) {
138-
var props = propertieses[p];
137+
for (const p in propertieses) {
138+
const props = propertieses[p];
139139
println('case %d: return %s(buf);', props.id, props.decoder);
140140
}
141141
println('default: throw new Error("Unknown class/method ID");');
@@ -144,8 +144,8 @@ nl();
144144

145145
println('module.exports.encodeMethod =', 'function(id, channel, fields) {');
146146
println('switch (id) {');
147-
for (var m in methods) {
148-
var method = methods[m];
147+
for (const m in methods) {
148+
const method = methods[m];
149149
println('case %d: return %s(channel, fields);', method.id, method.encoder);
150150
}
151151
println('default: throw new Error("Unknown class/method ID");');
@@ -154,8 +154,8 @@ nl();
154154

155155
println('module.exports.encodeProperties =', 'function(id, channel, size, fields) {');
156156
println('switch (id) {');
157-
for (var p in propertieses) {
158-
var props = propertieses[p];
157+
for (const p in propertieses) {
158+
const props = propertieses[p];
159159
println('case %d: return %s(channel, size, fields);', props.id, props.encoder);
160160
}
161161
println('default: throw new Error("Unknown class/properties ID");');
@@ -164,20 +164,20 @@ nl();
164164

165165
println('module.exports.info = function(id) {');
166166
println('switch(id) {');
167-
for (var m in methods) {
168-
var method = methods[m];
167+
for (const m in methods) {
168+
const method = methods[m];
169169
println('case %d: return %s; ', method.id, method.info);
170170
}
171-
for (var p in propertieses) {
172-
var properties = propertieses[p];
171+
for (const p in propertieses) {
172+
const properties = propertieses[p];
173173
println('case %d: return %s', properties.id, properties.info);
174174
}
175175
println('default: throw new Error("Unknown class/method ID");');
176176
println('}}');
177177
nl();
178178

179-
for (var m in methods) {
180-
var method = methods[m];
179+
for (const m in methods) {
180+
const method = methods[m];
181181
println('module.exports.%s = %d;', m, method.id);
182182
decoderFn(method);
183183
nl();
@@ -187,8 +187,8 @@ for (var m in methods) {
187187
nl();
188188
}
189189

190-
for (var p in propertieses) {
191-
var properties = propertieses[p];
190+
for (const p in propertieses) {
191+
const properties = propertieses[p];
192192
println('module.exports.%s = %d;', p, properties.id);
193193
encodePropsFn(properties);
194194
nl();
@@ -291,7 +291,7 @@ function checkAssignArg(a) {
291291
// another buffer before returning. `scratchOffset`, `val`, `len` are
292292
// expected to have been declared.
293293
function assignTable(a) {
294-
var varname = tableVar(a);
294+
const varname = tableVar(a);
295295
println('len = encodeTable(SCRATCH, val, scratchOffset);');
296296
println('var %s = SCRATCH.slice(scratchOffset, scratchOffset + len);', varname);
297297
println('scratchOffset += len;');
@@ -306,13 +306,13 @@ function stringLenVar(a) {
306306
}
307307

308308
function assignStringLen(a) {
309-
var v = stringLenVar(a);
309+
const v = stringLenVar(a);
310310
// Assumes the value or default is in val
311311
println("var %s = Buffer.byteLength(val, 'utf8');", v);
312312
}
313313

314314
function encoderFn(method) {
315-
var args = method['args'];
315+
const args = method['args'];
316316
println('function %s(channel, fields) {', method.encoder);
317317
println('var offset = 0, val = null, bits = 0, varyingSize = 0;');
318318
println('var len, scratchOffset = 0;');
@@ -323,12 +323,12 @@ function encoderFn(method) {
323323
// either 1. contribute to the fixed size; or 2. emit code to
324324
// calculate the size (and possibly the encoded value, in the case
325325
// of tables).
326-
var fixedSize = METHOD_OVERHEAD;
326+
let fixedSize = METHOD_OVERHEAD;
327327

328-
var bitsInARow = 0;
328+
let bitsInARow = 0;
329329

330-
for (var i = 0, len = args.length; i < len; i++) {
331-
var arg = args[i];
330+
for (let i = 0, len = args.length; i < len; i++) {
331+
const arg = args[i];
332332

333333
if (arg.type != 'bit') bitsInARow = 0;
334334

@@ -387,8 +387,8 @@ function encoderFn(method) {
387387

388388
bitsInARow = 0;
389389

390-
for (var i = 0, len = args.length; i < len; i++) {
391-
var a = args[i];
390+
for (let i = 0, len = args.length; i < len; i++) {
391+
const a = args[i];
392392

393393
// Flush any collected bits before doing a new field
394394
if (a.type != 'bit' && bitsInARow > 0) {
@@ -457,23 +457,23 @@ function encoderFn(method) {
457457

458458
function fieldsDecl(args) {
459459
println('var fields = {');
460-
for (var i = 0, num = args.length; i < num; i++) {
460+
for (let i = 0, num = args.length; i < num; i++) {
461461
println('%s: undefined,', args[i].name);
462462
}
463463
println('};');
464464
}
465465

466466
function decoderFn(method) {
467-
var args = method.args;
467+
const args = method.args;
468468
println('function %s(buffer) {', method.decoder);
469469
println('var offset = 0, val, len;');
470470
fieldsDecl(args);
471471

472-
var bitsInARow = 0;
472+
let bitsInARow = 0;
473473

474-
for (var i = 0, num = args.length; i < num; i++) {
475-
var a = args[i];
476-
var field = "fields['" + a.name + "']";
474+
for (let i = 0, num = args.length; i < num; i++) {
475+
const a = args[i];
476+
const field = "fields['" + a.name + "']";
477477

478478
// Flush any collected bits before doing a new field
479479
if (a.type != 'bit' && bitsInARow > 0) {
@@ -496,7 +496,7 @@ function decoderFn(method) {
496496
println('val = ints.readUInt64BE(buffer, offset); offset += 8;');
497497
break;
498498
case 'bit':
499-
var bit = 1 << bitsInARow;
499+
const bit = 1 << bitsInARow;
500500
println('val = !!(buffer[offset] & %d);', bit);
501501
if (bitsInARow === 7) {
502502
println('offset++;');
@@ -528,7 +528,7 @@ function decoderFn(method) {
528528
}
529529

530530
function infoObj(thing) {
531-
var info = JSON.stringify({id: thing.id, classId: thing.clazzId, methodId: thing.methodId, name: thing.name, args: thing.args});
531+
const info = JSON.stringify({id: thing.id, classId: thing.clazzId, methodId: thing.methodId, name: thing.name, args: thing.args});
532532
println('var %s = module.exports.%s = %s;', thing.info, thing.info, info);
533533
}
534534

@@ -551,16 +551,16 @@ function encodePropsFn(props) {
551551
println('var offset = 0, flags = 0, val, len;');
552552
println('var scratchOffset = 0, varyingSize = 0;');
553553

554-
var fixedSize = PROPERTIES_OVERHEAD;
554+
const fixedSize = PROPERTIES_OVERHEAD;
555555

556-
var args = props.args;
556+
const args = props.args;
557557

558558
function incVarying(by) {
559559
println('varyingSize += %d;', by);
560560
}
561561

562-
for (var i = 0, num = args.length; i < num; i++) {
563-
var p = args[i];
562+
for (let i = 0, num = args.length; i < num; i++) {
563+
const p = args[i];
564564

565565
assignArg(p);
566566
println('if (val != undefined) {');
@@ -617,9 +617,9 @@ function encodePropsFn(props) {
617617
// we'll write the flags later too
618618
println('offset = 21;');
619619

620-
for (var i = 0, num = args.length; i < num; i++) {
621-
var p = args[i];
622-
var flag = flagAt(i);
620+
for (let i = 0, num = args.length; i < num; i++) {
621+
const p = args[i];
622+
const flag = flagAt(i);
623623

624624
assignArg(p);
625625
println('if (val != undefined) {');
@@ -645,7 +645,7 @@ function encodePropsFn(props) {
645645
println('offset += 8;');
646646
break;
647647
case 'shortstr':
648-
var v = stringLenVar(p);
648+
const v = stringLenVar(p);
649649
println('buffer[offset] = %s; offset++;', v);
650650
println("buffer.write(val, offset, 'utf8');");
651651
println('offset += %s;', v);
@@ -674,7 +674,7 @@ function encodePropsFn(props) {
674674
}
675675

676676
function decodePropsFn(props) {
677-
var args = props.args;
677+
const args = props.args;
678678

679679
println('function %s(buffer) {', props.decoder);
680680
println('var flags, offset = 2, val, len;');
@@ -684,9 +684,9 @@ function decodePropsFn(props) {
684684

685685
fieldsDecl(args);
686686

687-
for (var i = 0, num = args.length; i < num; i++) {
688-
var p = argument(args[i]);
689-
var field = "fields['" + p.name + "']";
687+
for (let i = 0, num = args.length; i < num; i++) {
688+
const p = argument(args[i]);
689+
const field = "fields['" + p.name + "']";
690690

691691
println('if (flags & %d) {', flagAt(i));
692692
if (p.type === 'bit') {

biome.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
"correctness": {
2323
"noUnusedFunctionParameters": "error",
2424
"noUnusedVariables": "error",
25-
"noInnerDeclarations": "off",
25+
"noInnerDeclarations": "error",
2626
"useParseIntRadix": "off",
2727
"noSwitchDeclarations": "off",
28-
"noInvalidUseBeforeDeclaration": "off",
28+
"noInvalidUseBeforeDeclaration": "error",
2929
"noPrecisionLoss": "off"
3030
},
3131
"style": {
@@ -42,7 +42,8 @@
4242
"noGlobalIsNan": "off",
4343
"noRedeclare": "off",
4444
"noGlobalIsFinite": "off",
45-
"noPrototypeBuiltins": "off"
45+
"noPrototypeBuiltins": "off",
46+
"noVar": "error"
4647
}
4748
}
4849
},

callback_api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var raw_connect = require('./lib/connect').connect;
2-
var CallbackModel = require('./lib/callback_model').CallbackModel;
1+
const raw_connect = require('./lib/connect').connect;
2+
const CallbackModel = require('./lib/callback_model').CallbackModel;
33

44
// Supports three shapes:
55
// connect(url, options, callback)

channel_api.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
var raw_connect = require('./lib/connect').connect;
2-
var ChannelModel = require('./lib/channel_model').ChannelModel;
3-
var promisify = require('util').promisify;
1+
const raw_connect = require('./lib/connect').connect;
2+
const ChannelModel = require('./lib/channel_model').ChannelModel;
3+
const promisify = require('util').promisify;
44

55
function connect(url, connOptions) {
66
return promisify(function (cb) {

examples/waitForConfirms.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const amqp = require('../');
66
connection = await amqp.connect();
77
const channel = await connection.createConfirmChannel();
88

9-
for (var i = 0; i < 20; i++) {
9+
for (let i = 0; i < 20; i++) {
1010
channel.publish('amq.topic', 'whatever', Buffer.from('blah'));
1111
}
1212

0 commit comments

Comments
 (0)