Skip to content

Commit

Permalink
Fix broken linter
Browse files Browse the repository at this point in the history
Fixes #606

XO started failing with some internal ESLint error for some reason,
so I updated XO along with its config and plugins to fix this. It
also required some minor code changes to fix new errors that popped up.
  • Loading branch information
vadimdemedes committed Jul 16, 2023
1 parent 8a04760 commit 5b1e57c
Show file tree
Hide file tree
Showing 16 changed files with 49 additions and 28 deletions.
2 changes: 1 addition & 1 deletion benchmark/static/static.tsx
Expand Up @@ -10,7 +10,7 @@ function App() {
const itemCountRef = React.useRef(0);

React.useEffect(() => {
let timer;
let timer: NodeJS.Timeout | undefined;

const run = () => {
if (itemCountRef.current++ > 1000) {
Expand Down
2 changes: 1 addition & 1 deletion examples/jest/jest.tsx
Expand Up @@ -32,7 +32,7 @@ type State = {
};

class Jest extends React.Component<Record<string, unknown>, State> {
constructor(props) {
constructor(props: Record<string, unknown>) {
super(props);

this.state = {
Expand Down
2 changes: 1 addition & 1 deletion examples/static/static.tsx
Expand Up @@ -11,7 +11,7 @@ function Example() {

React.useEffect(() => {
let completedTests = 0;
let timer;
let timer: NodeJS.Timeout | undefined;

const run = () => {
if (completedTests++ < 10) {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Expand Up @@ -102,7 +102,7 @@
"strip-ansi": "^6.0.0",
"ts-node": "10.9.1",
"typescript": "^4.9.4",
"xo": "^0.53.0"
"xo": "^0.55.0"
},
"peerDependencies": {
"@types/react": ">=18.0.0",
Expand Down Expand Up @@ -180,7 +180,9 @@
"@typescript-eslint/explicit-function-return": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"dot-notation": "off",
"react/boolean-prop-naming": "off"
"react/boolean-prop-naming": "off",
"unicorn/prefer-dom-node-remove": "off",
"unicorn/prefer-event-target": "off"
}
},
{
Expand Down
3 changes: 1 addition & 2 deletions src/components/App.tsx
Expand Up @@ -247,8 +247,7 @@ export default class App extends PureComponent<Props, State> {

focusPrevious = (): void => {
this.setState(previousState => {
const lastFocusableId =
previousState.focusables[previousState.focusables.length - 1]?.id;
const lastFocusableId = previousState.focusables.at(-1)?.id;
const previousFocusableId = this.findPreviousFocusable(previousState);

return {
Expand Down
2 changes: 2 additions & 0 deletions src/devtools.ts
@@ -1,3 +1,5 @@
/* eslint-disable import/order */

// eslint-disable-next-line import/no-unassigned-import
import './devtools-window-polyfill.js';

Expand Down
2 changes: 2 additions & 0 deletions src/global.d.ts
Expand Up @@ -26,6 +26,8 @@ declare namespace Ink {
children?: ReactNode;
key?: Key;
style?: Styles;

// eslint-disable-next-line @typescript-eslint/naming-convention
internal_transform?: (children: string) => string;
};
}
2 changes: 1 addition & 1 deletion src/output.ts
Expand Up @@ -114,7 +114,7 @@ export default class Output {
let {x, y} = operation;
let lines = text.split('\n');

const clip = clips[clips.length - 1];
const clip = clips.at(-1);

if (clip) {
const clipHorizontally =
Expand Down
1 change: 0 additions & 1 deletion src/reconciler.ts
Expand Up @@ -26,7 +26,6 @@ import {type OutputTransformer} from './render-node-to-output.js';
if (process.env['DEV'] === 'true') {
try {
await import('./devtools.js');
// eslint-disable-next-line @typescript-eslint/no-implicit-any-catch
} catch (error: any) {
if (error.code === 'MODULE_NOT_FOUND') {
console.warn(
Expand Down
12 changes: 5 additions & 7 deletions src/render.ts
@@ -1,6 +1,6 @@
import {Stream} from 'node:stream';
import process from 'node:process';
import {type ReactElement} from 'react';
import type {ReactNode} from 'react';
import Ink, {type Options as InkOptions} from './ink.js';
import instances from './instances.js';

Expand Down Expand Up @@ -64,15 +64,13 @@ export type Instance = {
clear: () => void;
};

type RenderFunction = <Props, K extends NodeJS.WriteStream | RenderOptions>(
tree: ReactElement<Props>,
options?: K
) => Instance;

/**
* Mount a component and render the output.
*/
const render: RenderFunction = (node, options): Instance => {
const render = (
node: ReactNode,
options?: NodeJS.WriteStream | RenderOptions
): Instance => {
const inkOptions: InkOptions = {
stdout: process.stdout,
stdin: process.stdin,
Expand Down
4 changes: 3 additions & 1 deletion test/components.tsx
@@ -1,5 +1,5 @@
import EventEmitter from 'node:events';
import React, {useState, Component, FC} from 'react';
import React, {useState, Component} from 'react';
import chalk from 'chalk';
import {spy} from 'sinon';
import test from 'ava';
Expand Down Expand Up @@ -465,6 +465,7 @@ test('disable raw mode when all input components are unmounted', t => {

const {rerender} = render(
<Test renderFirstInput renderSecondInput />,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
options as any
);

Expand Down Expand Up @@ -596,6 +597,7 @@ test('render different component based on whether stdin is a TTY or not', t => {

const {rerender} = render(
<Test renderFirstInput renderSecondInput />,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
options as any
);

Expand Down
2 changes: 1 addition & 1 deletion test/errors.tsx
Expand Up @@ -25,7 +25,7 @@ test('catch and display error', t => {
render(<Test />, {stdout});

t.deepEqual(
stripAnsi((stdout.write as any).lastCall.args[0])
stripAnsi((stdout.write as any).lastCall.args[0] as string)
.split('\n')
.slice(0, 14),
[
Expand Down
2 changes: 1 addition & 1 deletion test/focus.tsx
Expand Up @@ -7,7 +7,7 @@ import {render, Box, Text, useFocus, useFocusManager} from '../src/index.js';
import createStdout from './helpers/create-stdout.js';

const createStdin = () => {
const stdin = new EventEmitter() as NodeJS.WriteStream;
const stdin = new EventEmitter() as unknown as NodeJS.WriteStream;
stdin.isTTY = true;
stdin.setRawMode = spy();
stdin.setEncoding = () => {};
Expand Down
2 changes: 1 addition & 1 deletion test/helpers/create-stdout.ts
Expand Up @@ -7,7 +7,7 @@ type FakeStdout = {
} & NodeJS.WriteStream;

const createStdout = (columns?: number): FakeStdout => {
const stdout = new EventEmitter() as FakeStdout;
const stdout = new EventEmitter() as unknown as FakeStdout;
stdout.columns = columns ?? 100;

const write = spy();
Expand Down
29 changes: 23 additions & 6 deletions test/measure-element.tsx
Expand Up @@ -2,18 +2,28 @@ import React, {useState, useRef, useEffect} from 'react';
import test from 'ava';
import delay from 'delay';
import stripAnsi from 'strip-ansi';
import {Box, Text, render, measureElement} from '../src/index.js';
import {
Box,
Text,
render,
measureElement,
type DOMElement
} from '../src/index.js';
import createStdout from './helpers/create-stdout.js';

test('measure element', async t => {
const stdout = createStdout();

function Test() {
const [width, setWidth] = useState(0);
const ref = useRef(null);
const ref = useRef<DOMElement>(null);

useEffect(() => {
setWidth(measureElement(ref.current as any).width);
if (!ref.current) {
return;
}

setWidth(measureElement(ref.current).width);
}, []);

return (
Expand All @@ -34,10 +44,14 @@ test.serial('calculate layout while rendering is throttled', async t => {

function Test() {
const [width, setWidth] = useState(0);
const ref = useRef(null);
const ref = useRef<DOMElement>(null);

useEffect(() => {
setWidth(measureElement(ref.current as any).width);
if (!ref.current) {
return;
}

setWidth(measureElement(ref.current).width);
}, []);

return (
Expand All @@ -51,5 +65,8 @@ test.serial('calculate layout while rendering is throttled', async t => {
rerender(<Test />);
await delay(50);

t.is(stripAnsi((stdout.write as any).lastCall.firstArg).trim(), 'Width: 100');
t.is(
stripAnsi((stdout.write as any).lastCall.firstArg as string).trim(),
'Width: 100'
);
});
4 changes: 2 additions & 2 deletions test/render.tsx
Expand Up @@ -161,7 +161,7 @@ test.serial('rerender on resize', async t => {
const {unmount} = render(<Test />, {stdout});

t.is(
stripAnsi((stdout.write as any).firstCall.args[0]),
stripAnsi((stdout.write as any).firstCall.args[0] as string),
boxen('Test'.padEnd(8), {borderStyle: 'round'}) + '\n'
);

Expand All @@ -172,7 +172,7 @@ test.serial('rerender on resize', async t => {
await delay(100);

t.is(
stripAnsi((stdout.write as any).lastCall.args[0]),
stripAnsi((stdout.write as any).lastCall.args[0] as string),
boxen('Test'.padEnd(6), {borderStyle: 'round'}) + '\n'
);

Expand Down

0 comments on commit 5b1e57c

Please sign in to comment.