Skip to content

Commit 7fb4bfb

Browse files
ggrossetieclaude
andcommitted
chore: prepare for TypeScript strict mode
Enable every strict-family flag that already passes cleanly (noImplicitThis, strictFunctionTypes, strictBindCallApply, alwaysStrict, useUnknownInCatchVariables), declare the Node types explicitly via "types" (the browser profile keeps excluding them), and add a "typecheck" script so these flags are enforced outside build-test. The noImplicitThis fixes type the `this` parameter of the Asciidoctor extension callbacks that stash state on the processor instance, using dedicated state interfaces instead of `as any` casts. Remaining before "strict": true: noImplicitAny (106 errors), then strictNullChecks (86) and strictPropertyInitialization. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent fb93bf5 commit 7fb4bfb

5 files changed

Lines changed: 62 additions & 26 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,7 @@
769769
"package": "npm run build && npm run build-web && vsce package",
770770
"deploy": "vsce publish -p",
771771
"lint": "npx @biomejs/biome check --write",
772+
"typecheck": "tsc --noEmit",
772773
"format": "npx @biomejs/biome format --write",
773774
"pretest": "npm run build && npm run build-test",
774775
"test": "node ./src/test/runTest.mjs && npm run test:unit && npm run test:grammar",

src/features/asciidoctor/asciidoctorConfig.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ export interface AsciidoctorConfigProvider {
88
activate(registry: Registry, documentUri: vscode.Uri): Promise<void>
99
}
1010

11+
/** State stored on the preprocessor extension instance between renders. */
12+
interface PrependConfigPreprocessorState {
13+
asciidoctorConfigContent: string
14+
}
15+
1116
/**
1217
* .asciidoctorconfig support.
1318
*/
@@ -18,10 +23,10 @@ export class AsciidoctorConfig implements AsciidoctorConfigProvider {
1823
this.prependExtension = Extensions.newPreprocessor(
1924
'PrependConfigPreprocessorExtension',
2025
{
21-
postConstruct: function () {
26+
postConstruct: function (this: PrependConfigPreprocessorState) {
2227
this.asciidoctorConfigContent = ''
2328
},
24-
process: function (doc, reader) {
29+
process: function (this: PrependConfigPreprocessorState, doc, reader) {
2530
if (this.asciidoctorConfigContent.length > 0) {
2631
// otherwise an empty line at the beginning breaks level 0 detection
2732
reader.pushInclude(
@@ -47,12 +52,9 @@ export class AsciidoctorConfig implements AsciidoctorConfigProvider {
4752
) {
4853
const asciidoctorConfigContent =
4954
await getAsciidoctorConfigContent(documentUri)
50-
if (asciidoctorConfigContent !== undefined) {
51-
;(this.prependExtension as any).asciidoctorConfigContent =
52-
asciidoctorConfigContent
53-
} else {
54-
;(this.prependExtension as any).asciidoctorConfigContent = ''
55-
}
55+
const state = this
56+
.prependExtension as unknown as PrependConfigPreprocessorState
57+
state.asciidoctorConfigContent = asciidoctorConfigContent ?? ''
5658
}
5759
}
5860

@@ -63,7 +65,7 @@ export async function getAsciidoctorConfigContent(
6365
| undefined = vscode.workspace.workspaceFolders?.map(
6466
(workspaceFolder) => workspaceFolder.uri,
6567
),
66-
): Promise<String | undefined> {
68+
): Promise<string | undefined> {
6769
const directories = getConfigSearchDirectories(
6870
documentUri,
6971
workspaceFolderUris,

src/features/asciidoctor/asciidoctorIncludeItems.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ interface IncludeEntry {
99

1010
export interface IncludeItems extends Array<IncludeEntry> {}
1111

12+
/** State stored on the include processor extension instance between renders. */
13+
interface FindIncludeProcessorState {
14+
includeItems: IncludeItems
15+
includeIndex: number
16+
}
17+
1218
export interface AsciidoctorIncludeItemsProvider {
1319
activate(registry: Registry)
1420

@@ -26,15 +32,21 @@ export class AsciidoctorIncludeItems
2632
this.findIncludeProcessorExtension = Extensions.newIncludeProcessor(
2733
'FindIncludeProcessorExtension',
2834
{
29-
postConstruct: function () {
35+
postConstruct: function (this: FindIncludeProcessorState) {
3036
this.includeItems = []
3137
this.includeIndex = 0
3238
},
3339
// @ts-ignore
3440
handles: function (_target) {
3541
return true
3642
},
37-
process: function (doc, reader, target, attrs) {
43+
process: function (
44+
this: FindIncludeProcessorState,
45+
doc,
46+
reader,
47+
target,
48+
attrs,
49+
) {
3850
// We don't meaningfully process the includes, we just want to identify
3951
// their line number and path if they belong in the base document.
4052
//
@@ -73,11 +85,15 @@ export class AsciidoctorIncludeItems
7385
}
7486

7587
get() {
76-
return (this.findIncludeProcessorExtension as any).includeItems
88+
return (
89+
this.findIncludeProcessorExtension as unknown as FindIncludeProcessorState
90+
).includeItems
7791
}
7892

7993
reset() {
80-
;(this.findIncludeProcessorExtension as any).includeIndex = 0
81-
;(this.findIncludeProcessorExtension as any).includeItems = []
94+
const state = this
95+
.findIncludeProcessorExtension as unknown as FindIncludeProcessorState
96+
state.includeIndex = 0
97+
state.includeItems = []
8298
}
8399
}

src/test/unit/asciidoctorExtensionContributions.test.ts

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
import assert from 'node:assert/strict'
22
import { describe, test } from 'node:test'
3-
import { Extensions, type Registry } from '@asciidoctor/core'
3+
import {
4+
type Block,
5+
type BlockProcessor,
6+
type BlockProcessorDslInterface,
7+
Extensions,
8+
type Reader,
9+
type Registry,
10+
} from '@asciidoctor/core'
411
import {
512
ASCIIDOCTOR_EXTENSIONS_CONTRIBUTION_POINT,
613
type AsciidoctorExtensionContext,
@@ -226,17 +233,21 @@ describe('registerContributedAsciidoctorExtensions', () => {
226233
contributes: contributingPackageJSON,
227234
exports: {
228235
registerAsciidoctorExtensions(r: Registry) {
229-
r.block('shout', function () {
230-
const self = this
231-
self.onContext('paragraph')
232-
self.process((parent, reader) =>
233-
self.createBlock(
234-
parent,
235-
'paragraph',
236-
reader.getLines().join('\n').toUpperCase(),
237-
),
238-
)
239-
})
236+
r.block(
237+
'shout',
238+
function (this: BlockProcessor & BlockProcessorDslInterface) {
239+
const self = this
240+
self.onContext('paragraph')
241+
self.process((parent, reader) =>
242+
self.createBlock(
243+
parent as Block,
244+
'paragraph',
245+
(reader as Reader).getLines().join('\n').toUpperCase(),
246+
{},
247+
),
248+
)
249+
},
250+
)
240251
},
241252
},
242253
})

tsconfig.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77
"target": "es2022",
88
"outDir": "dist",
99
"lib": ["dom", "es2022"],
10+
"types": ["node"],
11+
"noImplicitThis": true,
12+
"strictFunctionTypes": true,
13+
"strictBindCallApply": true,
14+
"alwaysStrict": true,
15+
"useUnknownInCatchVariables": true,
1016
"sourceMap": true,
1117
"rootDir": "."
1218
},

0 commit comments

Comments
 (0)