Skip to content

Commit 1dac06f

Browse files
committed
fix: declare long only once
1 parent 657fb4d commit 1dac06f

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

cli/targets/static.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ function beautifyCode(code) {
181181
output: { beautify: true }
182182
}).code;
183183
// Properly beautify
184-
var ast = espree.parse(code);
184+
var ast = espree.parse(code, { ecmaVersion: 2020 });
185185
estraverse.replace(ast, {
186186
enter: function(node, parent) {
187187
// rename short vars
@@ -227,7 +227,7 @@ function buildFunction(type, functionName, gen, scope) {
227227
var code = gen.toString(functionName)
228228
.replace(/((?!\.)types\[\d+])(\.values)/g, "$1"); // enums: use types[N] instead of reflected types[N].values
229229

230-
var ast = espree.parse(code);
230+
var ast = espree.parse(code, { ecmaVersion: 2020 });
231231
/* eslint-disable no-extra-parens */
232232
estraverse.replace(ast, {
233233
enter: function(node, parent) {

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@seorii/protobufjs",
3-
"version": "7.2.2",
3+
"version": "7.2.3",
44
"versionScheme": "~",
55
"description": "Protocol Buffers for JavaScript (& TypeScript).",
66
"author": "Daniel Wirtz <dcode+protobufjs@dcode.io>",

src/converter.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ converter.toObject = function toObject(mtype) {
232232
gen
233233
("}");
234234
}
235+
let hasLong = false;
235236

236237
if (normalFields.length) { gen
237238
("if(o.defaults){");
@@ -240,9 +241,13 @@ converter.toObject = function toObject(mtype) {
240241
prop = util.safeProp(field.name);
241242
if (field.resolvedType instanceof Enum) gen
242243
("d%s=o.enums===String?%j:%j", prop, field.resolvedType.valuesById[field.typeDefault], field.typeDefault);
243-
else if (field.long) gen
244-
("var n=new util.LongBits(%i,%i,%j)", field.typeDefault.low, field.typeDefault.high, field.typeDefault.unsigned)
245-
("d%s=o.longs===String?n.toBigInt().toString():o.longs===BigInt?n.toBigInt().toString():n", prop);
244+
else if (field.long) {
245+
if(!hasLong) {
246+
gen("var n=new util.LongBits(%i,%i,%j)", field.typeDefault.low, field.typeDefault.high, field.typeDefault.unsigned)
247+
hasLong = true;
248+
}
249+
gen("d%s=o.longs===String?n.toBigInt().toString():o.longs===BigInt?n.toBigInt().toString():n", prop);
250+
}
246251
else if (field.bytes) {
247252
var arrayDefault = "[" + Array.prototype.slice.call(field.typeDefault).join(",") + "]";
248253
gen

tests/data/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16440,7 +16440,7 @@ $root.google = (function() {
1644016440
object.identifierValue = "";
1644116441
var long = new $util.LongBits(NaN, NaN, undefined);
1644216442
object.positiveIntValue = options.longs === String ? long.toBigInt().toString() : options.longs === BigInt ? long.toBigInt().toString() : long;
16443-
long = new $util.LongBits(NaN, NaN, undefined);
16443+
var long = new $util.LongBits(NaN, NaN, undefined);
1644416444
object.negativeIntValue = options.longs === String ? long.toBigInt().toString() : options.longs === BigInt ? long.toBigInt().toString() : long;
1644516445
object.doubleValue = 0;
1644616446
if (options.bytes === String)

0 commit comments

Comments
 (0)