From 66a2b87c4237644acd7738522a61cc98abdc939d Mon Sep 17 00:00:00 2001 From: Alex Zhu Date: Wed, 6 May 2026 23:43:14 +0800 Subject: [PATCH] fix: add getIntl() support to i18n-remove script (#11767) Co-authored-by: Claude Opus 4.7 --- .lintstagedrc | 3 --- scripts/i18n-remove.js | 24 +++++++++++++++--------- 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/.lintstagedrc b/.lintstagedrc index 569601c8..864c360c 100644 --- a/.lintstagedrc +++ b/.lintstagedrc @@ -4,8 +4,5 @@ ], "src/**/*.md": [ "npx @biomejs/biome check --write" - ], - "*.json": [ - "npx @biomejs/biome check --write" ] } \ No newline at end of file diff --git a/scripts/i18n-remove.js b/scripts/i18n-remove.js index 47ce6b07..8b06f0a0 100644 --- a/scripts/i18n-remove.js +++ b/scripts/i18n-remove.js @@ -17,6 +17,7 @@ const path = require('node:path'); const I18N_SYMBOLS = [ 'useIntl', + 'getIntl', 'FormattedMessage', 'SelectLang', 'getLocale', @@ -26,6 +27,7 @@ const I18N_SYMBOLS = [ const FORMAT_MESSAGE_PATTERNS = [ 'intl.formatMessage(', 'useIntl().formatMessage(', + 'getIntl().formatMessage(', ]; const QSTR = `'((?:[^'\\\\]|\\\\.)*)'|"((?:[^"\\\\]|\\\\.)*)"`; @@ -241,6 +243,7 @@ function processFile(filePath, localeMap) { !content.includes('formatMessage') && !content.includes('FormattedMessage') && !content.includes('useIntl') && + !content.includes('getIntl') && !content.includes('SelectLang') && !content.includes('getLocale') && !content.includes('getAllLocales') && @@ -257,15 +260,11 @@ function processFile(filePath, localeMap) { // 只替换 id 为字符串字面量的调用 content = replaceFormattedMessageComponents(content, localeMap); - // ── 3. 移除 const intl = useIntl() 声明 + // ── 3. 移除 const intl = useIntl() / const intl = getIntl() 声明 // 只在文件中不再有其他 intl. 引用时移除 if (!content.match(/\bintl\./)) { content = content.replace( - /\n\s*\/\*\*[\s\S]*?\*\/\n\s*const\s+intl\s*=\s*useIntl\(\)\s*;?\n/g, - '\n', - ); - content = content.replace( - /\n\s*const\s+intl\s*=\s*useIntl\(\)\s*;?\n/g, + /\n\s*(?:\/\*\*[\s\S]*?\*\/\n\s*)?const\s+intl\s*=\s*(?:useIntl|getIntl)\(\)\s*;?\n/g, '\n', ); } @@ -404,7 +403,7 @@ function replaceFormatMessageCalls(content, localeMap) { const id = idMatch[1] || idMatch[2]; const dmMatch = fullCall.match(new RegExp(`defaultMessage:\\s*${QSTR}`)); - const defaultMsg = dmMatch ? (dmMatch[1] || dmMatch[2]) : ''; + const defaultMsg = dmMatch ? dmMatch[1] || dmMatch[2] : ''; // 检查是否有第二参数(ICU 格式化参数),如果有则跳过 // 第一参数结束位置: 找到第一个 },{ 或 },\s*{ @@ -454,7 +453,7 @@ function replaceFormattedMessageComponents(content, localeMap) { const id = idMatch[1] || idMatch[2]; const dmMatch = fullTag.match(new RegExp(`defaultMessage=${QSTR}`)); - const defaultMsg = dmMatch ? (dmMatch[1] || dmMatch[2]) : ''; + const defaultMsg = dmMatch ? dmMatch[1] || dmMatch[2] : ''; const zhText = resolveText(id, defaultMsg, localeMap); const replacement = toStrLiteral(zhText); @@ -503,7 +502,14 @@ function deleteLocalesDir() { // ─── 残留检查 ──────────────────────────────────────────── function checkResiduals() { - const residualSymbols = ['getLocale', 'getAllLocales', 'setLocale']; + const residualSymbols = [ + 'useIntl', + 'getIntl', + 'FormattedMessage', + 'getLocale', + 'getAllLocales', + 'setLocale', + ]; const srcDir = path.join('src'); const files = readDirRecursive(srcDir).filter((f) => { const ext = path.extname(f);