Migrate 6 files from JavaScript to TypeScript (#57885) · github/docs@6d3f74a · GitHub
Skip to content

Commit 6d3f74a

Browse files
authored
Migrate 6 files from JavaScript to TypeScript (#57885)
1 parent 328c2f6 commit 6d3f74a

9 files changed

Lines changed: 251 additions & 107 deletions

File tree

src/fixtures/tests/spotlight-processing.js renamed to src/fixtures/tests/spotlight-processing.ts

Lines changed: 40 additions & 18 deletions

‎src/frame/middleware/context/whats-new-changelog.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default async function whatsNewChangelog(
1919
const changelogVersions = getApplicableVersions(req.context.page.changelog.versions)
2020

2121
// If the current version is not included, do not display a changelog.
22-
if (!changelogVersions.includes(req.context.currentVersion)) {
22+
if (!req.context.currentVersion || !changelogVersions.includes(req.context.currentVersion)) {
2323
return next()
2424
}
2525
}

‎src/learning-track/lib/process-learning-tracks.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export default async function processLearningTracks(
8282
const trackVersions = getApplicableVersions(track.versions)
8383

8484
// If the current version is not included, do not display the track.
85-
if (!trackVersions.includes(context.currentVersion)) {
85+
if (!context.currentVersion || !trackVersions.includes(context.currentVersion)) {
8686
continue
8787
}
8888
}
Lines changed: 66 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,33 @@ import {
66
getLocalizedGroupNames,
77
} from '@/products/lib/get-product-groups'
88

9+
// Mock data interface for tests - uses required name to match library expectations
10+
interface MockProductGroupData {
11+
name: string
12+
octicon?: string
13+
children: string[]
14+
}
15+
16+
// Mock data for testing edge cases with optional fields
17+
interface PartialProductGroupData {
18+
name?: string
19+
octicon?: string
20+
children: string[]
21+
}
22+
923
describe('get-product-groups helper functions', () => {
1024
describe('createOcticonToNameMap', () => {
1125
test('creates correct mapping from childGroups', () => {
12-
const mockChildGroups = [
26+
const mockChildGroups: MockProductGroupData[] = [
1327
{ name: 'Get started', octicon: 'RocketIcon', children: ['get-started'] },
1428
{ name: 'GitHub Copilot', octicon: 'CopilotIcon', children: ['copilot'] },
1529
{ name: 'Security', octicon: 'ShieldLockIcon', children: ['code-security'] },
1630
]
1731

18-
const octiconToName = createOcticonToNameMap(mockChildGroups)
32+
// Using any to cast mock data structure to match library's expected ProductGroupData type
33+
const octiconToName: { [key: string]: string } = createOcticonToNameMap(
34+
mockChildGroups as any,
35+
)
1936

2037
expect(octiconToName['RocketIcon']).toBe('Get started')
2138
expect(octiconToName['CopilotIcon']).toBe('GitHub Copilot')
@@ -24,14 +41,17 @@ describe('get-product-groups helper functions', () => {
2441
})
2542

2643
test('handles missing octicon or name gracefully', () => {
27-
const mockChildGroups = [
44+
const mockChildGroups: PartialProductGroupData[] = [
2845
{ name: 'Valid Group', octicon: 'RocketIcon', children: [] },
2946
{ octicon: 'MissingNameIcon', children: [] }, // missing name
3047
{ name: 'Missing Octicon', children: [] }, // missing octicon
3148
{ name: '', octicon: 'EmptyNameIcon', children: [] }, // empty name
3249
]
3350

34-
const octiconToName = createOcticonToNameMap(mockChildGroups)
51+
// Using any to test edge cases with partial/missing fields that wouldn't normally pass strict typing
52+
const octiconToName: { [key: string]: string } = createOcticonToNameMap(
53+
mockChildGroups as any,
54+
)
3555

3656
expect(octiconToName['RocketIcon']).toBe('Valid Group')
3757
expect(octiconToName['MissingNameIcon']).toBeUndefined()
@@ -42,19 +62,23 @@ describe('get-product-groups helper functions', () => {
4262

4363
describe('mapEnglishToLocalizedNames', () => {
4464
test('maps English names to localized names using octicon as key', () => {
45-
const englishGroups = [
65+
const englishGroups: MockProductGroupData[] = [
4666
{ name: 'Get started', octicon: 'RocketIcon', children: [] },
4767
{ name: 'Security', octicon: 'ShieldLockIcon', children: [] },
4868
{ name: 'GitHub Copilot', octicon: 'CopilotIcon', children: [] },
4969
]
5070

51-
const localizedByOcticon = {
71+
const localizedByOcticon: { [key: string]: string } = {
5272
RocketIcon: 'Empezar',
5373
ShieldLockIcon: 'Seguridad',
5474
CopilotIcon: 'GitHub Copilot', // Some names stay the same
5575
}
5676

57-
const nameMap = mapEnglishToLocalizedNames(englishGroups, localizedByOcticon)
77+
// Using any to cast mock data structure to match library's expected ProductGroupData type
78+
const nameMap: { [key: string]: string } = mapEnglishToLocalizedNames(
79+
englishGroups as any,
80+
localizedByOcticon,
81+
)
5882

5983
expect(nameMap['Get started']).toBe('Empezar')
6084
expect(nameMap['Security']).toBe('Seguridad')
@@ -63,18 +87,22 @@ describe('get-product-groups helper functions', () => {
6387
})
6488

6589
test('handles missing translations gracefully', () => {
66-
const englishGroups = [
90+
const englishGroups: MockProductGroupData[] = [
6791
{ name: 'Get started', octicon: 'RocketIcon', children: [] },
6892
{ name: 'Missing Translation', octicon: 'MissingIcon', children: [] },
6993
{ name: 'No Octicon', children: [] },
7094
]
7195

72-
const localizedByOcticon = {
96+
const localizedByOcticon: { [key: string]: string } = {
7397
RocketIcon: 'Empezar',
7498
// MissingIcon is not in the localized map
7599
}
76100

77-
const nameMap = mapEnglishToLocalizedNames(englishGroups, localizedByOcticon)
101+
// Using any to cast mock data structure to match library's expected ProductGroupData type
102+
const nameMap: { [key: string]: string } = mapEnglishToLocalizedNames(
103+
englishGroups as any,
104+
localizedByOcticon,
105+
)
78106

79107
expect(nameMap['Get started']).toBe('Empezar')
80108
expect(nameMap['Missing Translation']).toBeUndefined()
@@ -84,18 +112,22 @@ describe('get-product-groups helper functions', () => {
84112

85113
test('handles different ordering between English and localized groups', () => {
86114
// English groups in one order
87-
const englishGroups = [
115+
const englishGroups: MockProductGroupData[] = [
88116
{ name: 'Get started', octicon: 'RocketIcon', children: [] },
89117
{ name: 'Security', octicon: 'ShieldLockIcon', children: [] },
90118
]
91119

92120
// Localized groups in different order (but mapped by octicon)
93-
const localizedByOcticon = {
121+
const localizedByOcticon: { [key: string]: string } = {
94122
ShieldLockIcon: 'Seguridad', // Security comes first in localized
95123
RocketIcon: 'Empezar', // Get started comes second
96124
}
97125

98-
const nameMap = mapEnglishToLocalizedNames(englishGroups, localizedByOcticon)
126+
// Using any to cast mock data structure to match library's expected ProductGroupData type
127+
const nameMap: { [key: string]: string } = mapEnglishToLocalizedNames(
128+
englishGroups as any,
129+
localizedByOcticon,
130+
)
99131

100132
// Should correctly map regardless of order
101133
expect(nameMap['Get started']).toBe('Empezar')
@@ -105,17 +137,20 @@ describe('get-product-groups helper functions', () => {
105137

106138
describe('getLocalizedGroupNames integration', () => {
107139
test('returns empty object for English language', async () => {
108-
const result = await getLocalizedGroupNames('en')
140+
const result: { [key: string]: string } = await getLocalizedGroupNames('en')
109141
expect(result).toEqual({})
110142
})
111143

112144
test('returns empty object when no translation root available', () => {
113145
// Test the fallback when translation root is not found
114146
const lang = 'unknown-lang'
115-
const languages = { en: { dir: '/en' }, es: { dir: '/es' } }
147+
const languages: { [key: string]: { dir: string } } = {
148+
en: { dir: '/en' },
149+
es: { dir: '/es' },
150+
}
116151

117-
const translationRoot = languages[lang]?.dir
118-
const result = translationRoot
152+
const translationRoot: string | undefined = languages[lang]?.dir
153+
const result: { [key: string]: string } = translationRoot
119154
? {
120155
/* would proceed */
121156
}
@@ -126,7 +161,7 @@ describe('get-product-groups helper functions', () => {
126161

127162
test('handles file read errors gracefully', () => {
128163
// Test the try/catch behavior when file read fails
129-
let result
164+
let result: { [key: string]: string }
130165
try {
131166
// Simulate file read error
132167
throw new Error('File not found')
@@ -141,28 +176,35 @@ describe('get-product-groups helper functions', () => {
141176
describe('full translation pipeline', () => {
142177
test('complete flow from English groups to localized names', () => {
143178
// Simulate the complete flow
144-
const englishChildGroups = [
179+
const englishChildGroups: MockProductGroupData[] = [
145180
{ name: 'Get started', octicon: 'RocketIcon', children: ['get-started'] },
146181
{ name: 'Security', octicon: 'ShieldLockIcon', children: ['code-security'] },
147182
{ name: 'GitHub Copilot', octicon: 'CopilotIcon', children: ['copilot'] },
148183
]
149184

150185
// Simulate what would come from a Spanish localized file
151-
const mockLocalizedChildGroups = [
186+
const mockLocalizedChildGroups: MockProductGroupData[] = [
152187
{ name: 'Empezar', octicon: 'RocketIcon', children: ['get-started'] },
153188
{ name: 'Seguridad', octicon: 'ShieldLockIcon', children: ['code-security'] },
154189
{ name: 'GitHub Copilot', octicon: 'CopilotIcon', children: ['copilot'] },
155190
]
156191

157192
// Step 1: Create octicon -> localized name mapping
158-
const localizedByOcticon = createOcticonToNameMap(mockLocalizedChildGroups)
193+
// Using any to cast mock data structure to match library's expected ProductGroupData type
194+
const localizedByOcticon: { [key: string]: string } = createOcticonToNameMap(
195+
mockLocalizedChildGroups as any,
196+
)
159197

160198
// Step 2: Map English names to localized names
161-
const localizedNames = mapEnglishToLocalizedNames(englishChildGroups, localizedByOcticon)
199+
// Using any to cast mock data structure to match library's expected ProductGroupData type
200+
const localizedNames: { [key: string]: string } = mapEnglishToLocalizedNames(
201+
englishChildGroups as any,
202+
localizedByOcticon,
203+
)
162204

163205
// Step 3: Use in final mapping
164-
const finalResult = englishChildGroups.map((group) => {
165-
const localizedName = localizedNames[group.name] || group.name
206+
const finalResult = englishChildGroups.map((group: MockProductGroupData) => {
207+
const localizedName: string = localizedNames[group.name] || group.name
166208
return {
167209
name: localizedName,
168210
octicon: group.octicon,

‎src/secret-scanning/middleware/secret-scanning.ts‎

Lines changed: 1 addition & 1 deletion

0 commit comments

Comments
 (0)