Skip to content

Commit 6a5f201

Browse files
authored
Convert remaining src/languages scripts and tests to TypeScript (#56573)
1 parent c2ff56c commit 6a5f201

File tree

6 files changed

+21
-19
lines changed

6 files changed

+21
-19
lines changed

src/languages/scripts/purge-fastly-edge-cache-per-language.js renamed to src/languages/scripts/purge-fastly-edge-cache-per-language.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import purgeEdgeCache from '@/workflows/purge-edge-cache'
2020
*/
2121
const DELAY_BETWEEN_LANGUAGES = 10 * 1000
2222

23-
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
23+
const sleep = (ms: number): Promise<void> => new Promise((resolve) => setTimeout(resolve, ms))
2424

2525
// This covers things like `/api/webhooks` which isn't language specific.
2626
await purgeEdgeCache(makeLanguageSurrogateKey())
@@ -40,7 +40,7 @@ for (const language of languages) {
4040
await purgeEdgeCache(makeLanguageSurrogateKey(language))
4141
}
4242

43-
function languagesFromString(str) {
43+
function languagesFromString(str: string): string[] {
4444
const languages = str
4545
.split(/,/)
4646
.map((x) => x.trim())

src/languages/tests/translation-error-comments.js renamed to src/languages/tests/translation-error-comments.ts

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, test, vi, beforeEach, afterEach } from 'vitest'
1+
import { describe, expect, test, vi, beforeEach, afterEach, type MockedFunction } from 'vitest'
22
import {
33
createTranslationFallbackComment,
44
EmptyTitleError,
@@ -10,7 +10,7 @@ import Page from '@/frame/lib/page'
1010

1111
describe('Translation Error Comments', () => {
1212
// Mock renderContent for integration tests
13-
let mockRenderContent
13+
let mockRenderContent: MockedFunction<(template: string, context: any) => string>
1414

1515
beforeEach(() => {
1616
mockRenderContent = vi.fn()
@@ -26,7 +26,7 @@ describe('Translation Error Comments', () => {
2626
test('includes all fields when token information is available', () => {
2727
const error = new Error("Unknown tag 'badtag', line:1, col:3")
2828
error.name = 'ParseError'
29-
error.token = {
29+
;(error as any).token = {
3030
file: '/content/test/article.md',
3131
getPosition: () => [1, 3],
3232
}
@@ -48,11 +48,11 @@ describe('Translation Error Comments', () => {
4848
test('includes original error message when available', () => {
4949
const error = new Error("Unknown variable 'variables.nonexistent.value'")
5050
error.name = 'RenderError'
51-
error.token = {
51+
;(error as any).token = {
5252
file: '/content/test/intro.md',
5353
getPosition: () => [3, 15],
5454
}
55-
error.originalError = new Error('Variable not found: variables.nonexistent.value')
55+
;(error as any).originalError = new Error('Variable not found: variables.nonexistent.value')
5656

5757
const result = createTranslationFallbackComment(error, 'rawIntro')
5858

@@ -67,7 +67,7 @@ describe('Translation Error Comments', () => {
6767
test('falls back to main error message when no originalError', () => {
6868
const error = new Error('Main error message')
6969
error.name = 'RenderError'
70-
error.token = {
70+
;(error as any).token = {
7171
file: '/content/test.md',
7272
getPosition: () => [1, 1],
7373
}
@@ -82,7 +82,7 @@ describe('Translation Error Comments', () => {
8282
test('includes tokenization error details', () => {
8383
const error = new Error('Unexpected token, line:1, col:10')
8484
error.name = 'TokenizationError'
85-
error.token = {
85+
;(error as any).token = {
8686
file: '/content/test/page.md',
8787
getPosition: () => [1, 10],
8888
}
@@ -152,7 +152,7 @@ describe('Translation Error Comments', () => {
152152
test('handles error with token but no file', () => {
153153
const error = new Error('Error message')
154154
error.name = 'ParseError'
155-
error.token = {
155+
;(error as any).token = {
156156
// No file property
157157
getPosition: () => [5, 10],
158158
}
@@ -167,7 +167,7 @@ describe('Translation Error Comments', () => {
167167
test('handles error with token but no getPosition method', () => {
168168
const error = new Error('Error message')
169169
error.name = 'ParseError'
170-
error.token = {
170+
;(error as any).token = {
171171
file: '/content/test.md',
172172
// No getPosition method
173173
}
@@ -192,7 +192,9 @@ describe('Translation Error Comments', () => {
192192
// Extract the message part to verify truncation
193193
const msgMatch = result.match(/msg="([^"]*)"/)
194194
expect(msgMatch).toBeTruthy()
195-
expect(msgMatch[1].length).toBeLessThanOrEqual(203) // 200 + '...'
195+
if (msgMatch?.[1]) {
196+
expect(msgMatch[1].length).toBeLessThanOrEqual(203) // 200 + '...'
197+
}
196198
})
197199

198200
test('properly escapes quotes in error messages', () => {
@@ -244,7 +246,7 @@ describe('Translation Error Comments', () => {
244246
test('comment format is valid HTML', () => {
245247
const error = new Error('Test error')
246248
error.name = 'ParseError'
247-
error.token = {
249+
;(error as any).token = {
248250
file: '/content/test.md',
249251
getPosition: () => [1, 1],
250252
}
@@ -262,7 +264,7 @@ describe('Translation Error Comments', () => {
262264
test('contains all required fields when available', () => {
263265
const error = new Error('Detailed error message')
264266
error.name = 'RenderError'
265-
error.token = {
267+
;(error as any).token = {
266268
file: '/content/detailed-test.md',
267269
getPosition: () => [42, 15],
268270
}
@@ -281,7 +283,7 @@ describe('Translation Error Comments', () => {
281283
test('maintains consistent field order', () => {
282284
const error = new Error('Test message')
283285
error.name = 'ParseError'
284-
error.token = {
286+
;(error as any).token = {
285287
file: '/content/test.md',
286288
getPosition: () => [1, 1],
287289
}
@@ -318,11 +320,11 @@ describe('Translation Error Comments', () => {
318320
}
319321

320322
// Mock renderContent to simulate error for Japanese, success for English
321-
mockRenderContent.mockImplementation((template, context) => {
323+
mockRenderContent.mockImplementation((template: string, context: any) => {
322324
if (context.currentLanguage !== 'en' && template.includes('badtag')) {
323325
const error = new Error("Unknown tag 'badtag'")
324326
error.name = 'ParseError'
325-
error.token = {
327+
;(error as any).token = {
326328
file: '/content/test.md',
327329
getPosition: () => [1, 5],
328330
}
@@ -355,7 +357,7 @@ describe('Translation Error Comments', () => {
355357
},
356358
}
357359

358-
mockRenderContent.mockImplementation((template, context) => {
360+
mockRenderContent.mockImplementation((template: string, context: any) => {
359361
if (context.currentLanguage !== 'en' && template.includes('badtag')) {
360362
const error = new Error("Unknown tag 'badtag'")
361363
error.name = 'ParseError'
@@ -382,7 +384,7 @@ describe('Translation Error Comments', () => {
382384
const failingCallable = async () => {
383385
const error = new Error("Unknown variable 'variables.bad'")
384386
error.name = 'RenderError'
385-
error.token = {
387+
;(error as any).token = {
386388
file: '/content/article.md',
387389
getPosition: () => [10, 20],
388390
}

0 commit comments

Comments
 (0)