Skip to content

Commit 90c6940

Browse files
heiskrCopilot
andauthored
Remove any type from 11 legacy exception files (#60116)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 9021845 commit 90c6940

File tree

14 files changed

+150
-96
lines changed

14 files changed

+150
-96
lines changed

eslint.config.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,13 +204,9 @@ export default [
204204
'src/article-api/transformers/audit-logs-transformer.ts',
205205
'src/article-api/transformers/rest-transformer.ts',
206206
'src/codeql-cli/scripts/convert-markdown-for-docs.ts',
207-
'src/content-linter/lib/helpers/get-lintable-yml.ts',
208-
'src/content-linter/lib/helpers/print-annotations.ts',
209-
'src/content-linter/lib/helpers/utils.ts',
210207
'src/content-linter/lib/init-test.ts',
211208
'src/content-linter/lib/linting-rules/code-annotations.ts',
212209
'src/content-linter/lib/linting-rules/index.ts',
213-
'src/content-linter/lib/linting-rules/internal-links-no-lang.ts',
214210
'src/content-linter/lib/linting-rules/journey-tracks-liquid.ts',
215211
'src/content-linter/lib/linting-rules/liquid-ifversion-versions.ts',
216212
'src/content-linter/lib/linting-rules/liquid-versioning.ts',
@@ -253,8 +249,6 @@ export default [
253249
'src/frame/lib/page-data.ts',
254250
'src/frame/lib/page.ts',
255251
'src/frame/lib/read-frontmatter.ts',
256-
'src/frame/middleware/find-page.ts',
257-
'src/frame/middleware/resolve-carousels.ts',
258252
'src/frame/tests/page.ts',
259253
'src/frame/tests/read-frontmatter.ts',
260254
'src/frame/tests/server.ts',
@@ -278,7 +272,6 @@ export default [
278272
'src/links/lib/update-internal-links.ts',
279273
'src/links/scripts/check-github-github-links.ts',
280274
'src/links/scripts/update-internal-links.ts',
281-
'src/pages/_error.tsx',
282275
'src/redirects/middleware/handle-redirects.ts',
283276
'src/rest/components/get-rest-code-samples.ts',
284277
'src/rest/lib/index.ts',
@@ -302,8 +295,6 @@ export default [
302295
'src/search/lib/get-elasticsearch-results/general-search.ts',
303296
'src/search/lib/routes/combined-search-route.ts',
304297
'src/search/lib/search-request-params/get-search-from-request-params.ts',
305-
'src/search/lib/search-request-params/search-params-objects.ts',
306-
'src/search/lib/search-request-params/types.ts',
307298
'src/search/middleware/search-routes.ts',
308299
'src/search/scripts/index/index-cli.ts',
309300
'src/search/scripts/index/utils/indexing-elasticsearch-utils.ts',
@@ -318,9 +309,7 @@ export default [
318309
'src/types/markdownlint-rule-search-replace.d.ts',
319310
'src/types/primer__octicons.d.ts',
320311
'src/versions/scripts/use-short-versions.ts',
321-
'src/webhooks/lib/index.ts',
322312
'src/workflows/projects.ts',
323-
'src/workflows/tests/actions-workflows.ts',
324313
],
325314
rules: {
326315
'@typescript-eslint/no-explicit-any': 'off',

src/content-linter/lib/helpers/get-lintable-yml.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@ ajv.addKeyword({
3737
type: 'string',
3838
// For docs on defining validate see
3939
// https://ajv.js.org/keywords.html#define-keyword-with-validate-function
40-
// Using any for validate function params because AJV's type definitions for custom keywords are complex
41-
validate: (compiled: any, data: any, schema: any, parentInfo: any): boolean => {
42-
mdDict.set(parentInfo.instancePath, data)
40+
validate: (
41+
_compiled: boolean,
42+
data: string,
43+
_schema: unknown,
44+
parentInfo?: { instancePath: string },
45+
): boolean => {
46+
if (parentInfo) mdDict.set(parentInfo.instancePath, data)
4347
return true
4448
},
4549
errors: false,

src/content-linter/lib/helpers/print-annotations.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,25 @@
55
*
66
*/
77

8+
interface LintFlaw {
9+
ruleNames: string[]
10+
severity: string
11+
lineNumber?: number
12+
ruleDescription?: string
13+
errorDetail?: string
14+
context?: string
15+
[key: string]: unknown
16+
}
17+
818
export function printAnnotationResults(
9-
// Using 'any' type as results structure is dynamic and comes from various linting tools with different formats
10-
results: any,
19+
results: Record<string, LintFlaw[]>,
1120
{
1221
skippableRules = [],
1322
skippableFlawProperties = [],
1423
}: { skippableRules?: string[]; skippableFlawProperties?: string[] } = {},
1524
) {
1625
for (const [file, flaws] of Object.entries(results)) {
17-
// Using 'any' type for flaws as they have varying structures depending on the linting rule
18-
for (const flaw of flaws as any) {
26+
for (const flaw of flaws) {
1927
if (intersection(flaw.ruleNames, skippableRules)) {
2028
continue
2129
}
@@ -57,7 +65,6 @@ export function printAnnotationResults(
5765
}
5866
}
5967

60-
// Using 'any' types for generic array intersection utility function
61-
function intersection(arr1: any[], arr2: any[]) {
62-
return arr1.some((item: any) => arr2.includes(item))
68+
function intersection(arr1: string[], arr2: string[]) {
69+
return arr1.some((item) => arr2.includes(item))
6370
}

src/content-linter/lib/helpers/utils.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,14 @@ export function addFixErrorDetail(
1717
addError(onError, lineNumber, `Expected: ${expected}`, ` Actual: ${actual}`, range, fixInfo)
1818
}
1919

20-
export function forEachInlineChild(
20+
export function forEachInlineChild<T = MarkdownToken>(
2121
params: RuleParams,
2222
type: string,
23-
// Handler uses `any` for function parameter variance reasons. TypeScript's contravariance rules for function
24-
// parameters mean that a function accepting a specific type cannot be assigned to a parameter of type `unknown`.
25-
// Therefore, `unknown` cannot be used here, as different linting rules pass tokens with varying structures
26-
// beyond the base MarkdownToken interface, and some handlers are async.
27-
handler: (child: any, token?: any) => void | Promise<void>,
23+
handler: (child: T, token?: MarkdownToken) => void | Promise<void>,
2824
): void {
2925
filterTokens(params, 'inline', (token: MarkdownToken) => {
3026
for (const child of token.children!.filter((c) => c.type === type)) {
31-
handler(child, token)
27+
handler(child as unknown as T, token)
3228
}
3329
})
3430
}

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@ import { filterTokens } from 'markdownlint-rule-helpers'
22

33
import { addFixErrorDetail, getRange } from '../helpers/utils'
44
import { languageKeys } from '@/languages/lib/languages'
5-
import type { RuleParams, RuleErrorCallback, Rule } from '../../types'
5+
import type { RuleParams, RuleErrorCallback, Rule, MarkdownToken } from '../../types'
66

77
export const internalLinksNoLang: Rule = {
88
names: ['GHD002', 'internal-links-no-lang'],
99
description: 'Internal links must not have a hardcoded language code',
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
14-
filterTokens(params, 'inline', (token: any) => {
15-
for (const child of token.children) {
13+
filterTokens(params, 'inline', (token: MarkdownToken) => {
14+
for (const child of token.children!) {
1615
if (child.type !== 'link_open') continue
1716

1817
// Example child.attrs:
@@ -21,8 +20,8 @@ export const internalLinksNoLang: Rule = {
2120
// ['rel', 'canonical'],
2221
// ]
2322
// Attribute arrays are tuples of [attributeName, attributeValue] from markdownit parser
24-
const hrefsMissingSlashes = child.attrs
25-
// The attribute could also be `target` or `rel`
23+
const hrefsMissingSlashes = child
24+
.attrs! // The attribute could also be `target` or `rel`
2625
.filter((attr: [string, string]) => attr[0] === 'href')
2726
.filter((attr: [string, string]) => attr[1].startsWith('/') || !attr[1].startsWith('//'))
2827
// Filter out link paths that start with language code

src/frame/middleware/find-page.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export default async function findPage(
2525
isDev = process.env.NODE_ENV === 'development',
2626
contentRoot = CONTENT_ROOT,
2727
}: FindPageOptions = {},
28-
): Promise<any> {
28+
): Promise<void> {
2929
// Filter out things like `/will/redirect` or `/_next/data/...`
3030
if (!req.pagePath || !languagePrefixPathRegex.test(req.pagePath)) {
3131
return next()
@@ -36,7 +36,7 @@ export default async function findPage(
3636
}
3737

3838
// Using any for page because it's dynamically assigned properties (like version) that aren't in the Page type
39-
let page: any = req.context.pages[req.pagePath]
39+
let page = req.context.pages[req.pagePath] as Page | undefined
4040
if (page && isDev && englishPrefixRegex.test(req.pagePath)) {
4141
// The .applicableVersions and .permalinks properties are computed
4242
// when the page is read in from disk. But when the initial tree
@@ -67,19 +67,20 @@ export default async function findPage(
6767
req.context?.currentVersion &&
6868
!page.applicableVersions.includes(req.context.currentVersion)
6969
) {
70-
return res
70+
res
7171
.status(404)
7272
.send(
7373
`After re-reading the page, '${req.context?.currentVersion}' is no longer an applicable version. ` +
7474
'A restart is required.',
7575
)
76+
return
7677
}
7778
}
7879

7980
if (page && req.context) {
8081
req.context.page = page
8182
// Note: Page doesn't have a version property, this might be setting it dynamically
82-
;(req.context.page as any).version = req.context.currentVersion
83+
;(req.context.page as Page & { version: string }).version = req.context.currentVersion || ''
8384

8485
// We can't depend on `page.hidden` because the dedicated search
8586
// results page is a hidden page but it needs to offer all possible

src/frame/middleware/resolve-carousels.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1-
import type { ExtendedRequest, ResolvedArticle } from '@/types'
1+
import type { ExtendedRequest, Page, ResolvedArticle } from '@/types'
22
import type { Response, NextFunction } from 'express'
33
import findPage from '@/frame/lib/find-page'
44
import { renderContent } from '@/content-render/index'
55
import Permalink from '@/frame/lib/permalink'
66

77
import { createLogger } from '@/observability/logger/index'
88

9+
// The Page class has rawCarousels and carousels properties that aren't on the Page type
10+
interface PageCarouselProps {
11+
rawCarousels?: Record<string, string[]>
12+
carousels?: Record<string, ResolvedArticle[]>
13+
}
14+
915
const logger = createLogger('middleware:resolve-carousels')
1016

1117
/**
@@ -24,7 +30,7 @@ function tryResolveArticlePath(
2430
rawPath: string,
2531
pageRelativePath: string | undefined,
2632
req: ExtendedRequest,
27-
): any {
33+
): Page | undefined {
2834
const { pages, redirects } = req.context!
2935
const currentLanguage = req.context!.currentLanguage || 'en'
3036

@@ -86,7 +92,7 @@ function tryResolveArticlePath(
8692
/**
8793
* Get the path for a page (without language/version)
8894
*/
89-
function getPageHref(page: any): string {
95+
function getPageHref(page: Page): string {
9096
if (page.relativePath) {
9197
return Permalink.relativePathToSuffix(page.relativePath)
9298
}
@@ -103,7 +109,7 @@ async function resolveCarousels(
103109
): Promise<void> {
104110
try {
105111
const page = req.context?.page
106-
const rawCarousels = (page as any)?.rawCarousels
112+
const rawCarousels = (page as unknown as PageCarouselProps)?.rawCarousels
107113

108114
// Handle carousels format
109115
if (rawCarousels && typeof rawCarousels === 'object') {
@@ -158,7 +164,7 @@ async function resolveCarousels(
158164

159165
// Store resolved carousels on the page
160166
if (page && Object.keys(resolvedCarousels).length > 0) {
161-
;(page as any).carousels = resolvedCarousels
167+
;(page as unknown as PageCarouselProps).carousels = resolvedCarousels
162168
}
163169
}
164170
} catch (error) {

src/pages/_error.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ import type { NextPageContext } from 'next'
22

33
import { GenericError } from '@/frame/components/GenericError'
44

5+
interface ExpressRequestExtensions {
6+
FailBot?: {
7+
report: (
8+
err: Error,
9+
context: Record<string, string>,
10+
) => Array<Promise<Response | void>> | undefined
11+
}
12+
method?: string
13+
query?: Record<string, unknown>
14+
language?: string
15+
}
16+
517
function Error() {
618
return <GenericError />
719
}
@@ -34,15 +46,15 @@ Error.getInitialProps = async (ctx: NextPageContext) => {
3446
// do which mutate the Express request object by attaching
3547
// callables to it. This way it's only ever present in SSR executed
3648
// code and doesn't need any custom webpack configuration.
37-
const expressRequest = req as any
49+
const expressRequest = req as unknown as ExpressRequestExtensions
3850
const FailBot = expressRequest.FailBot
3951
if (FailBot) {
4052
try {
4153
// An inclusion-list of headers we're OK with sending because
4254
// they don't contain an PII.
4355
const OK_HEADER_KEYS = ['user-agent', 'referer', 'accept-encoding', 'accept-language']
4456
const reported = FailBot.report(err, {
45-
path: req.url,
57+
path: req.url || '',
4658
request: JSON.stringify(
4759
{
4860
method: expressRequest.method,

src/search/lib/search-request-params/get-search-from-request-params.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export function getSearchFromRequestParams<Type extends SearchTypes>(
3737
continue
3838
}
3939

40-
let value = req.query[key]
40+
let value: unknown = req.query[key]
4141
if (!value || (typeof value === 'string' && !value.trim())) {
4242
if (default_ === undefined) {
4343
validationErrors.push({ error: `No truthy value for key '${key}'`, key })

0 commit comments

Comments
 (0)