Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: safely parse emoji shortcodes #20814

Merged
merged 2 commits into from Mar 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 18 additions & 4 deletions lib/util/emoji.ts
Expand Up @@ -7,8 +7,10 @@ import {
} from 'emojibase';
import emojibaseEmojiRegex from 'emojibase-regex/emoji.js';
import SHORTCODE_REGEX from 'emojibase-regex/shortcode.js';
import { z } from 'zod';
import type { RenovateConfig } from '../config/types';
import dataFiles from '../data-files.generated';
import { logger } from '../logger';
import { regEx } from './regex';

let unicodeEmoji = true;
Expand All @@ -17,13 +19,25 @@ let mappingsInitialized = false;
const shortCodesByHex = new Map<string, string>();
const hexCodesByShort = new Map<string, string>();

const EmojiShortcodesSchema = z.record(
z.string(),
z.union([z.string(), z.array(z.string())])
);

function lazyInitMappings(): void {
if (!mappingsInitialized) {
const table: Record<string, string | string[]> = JSON.parse(
dataFiles.get('node_modules/emojibase-data/en/shortcodes/github.json')!
const result = EmojiShortcodesSchema.safeParse(
JSON.parse(
dataFiles.get('node_modules/emojibase-data/en/shortcodes/github.json')!
)
);
for (const [hex, val] of Object.entries(table)) {
const shortCodes: string[] = is.array<string>(val) ? val : [val];
// istanbul ignore if: not easily testable
if (!result.success) {
logger.warn({ error: result.error }, 'Unable to parse emoji shortcodes');
return;
}
for (const [hex, val] of Object.entries(result.data)) {
const shortCodes = is.array(val) ? val : [val];
shortCodesByHex.set(hex, `:${shortCodes[0]}:`);
shortCodes.forEach((shortCode) => {
hexCodesByShort.set(`:${shortCode}:`, hex);
Expand Down