cloudflare-next-intl 0.9.53 → 0.9.54

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,10 +1,11 @@
1
1
  import { readFileSync, writeFileSync } from 'node:fs';
2
2
  import { relative } from 'node:path';
3
3
  import { detectLocaleParams } from './detect_locale_params.js';
4
- import { insertLocaleParamsSignature, insertLocaleParamsBody, ensureLocaleInParamsType, addParamsPropToExistingDestructure, ensureSetLocaleImport } from './insert_locale_params.js';
4
+ import { insertLocaleParamsSignature, insertLocaleParamsBody, ensureLocaleInParamsType, addParamsPropToExistingDestructure, ensureSetLocaleImport, wrapSyncDefaultExportWithParams, extractParamsPromiseType } from './insert_locale_params.js';
5
5
  import { findLocaleScopedFiles } from './find_locale_scoped_files.js';
6
6
  import { deriveRoute, makePageLabeler } from '../dynamic_pages_check/derive_page_label.js';
7
7
  const ZERO_ARG_DEFAULT_EXPORT = /export\s+default\s+(async\s+)?function\s+[A-Za-z_$][\w$]*\s*\(\s*\)/;
8
+ const SYNC_DEFAULT_EXPORT = /export\s+default\s+function\s+[A-Za-z_$][\w$]*\s*\(/;
8
9
  const LEGEND = '✓ Set up + Added ? Needs manual edit · Skipped';
9
10
  function actionGlyph(action) {
10
11
  switch (action) {
@@ -76,6 +77,18 @@ export async function checkLocaleParams(options, io = {}) {
76
77
  reports.push({ file, action: 'would-add-locale-params' });
77
78
  continue;
78
79
  }
80
+ const isSyncDefaultExport = !detection.hasInlineDestructure && SYNC_DEFAULT_EXPORT.test(source);
81
+ if (isSyncDefaultExport && (isZeroArg || canAddParamsKey || canReuseExistingParams)) {
82
+ const existingParamsType = canReuseExistingParams ? extractParamsPromiseType(source) ?? undefined : undefined;
83
+ const wrapped = wrapSyncDefaultExportWithParams(source, localeParam, existingParamsType);
84
+ if (wrapped === source) {
85
+ reports.push({ file, action: 'needs-manual-edit' });
86
+ continue;
87
+ }
88
+ writeFile(file, ensureSetLocaleImport(wrapped));
89
+ reports.push({ file, action: 'added-locale-params' });
90
+ continue;
91
+ }
79
92
  let updated = source;
80
93
  if (isZeroArg) {
81
94
  updated = insertLocaleParamsSignature(updated, localeParam);
@@ -1,5 +1,7 @@
1
1
  export declare function insertLocaleParamsSignature(sourceText: string, localeParam: string): string;
2
+ export declare function wrapSyncDefaultExportWithParams(sourceText: string, localeParam: string, existingParamsType?: string): string;
2
3
  export declare function addParamsPropToExistingDestructure(sourceText: string, localeParam: string): string;
3
4
  export declare function insertLocaleParamsBody(sourceText: string, localeParam: string, hasInlineDestructure: boolean): string;
5
+ export declare function extractParamsPromiseType(sourceText: string): string | null;
4
6
  export declare function ensureLocaleInParamsType(sourceText: string, localeParam: string): string;
5
7
  export declare function ensureSetLocaleImport(sourceText: string): string;
@@ -18,7 +18,134 @@ export function insertLocaleParamsSignature(sourceText, localeParam) {
18
18
  const replacement = `({ params }: {\n params: Promise<{ ${localeParam}: Language }>;\n})`;
19
19
  return sourceText.slice(0, parensStart) + replacement + sourceText.slice(parensEnd);
20
20
  }
21
+ const SYNC_DEFAULT_EXPORT_FUNCTION = /export\s+default\s+function\s+([A-Za-z_$][\w$]*)\s*\(/;
22
+ function findUnusedContentName(sourceText, baseName) {
23
+ let candidate = `${baseName}ContentCloudflareNextIntl`;
24
+ let suffix = 2;
25
+ while (new RegExp(`\\b${candidate}\\b`).test(sourceText)) {
26
+ candidate = `${baseName}ContentCloudflareNextIntl${suffix}`;
27
+ suffix += 1;
28
+ }
29
+ return candidate;
30
+ }
21
31
  const DEFAULT_EXPORT_FUNCTION_OPEN_PAREN = /export\s+default\s+(?:async\s+)?function\s+[A-Za-z_$][\w$]*\s*\(/;
32
+ function findParamListSpan(sourceText) {
33
+ const openParenMatch = DEFAULT_EXPORT_FUNCTION_OPEN_PAREN.exec(sourceText);
34
+ if (openParenMatch === null)
35
+ return null;
36
+ const start = openParenMatch.index + openParenMatch[0].length - 1;
37
+ let depth = 0;
38
+ for (let i = start; i < sourceText.length; i++) {
39
+ if (sourceText[i] === '(')
40
+ depth++;
41
+ else if (sourceText[i] === ')') {
42
+ depth--;
43
+ if (depth === 0)
44
+ return { start, end: i + 1 };
45
+ }
46
+ }
47
+ return null;
48
+ }
49
+ function destructuredKeyNames(inner) {
50
+ const names = [];
51
+ let depth = 0;
52
+ let start = 0;
53
+ const parts = [];
54
+ for (let i = 0; i <= inner.length; i++) {
55
+ const char = inner[i];
56
+ if (i === inner.length || (char === ',' && depth === 0)) {
57
+ parts.push(inner.slice(start, i));
58
+ start = i + 1;
59
+ continue;
60
+ }
61
+ if (char === '{' || char === '(' || char === '[')
62
+ depth++;
63
+ else if (char === '}' || char === ')' || char === ']')
64
+ depth--;
65
+ }
66
+ for (const part of parts) {
67
+ const trimmed = part.trim();
68
+ if (trimmed === '' || trimmed.startsWith('...'))
69
+ continue;
70
+ const key = trimmed.split(':')[0].split('=')[0].trim();
71
+ if (/^[A-Za-z_$][\w$]*$/.test(key))
72
+ names.push(key);
73
+ }
74
+ return names;
75
+ }
76
+ export function wrapSyncDefaultExportWithParams(sourceText, localeParam, existingParamsType) {
77
+ const nameMatch = SYNC_DEFAULT_EXPORT_FUNCTION.exec(sourceText);
78
+ if (nameMatch === null)
79
+ return sourceText;
80
+ const name = nameMatch[1];
81
+ const paramList = findParamListSpan(sourceText);
82
+ if (paramList === null)
83
+ return sourceText;
84
+ const bodyStart = findFunctionBodyStart(sourceText);
85
+ if (bodyStart === null)
86
+ return sourceText;
87
+ const bodyEnd = findMatchingBraceEnd(sourceText, bodyStart - 1);
88
+ if (bodyEnd === null)
89
+ return sourceText;
90
+ const contentName = findUnusedContentName(sourceText, name);
91
+ const originalParams = sourceText.slice(paramList.start, paramList.end);
92
+ const originalBody = sourceText.slice(bodyStart, bodyEnd - 1);
93
+ let forwardKeys = [];
94
+ let forwardKeyTypes = [];
95
+ if (originalParams !== '()') {
96
+ const openBrace = originalParams.indexOf('{');
97
+ if (openBrace === -1)
98
+ return sourceText;
99
+ const keysEnd = findMatchingBraceEnd(originalParams, openBrace);
100
+ if (keysEnd === null)
101
+ return sourceText;
102
+ forwardKeys = destructuredKeyNames(originalParams.slice(openBrace + 1, keysEnd - 1));
103
+ let j = keysEnd;
104
+ while (j < originalParams.length && /\s/.test(originalParams[j]))
105
+ j++;
106
+ if (originalParams[j] === ':') {
107
+ j++;
108
+ while (j < originalParams.length && /\s/.test(originalParams[j]))
109
+ j++;
110
+ if (originalParams[j] === '{') {
111
+ const typeEnd = findMatchingBraceEnd(originalParams, j);
112
+ if (typeEnd !== null) {
113
+ const typeBody = originalParams.slice(j + 1, typeEnd - 1).trim();
114
+ forwardKeyTypes = typeBody === '' ? [] : typeBody.split(';').map((s) => s.trim()).filter(Boolean);
115
+ }
116
+ }
117
+ }
118
+ }
119
+ const otherForwardKeys = forwardKeys.filter((key) => key !== 'params');
120
+ const otherForwardKeyTypes = forwardKeyTypes.filter((type) => !/^params\s*:/.test(type));
121
+ const contentFunction = `function ${contentName}${originalParams} {${originalBody}}`;
122
+ const alreadyHasLocaleParam = existingParamsType !== undefined && new RegExp(`\\b${localeParam}\\b`).test(existingParamsType);
123
+ const wrapperParamsType = existingParamsType === undefined
124
+ ? `${localeParam}: Language`
125
+ : alreadyHasLocaleParam
126
+ ? existingParamsType
127
+ : `${existingParamsType.replace(/;?\s*$/, '')}; ${localeParam}: Language`;
128
+ const wrapperSignature = otherForwardKeys.length === 0 ? `{ params }` : `{ ${otherForwardKeys.join(', ')}, params }`;
129
+ const forwardedJsx = forwardKeys.map((key) => (key === 'params' ? ` params={params}` : ` ${key}={${key}}`)).join('');
130
+ const forwardedTypeLines = otherForwardKeyTypes.map((type) => ` ${type.replace(/;?\s*$/, '')};`);
131
+ const wrapperFunction = [
132
+ `// Auto-inserted by cloudflare-next-intl's checkLocaleParams (mode: "fix") — `
133
+ + `"${name}" was split into this async wrapper plus the sync "${contentName}" above `
134
+ + `it (see that function's own body, unchanged) so \`await params\` never lands in a `
135
+ + `function this scan didn't confirm was safe to make async. To opt this file out on a `
136
+ + `later run, pass it in checkLocaleParams' \`skip\` list instead of hand-editing here.`,
137
+ `export default async function ${name}(${wrapperSignature}: {`,
138
+ ...forwardedTypeLines,
139
+ ` params: Promise<{ ${wrapperParamsType} }>;`,
140
+ `}) {`,
141
+ ` const { ${localeParam} } = await params;`,
142
+ ` setLocale(${localeParam});`,
143
+ ``,
144
+ ` return <${contentName}${forwardedJsx} />;`,
145
+ `}`,
146
+ ].join('\n');
147
+ return sourceText.slice(0, nameMatch.index) + contentFunction + '\n\n' + wrapperFunction + sourceText.slice(bodyEnd);
148
+ }
22
149
  function findMatchingBraceEnd(code, openBraceIndex) {
23
150
  let depth = 0;
24
151
  for (let i = openBraceIndex; i < code.length; i++) {
@@ -90,6 +217,9 @@ export function insertLocaleParamsBody(sourceText, localeParam, hasInlineDestruc
90
217
  return sourceText.slice(0, bodyStart) + line + sourceText.slice(bodyStart);
91
218
  }
92
219
  const PARAMS_PROMISE_TYPE = /params\s*:\s*Promise<\{([^}]*)\}>/;
220
+ export function extractParamsPromiseType(sourceText) {
221
+ return PARAMS_PROMISE_TYPE.exec(sourceText)?.[1]?.trim() ?? null;
222
+ }
93
223
  export function ensureLocaleInParamsType(sourceText, localeParam) {
94
224
  const match = PARAMS_PROMISE_TYPE.exec(sourceText);
95
225
  if (match === null)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cloudflare-next-intl",
3
- "version": "0.9.53",
3
+ "version": "0.9.54",
4
4
  "description": "Optimized Next Intl Package Special for App Router and Cloudflare",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",