Skip to content

Commit a415892

Browse files
authored
Migrate 6 JavaScript files to TypeScript (#57969)
1 parent 5268d1a commit a415892

File tree

12 files changed

+152
-106
lines changed

12 files changed

+152
-106
lines changed

src/content-linter/tests/unit/lint-report-exclusions.js renamed to src/content-linter/tests/unit/lint-report-exclusions.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@ import { describe, expect, test } from 'vitest'
22
import { shouldIncludeResult } from '../../lib/helpers/should-include-result'
33
import { reportingConfig } from '../../style/github-docs'
44

5+
interface LintFlaw {
6+
severity: string
7+
ruleNames: string[]
8+
errorDetail?: string
9+
}
10+
511
describe('lint report exclusions', () => {
612
// Helper function to simulate the reporting logic from lint-report.ts
7-
function shouldIncludeInReport(flaw, filePath) {
13+
function shouldIncludeInReport(flaw: LintFlaw, filePath: string): boolean {
814
if (!flaw.ruleNames || !Array.isArray(flaw.ruleNames)) {
915
return false
1016
}

src/content-linter/tests/unit/octicon-aria-labels.js renamed to src/content-linter/tests/unit/octicon-aria-labels.ts

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
import { describe, expect, test } from 'vitest'
22
import { octiconAriaLabels } from '../../lib/linting-rules/octicon-aria-labels'
33

4+
interface ErrorInfo {
5+
lineNumber: number
6+
detail?: string
7+
context?: string
8+
range?: [number, number]
9+
fixInfo?: any // Matches RuleErrorCallback signature - fixInfo structure varies by rule
10+
}
11+
412
describe('octicon-aria-labels', () => {
513
const rule = octiconAriaLabels
614

7-
test('detects octicon without aria-label', () => {
8-
const errors = []
9-
const onError = (errorInfo) => {
15+
// Helper to create onError callback that captures errors
16+
function createErrorCollector() {
17+
const errors: ErrorInfo[] = []
18+
// Using any because the actual rule implementation calls onError with an object,
19+
// not individual parameters as defined in RuleErrorCallback
20+
const onError = (errorInfo: any) => {
1021
errors.push(errorInfo)
1122
}
23+
return { errors, onError }
24+
}
25+
26+
test('detects octicon without aria-label', () => {
27+
const { errors, onError } = createErrorCollector()
1228

1329
const content = ['This is a test with an octicon:', '{% octicon "alert" %}', 'Some more text.']
1430

15-
rule.function({ lines: content }, onError)
31+
rule.function({ name: 'test.md', lines: content, frontMatterLines: [] }, onError)
1632

1733
expect(errors.length).toBe(1)
1834
expect(errors[0].lineNumber).toBe(2)
@@ -21,27 +37,21 @@ describe('octicon-aria-labels', () => {
2137
})
2238

2339
test('ignores octicons with aria-label', () => {
24-
const errors = []
25-
const onError = (errorInfo) => {
26-
errors.push(errorInfo)
27-
}
40+
const { errors, onError } = createErrorCollector()
2841

2942
const content = [
3043
'This is a test with a proper octicon:',
3144
'{% octicon "alert" aria-label="alert" %}',
3245
'Some more text.',
3346
]
3447

35-
rule.function({ lines: content }, onError)
48+
rule.function({ name: 'test.md', lines: content, frontMatterLines: [] }, onError)
3649

3750
expect(errors.length).toBe(0)
3851
})
3952

4053
test('detects multiple octicons without aria-label', () => {
41-
const errors = []
42-
const onError = (errorInfo) => {
43-
errors.push(errorInfo)
44-
}
54+
const { errors, onError } = createErrorCollector()
4555

4656
const content = [
4757
'This is a test with multiple octicons:',
@@ -51,7 +61,7 @@ describe('octicon-aria-labels', () => {
5161
'More text.',
5262
]
5363

54-
rule.function({ lines: content }, onError)
64+
rule.function({ name: 'test.md', lines: content, frontMatterLines: [] }, onError)
5565

5666
expect(errors.length).toBe(2)
5767
expect(errors[0].lineNumber).toBe(2)
@@ -61,10 +71,7 @@ describe('octicon-aria-labels', () => {
6171
})
6272

6373
test('ignores non-octicon liquid tags', () => {
64-
const errors = []
65-
const onError = (errorInfo) => {
66-
errors.push(errorInfo)
67-
}
74+
const { errors, onError } = createErrorCollector()
6875

6976
const content = [
7077
'This is a test with non-octicon tags:',
@@ -74,24 +81,21 @@ describe('octicon-aria-labels', () => {
7481
'{% endif %}',
7582
]
7683

77-
rule.function({ lines: content }, onError)
84+
rule.function({ name: 'test.md', lines: content, frontMatterLines: [] }, onError)
7885

7986
expect(errors.length).toBe(0)
8087
})
8188

8289
test('suggests correct fix for octicon with other attributes', () => {
83-
const errors = []
84-
const onError = (errorInfo) => {
85-
errors.push(errorInfo)
86-
}
90+
const { errors, onError } = createErrorCollector()
8791

8892
const content = [
8993
'This is a test with an octicon with other attributes:',
9094
'{% octicon "plus" aria-hidden="true" class="foo" %}',
9195
'Some more text.',
9296
]
9397

94-
rule.function({ lines: content }, onError)
98+
rule.function({ name: 'test.md', lines: content, frontMatterLines: [] }, onError)
9599

96100
expect(errors.length).toBe(1)
97101
expect(errors[0].lineNumber).toBe(2)
@@ -101,29 +105,23 @@ describe('octicon-aria-labels', () => {
101105
})
102106

103107
test('handles octicons with unusual spacing', () => {
104-
const errors = []
105-
const onError = (errorInfo) => {
106-
errors.push(errorInfo)
107-
}
108+
const { errors, onError } = createErrorCollector()
108109

109110
const content = [
110111
'This is a test with unusual spacing:',
111112
'{% octicon "x" %}',
112113
'Some more text.',
113114
]
114115

115-
rule.function({ lines: content }, onError)
116+
rule.function({ name: 'test.md', lines: content, frontMatterLines: [] }, onError)
116117

117118
expect(errors.length).toBe(1)
118119
expect(errors[0].lineNumber).toBe(2)
119120
expect(errors[0].detail).toContain('aria-label=x')
120121
})
121122

122123
test('handles octicons split across multiple lines', () => {
123-
const errors = []
124-
const onError = (errorInfo) => {
125-
errors.push(errorInfo)
126-
}
124+
const { errors, onError } = createErrorCollector()
127125

128126
const content = [
129127
'This is a test with a multi-line octicon:',
@@ -133,25 +131,22 @@ describe('octicon-aria-labels', () => {
133131
'Some more text.',
134132
]
135133

136-
rule.function({ lines: content }, onError)
134+
rule.function({ name: 'test.md', lines: content, frontMatterLines: [] }, onError)
137135

138136
expect(errors.length).toBe(1)
139137
expect(errors[0].detail).toContain('aria-label=chevron-down')
140138
})
141139

142140
test('falls back to "icon" when octicon name cannot be determined', () => {
143-
const errors = []
144-
const onError = (errorInfo) => {
145-
errors.push(errorInfo)
146-
}
141+
const { errors, onError } = createErrorCollector()
147142

148143
const content = [
149144
'This is a test with a malformed octicon:',
150145
'{% octicon variable %}',
151146
'Some more text.',
152147
]
153148

154-
rule.function({ lines: content }, onError)
149+
rule.function({ name: 'test.md', lines: content, frontMatterLines: [] }, onError)
155150

156151
expect(errors.length).toBe(1)
157152
expect(errors[0].detail).toContain('aria-label=icon')

src/content-render/tests/link-error-line-numbers.js renamed to src/content-render/tests/link-error-line-numbers.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { describe, expect, test, beforeEach, afterEach } from 'vitest'
22
import { renderContent } from '@/content-render/index'
33
import { TitleFromAutotitleError } from '@/content-render/unified/rewrite-local-links'
4+
import type { Context } from '@/types'
45

56
describe('link error line numbers', () => {
6-
let fs
7-
let originalReadFileSync
8-
let originalExistsSync
9-
let mockContext
7+
let fs: any // Dynamic import of fs module for mocking in tests
8+
let originalReadFileSync: any // Storing original fs.readFileSync for restoration after test
9+
let originalExistsSync: any // Storing original fs.existsSync for restoration after test
10+
let mockContext: Context
1011

1112
beforeEach(async () => {
1213
// Set up file system mocking
@@ -20,11 +21,11 @@ describe('link error line numbers', () => {
2021
mockContext = {
2122
currentLanguage: 'en',
2223
currentVersion: 'free-pro-team@latest',
23-
pages: new Map(),
24-
redirects: new Map(),
24+
pages: {} as any,
25+
redirects: {} as any,
2526
page: {
2627
fullPath: '/fake/test-file.md',
27-
},
28+
} as any,
2829
}
2930
})
3031

@@ -60,14 +61,16 @@ More content here.`
6061
// The broken link is on line 10 in the original file
6162
// (3 lines of frontmatter + 1 blank line + 1 title + 1 blank + 1 content + 1 blank + 1 link line)
6263
// The error message should reference the correct line number
63-
expect(error.message).toContain('/nonexistent/page')
64-
expect(error.message).toContain('could not be resolved')
65-
expect(error.message).toContain('(Line: 10)')
64+
expect((error as TitleFromAutotitleError).message).toContain('/nonexistent/page')
65+
expect((error as TitleFromAutotitleError).message).toContain('could not be resolved')
66+
expect((error as TitleFromAutotitleError).message).toContain('(Line: 10)')
6667
}
6768
})
6869

6970
test('reports correct line numbers with different frontmatter sizes', async () => {
70-
mockContext.page.fullPath = '/fake/test-file-2.md'
71+
mockContext.page = {
72+
fullPath: '/fake/test-file-2.md',
73+
} as any
7174

7275
// Test with more extensive frontmatter
7376
const template = `---
@@ -96,13 +99,15 @@ Content with a [AUTOTITLE](/another/nonexistent/page) link.`
9699
expect.fail('Expected TitleFromAutotitleError to be thrown')
97100
} catch (error) {
98101
expect(error).toBeInstanceOf(TitleFromAutotitleError)
99-
expect(error.message).toContain('/another/nonexistent/page')
100-
expect(error.message).toContain('could not be resolved')
102+
expect((error as TitleFromAutotitleError).message).toContain('/another/nonexistent/page')
103+
expect((error as TitleFromAutotitleError).message).toContain('could not be resolved')
101104
}
102105
})
103106

104107
test('handles files without frontmatter correctly', async () => {
105-
mockContext.page.fullPath = '/fake/no-frontmatter.md'
108+
mockContext.page = {
109+
fullPath: '/fake/no-frontmatter.md',
110+
} as any
106111

107112
// Test content without frontmatter
108113
const template = `# Simple Title
@@ -118,13 +123,15 @@ Here is a broken link: [AUTOTITLE](/missing/page).`
118123
expect.fail('Expected TitleFromAutotitleError to be thrown')
119124
} catch (error) {
120125
expect(error).toBeInstanceOf(TitleFromAutotitleError)
121-
expect(error.message).toContain('/missing/page')
122-
expect(error.message).toContain('could not be resolved')
126+
expect((error as TitleFromAutotitleError).message).toContain('/missing/page')
127+
expect((error as TitleFromAutotitleError).message).toContain('could not be resolved')
123128
}
124129
})
125130

126131
test('error message format is improved', async () => {
127-
mockContext.page.fullPath = '/fake/message-test.md'
132+
mockContext.page = {
133+
fullPath: '/fake/message-test.md',
134+
} as any
128135

129136
const template = `---
130137
title: Message Test
@@ -142,13 +149,17 @@ title: Message Test
142149
expect(error).toBeInstanceOf(TitleFromAutotitleError)
143150

144151
// Check that the new error message format is used
145-
expect(error.message).toContain('could not be resolved in one or more versions')
146-
expect(error.message).toContain('Make sure that this link can be reached from all versions')
147-
expect(error.message).toContain('/test/broken/link')
152+
expect((error as TitleFromAutotitleError).message).toContain(
153+
'could not be resolved in one or more versions',
154+
)
155+
expect((error as TitleFromAutotitleError).message).toContain(
156+
'Make sure that this link can be reached from all versions',
157+
)
158+
expect((error as TitleFromAutotitleError).message).toContain('/test/broken/link')
148159

149160
// Check that the old error message format is NOT used
150-
expect(error.message).not.toContain('Unable to find Page by')
151-
expect(error.message).not.toContain('To fix it, look at')
161+
expect((error as TitleFromAutotitleError).message).not.toContain('Unable to find Page by')
162+
expect((error as TitleFromAutotitleError).message).not.toContain('To fix it, look at')
152163
}
153164
})
154165
})

0 commit comments

Comments
 (0)