Skip to content

Commit

Permalink
[Expressions] Remove the any type usages (elastic#113477)
Browse files Browse the repository at this point in the history
* Update ESLint config to disallow usage of the any type
* Remove the any type usages from the expressions plugin
* Update plugins using expressions according to the updated public API
  • Loading branch information
dokmic committed Oct 4, 2021
1 parent 98d7a37 commit 11e5b35
Show file tree
Hide file tree
Showing 68 changed files with 420 additions and 299 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export function ActionsExpressionsExample({ expressions, actions }: Props) {
};

const handleEvents = (event: any) => {
if (event.id !== 'NAVIGATE') return;
if (event.name !== 'NAVIGATE') return;
// enrich event context with some extra data
event.baseUrl = 'http://www.google.com';

actions.executeTriggerActions(NAVIGATE_TRIGGER_ID, event.value);
actions.executeTriggerActions(NAVIGATE_TRIGGER_ID, event.data);
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export function ActionsExpressionsExample2({ expressions, actions }: Props) {
};

const handleEvents = (event: any) => {
updateVariables({ color: event.value.href === 'http://www.google.com' ? 'red' : 'blue' });
updateVariables({ color: event.data.href === 'http://www.google.com' ? 'red' : 'blue' });
};

return (
Expand Down
4 changes: 2 additions & 2 deletions examples/expressions_explorer/public/renderers/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const buttonRenderer: ExpressionRenderDefinition<any> = {
render(domNode, config, handlers) {
const buttonClick = () => {
handlers.event({
id: 'NAVIGATE',
value: {
name: 'NAVIGATE',
data: {
href: config.href,
},
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { functionWrapper } from '../../../../expressions/common/expression_funct
import { ExpressionValueVisDimension } from '../../../../visualizations/public';
import { Datatable } from '../../../../expressions/common/expression_types/specs';

type Arguments = Parameters<ReturnType<typeof tagcloudFunction>['fn']>[1];

describe('interpreter/functions#tagcloud', () => {
const fn = functionWrapper(tagcloudFunction());
const column1 = 'Count';
Expand All @@ -26,7 +28,7 @@ describe('interpreter/functions#tagcloud', () => {
{ [column1]: 0, [column2]: 'US' },
{ [column1]: 10, [column2]: 'UK' },
],
};
} as unknown as Datatable;
const visConfig = {
scale: 'linear',
orientation: 'single',
Expand Down Expand Up @@ -73,12 +75,12 @@ describe('interpreter/functions#tagcloud', () => {
};

it('returns an object with the correct structure for number accessors', () => {
const actual = fn(context, { ...visConfig, ...numberAccessors }, undefined);
const actual = fn(context, { ...visConfig, ...numberAccessors } as Arguments, undefined);
expect(actual).toMatchSnapshot();
});

it('returns an object with the correct structure for string accessors', () => {
const actual = fn(context, { ...visConfig, ...stringAccessors }, undefined);
const actual = fn(context, { ...visConfig, ...stringAccessors } as Arguments, undefined);
expect(actual).toMatchSnapshot();
});

Expand All @@ -93,7 +95,7 @@ describe('interpreter/functions#tagcloud', () => {
},
},
};
await fn(context, { ...visConfig, ...numberAccessors }, handlers as any);
await fn(context, { ...visConfig, ...numberAccessors } as Arguments, handlers as any);

expect(loggedTable!).toMatchSnapshot();
});
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/expressions/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"rules": {
"@typescript-eslint/consistent-type-definitions": 0
"@typescript-eslint/consistent-type-definitions": 0,
"@typescript-eslint/no-explicit-any": ["error", { "ignoreRestArgs": true }]
}
}
8 changes: 4 additions & 4 deletions src/plugins/expressions/common/ast/build_expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ import { parse } from './parse';
* @param val Value you want to check.
* @return boolean
*/
export function isExpressionAstBuilder(val: any): val is ExpressionAstExpressionBuilder {
return val?.type === 'expression_builder';
export function isExpressionAstBuilder(val: unknown): val is ExpressionAstExpressionBuilder {
return (val as Record<string, unknown> | undefined)?.type === 'expression_builder';
}

/** @internal */
export function isExpressionAst(val: any): val is ExpressionAstExpression {
return val?.type === 'expression';
export function isExpressionAst(val: unknown): val is ExpressionAstExpression {
return (val as Record<string, unknown> | undefined)?.type === 'expression';
}

export interface ExpressionAstExpressionBuilder {
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/expressions/common/ast/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export type ExpressionAstFunctionDebug = {
/**
* Raw error that was thrown by the function, if any.
*/
rawError?: any | Error;
rawError?: any | Error; // eslint-disable-line @typescript-eslint/no-explicit-any

/**
* Time in milliseconds it took to execute the function. Duration can be
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('Execution abortion tests', () => {
const completed = jest.fn();
const aborted = jest.fn();

const defer: ExpressionFunctionDefinition<'defer', any, { time: number }, any> = {
const defer: ExpressionFunctionDefinition<'defer', unknown, { time: number }, unknown> = {
name: 'defer',
args: {
time: {
Expand Down
46 changes: 28 additions & 18 deletions src/plugins/expressions/common/execution/execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { ExecutionContract } from './execution_contract';

beforeAll(() => {
if (typeof performance === 'undefined') {
(global as any).performance = { now: Date.now };
global.performance = { now: Date.now } as typeof performance;
}
});

Expand All @@ -41,7 +41,7 @@ const createExecution = (
const run = async (
expression: string = 'foo bar=123',
context?: Record<string, unknown>,
input: any = null
input: unknown = null
) => {
const execution = createExecution(expression, context);
execution.start(input);
Expand Down Expand Up @@ -262,45 +262,45 @@ describe('Execution', () => {

describe('execution context', () => {
test('context.variables is an object', async () => {
const { result } = (await run('introspectContext key="variables"')) as any;
const { result } = await run('introspectContext key="variables"');

expect(result).toHaveProperty('result', expect.any(Object));
});

test('context.types is an object', async () => {
const { result } = (await run('introspectContext key="types"')) as any;
const { result } = await run('introspectContext key="types"');

expect(result).toHaveProperty('result', expect.any(Object));
});

test('context.abortSignal is an object', async () => {
const { result } = (await run('introspectContext key="abortSignal"')) as any;
const { result } = await run('introspectContext key="abortSignal"');

expect(result).toHaveProperty('result', expect.any(Object));
});

test('context.inspectorAdapters is an object', async () => {
const { result } = (await run('introspectContext key="inspectorAdapters"')) as any;
const { result } = await run('introspectContext key="inspectorAdapters"');

expect(result).toHaveProperty('result', expect.any(Object));
});

test('context.getKibanaRequest is a function if provided', async () => {
const { result } = (await run('introspectContext key="getKibanaRequest"', {
const { result } = await run('introspectContext key="getKibanaRequest"', {
kibanaRequest: {},
})) as any;
});

expect(result).toHaveProperty('result', expect.any(Function));
});

test('context.getKibanaRequest is undefined if not provided', async () => {
const { result } = (await run('introspectContext key="getKibanaRequest"')) as any;
const { result } = await run('introspectContext key="getKibanaRequest"');

expect(result).toHaveProperty('result', undefined);
});

test('unknown context key is undefined', async () => {
const { result } = (await run('introspectContext key="foo"')) as any;
const { result } = await run('introspectContext key="foo"');

expect(result).toHaveProperty('result', undefined);
});
Expand All @@ -314,7 +314,7 @@ describe('Execution', () => {

describe('inspector adapters', () => {
test('by default, "tables" and "requests" inspector adapters are available', async () => {
const { result } = (await run('introspectContext key="inspectorAdapters"')) as any;
const { result } = await run('introspectContext key="inspectorAdapters"');
expect(result).toHaveProperty(
'result',
expect.objectContaining({
Expand All @@ -326,9 +326,9 @@ describe('Execution', () => {

test('can set custom inspector adapters', async () => {
const inspectorAdapters = {};
const { result } = (await run('introspectContext key="inspectorAdapters"', {
const { result } = await run('introspectContext key="inspectorAdapters"', {
inspectorAdapters,
})) as any;
});
expect(result).toHaveProperty('result', inspectorAdapters);
});

Expand All @@ -351,7 +351,7 @@ describe('Execution', () => {

describe('expression abortion', () => {
test('context has abortSignal object', async () => {
const { result } = (await run('introspectContext key="abortSignal"')) as any;
const { result } = await run('introspectContext key="abortSignal"');

expect(result).toHaveProperty('result.aborted', false);
});
Expand Down Expand Up @@ -400,7 +400,7 @@ describe('Execution', () => {
testScheduler.run(({ cold, expectObservable }) => {
const arg = cold(' -a-b-c|', { a: 1, b: 2, c: 3 });
const expected = ' -a-b-c|';
const observable: ExpressionFunctionDefinition<'observable', any, {}, any> = {
const observable: ExpressionFunctionDefinition<'observable', unknown, {}, unknown> = {
name: 'observable',
args: {},
help: '',
Expand Down Expand Up @@ -468,7 +468,7 @@ describe('Execution', () => {
});

test('does not execute remaining functions in pipeline', async () => {
const spy: ExpressionFunctionDefinition<'spy', any, {}, any> = {
const spy: ExpressionFunctionDefinition<'spy', unknown, {}, unknown> = {
name: 'spy',
args: {},
help: '',
Expand Down Expand Up @@ -621,7 +621,12 @@ describe('Execution', () => {
help: '',
fn: () => arg2,
};
const max: ExpressionFunctionDefinition<'max', any, { val1: number; val2: number }, any> = {
const max: ExpressionFunctionDefinition<
'max',
unknown,
{ val1: number; val2: number },
unknown
> = {
name: 'max',
args: {
val1: { help: '', types: ['number'] },
Expand Down Expand Up @@ -679,7 +684,12 @@ describe('Execution', () => {

describe('when arguments are missing', () => {
it('when required argument is missing and has not alias, returns error', async () => {
const requiredArg: ExpressionFunctionDefinition<'requiredArg', any, { arg: any }, any> = {
const requiredArg: ExpressionFunctionDefinition<
'requiredArg',
unknown,
{ arg: unknown },
unknown
> = {
name: 'requiredArg',
args: {
arg: {
Expand Down
Loading

0 comments on commit 11e5b35

Please sign in to comment.