Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: enforce type checks, fix type errors #338

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/decorators/src/plugin/autocmd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ export function Autocmd(name: string, options?: AutocmdOptions) {
pattern: '',
};

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

const nameWithPattern = `${name}${
options.pattern ? `:${options.pattern}` : ''
options?.pattern ? `:${options.pattern}` : ''
}`;
Object.defineProperty(f, NVIM_METHOD_NAME, {
value: `autocmd:${nameWithPattern}`,
Expand Down
1 change: 1 addition & 0 deletions packages/decorators/src/plugin/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export function Command(name: string, options?: CommandOptions) {
const f = isMethod ? cls[methodName] : cls;
const opts: CommandOptions = {};

// @ts-expect-error changing `option: keyof …` to `option: string` causes other errors.
['range', 'nargs', 'complete'].forEach((option: keyof CommandOptions) => {
if (options && typeof options[option] !== 'undefined') {
(opts[option] as any) = options[option];
Expand Down
10 changes: 7 additions & 3 deletions packages/decorators/src/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export interface PluginWrapperConstructor {
}
function wrapper(cls: PluginWrapperConstructor, options?: PluginOptions): any {
return class WrapperClass extends cls implements PluginWrapperInterface {
public nvim: Neovim;
public nvim!: Neovim;

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

plugin.registerAutocmd(spec.name, [this, method], autoCmdOpts);
plugin.registerAutocmd(
spec.name,
[this, method],
autoCmdOpts as any
);
break;
case 'command':
const cmdOpts: CommandOptions = {
Expand Down Expand Up @@ -94,7 +98,7 @@ function wrapper(cls: PluginWrapperConstructor, options?: PluginOptions): any {
});
}

setApi(nvim: Neovim) {
override setApi(nvim: Neovim) {
this.nvim = nvim;
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/decorators/src/plugin/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export interface FunctionOptions {
}

export interface AutocmdOptions {
pattern: string;
pattern?: string;
eval?: string;
sync?: boolean;
}
Expand Down
14 changes: 5 additions & 9 deletions packages/neovim/src/api/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { Logger, getLogger } from '../utils/logger';
import { VimValue } from '../types/VimValue';

export interface BaseConstructorOptions {
transport?: Transport;
transport: Transport;
logger?: Logger;
data?: Buffer;
metadata?: any;
Expand All @@ -28,13 +28,13 @@ const DO_REQUEST = Symbol('DO_REQUEST');
export class BaseApi extends EventEmitter {
protected transport: Transport;

protected _isReady: Promise<boolean>;
protected _isReady: Promise<boolean> = Promise.resolve(false);

protected prefix: string;
protected prefix!: string;

public logger: Logger;

public data: Buffer | number;
public data?: Buffer | number;

// Node Buffer
protected client: any;
Expand All @@ -48,7 +48,7 @@ export class BaseApi extends EventEmitter {
}: BaseConstructorOptions) {
super();

this.setTransport(transport);
this.transport = transport;
this.data = data;
this.logger = logger || getLogger();
this.client = client;
Expand All @@ -58,10 +58,6 @@ export class BaseApi extends EventEmitter {
}
}

protected setTransport(transport: Transport): void {
this.transport = transport;
}

equals(other: BaseApi): boolean {
try {
return String(this.data) === String(other.data);
Expand Down
8 changes: 4 additions & 4 deletions packages/neovim/src/api/Buffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
export const ATTACH = Symbol('attachBuffer');

export class Buffer extends BaseApi {
public prefix: string = Metadata[ExtType.Buffer].prefix;
public override prefix: string = Metadata[ExtType.Buffer].prefix;

public get isAttached(): boolean {
return this.client.isAttached(this);
Expand Down Expand Up @@ -106,9 +106,9 @@
strictIndexing: true,
}
) {
// TODO: Error checking
// if (typeof start === 'undefined' || typeof end === 'undefined') {
// }
if (_start === undefined || _end === undefined) {
throw new Error('start and end are required');

Check warning on line 110 in packages/neovim/src/api/Buffer.ts

View check run for this annotation

Codecov / codecov/patch

packages/neovim/src/api/Buffer.ts#L110

Added line #L110 was not covered by tests
}
const indexing =
typeof strictIndexing === 'undefined' ? true : strictIndexing;
const lines = typeof _lines === 'string' ? [_lines] : _lines;
Expand Down
9 changes: 0 additions & 9 deletions packages/neovim/src/api/Neovim.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-env jest */
import * as path from 'node:path';
import { Neovim } from './Neovim';
import * as testUtil from '../testUtil';

describe('Neovim API', () => {
Expand All @@ -14,14 +13,6 @@ describe('Neovim API', () => {
testUtil.stopNvim();
});

it('sets transport when initialized', () => {
const transport = {};
const spy = jest.spyOn(Neovim.prototype, 'setTransport');
const neovim = new Neovim({ transport });
expect(spy).toHaveBeenCalledWith(transport);
expect(neovim.transport).toBe(transport);
});

describe('Normal API calls', () => {
it('gets a list of buffers and switches buffers', async () => {
const buffers = await nvim.buffers;
Expand Down
51 changes: 25 additions & 26 deletions packages/neovim/src/api/Neovim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export interface OpenWindowOptions {
* Neovim API
*/
export class Neovim extends BaseApi {
protected prefix = 'nvim_';
protected override prefix = 'nvim_';

public Buffer = Buffer;

Expand Down Expand Up @@ -202,8 +202,8 @@ export class Neovim extends BaseApi {
/**
* Gets a map of buffer-local |user-commands|.
*
* @param {Object} options Optional parameters (currently not used)
* @return {Object} Map of maps describing commands
* @param options Optional parameters (currently not used)
* @return Map of maps describing commands
*/
getCommands(options = {}): Promise<Command> {
return this.request(`${this.prefix}get_commands`, [options]);
Expand Down Expand Up @@ -248,7 +248,7 @@ export class Neovim extends BaseApi {
/**
* Gets the current window
*
* @return {Window} Window handle
* @return Window handle
*/
get window(): AsyncWindow {
return this.getWindow();
Expand All @@ -257,7 +257,7 @@ export class Neovim extends BaseApi {
/**
* Sets the current window
*
* @param {Window} Window handle
* @param win Window handle
*/
set window(win: AsyncWindow) {
if (win instanceof Window) this.setWindow(win);
Expand Down Expand Up @@ -287,7 +287,7 @@ export class Neovim extends BaseApi {
/**
* Sets the current window
*
* @param {Window} Window handle
* @param win Window handle
*/
setWindow(win: Window) {
// Throw error if win is not instance of Window?
Expand All @@ -306,7 +306,7 @@ export class Neovim extends BaseApi {
/**
* Changes the global working directory
*
* @param {String} Directory path
* @param dir Directory path
*
*/
set dir(dir: string) {
Expand Down Expand Up @@ -365,7 +365,7 @@ export class Neovim extends BaseApi {
/**
* Gets the current mode. |mode()| "blocking" is true if Nvim is waiting for input.
*
* @return {Object} Dictionary { "mode": String, "blocking": Boolean }
* @return Mode info
*/
get mode(): Promise<{ mode: string; blocking: boolean }> {
return this.request(`${this.prefix}get_mode`);
Expand All @@ -374,7 +374,7 @@ export class Neovim extends BaseApi {
/**
* Gets map of defined colors
*
* @return {Object} Color map
* @return Color map
*/
get colorMap(): Promise<{ [name: string]: number }> {
return this.request(`${this.prefix}get_color_map`);
Expand All @@ -383,8 +383,8 @@ export class Neovim extends BaseApi {
/**
* Get color by name
*
* @param {String} name Color name
* @return {Number} Color value
* @param name Color name
* @return Color value
*/
getColorByName(name: string): Promise<number> {
return this.request(`${this.prefix}get_color_by_name`, [name]);
Expand All @@ -393,9 +393,9 @@ export class Neovim extends BaseApi {
/**
* Get highlight by name or id
*
* @param {String|Number} nameOrId Name or ID
* @param {Boolean} isRgb Should export RGB colors
* @return {Object} Highlight definition map
* @param nameOrId Name or ID
* @param isRgb Should export RGB colors
* @return Highlight definition map
*/
getHighlight(
nameOrId: string | number,
Expand All @@ -411,9 +411,9 @@ export class Neovim extends BaseApi {
/**
* Get highlight definition by name
*
* @param {String} name Highlight group name
* @param {Boolean} isRgb Should export RGB colors
* @return {Object} Highlight definition map
* @param name Highlight group name
* @param isRgb Should export RGB colors
* @return Highlight definition map
*/
getHighlightByName(name: string, isRgb = true): Promise<object> {
return this.request(`${this.prefix}get_hl_by_name`, [name, isRgb]);
Expand All @@ -422,9 +422,9 @@ export class Neovim extends BaseApi {
/**
* Get highlight definition by id |hlID()|
*
* @param {Number} id Highlight id as returned by |hlID()|
* @param {Boolean} isRgb Should export RGB colors
* @return {Object} Highlight definition map
* @param id Highlight id as returned by |hlID()|
* @param isRgb Should export RGB colors
* @return Highlight definition map
*/
getHighlightById(id: number, isRgb = true): Promise<object> {
return this.request(`${this.prefix}get_hl_by_id`, [id, isRgb]);
Expand Down Expand Up @@ -839,8 +839,8 @@ export class Neovim extends BaseApi {
* existing namespace, the associated id is returned. If `name`
* is an empty string a new, anonymous namespace is created.
*
* @param {String} name Namespace name or empty string
* @return {Number} Namespace id
* @param name Namespace name or empty string
* @return Namespace id
*/
createNamespace(name = ''): Promise<number> {
return this.request(`${this.prefix}create_namespace`, [name]);
Expand Down Expand Up @@ -962,10 +962,9 @@ export class Neovim extends BaseApi {
* and `relative` must be reconfigured together. Only changing a
* subset of these is an error.
*
* @param {Window} window Window handle
* @param {Number} width Width of window (in character cells)
* @param {Number} height Height of window (in character cells)
* @Param {Object} options Options object
* @param window Window handle
* @param options height, width of window (in character cells)
* @Param Options object
*/
private winConfig(window: Window, options: object = {}) {
return window.config(options);
Expand Down
6 changes: 3 additions & 3 deletions packages/neovim/src/api/Tabpage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createChainableApi } from './utils/createChainableApi';
import { Window, AsyncWindow } from './Window';

export class Tabpage extends BaseApi {
public prefix: string = Metadata[ExtType.Tabpage].prefix;
public override prefix: string = Metadata[ExtType.Tabpage].prefix;

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

/** Invalid */
getOption(): void {
override getOption(): void {
this.logger.error('Tabpage does not have `getOption`');
}

/** Invalid */
setOption(): void {
override setOption(): void {
this.logger.error('Tabpage does not have `setOption`');
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/neovim/src/api/Window.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Tabpage, AsyncTabpage } from './Tabpage';
import { Buffer, AsyncBuffer } from './Buffer';

export class Window extends BaseApi {
public prefix: string = Metadata[ExtType.Window].prefix;
public override prefix: string = Metadata[ExtType.Window].prefix;

/**
* The windowid that not change within a Vim session
Expand Down
Loading
Loading