Skip to content

Commit f949ea3

Browse files
heiskrCopilot
andauthored
Remove final no-explicit-any exception (MainContext.tsx) (#61695)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 346f3a2 commit f949ea3

19 files changed

Lines changed: 115 additions & 72 deletions

eslint.config.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,6 @@ export default [
231231
},
232232
},
233233

234-
// Legacy files with @typescript-eslint/no-explicit-any violations (see github/docs-engineering#5797)
235-
{
236-
files: ['src/frame/components/context/MainContext.tsx'],
237-
rules: {
238-
'@typescript-eslint/no-explicit-any': 'off',
239-
},
240-
},
241-
242234
// Ignored patterns
243235
// CodeQL scripts included because cocofix is install manually by the workflow
244236
{

src/audit-logs/pages/audit-log-events.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { GetServerSideProps } from 'next'
2+
import type { Response } from 'express'
3+
import type { ExtendedRequest } from '@/types'
24

35
import {
46
addUINamespaces,
@@ -60,8 +62,8 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
6062
const { getAutomatedPageMiniTocItems } = await import('@/frame/lib/get-mini-toc-items')
6163
const { getCategorizedAuditLogEvents, getCategoryNotes } = await import('../lib')
6264

63-
const req = context.req as object
64-
const res = context.res as object
65+
const req = context.req as unknown as ExtendedRequest
66+
const res = context.res as unknown as Response
6567
const currentVersion = context.query.versionId as string
6668
const url = context.req.url
6769

src/frame/components/context/MainContext.tsx

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { createContext, useContext } from 'react'
22
import pick from 'lodash/pick'
3+
import type { Response } from 'express'
34

45
import type { BreadcrumbT } from '@/frame/components/page-header/Breadcrumbs'
56
import type { FeatureFlags } from '@/frame/components/hooks/useFeatureFlags'
6-
import type { SidebarLink } from '@/types'
7+
import type { ExtendedRequest, Permalink, SidebarLink } from '@/types'
78

89
export type ProductT = {
910
external: boolean
@@ -106,7 +107,7 @@ export type MainContextT = {
106107
currentProduct?: ProductT
107108
currentProductName: string
108109
currentProductTree?: ProductTreeNode | null
109-
currentLayoutName?: string
110+
currentLayoutName?: string | null
110111
currentVersion?: string
111112
data: DataT
112113
enterpriseServerReleases: EnterpriseServerReleases
@@ -127,7 +128,7 @@ export type MainContextT = {
127128
applicableVersions: string[]
128129
docsTeamMetrics: string[] | null
129130
} | null
130-
relativePath?: string
131+
relativePath?: string | null
131132
sidebarTree?: ProductTreeNode | null
132133
status: number
133134
xHost?: string
@@ -154,8 +155,8 @@ const DEFAULT_UI_NAMESPACES = [
154155
'cookbook_landing',
155156
]
156157

157-
export function addUINamespaces(req: any, ui: UIStrings, namespaces: string[]) {
158-
const pool = req.context.site.data.ui
158+
export function addUINamespaces(req: ExtendedRequest, ui: UIStrings, namespaces: string[]) {
159+
const pool = req.context!.site!.data.ui
159160
for (const namespace of namespaces) {
160161
if (!(namespace in pool)) {
161162
throw new Error(
@@ -168,22 +169,26 @@ export function addUINamespaces(req: any, ui: UIStrings, namespaces: string[]) {
168169
}
169170
}
170171

171-
export const getMainContext = async (req: any, res: any): Promise<MainContextT> => {
172+
export const getMainContext = async (
173+
req: ExtendedRequest,
174+
res: Response,
175+
): Promise<MainContextT> => {
176+
const context = req.context!
172177
// Our current translation process adds 'ms.*' frontmatter properties to files
173178
// it translates including when data/ui.yml is translated. We don't use these
174179
// properties and their syntax (e.g. 'ms.openlocfilehash',
175180
// 'ms.sourcegitcommit', etc.) causes problems so just delete them.
176-
if (req.context.site.data.ui.ms) {
177-
delete req.context.site.data.ui.ms
181+
if (context.site!.data.ui.ms) {
182+
delete context.site!.data.ui.ms
178183
}
179184

180-
const { page } = req.context
185+
const { page } = context
181186

182187
const documentType = page ? (page.documentType as string) : undefined
183188

184189
const ui: UIStrings = {}
185190
addUINamespaces(req, ui, DEFAULT_UI_NAMESPACES)
186-
if (req.context.currentJourneyTrack?.trackId) {
191+
if (context.currentJourneyTrack?.trackId) {
187192
addUINamespaces(req, ui, ['journey_track_nav'])
188193
}
189194

@@ -197,65 +202,65 @@ export const getMainContext = async (req: any, res: any): Promise<MainContextT>
197202
// To know whether we need this key, we need to match this
198203
// with the business logic in `DeprecationBanner.tsx` which is as follows:
199204
if (
200-
req.context.enterpriseServerReleases.releasesWithOldestDeprecationDate.includes(
201-
req.context.currentRelease,
205+
context.enterpriseServerReleases!.releasesWithOldestDeprecationDate.includes(
206+
context.currentRelease as string,
202207
)
203208
) {
204209
reusables.enterprise_deprecation = {
205-
version_was_deprecated: req.context.getDottedData(
210+
version_was_deprecated: context.getDottedData!(
206211
'reusables.enterprise_deprecation.version_was_deprecated',
207-
),
208-
version_will_be_deprecated: req.context.getDottedData(
212+
) as string,
213+
version_will_be_deprecated: context.getDottedData!(
209214
'reusables.enterprise_deprecation.version_will_be_deprecated',
210-
),
211-
deprecation_details: req.context.getDottedData(
215+
) as string,
216+
deprecation_details: context.getDottedData!(
212217
'reusables.enterprise_deprecation.deprecation_details',
213-
),
218+
) as string,
214219
}
215220
}
216221

217222
// This is a number, like 3.13 or it's possibly null if there is no
218223
// supported release candidate at the moment.
219-
const { releaseCandidate } = req.context.enterpriseServerReleases
224+
const { releaseCandidate } = context.enterpriseServerReleases!
220225
// Combine the version number with the prefix so it can appear
221226
// as a full version string if the release candidate is set.
222227
const releaseCandidateVersion = releaseCandidate ? `enterprise-server@${releaseCandidate}` : null
223228

224229
const pageInfo =
225230
(page && {
226231
documentType,
227-
contentType: req.context.page.contentType || null,
228-
title: req.context.page.title,
229-
fullTitle: req.context.page.fullTitle || null,
230-
introPlainText: req.context.page?.introPlainText || null,
231-
applicableVersions: req.context.page?.permalinks.map((obj: any) => obj.pageVersion) || [],
232-
hidden: req.context.page.hidden || false,
233-
noEarlyAccessBanner: req.context.page.noEarlyAccessBanner || false,
234-
docsTeamMetrics: req.context.page.docsTeamMetrics || null,
232+
contentType: page.contentType || null,
233+
title: page.title,
234+
fullTitle: page.fullTitle || null,
235+
introPlainText: page.introPlainText || null,
236+
applicableVersions: page.permalinks.map((obj: Permalink) => obj.pageVersion),
237+
hidden: page.hidden || false,
238+
noEarlyAccessBanner: page.noEarlyAccessBanner || false,
239+
docsTeamMetrics: page.docsTeamMetrics || null,
235240
}) ||
236241
null
237242

238-
const currentProduct: ProductT = req.context.productMap[req.context.currentProduct] || null
239-
const currentProductName: string = req.context.currentProductName || ''
243+
const currentProduct = (context.productMap?.[context.currentProduct || ''] || null) as ProductT
244+
const currentProductName: string = context.currentProductName || ''
240245

241246
const props: MainContextT = {
242-
allVersions: minimalAllVersions(req.context.allVersions),
243-
breadcrumbs: req.context.breadcrumbs || {},
244-
communityRedirect: req.context.page?.communityRedirect || {},
245-
currentCategory: req.context.currentCategory || '',
246-
currentLayoutName: req.context.currentLayoutName || null,
247-
currentPathWithoutLanguage: req.context.currentPathWithoutLanguage,
247+
allVersions: minimalAllVersions(context.allVersions!),
248+
breadcrumbs: (context.breadcrumbs || {}) as MainContextT['breadcrumbs'],
249+
communityRedirect: (context.page?.communityRedirect || {}) as MainContextT['communityRedirect'],
250+
currentCategory: context.currentCategory || '',
251+
currentLayoutName: context.currentLayoutName || null,
252+
currentPathWithoutLanguage: context.currentPathWithoutLanguage!,
248253
currentProduct,
249254
currentProductName,
250-
// This is a slimmed down version of `req.context.currentProductTree`
255+
// This is a slimmed down version of `context.currentProductTree`
251256
// that only has the minimal titles stuff needed for sidebars and
252257
// any page that is hidden is omitted.
253258
// However, it's not needed on most pages. For example, on article pages,
254259
// you don't need it. It's similar to the minimal product tree but,
255260
// has the full length titles and not just the short titles.
256261
currentProductTree:
257-
(includeFullProductTree && req.context.currentProductTreeTitlesExcludeHidden) || null,
258-
currentVersion: req.context.currentVersion,
262+
(includeFullProductTree && context.currentProductTreeTitlesExcludeHidden) || null,
263+
currentVersion: context.currentVersion,
259264
data: {
260265
ui,
261266
reusables,
@@ -265,24 +270,24 @@ export const getMainContext = async (req: any, res: any): Promise<MainContextT>
265270
},
266271
},
267272
},
268-
enterpriseServerReleases: pick(req.context.enterpriseServerReleases, [
273+
enterpriseServerReleases: pick(context.enterpriseServerReleases!, [
269274
'isOldestReleaseDeprecated',
270275
'oldestSupported',
271276
'nextDeprecationDate',
272277
'supported',
273278
'releasesWithOldestDeprecationDate',
274-
]),
275-
enterpriseServerVersions: req.context.enterpriseServerVersions,
276-
error: req.context.error ? req.context.error.toString() : '',
279+
]) as EnterpriseServerReleases,
280+
enterpriseServerVersions: context.enterpriseServerVersions!,
281+
error: context.error ? context.error.toString() : '',
277282
featureFlags: {},
278283
fullUrl: `${req.protocol}://${req.hostname}${req.originalUrl}`, // does not include port for localhost
279-
isHomepageVersion: req.context.page?.documentType === 'homepage',
280-
nonEnterpriseDefaultVersion: req.context.nonEnterpriseDefaultVersion,
281-
page: pageInfo,
282-
relativePath: req.context.page?.relativePath || null,
284+
isHomepageVersion: context.page?.documentType === 'homepage',
285+
nonEnterpriseDefaultVersion: context.nonEnterpriseDefaultVersion!,
286+
page: pageInfo as MainContextT['page'],
287+
relativePath: context.page?.relativePath || null,
283288
// The minimal product tree is needed on all pages that depend on
284289
// the product sidebar or the rest sidebar.
285-
sidebarTree: (includeSidebarTree && req.context.sidebarTree) || null,
290+
sidebarTree: (includeSidebarTree && context.sidebarTree) || null,
286291
status: res.statusCode,
287292
xHost: req.get('x-host') || '',
288293
}

src/github-apps/pages/endpoints-available-for-fine-grained-personal-access-tokens.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { GetServerSideProps } from 'next'
2+
import type { Response } from 'express'
3+
import type { ExtendedRequest } from '@/types'
24

35
import {
46
AutomatedPageContextT,
@@ -41,7 +43,10 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
4143

4244
return {
4345
props: {
44-
mainContext: await getMainContext(context.req, context.res),
46+
mainContext: await getMainContext(
47+
context.req as unknown as ExtendedRequest,
48+
context.res as unknown as Response,
49+
),
4550
currentVersion,
4651
appsItems: appsItems as EnabledListT,
4752
automatedPageContext: getAutomatedPageContextFromRequest(context.req),

src/github-apps/pages/endpoints-available-for-github-app-installation-access-tokens.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { GetServerSideProps } from 'next'
2+
import type { Response } from 'express'
3+
import type { ExtendedRequest } from '@/types'
24

35
import {
46
AutomatedPageContextT,
@@ -41,7 +43,10 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
4143

4244
return {
4345
props: {
44-
mainContext: await getMainContext(context.req, context.res),
46+
mainContext: await getMainContext(
47+
context.req as unknown as ExtendedRequest,
48+
context.res as unknown as Response,
49+
),
4550
currentVersion,
4651
appsItems: appsItems as EnabledListT,
4752
automatedPageContext: getAutomatedPageContextFromRequest(context.req),

src/github-apps/pages/endpoints-available-for-github-app-user-access-tokens.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { GetServerSideProps } from 'next'
2+
import type { Response } from 'express'
3+
import type { ExtendedRequest } from '@/types'
24

35
import {
46
AutomatedPageContextT,
@@ -41,7 +43,10 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
4143

4244
return {
4345
props: {
44-
mainContext: await getMainContext(context.req, context.res),
46+
mainContext: await getMainContext(
47+
context.req as unknown as ExtendedRequest,
48+
context.res as unknown as Response,
49+
),
4550
currentVersion,
4651
appsItems: appsItems as EnabledListT,
4752
automatedPageContext: getAutomatedPageContextFromRequest(context.req),

src/github-apps/pages/permissions-required-for-fine-grained-personal-access-tokens.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { GetServerSideProps } from 'next'
2+
import type { Response } from 'express'
3+
import type { ExtendedRequest } from '@/types'
24

35
import {
46
AutomatedPageContextT,
@@ -42,7 +44,10 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
4244

4345
return {
4446
props: {
45-
mainContext: await getMainContext(context.req, context.res),
47+
mainContext: await getMainContext(
48+
context.req as unknown as ExtendedRequest,
49+
context.res as unknown as Response,
50+
),
4651
currentVersion,
4752
appsItems: appsItems as PermissionListT,
4853
automatedPageContext: getAutomatedPageContextFromRequest(context.req),

src/github-apps/pages/permissions-required-for-github-apps.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { GetServerSideProps } from 'next'
2+
import type { Response } from 'express'
3+
import type { ExtendedRequest } from '@/types'
24
import {
35
AutomatedPageContextT,
46
getAutomatedPageContextFromRequest,
@@ -42,7 +44,10 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
4244

4345
return {
4446
props: {
45-
mainContext: await getMainContext(context.req, context.res),
47+
mainContext: await getMainContext(
48+
context.req as unknown as ExtendedRequest,
49+
context.res as unknown as Response,
50+
),
4651
currentVersion,
4752
appsItems: appsItems as PermissionListT,
4853
automatedPageContext: getAutomatedPageContextFromRequest(context.req),

src/graphql/pages/breaking-changes.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { GetServerSideProps } from 'next'
2+
import type { Response } from 'express'
23
import GithubSlugger from 'github-slugger'
34
import type { ExtendedRequest } from '@/types'
45
import type { ServerResponse } from 'http'
@@ -73,7 +74,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
7374

7475
return {
7576
props: {
76-
mainContext: await getMainContext(req, res),
77+
mainContext: await getMainContext(req, res as unknown as Response),
7778
automatedPageContext,
7879
schema,
7980
headings,

src/graphql/pages/changelog-year.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { GetServerSideProps } from 'next'
2+
import type { Response } from 'express'
23
import type { ExtendedRequest } from '@/types'
34
import type { ServerResponse } from 'http'
45

@@ -64,7 +65,7 @@ export const getServerSideProps: GetServerSideProps<Props> = async (context) =>
6465

6566
return {
6667
props: {
67-
mainContext: await getMainContext(req, res),
68+
mainContext: await getMainContext(req, res as unknown as Response),
6869
automatedPageContext,
6970
schema,
7071
years,

0 commit comments

Comments
 (0)