Skip to content

Commit a67e85c

Browse files
use type assertions
1 parent 3ef5c06 commit a67e85c

File tree

3 files changed

+54
-59
lines changed

3 files changed

+54
-59
lines changed

tests/DocsParser.spec.ts

Lines changed: 47 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
22
import fs from 'node:fs';
33
import path from 'node:path';
44
import { DocsParser } from '../src/DocsParser.js';
5-
import type { ModuleDocumentationContainer } from '../src/ParsedDocumentation.js';
5+
import type {
6+
ModuleDocumentationContainer,
7+
ClassDocumentationContainer,
8+
ElementDocumentationContainer,
9+
StructureDocumentationContainer,
10+
} from '../src/ParsedDocumentation.js';
611

712
describe('DocsParser', () => {
813
let tempDir: string;
@@ -71,16 +76,15 @@ A \`string\` property that indicates the current application's name.
7176
expect((appModule as ModuleDocumentationContainer).process.main).toBe(true);
7277
expect(appModule?.description).toContain('Control your application');
7378

74-
if (appModule && appModule.type === 'Module') {
75-
expect(appModule.events).toHaveLength(1);
76-
expect(appModule.events[0].name).toBe('ready');
79+
const module = appModule as ModuleDocumentationContainer;
80+
expect(module.events).toHaveLength(1);
81+
expect(module.events[0].name).toBe('ready');
7782

78-
expect(appModule.methods).toHaveLength(1);
79-
expect(appModule.methods[0].name).toBe('quit');
83+
expect(module.methods).toHaveLength(1);
84+
expect(module.methods[0].name).toBe('quit');
8085

81-
expect(appModule.properties).toHaveLength(1);
82-
expect(appModule.properties[0].name).toBe('name');
83-
}
86+
expect(module.properties).toHaveLength(1);
87+
expect(module.properties[0].name).toBe('name');
8488
});
8589

8690
it('should parse a class documentation', async () => {
@@ -133,20 +137,19 @@ Emitted when the window is closed.
133137
expect(browserWindowClass).toBeDefined();
134138
expect(browserWindowClass?.type).toBe('Class');
135139

136-
if (browserWindowClass && browserWindowClass.type === 'Class') {
137-
expect(browserWindowClass.constructorMethod).toBeDefined();
138-
expect(browserWindowClass.constructorMethod?.parameters).toHaveLength(1);
140+
const cls = browserWindowClass as ClassDocumentationContainer;
141+
expect(cls.constructorMethod).toBeDefined();
142+
expect(cls.constructorMethod!.parameters).toHaveLength(1);
139143

140-
expect(browserWindowClass.instanceMethods).toHaveLength(2);
141-
expect(browserWindowClass.instanceMethods[0].name).toBe('close');
142-
expect(browserWindowClass.instanceMethods[1].name).toBe('show');
144+
expect(cls.instanceMethods).toHaveLength(2);
145+
expect(cls.instanceMethods[0].name).toBe('close');
146+
expect(cls.instanceMethods[1].name).toBe('show');
143147

144-
expect(browserWindowClass.instanceProperties).toHaveLength(1);
145-
expect(browserWindowClass.instanceProperties[0].name).toBe('id');
148+
expect(cls.instanceProperties).toHaveLength(1);
149+
expect(cls.instanceProperties[0].name).toBe('id');
146150

147-
expect(browserWindowClass.instanceEvents).toHaveLength(1);
148-
expect(browserWindowClass.instanceEvents[0].name).toBe('closed');
149-
}
151+
expect(cls.instanceEvents).toHaveLength(1);
152+
expect(cls.instanceEvents[0].name).toBe('closed');
150153
});
151154

152155
it('should parse a class with static methods and properties', async () => {
@@ -190,14 +193,13 @@ Pops up this menu.
190193
const menuClass = result.find((c) => c.name === 'Menu');
191194
expect(menuClass).toBeDefined();
192195

193-
if (menuClass && menuClass.type === 'Class') {
194-
expect(menuClass.staticMethods).toHaveLength(1);
195-
expect(menuClass.staticMethods[0].name).toBe('buildFromTemplate');
196-
expect(menuClass.staticMethods[0].returns).toBeDefined();
196+
const cls = menuClass as ClassDocumentationContainer;
197+
expect(cls.staticMethods).toHaveLength(1);
198+
expect(cls.staticMethods[0].name).toBe('buildFromTemplate');
199+
expect(cls.staticMethods[0].returns).toBeDefined();
197200

198-
expect(menuClass.staticProperties).toHaveLength(1);
199-
expect(menuClass.staticProperties[0].name).toBe('applicationMenu');
200-
}
201+
expect(cls.staticProperties).toHaveLength(1);
202+
expect(cls.staticProperties[0].name).toBe('applicationMenu');
201203
});
202204

203205
it('should handle module with exported class in multi-package mode', async () => {
@@ -275,16 +277,15 @@ Fired when the navigation is done.
275277
expect(webviewElement?.type).toBe('Element');
276278
expect(webviewElement?.extends).toBe('HTMLElement');
277279

278-
if (webviewElement && webviewElement.type === 'Element') {
279-
expect(webviewElement.methods).toHaveLength(1);
280-
expect(webviewElement.methods[0].name).toBe('loadURL');
280+
const element = webviewElement as ElementDocumentationContainer;
281+
expect(element.methods).toHaveLength(1);
282+
expect(element.methods[0].name).toBe('loadURL');
281283

282-
expect(webviewElement.properties).toHaveLength(1);
283-
expect(webviewElement.properties[0].name).toBe('src');
284+
expect(element.properties).toHaveLength(1);
285+
expect(element.properties[0].name).toBe('src');
284286

285-
expect(webviewElement.events).toHaveLength(1);
286-
expect(webviewElement.events[0].name).toBe('did-finish-load');
287-
}
287+
expect(element.events).toHaveLength(1);
288+
expect(element.events[0].name).toBe('did-finish-load');
288289
});
289290

290291
it('should handle process tags correctly', async () => {
@@ -356,14 +357,13 @@ Additional description after the property list.
356357
expect(rectangleStructure).toBeDefined();
357358
expect(rectangleStructure?.type).toBe('Structure');
358359

359-
if (rectangleStructure && rectangleStructure.type === 'Structure') {
360-
expect(rectangleStructure.properties).toHaveLength(4);
361-
expect(rectangleStructure.properties[0].name).toBe('x');
362-
expect(rectangleStructure.properties[0].type).toBe('Integer');
363-
expect(rectangleStructure.properties[1].name).toBe('y');
364-
expect(rectangleStructure.properties[2].name).toBe('width');
365-
expect(rectangleStructure.properties[3].name).toBe('height');
366-
}
360+
const struct = rectangleStructure as StructureDocumentationContainer;
361+
expect(struct.properties).toHaveLength(4);
362+
expect(struct.properties[0].name).toBe('x');
363+
expect(struct.properties[0].type).toBe('Integer');
364+
expect(struct.properties[1].name).toBe('y');
365+
expect(struct.properties[2].name).toBe('width');
366+
expect(struct.properties[3].name).toBe('height');
367367
});
368368

369369
it('should parse a structure with optional properties', async () => {
@@ -380,13 +380,11 @@ Additional description after the property list.
380380
const parser = new DocsParser(tempDir, '1.0.0', [], [structurePath], 'single');
381381
const result = await parser.parse();
382382

383-
const optionsStructure = result.find((s) => s.name === 'Options');
383+
const optionsStructure = result.find((s) => s.name === 'Options') as StructureDocumentationContainer;
384384

385-
if (optionsStructure && optionsStructure.type === 'Structure') {
386-
expect(optionsStructure.properties[0].required).toBe(false);
387-
expect(optionsStructure.properties[1].required).toBe(false);
388-
expect(optionsStructure.properties[2].required).toBe(true);
389-
}
385+
expect(optionsStructure.properties[0].required).toBe(false);
386+
expect(optionsStructure.properties[1].required).toBe(false);
387+
expect(optionsStructure.properties[2].required).toBe(true);
390388
});
391389

392390
it('should handle structure with extends clause', async () => {

tests/helpers.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ describe('extendError', () => {
3030
expect(newError.stack).toBeNull();
3131
});
3232

33-
it('should handle error-like objects with custom properties', () => {
34-
const customError = { message: 'custom error', stack: 'custom stack trace' };
35-
const newError = extendError('prefix', customError);
33+
it('should handle plain objects with message and stack properties', () => {
34+
const errorLikeObject = { message: 'custom error', stack: 'custom stack trace' };
35+
const newError = extendError('prefix', errorLikeObject);
3636
expect(newError.message).toEqual('prefix - custom error');
3737
expect(newError.stack).toContain(chalk.red('prefix'));
3838
expect(newError.stack).toContain('custom stack trace');
@@ -49,8 +49,6 @@ describe('extendError', () => {
4949
const originalStack = originalError.stack;
5050
const newError = extendError('wrapped', originalError);
5151
expect(newError.stack).toContain(chalk.red('wrapped'));
52-
if (originalStack) {
53-
expect(newError.stack).toContain(originalStack.split('\n').slice(1).join('\n'));
54-
}
52+
expect(newError.stack).toContain(originalStack!.split('\n').slice(1).join('\n'));
5553
});
5654
});

tests/index.spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,9 @@ Test method.
126126
expect(testModule).toBeDefined();
127127
expect(testModule?.type).toBe('Module');
128128

129-
if (testModule && testModule.type === 'Module') {
130-
expect(testModule.exportedClasses).toHaveLength(1);
131-
expect(testModule.exportedClasses[0].name).toBe('TestClass');
132-
}
129+
const module = testModule as ModuleDocumentationContainer;
130+
expect(module.exportedClasses).toHaveLength(1);
131+
expect(module.exportedClasses[0].name).toBe('TestClass');
133132
});
134133

135134
it('should parse structures from structures directory', async () => {

0 commit comments

Comments
 (0)