mirror of
https://github.com/signalapp/Signal-Desktop.git
synced 2026-04-18 23:49:20 +01:00
Use protopiler for protocol buffers
Co-authored-by: Jamie Kyle <jamie@signal.org>
This commit is contained in:
35
codemods/protopiler-migration/01-remove-finish.js
Normal file
35
codemods/protopiler-migration/01-remove-finish.js
Normal 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);
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
34
codemods/protopiler-migration/02-remove-i.js
Normal file
34
codemods/protopiler-migration/02-remove-i.js
Normal 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')
|
||||
)
|
||||
);
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
76
codemods/protopiler-migration/03-to-number.js
Normal file
76
codemods/protopiler-migration/03-to-number.js
Normal 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,
|
||||
},
|
||||
};
|
||||
}
|
||||
53
codemods/protopiler-migration/04-long-from-number.js
Normal file
53
codemods/protopiler-migration/04-long-from-number.js
Normal 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]])
|
||||
);
|
||||
},
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user