Skip to content

Commit f89d1c9

Browse files
authored
Merge #338 build: enforce type checks, fix type errors
2 parents 3984910 + ff491d8 commit f89d1c9

24 files changed

+137
-108
lines changed

packages/decorators/src/plugin/autocmd.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ export function Autocmd(name: string, options?: AutocmdOptions) {
1212
pattern: '',
1313
};
1414

15+
// @ts-expect-error changing `option: keyof …` to `option: string` causes other errors.
1516
['pattern', 'eval'].forEach((option: keyof AutocmdOptions) => {
1617
if (options && typeof options[option] !== 'undefined') {
1718
(opts[option] as any) = options[option];
1819
}
1920
});
2021

2122
const nameWithPattern = `${name}${
22-
options.pattern ? `:${options.pattern}` : ''
23+
options?.pattern ? `:${options.pattern}` : ''
2324
}`;
2425
Object.defineProperty(f, NVIM_METHOD_NAME, {
2526
value: `autocmd:${nameWithPattern}`,

packages/decorators/src/plugin/command.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export function Command(name: string, options?: CommandOptions) {
1212
const f = isMethod ? cls[methodName] : cls;
1313
const opts: CommandOptions = {};
1414

15+
// @ts-expect-error changing `option: keyof …` to `option: string` causes other errors.
1516
['range', 'nargs', 'complete'].forEach((option: keyof CommandOptions) => {
1617
if (options && typeof options[option] !== 'undefined') {
1718
(opts[option] as any) = options[option];

packages/decorators/src/plugin/plugin.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export interface PluginWrapperConstructor {
3030
}
3131
function wrapper(cls: PluginWrapperConstructor, options?: PluginOptions): any {
3232
return class WrapperClass extends cls implements PluginWrapperInterface {
33-
public nvim: Neovim;
33+
public nvim!: Neovim;
3434

3535
constructor(plugin: NvimPlugin) {
3636
super(plugin.nvim, plugin);
@@ -57,7 +57,11 @@ function wrapper(cls: PluginWrapperConstructor, options?: PluginOptions): any {
5757
autoCmdOpts.eval = spec.opts.eval;
5858
}
5959

60-
plugin.registerAutocmd(spec.name, [this, method], autoCmdOpts);
60+
plugin.registerAutocmd(
61+
spec.name,
62+
[this, method],
63+
autoCmdOpts as any
64+
);
6165
break;
6266
case 'command':
6367
const cmdOpts: CommandOptions = {
@@ -94,7 +98,7 @@ function wrapper(cls: PluginWrapperConstructor, options?: PluginOptions): any {
9498
});
9599
}
96100

97-
setApi(nvim: Neovim) {
101+
override setApi(nvim: Neovim) {
98102
this.nvim = nvim;
99103
}
100104
};

packages/decorators/src/plugin/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export interface FunctionOptions {
55
}
66

77
export interface AutocmdOptions {
8-
pattern: string;
8+
pattern?: string;
99
eval?: string;
1010
sync?: boolean;
1111
}

packages/neovim/src/api/Base.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Logger, getLogger } from '../utils/logger';
66
import { VimValue } from '../types/VimValue';
77

88
export interface BaseConstructorOptions {
9-
transport?: Transport;
9+
transport: Transport;
1010
logger?: Logger;
1111
data?: Buffer;
1212
metadata?: any;
@@ -28,13 +28,13 @@ const DO_REQUEST = Symbol('DO_REQUEST');
2828
export class BaseApi extends EventEmitter {
2929
protected transport: Transport;
3030

31-
protected _isReady: Promise<boolean>;
31+
protected _isReady: Promise<boolean> = Promise.resolve(false);
3232

33-
protected prefix: string;
33+
protected prefix!: string;
3434

3535
public logger: Logger;
3636

37-
public data: Buffer | number;
37+
public data?: Buffer | number;
3838

3939
// Node Buffer
4040
protected client: any;
@@ -48,7 +48,7 @@ export class BaseApi extends EventEmitter {
4848
}: BaseConstructorOptions) {
4949
super();
5050

51-
this.setTransport(transport);
51+
this.transport = transport;
5252
this.data = data;
5353
this.logger = logger || getLogger();
5454
this.client = client;
@@ -58,10 +58,6 @@ export class BaseApi extends EventEmitter {
5858
}
5959
}
6060

61-
protected setTransport(transport: Transport): void {
62-
this.transport = transport;
63-
}
64-
6561
equals(other: BaseApi): boolean {
6662
try {
6763
return String(this.data) === String(other.data);

packages/neovim/src/api/Buffer.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const DETACH = Symbol('detachBuffer');
3232
export const ATTACH = Symbol('attachBuffer');
3333

3434
export class Buffer extends BaseApi {
35-
public prefix: string = Metadata[ExtType.Buffer].prefix;
35+
public override prefix: string = Metadata[ExtType.Buffer].prefix;
3636

3737
public get isAttached(): boolean {
3838
return this.client.isAttached(this);
@@ -106,9 +106,9 @@ export class Buffer extends BaseApi {
106106
strictIndexing: true,
107107
}
108108
) {
109-
// TODO: Error checking
110-
// if (typeof start === 'undefined' || typeof end === 'undefined') {
111-
// }
109+
if (_start === undefined || _end === undefined) {
110+
throw new Error('start and end are required');
111+
}
112112
const indexing =
113113
typeof strictIndexing === 'undefined' ? true : strictIndexing;
114114
const lines = typeof _lines === 'string' ? [_lines] : _lines;

packages/neovim/src/api/Neovim.test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* eslint-env jest */
22
import * as path from 'node:path';
3-
import { Neovim } from './Neovim';
43
import * as testUtil from '../testUtil';
54

65
describe('Neovim API', () => {
@@ -14,14 +13,6 @@ describe('Neovim API', () => {
1413
testUtil.stopNvim();
1514
});
1615

17-
it('sets transport when initialized', () => {
18-
const transport = {};
19-
const spy = jest.spyOn(Neovim.prototype, 'setTransport');
20-
const neovim = new Neovim({ transport });
21-
expect(spy).toHaveBeenCalledWith(transport);
22-
expect(neovim.transport).toBe(transport);
23-
});
24-
2516
describe('Normal API calls', () => {
2617
it('gets a list of buffers and switches buffers', async () => {
2718
const buffers = await nvim.buffers;

packages/neovim/src/api/Neovim.ts

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export interface OpenWindowOptions {
128128
* Neovim API
129129
*/
130130
export class Neovim extends BaseApi {
131-
protected prefix = 'nvim_';
131+
protected override prefix = 'nvim_';
132132

133133
public Buffer = Buffer;
134134

@@ -202,8 +202,8 @@ export class Neovim extends BaseApi {
202202
/**
203203
* Gets a map of buffer-local |user-commands|.
204204
*
205-
* @param {Object} options Optional parameters (currently not used)
206-
* @return {Object} Map of maps describing commands
205+
* @param options Optional parameters (currently not used)
206+
* @return Map of maps describing commands
207207
*/
208208
getCommands(options = {}): Promise<Command> {
209209
return this.request(`${this.prefix}get_commands`, [options]);
@@ -248,7 +248,7 @@ export class Neovim extends BaseApi {
248248
/**
249249
* Gets the current window
250250
*
251-
* @return {Window} Window handle
251+
* @return Window handle
252252
*/
253253
get window(): AsyncWindow {
254254
return this.getWindow();
@@ -257,7 +257,7 @@ export class Neovim extends BaseApi {
257257
/**
258258
* Sets the current window
259259
*
260-
* @param {Window} Window handle
260+
* @param win Window handle
261261
*/
262262
set window(win: AsyncWindow) {
263263
if (win instanceof Window) this.setWindow(win);
@@ -287,7 +287,7 @@ export class Neovim extends BaseApi {
287287
/**
288288
* Sets the current window
289289
*
290-
* @param {Window} Window handle
290+
* @param win Window handle
291291
*/
292292
setWindow(win: Window) {
293293
// Throw error if win is not instance of Window?
@@ -306,7 +306,7 @@ export class Neovim extends BaseApi {
306306
/**
307307
* Changes the global working directory
308308
*
309-
* @param {String} Directory path
309+
* @param dir Directory path
310310
*
311311
*/
312312
set dir(dir: string) {
@@ -365,7 +365,7 @@ export class Neovim extends BaseApi {
365365
/**
366366
* Gets the current mode. |mode()| "blocking" is true if Nvim is waiting for input.
367367
*
368-
* @return {Object} Dictionary { "mode": String, "blocking": Boolean }
368+
* @return Mode info
369369
*/
370370
get mode(): Promise<{ mode: string; blocking: boolean }> {
371371
return this.request(`${this.prefix}get_mode`);
@@ -374,7 +374,7 @@ export class Neovim extends BaseApi {
374374
/**
375375
* Gets map of defined colors
376376
*
377-
* @return {Object} Color map
377+
* @return Color map
378378
*/
379379
get colorMap(): Promise<{ [name: string]: number }> {
380380
return this.request(`${this.prefix}get_color_map`);
@@ -383,8 +383,8 @@ export class Neovim extends BaseApi {
383383
/**
384384
* Get color by name
385385
*
386-
* @param {String} name Color name
387-
* @return {Number} Color value
386+
* @param name Color name
387+
* @return Color value
388388
*/
389389
getColorByName(name: string): Promise<number> {
390390
return this.request(`${this.prefix}get_color_by_name`, [name]);
@@ -393,9 +393,9 @@ export class Neovim extends BaseApi {
393393
/**
394394
* Get highlight by name or id
395395
*
396-
* @param {String|Number} nameOrId Name or ID
397-
* @param {Boolean} isRgb Should export RGB colors
398-
* @return {Object} Highlight definition map
396+
* @param nameOrId Name or ID
397+
* @param isRgb Should export RGB colors
398+
* @return Highlight definition map
399399
*/
400400
getHighlight(
401401
nameOrId: string | number,
@@ -411,9 +411,9 @@ export class Neovim extends BaseApi {
411411
/**
412412
* Get highlight definition by name
413413
*
414-
* @param {String} name Highlight group name
415-
* @param {Boolean} isRgb Should export RGB colors
416-
* @return {Object} Highlight definition map
414+
* @param name Highlight group name
415+
* @param isRgb Should export RGB colors
416+
* @return Highlight definition map
417417
*/
418418
getHighlightByName(name: string, isRgb = true): Promise<object> {
419419
return this.request(`${this.prefix}get_hl_by_name`, [name, isRgb]);
@@ -422,9 +422,9 @@ export class Neovim extends BaseApi {
422422
/**
423423
* Get highlight definition by id |hlID()|
424424
*
425-
* @param {Number} id Highlight id as returned by |hlID()|
426-
* @param {Boolean} isRgb Should export RGB colors
427-
* @return {Object} Highlight definition map
425+
* @param id Highlight id as returned by |hlID()|
426+
* @param isRgb Should export RGB colors
427+
* @return Highlight definition map
428428
*/
429429
getHighlightById(id: number, isRgb = true): Promise<object> {
430430
return this.request(`${this.prefix}get_hl_by_id`, [id, isRgb]);
@@ -839,8 +839,8 @@ export class Neovim extends BaseApi {
839839
* existing namespace, the associated id is returned. If `name`
840840
* is an empty string a new, anonymous namespace is created.
841841
*
842-
* @param {String} name Namespace name or empty string
843-
* @return {Number} Namespace id
842+
* @param name Namespace name or empty string
843+
* @return Namespace id
844844
*/
845845
createNamespace(name = ''): Promise<number> {
846846
return this.request(`${this.prefix}create_namespace`, [name]);
@@ -962,10 +962,9 @@ export class Neovim extends BaseApi {
962962
* and `relative` must be reconfigured together. Only changing a
963963
* subset of these is an error.
964964
*
965-
* @param {Window} window Window handle
966-
* @param {Number} width Width of window (in character cells)
967-
* @param {Number} height Height of window (in character cells)
968-
* @Param {Object} options Options object
965+
* @param window Window handle
966+
* @param options height, width of window (in character cells)
967+
* @Param Options object
969968
*/
970969
private winConfig(window: Window, options: object = {}) {
971970
return window.config(options);

packages/neovim/src/api/Tabpage.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { createChainableApi } from './utils/createChainableApi';
44
import { Window, AsyncWindow } from './Window';
55

66
export class Tabpage extends BaseApi {
7-
public prefix: string = Metadata[ExtType.Tabpage].prefix;
7+
public override prefix: string = Metadata[ExtType.Tabpage].prefix;
88

99
/** Returns all windows of tabpage */
1010
get windows(): Promise<Window[]> {
@@ -30,12 +30,12 @@ export class Tabpage extends BaseApi {
3030
}
3131

3232
/** Invalid */
33-
getOption(): void {
33+
override getOption(): void {
3434
this.logger.error('Tabpage does not have `getOption`');
3535
}
3636

3737
/** Invalid */
38-
setOption(): void {
38+
override setOption(): void {
3939
this.logger.error('Tabpage does not have `setOption`');
4040
}
4141
}

packages/neovim/src/api/Window.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { Tabpage, AsyncTabpage } from './Tabpage';
55
import { Buffer, AsyncBuffer } from './Buffer';
66

77
export class Window extends BaseApi {
8-
public prefix: string = Metadata[ExtType.Window].prefix;
8+
public override prefix: string = Metadata[ExtType.Window].prefix;
99

1010
/**
1111
* The windowid that not change within a Vim session

0 commit comments

Comments
 (0)