Files
Desktop/codemods/protopiler-migration/04-long-from-number.js
Fedor Indutny c4ee32e9ee Use protopiler for protocol buffers
Co-authored-by: Jamie Kyle <jamie@signal.org>
2026-03-10 15:31:29 -07:00

54 lines
1.3 KiB
JavaScript

// Copyright 2026 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
export default function transform(babel) {
const { types: t } = babel;
return {
visitor: {
CallExpression(path) {
const { node } = path;
if (node.arguments.length !== 1) {
return;
}
if (node.callee.type !== 'MemberExpression') {
return;
}
const { object, property } = node.callee;
if (object.type !== 'Identifier' || object.name !== 'Long') {
return;
}
if (property.type !== 'Identifier') {
return;
}
if (property.name === 'isLong') {
path.replaceWith(
t.BinaryExpression(
'===',
t.UnaryExpression('typeof', node.arguments[0]),
t.StringLiteral('bigint')
)
);
return;
}
if (
property.name !== 'fromNumber' &&
property.name !== 'fromString' &&
property.name !== 'fromValue'
) {
return;
}
if (node.arguments[0].type === 'NumericLiteral') {
path.replaceWith(t.BigIntLiteral(node.arguments[0].value.toString()));
return;
}
path.replaceWith(
t.CallExpression(t.Identifier('BigInt'), [node.arguments[0]])
);
},
},
};
}