Skip to content

Commit 822bdff

Browse files
authored
Convert 28 JavaScript files to TypeScript (#57753)
1 parent 140a3f7 commit 822bdff

30 files changed

+126
-88
lines changed

src/content-linter/lib/helpers/print-annotations.js renamed to src/content-linter/lib/helpers/print-annotations.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
*/
77

88
export function printAnnotationResults(
9-
results,
9+
// Using 'any' type as results structure is dynamic and comes from various linting tools with different formats
10+
results: any,
1011
{ skippableRules = [], skippableFlawProperties = [] } = {},
1112
) {
1213
for (const [file, flaws] of Object.entries(results)) {
13-
for (const flaw of flaws) {
14+
// Using 'any' type for flaws as they have varying structures depending on the linting rule
15+
for (const flaw of flaws as any) {
1416
if (intersection(flaw.ruleNames, skippableRules)) {
1517
continue
1618
}
@@ -52,6 +54,7 @@ export function printAnnotationResults(
5254
}
5355
}
5456

55-
function intersection(arr1, arr2) {
56-
return arr1.some((item) => arr2.includes(item))
57+
// Using 'any' types for generic array intersection utility function
58+
function intersection(arr1: any[], arr2: any[]) {
59+
return arr1.some((item: any) => arr2.includes(item))
5760
}

src/content-linter/lib/linting-rules/frontmatter-schema.js renamed to src/content-linter/lib/linting-rules/frontmatter-schema.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
1+
// @ts-ignore - markdownlint-rule-helpers doesn't have TypeScript declarations
12
import { addError } from 'markdownlint-rule-helpers'
23
import { intersection } from 'lodash-es'
34

45
import { getFrontmatter } from '../helpers/utils'
56
import { formatAjvErrors } from '../helpers/schema-utils'
67
import { frontmatter, deprecatedProperties } from '@/frame/lib/frontmatter'
78
import readFrontmatter from '@/frame/lib/read-frontmatter'
9+
import type { RuleParams, RuleErrorCallback, Rule } from '../../types'
810

9-
export const frontmatterSchema = {
11+
export const frontmatterSchema: Rule = {
1012
names: ['GHD012', 'frontmatter-schema'],
1113
description: 'Frontmatter must conform to the schema',
1214
tags: ['frontmatter', 'schema'],
13-
function: (params, onError) => {
15+
function: (params: RuleParams, onError: RuleErrorCallback) => {
1416
const fm = getFrontmatter(params.lines)
1517
if (!fm) return
1618

@@ -22,25 +24,25 @@ export const frontmatterSchema = {
2224
for (const key of deprecatedKeys) {
2325
// Early access articles are allowed to have deprecated properties
2426
if (params.name.includes('early-access')) continue
25-
const line = params.lines.find((line) => line.trim().startsWith(key))
26-
const lineNumber = params.lines.indexOf(line) + 1
27+
const line = params.lines.find((line: string) => line.trim().startsWith(key))
28+
const lineNumber = params.lines.indexOf(line!) + 1
2729
addError(
2830
onError,
2931
lineNumber,
3032
`The frontmatter property '${key}' is deprecated. Please remove the property from your article's frontmatter.`,
31-
line,
32-
[1, line.length],
33+
line!,
34+
[1, line!.length],
3335
null, // No fix possible
3436
)
3537
}
3638

3739
// Check that the frontmatter matches the schema
3840
const { errors } = readFrontmatter(params.lines.join('\n'), { schema: frontmatter.schema })
39-
const formattedErrors = formatAjvErrors(errors)
41+
const formattedErrors = formatAjvErrors(errors as any)
4042
for (const error of formattedErrors) {
4143
// If the missing property is at the top level, we don't have a line
4244
// to point to. In that case, the error will be added to line 1.
43-
const query = (line) => line.trim().startsWith(`${error.searchProperty}:`)
45+
const query = (line: string) => line.trim().startsWith(`${error.searchProperty}:`)
4446
const line = error.searchProperty === '' ? null : params.lines.find(query)
4547
const lineNumber = line ? params.lines.indexOf(line) + 1 : 1
4648
addError(

src/content-linter/lib/linting-rules/image-alt-text-exclude-start-words.js renamed to src/content-linter/lib/linting-rules/image-alt-text-exclude-start-words.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,30 @@
1+
// @ts-ignore - markdownlint-rule-helpers doesn't have TypeScript declarations
12
import { addError } from 'markdownlint-rule-helpers'
23

34
import { forEachInlineChild, getRange } from '../helpers/utils'
5+
import type { RuleParams, RuleErrorCallback, MarkdownToken, Rule } from '../../types'
46

57
const excludeStartWords = ['image', 'graphic']
68

79
/*
810
Images should have meaningful alternative text (alt text)
911
and should not begin with words like "image" or "graphic".
1012
*/
11-
export const imageAltTextExcludeStartWords = {
13+
export const imageAltTextExcludeStartWords: Rule = {
1214
names: ['GHD031', 'image-alt-text-exclude-words'],
1315
description: 'Alternate text for images should not begin with words like "image" or "graphic"',
1416
tags: ['accessibility', 'images'],
1517
parser: 'markdownit',
16-
function: (params, onError) => {
17-
forEachInlineChild(params, 'image', function forToken(token) {
18-
const imageAltText = token.content.trim()
19-
18+
function: (params: RuleParams, onError: RuleErrorCallback) => {
19+
forEachInlineChild(params, 'image', function forToken(token: MarkdownToken) {
2020
// If the alt text is empty, there is nothing to check and you can't
2121
// produce a valid range.
2222
// We can safely return early because the image-alt-text-length rule
2323
// will fail this one.
2424
if (!token.content) return
2525

26+
const imageAltText = token.content.trim()
27+
2628
const range = getRange(token.line, imageAltText)
2729
if (
2830
excludeStartWords.some((excludeWord) => imageAltText.toLowerCase().startsWith(excludeWord))

src/content-linter/lib/linting-rules/internal-links-no-lang.js renamed to src/content-linter/lib/linting-rules/internal-links-no-lang.ts

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
1+
// @ts-ignore - markdownlint-rule-helpers doesn't have TypeScript declarations
12
import { filterTokens } from 'markdownlint-rule-helpers'
23

34
import { addFixErrorDetail, getRange } from '../helpers/utils'
45
import { allLanguageKeys } from '@/languages/lib/languages'
6+
import type { RuleParams, RuleErrorCallback, Rule } from '../../types'
57

6-
export const internalLinksNoLang = {
8+
export const internalLinksNoLang: Rule = {
79
names: ['GHD002', 'internal-links-no-lang'],
810
description: 'Internal links must not have a hardcoded language code',
911
tags: ['links', 'url'],
1012
parser: 'markdownit',
11-
function: (params, onError) => {
12-
filterTokens(params, 'inline', (token) => {
13+
function: (params: RuleParams, onError: RuleErrorCallback) => {
14+
// Using 'any' type for token as markdownlint-rule-helpers doesn't provide TypeScript types
15+
filterTokens(params, 'inline', (token: any) => {
1316
for (const child of token.children) {
1417
if (child.type !== 'link_open') continue
1518

@@ -18,14 +21,17 @@ export const internalLinksNoLang = {
1821
// ['href', 'get-started'], ['target', '_blank'],
1922
// ['rel', 'canonical'],
2023
// ]
24+
// Attribute arrays are tuples of [attributeName, attributeValue] from markdownit parser
2125
const hrefsMissingSlashes = child.attrs
2226
// The attribute could also be `target` or `rel`
23-
.filter((attr) => attr[0] === 'href')
24-
.filter((attr) => attr[1].startsWith('/') || !attr[1].startsWith('//'))
27+
.filter((attr: [string, string]) => attr[0] === 'href')
28+
.filter((attr: [string, string]) => attr[1].startsWith('/') || !attr[1].startsWith('//'))
2529
// Filter out link paths that start with language code
26-
.filter((attr) => allLanguageKeys.some((lang) => attr[1].split('/')[1] === lang))
30+
.filter((attr: [string, string]) =>
31+
allLanguageKeys.some((lang) => attr[1].split('/')[1] === lang),
32+
)
2733
// Get the link path from the attribute
28-
.map((attr) => attr[1])
34+
.map((attr: [string, string]) => attr[1])
2935
// Create errors for each link path that includes a language code
3036
for (const linkPath of hrefsMissingSlashes) {
3137
const range = getRange(child.line, linkPath)

src/content-linter/lib/linting-rules/internal-links-slash.js renamed to src/content-linter/lib/linting-rules/internal-links-slash.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1+
// @ts-ignore - markdownlint-rule-helpers doesn't have TypeScript declarations
12
import { filterTokens } from 'markdownlint-rule-helpers'
23

34
import { addFixErrorDetail, getRange } from '../helpers/utils'
5+
import type { RuleParams, RuleErrorCallback, Rule } from '../../types'
46

5-
export const internalLinksSlash = {
7+
export const internalLinksSlash: Rule = {
68
names: ['GHD003', 'internal-links-slash'],
79
description: 'Internal links must start with a /',
810
tags: ['links', 'url'],
911
parser: 'markdownit',
10-
function: (params, onError) => {
11-
filterTokens(params, 'inline', (token) => {
12+
function: (params: RuleParams, onError: RuleErrorCallback) => {
13+
// Using 'any' type for token as markdownlint-rule-helpers doesn't provide TypeScript types
14+
filterTokens(params, 'inline', (token: any) => {
1215
for (const child of token.children) {
1316
if (child.type !== 'link_open') continue
1417

@@ -17,20 +20,21 @@ export const internalLinksSlash = {
1720
// ['href', '/get-started'], ['target', '_blank'],
1821
// ['rel', 'canonical'],
1922
// ]
23+
// Attribute arrays are tuples of [attributeName, attributeValue] from markdownit parser
2024
const hrefsMissingSlashes = child.attrs
2125
// The attribute could also be `target` or `rel`
22-
.filter((attr) => attr[0] === 'href')
26+
.filter((attr: [string, string]) => attr[0] === 'href')
2327
// Filter out prefixes we don't want to check
2428
.filter(
25-
(attr) =>
29+
(attr: [string, string]) =>
2630
!['http', 'mailto', '#', '/'].some((ignorePrefix) =>
2731
attr[1].startsWith(ignorePrefix),
2832
),
2933
)
3034
// We can ignore empty links because MD042 from markdownlint catches empty links
31-
.filter((attr) => attr[1] !== '')
35+
.filter((attr: [string, string]) => attr[1] !== '')
3236
// Get the link path from the attribute
33-
.map((attr) => attr[1])
37+
.map((attr: [string, string]) => attr[1])
3438

3539
// Create errors for each link path that doesn't start with a /
3640
for (const linkPath of hrefsMissingSlashes) {

src/content-linter/lib/linting-rules/link-punctuation.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// @ts-ignore - markdownlint-rule-helpers doesn't have TypeScript declarations
22
import { addError, filterTokens } from 'markdownlint-rule-helpers'
3-
import type { RuleParams, RuleErrorCallback } from '../../types'
3+
import type { RuleParams, RuleErrorCallback, Rule } from '../../types'
44

55
import { doesStringEndWithPeriod, getRange, isStringQuoted } from '../helpers/utils'
66

7-
export const linkPunctuation = {
7+
export const linkPunctuation: Rule = {
88
names: ['GHD001', 'link-punctuation'],
99
description: 'Internal link titles must not contain punctuation',
1010
tags: ['links', 'url'],
1111
parser: 'markdownit',
1212
function: (params: RuleParams, onError: RuleErrorCallback) => {
13+
// Using 'any' type for token as markdownlint-rule-helpers doesn't provide TypeScript types
1314
filterTokens(params, 'inline', (token: any) => {
1415
const { children, line } = token
1516
let inLink = false

src/content-linter/lib/linting-rules/liquid-quoted-conditional-arg.js renamed to src/content-linter/lib/linting-rules/liquid-quoted-conditional-arg.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { TokenKind } from 'liquidjs'
2+
// @ts-ignore - markdownlint-rule-helpers doesn't have TypeScript declarations
23
import { addError } from 'markdownlint-rule-helpers'
34

45
import { getLiquidTokens, conditionalTags, getPositionData } from '../helpers/liquid-utils'
56
import { isStringQuoted } from '../helpers/utils'
7+
import type { RuleParams, RuleErrorCallback, Rule } from '../../types'
68

79
/*
810
Checks for instances where a Liquid conditional tag's argument is
@@ -12,18 +14,20 @@ import { isStringQuoted } from '../helpers/utils'
1214
{% if "foo" %}
1315
{% ifversion "bar" %}
1416
*/
15-
export const liquidQuotedConditionalArg = {
17+
export const liquidQuotedConditionalArg: Rule = {
1618
names: ['GHD016', 'liquid-quoted-conditional-arg'],
1719
description: 'Liquid conditional tags should not quote the conditional argument',
1820
tags: ['liquid', 'format'],
19-
function: (params, onError) => {
21+
function: (params: RuleParams, onError: RuleErrorCallback) => {
2022
const content = params.lines.join('\n')
23+
// Using 'any' type for tokens as getLiquidTokens returns tokens from liquid-utils.js which lacks type definitions
2124
const tokens = getLiquidTokens(content)
22-
.filter((token) => token.kind === TokenKind.Tag)
23-
.filter((token) => conditionalTags.includes(token.name))
24-
.filter((token) => {
25+
.filter((token: any) => token.kind === TokenKind.Tag)
26+
.filter((token: any) => conditionalTags.includes(token.name))
27+
.filter((token: any) => {
2528
const tokensArray = token.args.split(/\s+/g)
26-
if (tokensArray.some((arg) => isStringQuoted(arg))) return true
29+
// Using 'any' for args as they come from the untyped liquid token structure
30+
if (tokensArray.some((arg: any) => isStringQuoted(arg))) return true
2731
return false
2832
})
2933

src/content-linter/lib/linting-rules/octicon-aria-labels.js renamed to src/content-linter/lib/linting-rules/octicon-aria-labels.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TokenKind } from 'liquidjs'
22
import { getLiquidTokens, getPositionData } from '../helpers/liquid-utils'
33
import { addFixErrorDetail } from '../helpers/utils'
4+
import type { RuleParams, RuleErrorCallback, Rule } from '../../types'
45
/*
56
Octicons should always have an aria-label attribute even if aria hidden. For example:
67
@@ -9,20 +10,21 @@ Octicons should always have an aria-label attribute even if aria hidden. For exa
910
{% octicon "alert" aria-label="alert" aria-hidden="true" %}
1011
{% octicon "alert" aria-label="alert" aria-hidden="true" class="foo" %}
1112
12-
This is necessary for copilot to be able to recognize the svgs correctly when using our API.
13+
This is necessary for copilot to be able to recognize the svgs correctly when using our API.
1314
1415
*/
1516

16-
export const octiconAriaLabels = {
17+
export const octiconAriaLabels: Rule = {
1718
names: ['GHD044', 'octicon-aria-labels'],
1819
description: 'Octicons should always have an aria-label attribute even if aria-hidden.',
1920
tags: ['accessibility', 'octicons'],
2021
parser: 'markdownit',
21-
function: (params, onError) => {
22+
function: (params: RuleParams, onError: RuleErrorCallback) => {
2223
const content = params.lines.join('\n')
24+
// Using 'any' type for tokens as getLiquidTokens returns tokens from liquid-utils.js which lacks type definitions
2325
const tokens = getLiquidTokens(content)
24-
.filter((token) => token.kind === TokenKind.Tag)
25-
.filter((token) => token.name === 'octicon')
26+
.filter((token: any) => token.kind === TokenKind.Tag)
27+
.filter((token: any) => token.name === 'octicon')
2628

2729
for (const token of tokens) {
2830
const { lineNumber, column, length } = getPositionData(token, params.lines)

src/content-linter/tests/unit/early-access-references.js renamed to src/content-linter/tests/unit/early-access-references.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe(frontmatterEarlyAccessReferences.names.join(' - '), () => {
4141
expect(lineNumbers.includes(4)).toBe(false)
4242
expect(lineNumbers.includes(5)).toBe(false)
4343
expect(errors[0].errorRange).toEqual([8, 12])
44-
expect(errors[1].errorRange).toEqual([15, 12], [28, 12])
44+
expect(errors[1].errorRange).toEqual([15, 12])
4545
})
4646
test('early access file with early access references passes', async () => {
4747
const result = await runRule(frontmatterEarlyAccessReferences, {

0 commit comments

Comments
 (0)