Use protopiler for protocol buffers

Co-authored-by: Jamie Kyle <jamie@signal.org>
This commit is contained in:
Fedor Indutny
2026-03-10 15:31:29 -07:00
committed by GitHub
parent b0e19f334e
commit c4ee32e9ee
97 changed files with 6197 additions and 6362 deletions

View File

@@ -0,0 +1,35 @@
// Copyright 2026 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
export default function transform() {
return {
visitor: {
CallExpression(path) {
const { node } = path;
if (node.arguments.length !== 0) {
return;
}
if (node.callee.type !== 'MemberExpression') {
return;
}
const { object, property } = node.callee;
if (object.type !== 'CallExpression') {
return;
}
if (property.type !== 'Identifier' || property.name !== 'finish') {
return;
}
if (
object.callee.type !== 'MemberExpression' ||
object.callee.property.type !== 'Identifier' ||
object.callee.property.name !== 'encode'
) {
return;
}
path.replaceWith(object);
},
},
};
}

View File

@@ -0,0 +1,34 @@
// Copyright 2026 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
export default function transform(babel) {
const { types: t } = babel;
return {
visitor: {
TSQualifiedName(path) {
const { node } = path;
if (
node.right.type !== 'Identifier' ||
!/^I[A-Z][a-z]/.test(node.right.name)
) {
return;
}
// Don't touch fuse.js
if (node.left.type === 'Identifier' && node.left.name === 'Fuse') {
return;
}
path.replaceWith(
t.TSQualifiedName(
t.TSQualifiedName(
node.left,
t.Identifier(node.right.name.slice(1))
),
t.Identifier('Params')
)
);
},
},
};
}

View File

@@ -0,0 +1,76 @@
// Copyright 2026 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { relative, dirname, join } from 'node:path';
const importPath = join(__dirname, '..', 'ts', 'util', 'toNumber.std.js');
export default function transform(babel) {
const { types: t } = babel;
let program;
function Program({ node }) {
program = node;
}
function addImport(filename) {
if (program === undefined) {
return;
}
let index = program.body.findLastIndex(
stmt => stmt.type === 'ImportDeclaration'
);
if (index === -1) {
index = 0;
} else {
index += 1;
}
let relativePath = relative(dirname(filename), importPath);
if (!relativePath.startsWith('.')) {
relativePath = `./${relativePath}`;
}
program.body.splice(
index,
0,
t.ImportDeclaration(
[t.ImportSpecifier(t.Identifier('toNumber'), t.Identifier('toNumber'))],
t.StringLiteral(relativePath)
)
);
program = undefined;
}
function CallExpression(path, { filename }) {
const { callee, arguments: args } = path.node;
if (args.length !== 0) {
return;
}
if (
callee.type !== 'MemberExpression' &&
callee.type !== 'OptionalMemberExpression'
) {
return;
}
if (
callee.property.type !== 'Identifier' ||
callee.property.name !== 'toNumber'
) {
return;
}
const replacement = t.CallExpression(t.Identifier('toNumber'), [
callee.object,
]);
path.replaceWith(replacement);
addImport(filename);
}
return {
visitor: {
Program,
CallExpression,
OptionalCallExpression: CallExpression,
},
};
}

View File

@@ -0,0 +1,53 @@
// 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]])
);
},
},
};
}