tools: consolidate update-authors.js logic

Use a single regex and fewer logical branches in the code.

PR-URL: https://github.com/nodejs/node/pull/41255
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Mohammed Keyvanzadeh <mohammadkeyvanzade94@gmail.com>
This commit is contained in:
Rich Trott 2021-12-22 22:46:26 -08:00 committed by GitHub
parent 895c3d937e
commit f72c1978e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -39,25 +39,13 @@ const mailmap = new CaseIndifferentMap();
line = line.trim();
if (line.startsWith('#') || line === '') continue;
let match;
// Replaced Name <original@example.com>
if (match = line.match(/^([^<]+)\s+(<[^>]+>)$/)) {
mailmap.set(match[2].toLowerCase(), {
author: match[1], email: match[2]
});
// <replaced@example.com> <original@example.com>
} else if (match = line.match(/^<([^>]+)>\s+(<[^>]+>)$/)) {
mailmap.set(match[2].toLowerCase(), { email: match[1] });
// Replaced Name <replaced@example.com> <original@example.com>
} else if (match = line.match(/^([^<]+)\s+(<[^>]+>)\s+(<[^>]+>)$/)) {
mailmap.set(match[3].toLowerCase(), {
author: match[1], email: match[2]
});
// Replaced Name <replaced@example.com> Original Name <original@example.com>
} else if (match =
line.match(/^([^<]+)\s+(<[^>]+>)\s+([^<]+)\s+(<[^>]+>)$/)) {
mailmap.set(match[3] + '\0' + match[4].toLowerCase(), {
author: match[1], email: match[2]
const match = line.match(/^(?:([^<]+)\s+)?(?:(<[^>]+>)\s+)?(?:([^<]+)\s+)?(<[^>]+>)$/);
if (match) {
const [, replaceName, replaceEmail, originalName, originalEmail] = match;
const key = originalName ? `${originalName}\0${originalEmail.toLocaleLowerCase()}` : originalEmail.toLowerCase();
mailmap.set(key, {
author: replaceName || originalName,
email: replaceEmail || originalEmail,
});
} else {
console.warn('Unknown .mailmap format:', line);
@ -73,8 +61,8 @@ const previousAuthors = new CaseIndifferentMap();
line = line.trim();
if (line.startsWith('#') || line === '') continue;
let match;
if (match = line.match(/^([^<]+)\s+(<[^>]+>)$/)) {
const match = line.match(/^([^<]+)\s+(<[^>]+>)$/);
if (match) {
const name = match[1];
const email = match[2];
if (previousAuthors.has(name)) {