Skip to content

Commit a9cc23a

Browse files
heiskrCopilot
andauthored
⚙️ Remove explicit any types from 16 TypeScript files (#60657)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 39f3c20 commit a9cc23a

22 files changed

Lines changed: 267 additions & 207 deletions

File tree

eslint.config.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -219,46 +219,30 @@ export default [
219219
'src/content-linter/tests/lint-frontmatter-links.ts',
220220
'src/content-linter/tests/unit/table-column-integrity-simple.ts',
221221
'src/content-render/liquid/engine.ts',
222-
'src/content-render/liquid/ifversion.ts',
223-
'src/content-render/liquid/indented-data-reference.ts',
224222
'src/content-render/liquid/index.ts',
225-
'src/content-render/liquid/octicon.ts',
226-
'src/content-render/scripts/add-content-type.ts',
227223
'src/content-render/scripts/liquid-tags.ts',
228-
'src/content-render/scripts/move-by-content-type.ts',
229224
'src/content-render/scripts/move-content.ts',
230-
'src/content-render/tests/data.ts',
231225
'src/content-render/tests/link-error-line-numbers.ts',
232226
'src/content-render/unified/annotate.ts',
233227
'src/content-render/unified/index.ts',
234-
'src/content-render/unified/module-types.d.ts',
235-
'src/content-render/unified/rewrite-local-links.ts',
236228
'src/data-directory/lib/get-data.ts',
237229
'src/early-access/scripts/migrate-early-access-product.ts',
238-
'src/early-access/scripts/what-docs-early-access-branch.ts',
239230
'src/fixtures/tests/categories-and-subcategory.ts',
240231
'src/fixtures/tests/guides.ts',
241232
'src/fixtures/tests/translations.ts',
242-
'src/frame/components/context/ArticleContext.tsx',
243-
'src/frame/components/context/CategoryLandingContext.tsx',
244233
'src/frame/components/context/MainContext.tsx',
245-
'src/frame/components/context/TocLandingContext.tsx',
246234
'src/frame/lib/create-tree.ts',
247235
'src/frame/lib/frontmatter.ts',
248236
'src/frame/lib/page-data.ts',
249237
'src/frame/lib/page.ts',
250-
'src/frame/lib/read-frontmatter.ts',
251238
'src/frame/tests/page.ts',
252-
'src/frame/tests/read-frontmatter.ts',
253239
'src/frame/tests/server.ts',
254-
'src/ghes-releases/scripts/deprecate/update-content.ts',
255240
'src/github-apps/lib/index.ts',
256241
'src/graphql/lib/index.ts',
257242
'src/graphql/pages/reference.tsx',
258243
'src/graphql/scripts/utils/process-schemas.ts',
259244
'src/graphql/scripts/utils/schema-helpers.ts',
260245
'src/graphql/tests/validate-schema.ts',
261-
'src/journeys/lib/journey-path-resolver.ts',
262246
'src/landings/components/CookBookFilter.tsx',
263247
'src/landings/components/ProductGuidesContext.tsx',
264248
'src/landings/components/ProductLandingContext.tsx',
@@ -271,7 +255,6 @@ export default [
271255
'src/links/lib/update-internal-links.ts',
272256
'src/links/scripts/check-github-github-links.ts',
273257
'src/links/scripts/update-internal-links.ts',
274-
'src/redirects/middleware/handle-redirects.ts',
275258
'src/rest/components/get-rest-code-samples.ts',
276259
'src/rest/lib/index.ts',
277260
'src/rest/pages/category.tsx',
@@ -307,7 +290,6 @@ export default [
307290
'src/types/markdownlint-rule-helpers.d.ts',
308291
'src/types/markdownlint-rule-search-replace.d.ts',
309292
'src/types/primer__octicons.d.ts',
310-
'src/versions/scripts/use-short-versions.ts',
311293
'src/workflows/projects.ts',
312294
],
313295
rules: {

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

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { addError } from 'markdownlint-rule-helpers'
22
import { intersection } from 'lodash-es'
33

44
import { getFrontmatter } from '../helpers/utils'
5-
import { formatAjvErrors } from '../helpers/schema-utils'
65
import { frontmatter, deprecatedProperties } from '@/frame/lib/frontmatter'
76
import readFrontmatter from '@/frame/lib/read-frontmatter'
87
import type { RuleParams, RuleErrorCallback, Rule } from '../../types'
@@ -35,23 +34,46 @@ export const frontmatterSchema: Rule = {
3534
)
3635
}
3736

38-
// Check that the frontmatter matches the schema
37+
// Check that the frontmatter matches the schema.
38+
// readFrontmatter returns errors as { property, message, reason } objects.
3939
const { errors } = readFrontmatter(params.lines.join('\n'), { schema: frontmatter.schema })
40-
const formattedErrors = formatAjvErrors(
41-
errors as unknown as Parameters<typeof formatAjvErrors>[0],
42-
)
43-
for (const error of formattedErrors) {
44-
// If the missing property is at the top level, we don't have a line
40+
for (const error of errors) {
41+
const property = error.property || ''
42+
const message = error.message || ''
43+
const reason = error.reason || ''
44+
const parts = property.split('.')
45+
46+
let detail: string
47+
let context: string
48+
let searchProperty: string
49+
50+
if (reason === 'additionalProperties') {
51+
detail = 'The frontmatter includes an unsupported property.'
52+
context = `Remove the property \`${property}\`.`
53+
// Search for the offending additional property directly
54+
searchProperty = parts[parts.length - 1] || ''
55+
} else if (reason === 'required') {
56+
detail = 'The frontmatter has a missing required property'
57+
context = `Add the missing property \`${property}\``
58+
// The property is missing, so point to its parent container
59+
searchProperty = parts.length > 1 ? parts[parts.length - 2] : ''
60+
} else {
61+
detail = `Frontmatter ${message}.`
62+
context = property
63+
searchProperty = parts[0] || ''
64+
}
65+
66+
// If the property is at the top level or missing, we don't have a line
4567
// to point to. In that case, the error will be added to line 1.
46-
const query = (line: string) => line.trim().startsWith(`${error.searchProperty}:`)
47-
const line = error.searchProperty === '' ? null : params.lines.find(query)
68+
const query = (line: string) => line.trim().startsWith(`${searchProperty}:`)
69+
const line = searchProperty === '' ? null : params.lines.find(query)
4870
const lineNumber = line ? params.lines.indexOf(line) + 1 : 1
4971
addError(
5072
onError,
5173
lineNumber,
52-
error.detail,
53-
error.context,
54-
line ? [1, error.errorProperty.length] : null,
74+
detail,
75+
context,
76+
line ? [1, searchProperty.length] : null,
5577
null, // No fix possible
5678
)
5779
}

src/content-render/liquid/ifversion.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
type Emitter,
99
type Template,
1010
type TopLevelToken,
11+
type Liquid,
1112
} from 'liquidjs'
1213
import versionSatisfiesRange from '@/versions/lib/version-satisfies-range'
1314
import supportedOperators, {
@@ -26,6 +27,11 @@ interface VersionObj {
2627
internalLatestRelease?: string
2728
}
2829

30+
interface IfversionEnvironments {
31+
currentVersionObj?: VersionObj
32+
markdownRequested?: boolean
33+
}
34+
2935
const SyntaxHelp =
3036
"Syntax Error in 'ifversion' with range - Valid syntax: ifversion [plan] [operator] [releaseNumber]"
3137

@@ -44,7 +50,7 @@ export default class Ifversion extends Tag {
4450
currentVersionObj: VersionObj | null = null
4551

4652
// The following is verbatim from https://github.com/harttle/liquidjs/blob/v9.22.1/src/builtin/tags/if.ts
47-
constructor(tagToken: TagToken, remainTokens: TopLevelToken[], liquid: any) {
53+
constructor(tagToken: TagToken, remainTokens: TopLevelToken[], liquid: Liquid) {
4854
super(tagToken, remainTokens, liquid)
4955

5056
this.tagToken = tagToken
@@ -60,7 +66,7 @@ export default class Ifversion extends Tag {
6066
templates: (p = []),
6167
}),
6268
)
63-
.on('tag:elsif', (token: any) => {
69+
.on('tag:elsif', (token: TagToken) => {
6470
this.branches.push({
6571
cond: token.args,
6672
templates: (p = []),
@@ -78,10 +84,10 @@ export default class Ifversion extends Tag {
7884

7985
// The following is _mostly_ verbatim from https://github.com/harttle/liquidjs/blob/v9.22.1/src/builtin/tags/if.ts
8086
// The additions here are the handleNots(), handleOperators(), and handleVersionNames() calls.
81-
*render(ctx: Context, emitter: Emitter): Generator<any, void, unknown> {
87+
*render(ctx: Context, emitter: Emitter): Generator<unknown, void, unknown> {
8288
const r = this.liquid.renderer
8389

84-
this.currentVersionObj = (ctx.environments as any).currentVersionObj
90+
this.currentVersionObj = (ctx.environments as IfversionEnvironments).currentVersionObj ?? null
8591

8692
for (const branch of this.branches) {
8793
let resolvedBranchCond = branch.cond
@@ -96,7 +102,7 @@ export default class Ifversion extends Tag {
96102
// Resolve version names to boolean values for Markdown API context.
97103
// This will replace syntax like `fpt or ghec` with `true or false` based on current version.
98104
// Only apply this transformation in Markdown API context to avoid breaking existing functionality.
99-
if ((ctx.environments as any).markdownRequested) {
105+
if ((ctx.environments as IfversionEnvironments).markdownRequested) {
100106
resolvedBranchCond = this.handleVersionNames(resolvedBranchCond)
101107
}
102108

src/content-render/liquid/indented-data-reference.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
11
import assert from 'assert'
22

3+
import { type TagToken, type Liquid } from 'liquidjs'
34
import { THROW_ON_EMPTY, IndentedDataReferenceError } from './error-handling'
45
import { getDataByLanguage } from '@/data-directory/lib/get-data'
56

6-
// Note: Using 'any' for liquidjs-related types because liquidjs doesn't provide comprehensive TypeScript definitions
7-
interface LiquidTag {
8-
markup: string
9-
liquid: any
10-
parse(tagToken: any): void
11-
render(scope: any): Promise<string | undefined>
12-
}
13-
147
interface LiquidScope {
158
environments: {
169
currentLanguage: string
17-
[key: string]: any
10+
[key: string]: unknown
1811
}
1912
}
2013

@@ -28,11 +21,11 @@ interface LiquidScope {
2821
// reference is used inside a block element (like a list or nested list) without
2922
// affecting the formatting when the reference is used elsewhere via {{ site.data.foo.bar }}.
3023

31-
const IndentedDataReference: LiquidTag = {
24+
const IndentedDataReference = {
3225
markup: '',
33-
liquid: null as any,
26+
liquid: null as Liquid | null,
3427

35-
parse(tagToken: any): void {
28+
parse(tagToken: TagToken): void {
3629
this.markup = tagToken.args.trim()
3730
},
3831

src/content-render/liquid/octicon.ts

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,6 @@
1-
import { TokenizationError } from 'liquidjs'
1+
import { TokenizationError, type TagToken } from 'liquidjs'
22
import octicons from '@primer/octicons'
33

4-
// Note: Using 'any' for liquidjs-related types because liquidjs doesn't provide comprehensive TypeScript definitions
5-
interface LiquidTag {
6-
icon: string
7-
options: Record<string, string>
8-
parse(tagToken: any): void
9-
render(): Promise<string>
10-
}
11-
12-
interface OcticonsMatch {
13-
groups: {
14-
icon: string
15-
options?: string
16-
}
17-
}
18-
194
const OptionsSyntax = /([a-zA-Z-]+)="([\w\s-]+)"*/g
205
const Syntax = new RegExp(`"(?<icon>[a-zA-Z-]+)"(?<options>(?:\\s${OptionsSyntax.source})*)`)
216
const SyntaxHelp = 'Syntax Error in tag \'octicon\' - Valid syntax: octicon "<name>" <key="value">'
@@ -30,13 +15,13 @@ const SyntaxHelp = 'Syntax Error in tag \'octicon\' - Valid syntax: octicon "<na
3015
* {% octicon "check" %} <!-- auto-generates aria-label="check icon" -->
3116
* {% octicon "check" width="64" aria-label="Example label" %}
3217
*/
33-
const Octicon: LiquidTag = {
18+
const Octicon = {
3419
icon: '',
35-
options: {},
20+
options: {} as Record<string, string>,
3621

37-
parse(tagToken: any): void {
38-
const match: OcticonsMatch | null = tagToken.args.match(Syntax)
39-
if (!match) {
22+
parse(tagToken: TagToken): void {
23+
const match = tagToken.args.match(Syntax)
24+
if (!match || !match.groups) {
4025
throw new TokenizationError(SyntaxHelp, tagToken)
4126
}
4227

src/content-render/scripts/add-content-type.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,15 @@ function processFile(filePath: string, scriptOptions: ScriptOptions) {
128128
}
129129

130130
// Write the file back
131-
fs.writeFileSync(filePath, frontmatter.stringify(content, data, { lineWidth: -1 } as any))
131+
fs.writeFileSync(
132+
filePath,
133+
frontmatter.stringify(
134+
content,
135+
data,
136+
// lineWidth is a js-yaml option passed through gray-matter, not in gray-matter's type definitions
137+
{ lineWidth: -1 } as unknown as Parameters<typeof frontmatter.stringify>[2],
138+
),
139+
)
132140

133141
if (scriptOptions.verbose) {
134142
console.log(`\n${relativePath}`)

src/content-render/scripts/move-by-content-type.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ program
146146
}
147147

148148
// Validate contentType
149-
if (!CONTENT_TYPES.includes(contentType as any)) {
149+
if (!CONTENT_TYPES.includes(contentType)) {
150150
skipped.push({ file: relativePath, reason: `Invalid contentType: ${contentType}` })
151151
console.log(
152152
chalk.yellow(`⚠ Skipping ${relativePath}: Invalid contentType "${contentType}"`),

src/content-render/tests/data.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import nonEnterpriseDefaultVersion from '@/versions/lib/non-enterprise-default-v
66
import { DataDirectory } from '@/tests/helpers/data-directory'
77

88
describe('data tag', () => {
9-
// Using 'any' type as DataDirectory is from data-directory.ts which lacks type definitions
10-
let dd: any
9+
let dd: DataDirectory
1110
const enDirBefore = languages.en.dir
1211

1312
beforeAll(() => {

src/content-render/unified/module-types.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ declare module 'rehype-highlight' {
1919
import type { Options } from 'lowlight'
2020

2121
interface HighlightOptions extends Options {
22-
languages?: Record<string, any>
22+
languages?: Record<string, unknown>
2323
subset?: boolean
2424
aliases?: Record<string, string>
2525
}

src/content-render/unified/rewrite-local-links.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,13 @@ export default function rewriteLocalLinks(context?: Context) {
101101
const linkRefNode = node as LinkReference
102102
const definition = definitions.get(linkRefNode.identifier)
103103
if (definition) {
104-
// Replace the LinkReference node with a Link node
105-
// Using 'as any' because we're mutating the node type at runtime,
106-
// which TypeScript doesn't allow as LinkReference and Link are incompatible types
107-
;(linkRefNode as any).type = 'link'
108-
;(linkRefNode as any).url = definition.url
109-
;(linkRefNode as any).title = definition.title
104+
// Replace the LinkReference node with a Link node by mutating its
105+
// properties at runtime. Using 'as unknown as' because LinkReference
106+
// and Link are structurally incompatible in TypeScript.
107+
const mutableNode = linkRefNode as unknown as Link
108+
mutableNode.type = 'link'
109+
mutableNode.url = definition.url
110+
mutableNode.title = definition.title
110111
} else {
111112
console.warn(`Definition not found for identifier: ${linkRefNode.identifier}`)
112113
}

0 commit comments

Comments
 (0)